From d638f445e38926709702f02335ca16ad78514cc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Mon, 20 Oct 2025 11:46:19 +0800 Subject: [PATCH] vanadium/xmobar: make dpi aware --- .../vanadium/home/xmobar/xmobar.hs | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/nix/configurations/vanadium/home/xmobar/xmobar.hs b/nix/configurations/vanadium/home/xmobar/xmobar.hs index 99778b5b..42755533 100644 --- a/nix/configurations/vanadium/home/xmobar/xmobar.hs +++ b/nix/configurations/vanadium/home/xmobar/xmobar.hs @@ -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