Skip to content
Snippets Groups Projects
Commit dd496bf5 authored by Jens Nolte's avatar Jens Nolte
Browse files

Pass click event data to event handler

parent 7d95a511
No related branches found
No related tags found
No related merge requests found
...@@ -31,13 +31,14 @@ import Data.Colour.RGBSpace ...@@ -31,13 +31,14 @@ import Data.Colour.RGBSpace
data Block = Block { data Block = Block {
values :: HM.HashMap T.Text T.Text, values :: HM.HashMap T.Text T.Text,
clickAction :: Maybe (IO ()) clickAction :: Maybe (Click -> IO ())
} }
instance Show Block where instance Show Block where
show Block{values} = show values show Block{values} = show values
data Click = Click { data Click = Click {
name :: T.Text name :: T.Text,
button :: Int
} deriving Show } deriving Show
$(deriveJSON defaultOptions ''Click) $(deriveJSON defaultOptions ''Click)
...@@ -170,8 +171,8 @@ sharedInterval barUpdateChannel seconds = do ...@@ -170,8 +171,8 @@ sharedInterval barUpdateChannel seconds = do
clickAction = Just (updateClickHandler result) clickAction = Just (updateClickHandler result)
} }
where where
updateClickHandler :: Block -> IO () updateClickHandler :: Block -> Click -> IO ()
updateClickHandler block = do updateClickHandler block _ = do
-- Give user feedback that the block is updating -- Give user feedback that the block is updating
let outdatedBlock = setColor updatingColor $ removePango block let outdatedBlock = setColor updatingColor $ removePango block
void $ atomically $ send output $ outdatedBlock void $ atomically $ send output $ outdatedBlock
......
...@@ -28,7 +28,7 @@ import System.IO (stdin, stdout, stderr, hFlush, hPutStrLn) ...@@ -28,7 +28,7 @@ import System.IO (stdin, stdout, stderr, hFlush, hPutStrLn)
import System.Posix.Signals import System.Posix.Signals
data Handle = Handle { data Handle = Handle {
handleActionList :: IORef [(T.Text, IO ())], handleActionList :: IORef [(T.Text, Click -> IO ())],
handleActiveFilter :: IORef Filter handleActiveFilter :: IORef Filter
} }
...@@ -91,9 +91,9 @@ renderLine MainOptions{verbose} Handle{handleActionList} blockFilter blocks prev ...@@ -91,9 +91,9 @@ renderLine MainOptions{verbose} Handle{handleActionList} blockFilter blocks prev
return encodedOutput return encodedOutput
where where
clickActionList :: [(T.Text, IO ())] clickActionList :: [(T.Text, Click -> IO ())]
clickActionList = mapMaybe getClickAction blocks clickActionList = mapMaybe getClickAction blocks
getClickAction :: Block -> Maybe (T.Text, IO ()) getClickAction :: Block -> Maybe (T.Text, Click -> IO ())
getClickAction block = if hasBlockName && hasClickAction then Just (fromJust maybeBlockName, fromJust maybeClickAction) else Nothing getClickAction block = if hasBlockName && hasClickAction then Just (fromJust maybeBlockName, fromJust maybeClickAction) else Nothing
where where
maybeBlockName = getBlockName block maybeBlockName = getBlockName block
...@@ -106,7 +106,7 @@ createBarUpdateChannel = do ...@@ -106,7 +106,7 @@ createBarUpdateChannel = do
event <- Event.newSet event <- Event.newSet
return (BarUpdateChannel $ Event.set event, event) return (BarUpdateChannel $ Event.set event, event)
handleStdin :: MainOptions -> IORef [(T.Text, IO ())] -> IO () handleStdin :: MainOptions -> IORef [(T.Text, Click -> IO ())] -> IO ()
handleStdin options actionListIORef = forever $ do handleStdin options actionListIORef = forever $ do
line <- BSSC8.hGetLine stdin line <- BSSC8.hGetLine stdin
...@@ -116,17 +116,17 @@ handleStdin options actionListIORef = forever $ do ...@@ -116,17 +116,17 @@ handleStdin options actionListIORef = forever $ do
BSSC8.hPutStrLn stderr line BSSC8.hPutStrLn stderr line
hFlush stderr hFlush stderr
clickActionList <- readIORef actionListIORef let maybeClick = decode $ removeComma $ BS.fromStrict line
let maybeParsedClick = decode $ removeComma $ BS.fromStrict line case maybeClick of
let clickAction' = getClickAction clickActionList maybeParsedClick Just click -> do
async (fromMaybe (return ()) clickAction') >>= link clickActionList <- readIORef actionListIORef
let clickAction' = getClickAction clickActionList click
async ((fromMaybe discard clickAction') click) >>= link
Nothing -> return ()
where where
getClickAction :: [(T.Text, IO ())] -> Maybe Click -> Maybe (IO ()) getClickAction :: [(T.Text, Click -> IO ())] -> Click -> Maybe (Click -> IO ())
getClickAction clickActionList maybeParsedClick = do getClickAction clickActionList click = lookup (name click) clickActionList
parsedClick <- maybeParsedClick
let blockName = name parsedClick
lookup blockName clickActionList
removeComma :: C8.ByteString -> C8.ByteString removeComma :: C8.ByteString -> C8.ByteString
removeComma line removeComma line
| C8.head line == ',' = C8.tail line | C8.head line == ',' = C8.tail line
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment