on master help sign in
Vty: add nextEventNonblocking (fixes #87)
commitbf963565331324c88c342b22788a34a8f7d4c7d9 authorJonathan Daugherty <cygnus@foobox.com> dateMon 19 Mar 2018, 15:18 (9 years ago) parentadc4d70
1 file changed+14−5
src/Graphics/Vty.hs+14−5
src/Graphics/Vty.hs+14−5
@@ -80,6 +80,8 @@ data Vty = Vty
8080 update :: Picture -> IO ()
8181 -- | Return the next 'Event' or block until one becomes available.
8282 , nextEvent :: IO Event
83+ -- | Non-blocking version of 'nextEvent'.
84+ , nextEventNonblocking :: IO (Maybe Event)
8385 -- | The input interface. See 'Input'.
8486 , inputIface :: Input
8587 -- | The output interface. See 'Output'.
@@ -148,14 +150,21 @@ intMkVty input out = do
148150 mPic <- readIORef lastPicRef
149151 maybe (return ()) innerUpdate mPic
150152
151− let gkey = do k <- atomically $ readTChan $ _eventChannel input
152− case k of
153− (EvResize _ _) -> displayBounds out
154− >>= return . (\(w,h)-> EvResize w h)
155− _ -> return k
153+ let mkResize = uncurry EvResize <$> displayBounds out
154+ gkey = do
155+ k <- atomically $ readTChan $ _eventChannel input
156+ case k of
157+ (EvResize _ _) -> mkResize
158+ _ -> return k
159+ gkey' = do
160+ k <- atomically $ tryReadTChan $ _eventChannel input
161+ case k of
162+ (Just (EvResize _ _)) -> Just <$> mkResize
163+ _ -> return k
156164
157165 return $ Vty { update = innerUpdate
158166 , nextEvent = gkey
167+ , nextEventNonblocking = gkey'
159168 , inputIface = input
160169 , outputIface = out
161170 , refresh = innerRefresh