moved keyboard filtering to helper function in UI module rather than polluting explorer code

This commit is contained in:
Paul Chiusano 2015-11-05 21:16:09 -05:00
parent a75db5a232
commit 82a773d988
2 changed files with 11 additions and 8 deletions

View File

@ -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)

View File

@ -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