vanadium/xmobar: make dpi aware

This commit is contained in:
Primrose 2025-10-20 11:46:19 +08:00
parent 96508e6638
commit d638f445e3
Signed by: primrose
GPG key ID: 4E887A4CA9714ADA

View file

@ -2,6 +2,48 @@ import Xmobar
import Data.Function ((&))
import Data.List (intercalate)
import Data.Map qualified as M
import System.Process
parseXRDB :: String -> M.Map String String
parseXRDB = M.fromList . parseXRDBLines . lines
parseXRDBLines :: [String] -> [(String, String)]
parseXRDBLines = map parseXRDBLine
parseXRDBLine :: String -> (String, String)
parseXRDBLine s = case break (== ':') s of
(k, ':' : v) -> (k, dropWhile (== '\t') v)
_ -> error $ "Failed to parse " <> s
-- | Hack to adapt the bar height, fontsize, etc, with the chosen dpi in XRDB
withDynamicDPI :: Config -> IO Config
withDynamicDPI cfg = do
xrdbConfig <- parseXRDB <$> readCreateProcess (shell "xrdb -query") mempty
case xrdbConfig M.!? "Xft.dpi" of
Nothing -> pure cfg
Just dpiStr ->
let
dpi = read dpiStr
-- In case of low DPI:
-- * reduce height
-- * increase font size
isLowDPI = dpi < 120
updatePosition (BottomH x) = BottomH $ if isLowDPI then 15 else x
updatePosition x = x
updateFontSize x = k <> " " <> v'
where
(k, ' ' : v) = break (== ' ') x
v' = if isLowDPI then "9" else v
in
pure $ cfg
{ dpi = dpi
, position = updatePosition (position cfg)
, font = updateFontSize (font cfg)
}
config :: Config
config =
@ -99,4 +141,4 @@ minute :: Int -> Int
minute = (* 60) . second
main :: IO ()
main = xmobar config
main = withDynamicDPI config >>= xmobar