mirror of
https://github.com/jtdaugherty/brick.git
synced 2024-10-26 17:17:43 +03:00
Flip the arguments to application event handlers to make event pattern-matching more natural
This commit is contained in:
parent
2942514198
commit
afc0c95be8
@ -29,8 +29,8 @@ drawUI st = [a]
|
||||
<=>
|
||||
(str $ "Counter value is: " <> (show $ st^.stCounter))
|
||||
|
||||
appEvent :: CustomEvent -> St -> EventM (Next St)
|
||||
appEvent e st =
|
||||
appEvent :: St -> CustomEvent -> EventM (Next St)
|
||||
appEvent st e =
|
||||
case e of
|
||||
VtyEvent (EvKey KEsc []) -> halt st
|
||||
VtyEvent ev -> continue $ st & stLastVtyEvent .~ (Just ev)
|
||||
|
@ -18,8 +18,8 @@ drawUI e = [ui]
|
||||
where
|
||||
ui = center $ "Input: " <+> (hLimit 30 $ renderEditor e)
|
||||
|
||||
appEvent :: Event -> Editor -> EventM (Next Editor)
|
||||
appEvent ev e =
|
||||
appEvent :: Editor -> Event -> EventM (Next Editor)
|
||||
appEvent e ev =
|
||||
case ev of
|
||||
EvKey KEsc [] -> halt e
|
||||
EvKey KEnter [] -> halt e
|
||||
|
@ -33,8 +33,8 @@ drawUI l = [ui]
|
||||
, hCenter "Press Esc to exit."
|
||||
]
|
||||
|
||||
appEvent :: Event -> List Int -> EventM (Next (List Int))
|
||||
appEvent e l =
|
||||
appEvent :: List Int -> Event -> EventM (Next (List Int))
|
||||
appEvent l e =
|
||||
case e of
|
||||
EvKey (KChar '+') [] ->
|
||||
let el = length $ l^.listElementsL
|
||||
|
@ -88,8 +88,8 @@ drawUI st = [withBorderStyle bs a]
|
||||
<=> (hCenter (kw "Arrow keys" <+> " navigates the list"))
|
||||
<=> (hCenter (kw "Ctrl-Arrow keys" <+> " move the interface"))
|
||||
|
||||
appEvent :: Event -> St -> EventM (Next St)
|
||||
appEvent e st =
|
||||
appEvent :: St -> Event -> EventM (Next St)
|
||||
appEvent st e =
|
||||
case e of
|
||||
EvKey (KChar '+') [] ->
|
||||
continue $ st & stBorderStyle %~ ((`mod` (length styles)) . (+ 1))
|
||||
|
@ -101,8 +101,8 @@ pieceA, dumpA :: Attr
|
||||
pieceA = defAttr `withForeColor` blue `withBackColor` green
|
||||
dumpA = defAttr `withStyle` reverseVideo
|
||||
|
||||
processEvent :: Event -> World -> EventM (Next World)
|
||||
processEvent k world = do
|
||||
processEvent :: World -> Event -> EventM (Next World)
|
||||
processEvent world k = do
|
||||
case k of
|
||||
EvKey KEsc [] -> halt world
|
||||
EvKey KLeft [] -> continue $ movePlayer world (-1) 0
|
||||
|
@ -24,8 +24,8 @@ drawUI st = [ui]
|
||||
, "(Press Esc to quit or Space to ask for input)"
|
||||
]
|
||||
|
||||
appEvent :: Event -> St -> EventM (Next St)
|
||||
appEvent e st =
|
||||
appEvent :: St -> Event -> EventM (Next St)
|
||||
appEvent st e =
|
||||
case e of
|
||||
EvKey KEsc [] -> halt st
|
||||
EvKey (KChar ' ') [] -> suspendAndResume $ do
|
||||
|
@ -76,7 +76,7 @@ data App s e =
|
||||
-- is that many widgets may request a cursor placement but your
|
||||
-- application state is what you probably want to use to decide
|
||||
-- which one wins.
|
||||
, appHandleEvent :: e -> s -> EventM (Next s)
|
||||
, appHandleEvent :: s -> e -> EventM (Next s)
|
||||
-- ^ This function takes an event and your application state
|
||||
-- and returns an action to be taken. Possible options are
|
||||
-- 'continue', 'suspendAndResume', and 'halt'.
|
||||
@ -130,8 +130,8 @@ simpleMain w =
|
||||
-- a halt. This is a convenience function useful as an 'appHandleEvent'
|
||||
-- value for simple applications using the 'Event' type that do not need
|
||||
-- to get more sophisticated user input.
|
||||
resizeOrQuit :: Event -> s -> EventM (Next s)
|
||||
resizeOrQuit e a =
|
||||
resizeOrQuit :: s -> Event -> EventM (Next s)
|
||||
resizeOrQuit a e =
|
||||
case e of
|
||||
EvResize _ _ -> continue a
|
||||
_ -> halt a
|
||||
@ -193,7 +193,7 @@ runVty :: Vty -> Chan e -> App s e -> s -> RenderState -> IO (Next s, RenderStat
|
||||
runVty vty chan app appState rs = do
|
||||
firstRS <- renderApp vty app appState rs
|
||||
e <- readChan chan
|
||||
(next, scrollReqs) <- runStateT (appHandleEvent app e appState) []
|
||||
(next, scrollReqs) <- runStateT (appHandleEvent app appState e) []
|
||||
return (next, firstRS { _scrollRequests = scrollReqs })
|
||||
|
||||
withVty :: IO Vty -> (Vty -> IO a) -> IO a
|
||||
|
Loading…
Reference in New Issue
Block a user