module System.Taffybar.Widget.Generic.Icon
( iconImageWidgetNew
, pollingIconImageWidgetNew
) where
import Control.Concurrent ( forkIO, threadDelay )
import Control.Exception as E
import Control.Monad ( forever )
import Control.Monad.IO.Class
import GI.Gtk
import System.Taffybar.Util
iconImageWidgetNew :: MonadIO m => FilePath -> m Widget
iconImageWidgetNew path = liftIO $ imageNewFromFile path >>= putInBox
pollingIconImageWidgetNew
:: MonadIO m
=> FilePath
-> Double
-> IO FilePath
-> m Widget
pollingIconImageWidgetNew path interval cmd = liftIO $ do
icon <- imageNewFromFile path
_ <- onWidgetRealize icon $ do
_ <- forkIO $ forever $ do
let tryUpdate = do
str <- cmd
postGUIASync $ imageSetFromFile icon (Just str)
E.catch tryUpdate ignoreIOException
threadDelay $ floor (interval * 1000000)
return ()
putInBox icon
putInBox :: IsWidget child => child -> IO Widget
putInBox icon = do
box <- boxNew OrientationHorizontal 0
boxPackStart box icon False False 0
widgetShowAll box
toWidget box
ignoreIOException :: IOException -> IO ()
ignoreIOException _ = return ()