{-# LANGUAGE OverloadedStrings #-}
module System.Taffybar.Widget.Windows where
import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.Trans.Reader
import Data.Maybe
import qualified Data.Text as T
import GI.GLib (markupEscapeText)
import qualified GI.Gtk as Gtk
import System.Taffybar.Context
import System.Taffybar.Information.EWMHDesktopInfo
import System.Taffybar.Util
import System.Taffybar.Widget.Generic.DynamicMenu
import System.Taffybar.Widget.Util
data WindowsConfig = WindowsConfig
{ getMenuLabel :: X11Window -> TaffyIO T.Text
, getActiveLabel :: TaffyIO T.Text
}
defaultGetMenuLabel :: X11Window -> TaffyIO T.Text
defaultGetMenuLabel window = do
windowString <- runX11Def "(nameless window)" (getWindowTitle window)
return $ T.pack windowString
defaultGetActiveLabel :: TaffyIO T.Text
defaultGetActiveLabel = do
label <- fromMaybe "" <$> (runX11Def Nothing getActiveWindow >>= traverse defaultGetMenuLabel)
markupEscapeText label (-1)
truncatedGetActiveLabel :: Int -> TaffyIO T.Text
truncatedGetActiveLabel maxLength =
truncateText maxLength <$> defaultGetActiveLabel
truncatedGetMenuLabel :: Int -> X11Window -> TaffyIO T.Text
truncatedGetMenuLabel maxLength =
fmap (truncateText maxLength) . defaultGetMenuLabel
defaultWindowsConfig :: WindowsConfig
defaultWindowsConfig =
WindowsConfig
{ getMenuLabel = truncatedGetMenuLabel 35
, getActiveLabel = truncatedGetActiveLabel 35
}
windowsNew :: WindowsConfig -> TaffyIO Gtk.Widget
windowsNew config = do
label <- lift $ Gtk.labelNew Nothing
let setLabelTitle title = lift $ postGUIASync $ Gtk.labelSetMarkup label title
activeWindowUpdatedCallback _ = getActiveLabel config >>= setLabelTitle
subscription <-
subscribeToPropertyEvents [ewmhActiveWindow, ewmhWMName, ewmhWMClass]
activeWindowUpdatedCallback
_ <- liftReader (Gtk.onWidgetUnrealize label) (unsubscribe subscription)
context <- ask
labelWidget <- Gtk.toWidget label
menu <- dynamicMenuNew
DynamicMenuConfig { dmClickWidget = labelWidget
, dmPopulateMenu = flip runReaderT context . fillMenu config
}
widgetSetClassGI menu "windows"
fillMenu :: Gtk.IsMenuShell a => WindowsConfig -> a -> ReaderT Context IO ()
fillMenu config menu = ask >>= \context ->
runX11Def () $ do
windowIds <- getWindows
forM_ windowIds $ \windowId ->
lift $ do
labelText <- runReaderT (getMenuLabel config windowId) context
let focusCallback = runReaderT (runX11 $ focusWindow windowId) context >> return True
item <- Gtk.menuItemNewWithLabel labelText
_ <- Gtk.onWidgetButtonPressEvent item $ const focusCallback
Gtk.menuShellAppend menu item
Gtk.widgetShow item