module System.Taffybar.Widget.Generic.PollingLabel
( pollingLabelNew
, pollingLabelNewWithTooltip
) where
import Control.Exception.Enclosed as E
import Control.Monad
import Control.Monad.IO.Class
import Data.GI.Gtk.Threading
import qualified Data.Text as T
import GI.Gtk
import qualified Graphics.UI.Gtk as Gtk2hs
import System.Taffybar.Compat.GtkLibs
import System.Taffybar.Util
import System.Taffybar.Widget.Util
pollingLabelNew
:: MonadIO m
=> String
-> Double
-> IO String
-> m Gtk2hs.Widget
pollingLabelNew initialString interval cmd =
pollingLabelNewWithTooltip initialString interval $ (, Nothing) <$> cmd
pollingLabelNewWithTooltip
:: MonadIO m
=> String
-> Double
-> IO (String, Maybe String)
-> m Gtk2hs.Widget
pollingLabelNewWithTooltip initialString interval cmd =
liftIO $ fromGIWidget =<< do
grid <- gridNew
label <- labelNew $ Just $ T.pack initialString
let updateLabel (labelStr, tooltipStr) =
postGUIASync $ do
labelSetMarkup label $ T.pack labelStr
widgetSetTooltipMarkup label $ T.pack <$> tooltipStr
_ <- onWidgetRealize label $ void $ foreverWithDelay interval $
E.tryAny cmd >>= either (const $ return ()) updateLabel
vFillCenter label
vFillCenter grid
containerAdd grid label
widgetShowAll grid
toWidget grid