-- | A variant of the Graph widget that automatically updates itself
-- with a callback at a fixed interval.
module System.Taffybar.Widget.Generic.PollingGraph (
  -- * Types
  GraphHandle,
  GraphConfig(..),
  GraphDirection(..),
  GraphStyle(..),
  -- * Constructors and accessors
  pollingGraphNew,
  pollingGraphNewWithTooltip,
  defaultGraphConfig
  ) where

import           Control.Concurrent
import qualified Control.Exception.Enclosed as E
import           Control.Monad
import           Control.Monad.IO.Class
import qualified Data.Text as T
import           GI.Gtk
import           System.Taffybar.Util
import           System.Taffybar.Widget.Generic.Graph

pollingGraphNewWithTooltip
  :: MonadIO m
  => GraphConfig -> Double -> IO ([Double], Maybe T.Text) -> m GI.Gtk.Widget
pollingGraphNewWithTooltip :: forall (m :: * -> *).
MonadIO m =>
GraphConfig -> Double -> IO ([Double], Maybe Text) -> m Widget
pollingGraphNewWithTooltip GraphConfig
cfg Double
pollSeconds IO ([Double], Maybe Text)
action = IO Widget -> m Widget
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Widget -> m Widget) -> IO Widget -> m Widget
forall a b. (a -> b) -> a -> b
$ do
  (graphWidget, graphHandle) <- GraphConfig -> IO (Widget, GraphHandle)
forall (m :: * -> *).
MonadIO m =>
GraphConfig -> m (Widget, GraphHandle)
graphNew GraphConfig
cfg

  _ <- onWidgetRealize graphWidget $ do
       sampleThread <- foreverWithDelay pollSeconds $ do
         esample <- E.tryAny action
         case esample of
           Left SomeException
_ -> () -> WidgetRealizeCallback
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
           Right ([Double]
sample, Maybe Text
tooltipStr) -> do
             GraphHandle -> [Double] -> WidgetRealizeCallback
graphAddSample GraphHandle
graphHandle [Double]
sample
             Widget -> Maybe Text -> WidgetRealizeCallback
forall (m :: * -> *) a.
(HasCallStack, MonadIO m, IsWidget a) =>
a -> Maybe Text -> m ()
widgetSetTooltipMarkup Widget
graphWidget Maybe Text
tooltipStr
       void $ onWidgetUnrealize graphWidget $ killThread sampleThread

  return graphWidget

pollingGraphNew
  :: MonadIO m
  => GraphConfig -> Double -> IO [Double] -> m GI.Gtk.Widget
pollingGraphNew :: forall (m :: * -> *).
MonadIO m =>
GraphConfig -> Double -> IO [Double] -> m Widget
pollingGraphNew GraphConfig
cfg Double
pollSeconds IO [Double]
action =
  GraphConfig -> Double -> IO ([Double], Maybe Text) -> m Widget
forall (m :: * -> *).
MonadIO m =>
GraphConfig -> Double -> IO ([Double], Maybe Text) -> m Widget
pollingGraphNewWithTooltip GraphConfig
cfg Double
pollSeconds (IO ([Double], Maybe Text) -> m Widget)
-> IO ([Double], Maybe Text) -> m Widget
forall a b. (a -> b) -> a -> b
$ ([Double] -> ([Double], Maybe Text))
-> IO [Double] -> IO ([Double], Maybe Text)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (, Maybe Text
forall a. Maybe a
Nothing) IO [Double]
action