From 82a773d988c768016050015926cdbc2afa70f4e7 Mon Sep 17 00:00:00 2001 From: Paul Chiusano Date: Thu, 5 Nov 2015 21:16:09 -0500 Subject: [PATCH] moved keyboard filtering to helper function in UI module rather than polluting explorer code --- editor/src/Unison/Explorer.hs | 9 ++------- editor/src/Unison/UI.hs | 10 +++++++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/editor/src/Unison/Explorer.hs b/editor/src/Unison/Explorer.hs index d5bcb2261..4563cd93d 100644 --- a/editor/src/Unison/Explorer.hs +++ b/editor/src/Unison/Explorer.hs @@ -11,8 +11,7 @@ import Data.Maybe import Data.Semigroup import Reflex.Dom import Unison.Dimensions (X(..),Y(..)) -import GHCJS.DOM.EventM (preventDefault) -import GHCJS.DOM.Element (elementOnkeydown) +import qualified Unison.UI as UI import qualified Unison.Signals as Signals modal :: (MonadWidget t m, Reflex t) => Dynamic t Bool -> a -> m a -> m (Dynamic t a) @@ -38,11 +37,7 @@ explorer keydown processQuery topContent s0 = do attrs <- holdDyn ("class" =: "explorer") (fmap (\l -> if null l then invalidAttrs else validAttrs) valids) (valids, updatedS, closings) <- elDynAttr "div" attrs $ mdo searchbox <- textInput def - -- disable up/down keyboard events inside of text box; they are used for controlling what item is selected - let isArrow i = i == 38 || i == 40 - let tweak e = e >>= \i -> if isArrow i then i <$ preventDefault else pure i - tweakedKeydown <- wrapDomEvent (_textInput_element searchbox) elementOnkeydown (tweak getKeyEvent) - performEvent_ (fmap (const (pure ())) tweakedKeydown) + UI.keepKeyEventIf (\i -> i /= 38 && i /= 40) searchbox -- disable up/down inside searchbox elClass "div" "top-separator" $ pure () _ <- elClass "div" "top-content" $ widgetHold (pure ()) topContent -- todo: perhaps a spinner s <- sample (current s0) diff --git a/editor/src/Unison/UI.hs b/editor/src/Unison/UI.hs index 4c91746ca..a21aac2b8 100644 --- a/editor/src/Unison/UI.hs +++ b/editor/src/Unison/UI.hs @@ -1,9 +1,11 @@ {-# LANGUAGE CPP, ForeignFunctionInterface, JavaScriptFFI, OverloadedStrings #-} -module Unison.UI (mouseMove, mouseMove', preferredDimensions, windowKeydown, windowKeyup) where +module Unison.UI (keepKeyEventIf, mouseMove, mouseMove', preferredDimensions, windowKeydown, windowKeyup) where import Control.Monad.IO.Class import Data.Text (Text) +import GHCJS.DOM.Element (elementOnkeydown) +import GHCJS.DOM.EventM (preventDefault) import GHCJS.DOM.Types (Element,DOMWindow) import GHCJS.Marshal import GHCJS.Types (JSRef) @@ -32,6 +34,12 @@ mouseMove e = case Element.toElement e of Element.elementOnmousemove (liftIO . mouseLocal e =<< EventM.event) +keepKeyEventIf :: (MonadWidget t m, Reflex t) => (Int -> Bool) -> TextInput t -> m () +keepKeyEventIf f input = do + let tweak e = e >>= \i -> if f i then pure i else i <$ preventDefault + tweakedKeydown <- wrapDomEvent (_textInput_element input) Element.elementOnkeydown (tweak getKeyEvent) + performEvent_ (pure () <$ tweakedKeydown) + askWindow :: (MonadIO m, HasDocument m) => m DOMWindow askWindow = do (Just window) <- askDocument >>= liftIO . Document.documentGetDefaultView