{-# LANGUAGE OverloadedStrings #-}
module System.Taffybar.Widget.Layout
(
LayoutConfig(..)
, defaultLayoutConfig
, layoutNew
) where
import Control.Monad.Trans.Class
import Control.Monad.Trans.Reader
import Data.Default (Default(..))
import qualified Data.Text as T
import qualified GI.Gtk as Gtk
import GI.Gdk
import System.Taffybar.Context
import System.Taffybar.Information.X11DesktopInfo
import System.Taffybar.Util
import System.Taffybar.Widget.Util
newtype LayoutConfig = LayoutConfig
{ LayoutConfig -> Text -> TaffyIO Text
formatLayout :: T.Text -> TaffyIO T.Text
}
defaultLayoutConfig :: LayoutConfig
defaultLayoutConfig :: LayoutConfig
defaultLayoutConfig = (Text -> TaffyIO Text) -> LayoutConfig
LayoutConfig Text -> TaffyIO Text
forall a. a -> ReaderT Context IO a
forall (m :: * -> *) a. Monad m => a -> m a
return
instance Default LayoutConfig where
def :: LayoutConfig
def = LayoutConfig
defaultLayoutConfig
xLayoutProp :: String
xLayoutProp :: String
xLayoutProp = String
"_XMONAD_CURRENT_LAYOUT"
layoutNew :: LayoutConfig -> TaffyIO Gtk.Widget
layoutNew :: LayoutConfig -> TaffyIO Widget
layoutNew LayoutConfig
config = do
ctx <- ReaderT Context IO Context
forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
label <- lift $ Gtk.labelNew (Nothing :: Maybe T.Text)
_ <- widgetSetClassGI label "layout-label"
let callback Event
_ = (IO () -> IO ()) -> ReaderT Context IO () -> ReaderT Context IO ()
forall (m :: * -> *) a (n :: * -> *) b r.
(m a -> n b) -> ReaderT r m a -> ReaderT r n b
mapReaderT IO () -> IO ()
postGUIASync (ReaderT Context IO () -> ReaderT Context IO ())
-> ReaderT Context IO () -> ReaderT Context IO ()
forall a b. (a -> b) -> a -> b
$ do
layout <- String -> X11Property String -> TaffyIO String
forall a. a -> X11Property a -> TaffyIO a
runX11Def String
"" (X11Property String -> TaffyIO String)
-> X11Property String -> TaffyIO String
forall a b. (a -> b) -> a -> b
$ Maybe Atom -> String -> X11Property String
readAsString Maybe Atom
forall a. Maybe a
Nothing String
xLayoutProp
markup <- formatLayout config (T.pack layout)
lift $ Gtk.labelSetMarkup label markup
subscription <- subscribeToPropertyEvents [xLayoutProp] callback
do
ebox <- Gtk.eventBoxNew
Gtk.containerAdd ebox label
_ <- Gtk.onWidgetButtonPressEvent ebox $ dispatchButtonEvent ctx
Gtk.widgetShowAll ebox
_ <- Gtk.onWidgetUnrealize ebox $ flip runReaderT ctx $ unsubscribe subscription
Gtk.toWidget ebox
dispatchButtonEvent :: Context -> EventButton -> IO Bool
dispatchButtonEvent :: Context -> WidgetButtonPressEventCallback
dispatchButtonEvent Context
context EventButton
btn = do
pressType <- EventButton -> IO EventType
forall (m :: * -> *). MonadIO m => EventButton -> m EventType
getEventButtonType EventButton
btn
buttonNumber <- getEventButtonButton btn
case pressType of
EventType
EventTypeButtonPress ->
case Word32
buttonNumber of
Word32
1 -> ReaderT Context IO () -> Context -> IO ()
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (() -> X11Property () -> ReaderT Context IO ()
forall a. a -> X11Property a -> TaffyIO a
runX11Def () (Int -> X11Property ()
switch Int
1)) Context
context IO () -> IO Bool -> IO Bool
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
Word32
2 -> ReaderT Context IO () -> Context -> IO ()
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (() -> X11Property () -> ReaderT Context IO ()
forall a. a -> X11Property a -> TaffyIO a
runX11Def () (Int -> X11Property ()
switch (-Int
1))) Context
context IO () -> IO Bool -> IO Bool
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
Word32
_ -> Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
EventType
_ -> Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
switch :: Int -> X11Property ()
switch :: Int -> X11Property ()
switch Int
n = do
cmd <- String -> X11Property Atom
getAtom String
xLayoutProp
sendCommandEvent cmd (fromIntegral n)