Hi,
> Hey, just started using this for a pet project and this library is
> awesome, great job on the docs.
Thanks!
> One complication however, is that I want to have speed setting.
>
> ...
>
> Is this possible? That channel is passed to customMain so I'm
> wondering if we can acess it and perform actions with it from an
> event/app handler.
This is definitely possible. One way to do it is to set up a
communication mechanism from the main thread (which ultimately runs your
application event handler) to the custom event thread. I tried this out
by modifying CustomEventDemo.hs to use an STM TVar as follows:
import Control.Monad.IO.Class (liftIO)
import Control.Concurrent.STM
Add a TVar to the application state:
data St =
St { ...
, _stAmount :: TVar Int
}
In main, create a TVar with the initial "speed" setting (in my case the
number of seconds to sleep):
tv <- atomically $ newTVar 1
In the counter thread, start by reading from the TVar. Use the read
value to determine how long to sleep.
forkIO $ forever $ do
amt <- atomically $ readTVar tv
writeBChan chan Counter
threadDelay $ amt * 1000000
In the application event handler, on a speed change event, update the
TVar with the new speed.
appEvent st e =
case e of
VtyEvent (V.EvKey (V.KChar '+') []) -> do
let tv = st^.stAmount
liftIO $ atomically $ modifyTVar tv succ
continue st
VtyEvent (V.EvKey (V.KChar '-') []) -> do
let tv = st^.stAmount
liftIO $ atomically $ modifyTVar tv (max 0 . pred)
continue st
This way, whenever the counter thread wakes up it will get the new
setting. Whenever the user changes the speed setting, the transactional
variable used by the counter thread will be updated.
--
Jonathan Daugherty