Make customMain take an optional channel; do not set mouse mode by default

This commit is contained in:
Jonathan Daugherty 2016-11-01 13:22:25 -07:00
parent 036a1af956
commit e313cc6816
4 changed files with 21 additions and 12 deletions

View File

@ -343,7 +343,7 @@ our events over that channel. Once we've created the channel with
main :: IO ()
main = do
eventChan <- Control.Concurrent.newChan
finalState <- customMain (Graphics.Vty.mkVty Data.Default.def) eventChan app initialState
finalState <- customMain (Graphics.Vty.mkVty Data.Default.def) (Just eventChan) app initialState
-- Use finalState and exit
The ``customMain`` function lets us have control over how the ``vty``

View File

@ -76,4 +76,4 @@ main = do
writeChan chan Counter
threadDelay 1000000
void $ customMain (V.mkVty def) chan theApp initialState
void $ customMain (V.mkVty def) (Just chan) theApp initialState

View File

@ -3,6 +3,7 @@
module Main where
import Control.Applicative ((<$>))
import Control.Concurrent (newChan)
import Data.Monoid ((<>))
import Lens.Micro ((^.), (&), (.~))
import Lens.Micro.TH (makeLenses)
@ -88,4 +89,10 @@ app =
}
main :: IO ()
main = void $ M.defaultMain app $ St [] Nothing
main = do
let buildVty = do
v <- V.mkVty =<< V.standardIOConfig
V.setMode (V.outputIface v) V.Mouse True
return v
void $ M.customMain buildVty Nothing app $ St [] Nothing

View File

@ -42,7 +42,7 @@ where
import Control.Exception (finally)
import Lens.Micro ((^.), (&), (.~))
import Control.Monad (forever)
import Control.Monad (forever, void)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.State
import Control.Monad.Trans.Reader
@ -60,8 +60,6 @@ import Graphics.Vty
, Picture(..)
, Cursor(..)
, Event(..)
, Mode(..)
, setMode
, update
, outputIface
, displayBounds
@ -121,7 +119,7 @@ defaultMain :: (Ord n)
-> IO s
defaultMain app st = do
chan <- newChan
customMain (mkVty def) chan app st
customMain (mkVty def) (Just chan) app st
-- | A simple main entry point which takes a widget and renders it. This
-- event loop terminates when the user presses any key, but terminal
@ -160,7 +158,6 @@ runWithNewVty :: (Ord n)
-> IO (InternalNext n s)
runWithNewVty buildVty chan app initialRS initialSt =
withVty buildVty $ \vty -> do
setMode (outputIface vty) Mouse True
pid <- forkIO $ supplyVtyEvents vty chan
let runInner rs st = do
(result, newRS) <- runVty vty chan app st (rs & observedNamesL .~ S.empty
@ -182,16 +179,17 @@ customMain :: (Ord n)
-- ^ An IO action to build a Vty handle. This is used to
-- build a Vty handle whenever the event loop begins or is
-- resumed after suspension.
-> Chan e
-> Maybe (Chan e)
-- ^ An event channel for sending custom events to the event
-- loop (you write to this channel, the event loop reads from
-- it).
-- it). Provide 'Nothing' if you don't plan on sending custom
-- events.
-> App s e n
-- ^ The application.
-> s
-- ^ The initial application state.
-> IO s
customMain buildVty userChan app initialAppState = do
customMain buildVty mUserChan app initialAppState = do
let run rs st chan = do
result <- runWithNewVty buildVty chan app rs st
case result of
@ -205,7 +203,11 @@ customMain buildVty userChan app initialAppState = do
(st, eState) <- runStateT (runReaderT (runEventM (appStartEvent app initialAppState)) eventRO) emptyES
let initialRS = RS M.empty (esScrollRequests eState) S.empty mempty []
chan <- newChan
forkIO $ forever $ readChan userChan >>= (\userEvent -> writeChan chan $ AppEvent userEvent)
case mUserChan of
Just userChan ->
void $ forkIO $ forever $ readChan userChan >>= (\userEvent -> writeChan chan $ AppEvent userEvent)
Nothing -> return ()
run initialRS st chan
supplyVtyEvents :: Vty -> Chan (BrickEvent n e) -> IO ()