Skip to content

Commit a59c316

Browse files
committed
refactor: split taffybar config into modules
1 parent 63fcebf commit a59c316

7 files changed

Lines changed: 808 additions & 732 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module TaffybarConfig.Config
2+
( mkSimpleTaffyConfig,
3+
)
4+
where
5+
6+
import TaffybarConfig.Host (compactBarHosts, smallBarHosts)
7+
import TaffybarConfig.Widgets (clockWidget, endWidgetsForHost, startWidgetsForBackend)
8+
import System.Taffybar.Context (Backend)
9+
import System.Taffybar.SimpleConfig
10+
11+
mkSimpleTaffyConfig :: String -> Backend -> [FilePath] -> SimpleTaffyConfig
12+
mkSimpleTaffyConfig hostName backend cssFiles =
13+
defaultSimpleTaffyConfig
14+
{ startWidgets = startWidgetsForBackend backend,
15+
centerWidgets = [clockWidget],
16+
endWidgets = endWidgetsForHost hostName,
17+
barLevels = Nothing,
18+
barPosition = Top,
19+
widgetSpacing = 0,
20+
barPadding =
21+
if hostName `elem` smallBarHosts
22+
then 1
23+
else
24+
if hostName `elem` compactBarHosts
25+
then 2
26+
else 4,
27+
barHeight =
28+
if hostName `elem` smallBarHosts
29+
then ScreenRatio $ 1 / 48
30+
else
31+
if hostName `elem` compactBarHosts
32+
then ScreenRatio $ 1 / 40
33+
else ScreenRatio $ 1 / 33,
34+
cssPaths = cssFiles
35+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module TaffybarConfig.Host
2+
( compactBarHosts,
3+
cssFilesForHost,
4+
laptopHosts,
5+
smallBarHosts,
6+
)
7+
where
8+
9+
import Data.Maybe (fromMaybe)
10+
11+
-- NOTE: Keep `cssPaths` to a single entrypoint file per host. GTK's
12+
-- `cssProviderLoadFromPath` clears the provider before loading, so handing
13+
-- Taffybar multiple files here causes only the last file to take effect.
14+
defaultCssFiles :: [FilePath]
15+
defaultCssFiles = ["taffybar.css"]
16+
17+
cssFilesByHostname :: [(String, [FilePath])]
18+
cssFilesByHostname =
19+
[ ("ryzen-shine", ["ryzen-shine.css"]),
20+
("strixi-minaj", ["strixi-minaj.css"])
21+
]
22+
23+
compactBarHosts :: [String]
24+
compactBarHosts =
25+
["ryzen-shine"]
26+
27+
smallBarHosts :: [String]
28+
smallBarHosts =
29+
["strixi-minaj"]
30+
31+
laptopHosts :: [String]
32+
laptopHosts =
33+
[ "adell",
34+
"stevie-nixos",
35+
"strixi-minaj",
36+
"jay-lenovo"
37+
]
38+
39+
cssFilesForHost :: String -> [FilePath]
40+
cssFilesForHost hostName =
41+
fromMaybe defaultCssFiles $ lookup hostName cssFilesByHostname
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{-# LANGUAGE LambdaCase #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
4+
module TaffybarConfig.WidgetUtil
5+
( decorateWithClassAndBox,
6+
decorateWithClassAndBoxM,
7+
setFixedLabelWidth,
8+
setLabelAlignmentRecursively,
9+
stackInPill,
10+
usageLogoWidget,
11+
)
12+
where
13+
14+
import Control.Monad.IO.Class (MonadIO, liftIO)
15+
import Data.Foldable (for_)
16+
import Data.GI.Base (castTo)
17+
import Data.Int (Int32)
18+
import Data.Text (Text)
19+
import qualified GI.Gtk as Gtk
20+
import qualified GI.Pango as Pango
21+
import System.Environment.XDG.BaseDir (getUserConfigFile)
22+
import System.Taffybar.Context (TaffyIO)
23+
import System.Taffybar.Widget.Util
24+
( buildContentsBox,
25+
pixbufNewFromFileAtScaleByHeight,
26+
widgetSetClassGI,
27+
)
28+
29+
-- | Wrap the widget in a "TaffyBox" (via 'buildContentsBox') and add a CSS class.
30+
decorateWithClassAndBox :: (MonadIO m) => Text -> Gtk.Widget -> m Gtk.Widget
31+
decorateWithClassAndBox klass widget = do
32+
boxed <- buildContentsBox widget
33+
widgetSetClassGI boxed klass
34+
35+
decorateWithClassAndBoxM :: (MonadIO m) => Text -> m Gtk.Widget -> m Gtk.Widget
36+
decorateWithClassAndBoxM klass builder =
37+
builder >>= decorateWithClassAndBox klass
38+
39+
forEachLabelRecursively :: Gtk.Widget -> (Gtk.Label -> IO ()) -> IO ()
40+
forEachLabelRecursively widget action = do
41+
maybeLabel <- castTo Gtk.Label widget
42+
for_ maybeLabel action
43+
44+
maybeContainer <- castTo Gtk.Container widget
45+
case maybeContainer of
46+
Just container ->
47+
Gtk.containerGetChildren container >>= mapM_ (`forEachLabelRecursively` action)
48+
Nothing -> pure ()
49+
50+
setLabelAlignmentRecursively :: Float -> Gtk.Justification -> Gtk.Widget -> IO ()
51+
setLabelAlignmentRecursively xalign justify widget =
52+
forEachLabelRecursively widget $ \label -> do
53+
Gtk.labelSetXalign label xalign
54+
Gtk.labelSetJustify label justify
55+
56+
setFixedLabelWidth :: Int32 -> Gtk.Label -> IO ()
57+
setFixedLabelWidth width label = do
58+
Gtk.labelSetWidthChars label width
59+
Gtk.labelSetMaxWidthChars label width
60+
Gtk.labelSetEllipsize label Pango.EllipsizeModeEnd
61+
62+
stackInPill :: Text -> [TaffyIO Gtk.Widget] -> TaffyIO Gtk.Widget
63+
stackInPill klass builders =
64+
decorateWithClassAndBoxM klass $ do
65+
widgets <- sequence builders
66+
liftIO $ do
67+
box <- Gtk.boxNew Gtk.OrientationVertical 0
68+
mapM_ (\w -> Gtk.boxPackStart box w False False 0) widgets
69+
Gtk.widgetShowAll box
70+
Gtk.toWidget box
71+
72+
usageLogoWidget :: FilePath -> Text -> IO Gtk.Widget
73+
usageLogoWidget iconFile tooltip = do
74+
iconPath <- getUserConfigFile "taffybar" ("icons/" <> iconFile)
75+
iconWidget <-
76+
pixbufNewFromFileAtScaleByHeight 18 iconPath >>= \case
77+
Right pixbuf -> Gtk.toWidget =<< Gtk.imageNewFromPixbuf (Just pixbuf)
78+
Left _ -> Gtk.toWidget =<< Gtk.labelNew (Just "?")
79+
Gtk.widgetSetTooltipText iconWidget (Just tooltip)
80+
widgetSetClassGI iconWidget "usage-logo"

0 commit comments

Comments
 (0)