From 0cae5039d549ee757bcf382c46403335d05aa047 Mon Sep 17 00:00:00 2001 From: jao Date: Tue, 2 Dec 2014 05:02:30 +0100 Subject: New textOffset and iconOffset configuration parameters --- news.md | 5 +++++ readme.md | 9 +++++++++ samples/xmobar.config | 2 ++ src/Config.hs | 4 ++++ src/Parsers.hs | 4 ++++ src/Xmobar.hs | 42 ++++++++++++++++++++++++++---------------- 6 files changed, 50 insertions(+), 16 deletions(-) diff --git a/news.md b/news.md index 9f42561..a384d29 100644 --- a/news.md +++ b/news.md @@ -6,6 +6,11 @@ _New features_ - New variables in `Mem` monitor for available memory as reported by Linux 3.14 and newer, by Samuli Thomasson. + - New configuration parameters `textOffset` and `iconOffset` (see + discussion in [issue #171] and [issue #201]). + +[issue #171]: https://github.com/jaor/xmobar/issues/171 +[issue #201]: https://github.com/jaor/xmobar/issues/201 ## Version 0.22.1 (Oct 11, 2014) diff --git a/readme.md b/readme.md index 4600a84..1176401 100644 --- a/readme.md +++ b/readme.md @@ -274,6 +274,15 @@ Other configuration options: : position = Top +`textOffset` +: The vertical offset, in pixels, for the text baseline. If + negative or not given, xmobar will try to center text + vertically. + +`iconOffset` +: The vertical offset, in pixels, for icons bottom line. If negative + or not given, xmobar will try to center icons vertically. + `lowerOnStart` : When True the window is sent the bottom of the window stack initially. diff --git a/samples/xmobar.config b/samples/xmobar.config index 5b5a7d1..e1a1baa 100644 --- a/samples/xmobar.config +++ b/samples/xmobar.config @@ -4,6 +4,8 @@ Config { font = "-misc-fixed-*-*-*-*-10-*-*-*-*-*-*-*" , bgColor = "black" , fgColor = "grey" , position = Top + , textOffset = -1 + , iconOffset = -1 , lowerOnStart = True , pickBroadest = False , persistent = False diff --git a/src/Config.hs b/src/Config.hs index 3514e50..7e43e92 100644 --- a/src/Config.hs +++ b/src/Config.hs @@ -52,6 +52,8 @@ data Config = , bgColor :: String -- ^ Backgroud color , fgColor :: String -- ^ Default font color , position :: XPosition -- ^ Top Bottom or Static + , textOffset :: Int -- ^ Offset from top of window for text + , iconOffset :: Int -- ^ Offset from top of window for icons , border :: Border -- ^ NoBorder TopB BottomB or FullB , borderColor :: String -- ^ Border color , borderWidth :: Int -- ^ Border width @@ -113,6 +115,8 @@ defaultConfig = , border = NoBorder , borderColor = "#BFBFBF" , borderWidth = 1 + , textOffset = -1 + , iconOffset = -1 , hideOnStart = False , lowerOnStart = True , persistent = False diff --git a/src/Parsers.hs b/src/Parsers.hs index dceb4b7..cdc180d 100644 --- a/src/Parsers.hs +++ b/src/Parsers.hs @@ -199,6 +199,7 @@ parseConfig = runParser parseConf fields "Config" . stripComments perms = permute $ Config <$?> pFont <|?> pBgColor <|?> pFgColor <|?> pPosition + <|?> pTextOffset <|?> pIconOffset <|?> pBorder <|?> pBdColor <|?> pBdWidth <|?> pHideOnStart <|?> pAllDesktops <|?> pOverrideRedirect <|?> pPickBroadest <|?> pLowerOnStart <|?> pPersistent <|?> pIconRoot @@ -207,6 +208,7 @@ parseConfig = runParser parseConf fields "Config" . stripComments fields = [ "font", "bgColor", "fgColor", "sepChar", "alignSep" , "border", "borderColor" ,"template", "position" + , "textOffset", "iconOffset" , "allDesktops", "overrideRedirect", "pickBroadest" , "hideOnStart", "lowerOnStart", "persistent", "iconRoot" , "commands" @@ -220,6 +222,8 @@ parseConfig = runParser parseConf fields "Config" . stripComments pAlignSep = strField alignSep "alignSep" pTemplate = strField template "template" + pTextOffset = readField textOffset "textOffset" + pIconOffset = readField iconOffset "iconOffset" pPosition = readField position "position" pHideOnStart = readField hideOnStart "hideOnStart" pLowerOnStart = readField lowerOnStart "lowerOnStart" diff --git a/src/Xmobar.hs b/src/Xmobar.hs index 766b2fe..459d6ef 100644 --- a/src/Xmobar.hs +++ b/src/Xmobar.hs @@ -305,29 +305,39 @@ drawInWin (Rectangle _ _ wid ht) ~[left,center,right] = do -- resync io $ sync d True +verticalOffset :: (Integral b, Integral a, MonadIO m) => + a -> Widget -> XFont -> Config -> m b +verticalOffset ht (Text t) fontst conf + | textOffset conf > -1 = return $ fi (textOffset conf) + | otherwise = do + (as,ds) <- io $ textExtents fontst t + let bwidth = borderOffset (border conf) (borderWidth conf) + verticalMargin = (fi ht) - fi (as + ds) - 2 * fi (abs bwidth) + return $ (fi ht) - (fi ds) - (verticalMargin `div` 2) + bwidth + 1 +verticalOffset _ (Icon _) _ conf + | iconOffset conf > -1 = return $ fi (iconOffset conf) + | otherwise = do + let bwidth = borderOffset (border conf) (borderWidth conf) + return $ bwidth + 1 + -- | An easy way to print the stuff we need to print printStrings :: Drawable -> GC -> XFont -> Position -> Align -> [(Widget, String, Position)] -> X () printStrings _ _ _ _ _ [] = return () printStrings dr gc fontst offs a sl@((s,c,l):xs) = do r <- ask - (as,ds) <- case s of - Text t -> io $ textExtents fontst t - Icon _ -> return (0, 0) - let (conf,d) = (config &&& display) r - boffs = borderOffset (border conf) (borderWidth conf) + let (conf,d) = (config &&& display) r Rectangle _ _ wid ht = rect r - totSLen = foldr (\(_,_,len) -> (+) len) 0 sl - verticalMargin = (fi ht) - fi (as + ds) + boffs - valign = (fi ht) - (fi ds) - (verticalMargin `div` 2) - remWidth = fi wid - fi totSLen - offset = case a of - C -> (remWidth + offs) `div` 2 - R -> remWidth - L -> offs - (fc,bc) = case break (==',') c of - (f,',':b) -> (f, b ) - (f, _) -> (f, bgColor conf) + totSLen = foldr (\(_,_,len) -> (+) len) 0 sl + remWidth = fi wid - fi totSLen + offset = case a of + C -> (remWidth + offs) `div` 2 + R -> remWidth + L -> offs + (fc,bc) = case break (==',') c of + (f,',':b) -> (f, b ) + (f, _) -> (f, bgColor conf) + valign <- verticalOffset ht s fontst conf case s of (Text t) -> io $ printString d dr fontst gc fc bc offset valign t (Icon p) -> io $ maybe (return ()) (drawBitmap d dr gc fc bc offset valign) (lookup p (iconS r)) -- cgit v1.2.3