diff options
Diffstat (limited to 'accounts/gkleen@sif/taffybar/src/System/Taffybar/Widget/TooltipBattery.hs')
-rw-r--r-- | accounts/gkleen@sif/taffybar/src/System/Taffybar/Widget/TooltipBattery.hs | 101 |
1 files changed, 0 insertions, 101 deletions
diff --git a/accounts/gkleen@sif/taffybar/src/System/Taffybar/Widget/TooltipBattery.hs b/accounts/gkleen@sif/taffybar/src/System/Taffybar/Widget/TooltipBattery.hs deleted file mode 100644 index 9dc52774..00000000 --- a/accounts/gkleen@sif/taffybar/src/System/Taffybar/Widget/TooltipBattery.hs +++ /dev/null | |||
@@ -1,101 +0,0 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | ||
2 | {-# LANGUAGE ScopedTypeVariables #-} | ||
3 | module System.Taffybar.Widget.TooltipBattery ( batteryIconTooltipNew ) where | ||
4 | |||
5 | import Control.Applicative | ||
6 | import Control.Monad | ||
7 | import Control.Monad.IO.Class | ||
8 | import Control.Monad.Trans.Reader | ||
9 | import Data.Int (Int64) | ||
10 | import qualified Data.Text as T | ||
11 | import GI.Gtk | ||
12 | import Prelude | ||
13 | import StatusNotifier.Tray (scalePixbufToSize) | ||
14 | import System.Taffybar.Context | ||
15 | import System.Taffybar.Information.Battery | ||
16 | import System.Taffybar.Util | ||
17 | import System.Taffybar.Widget.Generic.AutoSizeImage | ||
18 | import System.Taffybar.Widget.Generic.ChannelWidget | ||
19 | import Text.Printf | ||
20 | import Text.StringTemplate | ||
21 | import Data.Function ((&)) | ||
22 | |||
23 | -- | Just the battery info that will be used for display (this makes combining | ||
24 | -- several easier). | ||
25 | data BatteryWidgetInfo = BWI | ||
26 | { seconds :: Maybe Int64 | ||
27 | , percent :: Double | ||
28 | , status :: String | ||
29 | , rate :: Maybe Double | ||
30 | } deriving (Eq, Show) | ||
31 | |||
32 | -- | Format a duration expressed as seconds to hours and minutes | ||
33 | formatDuration :: Int64 -> String | ||
34 | formatDuration secs = let minutes, hours, minutes' :: Int64 | ||
35 | minutes = secs `div` 60 | ||
36 | (hours, minutes') = minutes `divMod` 60 | ||
37 | in printf "%02d:%02d" hours minutes' | ||
38 | |||
39 | getBatteryWidgetInfo :: BatteryInfo -> BatteryWidgetInfo | ||
40 | getBatteryWidgetInfo info = | ||
41 | let battPctNum :: Double | ||
42 | battPctNum = batteryPercentage info | ||
43 | battTime :: Maybe Int64 | ||
44 | battTime = | ||
45 | case batteryState info of | ||
46 | BatteryStateCharging -> Just $ batteryTimeToFull info | ||
47 | BatteryStateDischarging -> Just $ batteryTimeToEmpty info | ||
48 | _ -> Nothing | ||
49 | battStatus :: String | ||
50 | battStatus = | ||
51 | case batteryState info of | ||
52 | BatteryStateCharging -> "↑" | ||
53 | BatteryStateDischarging -> "↓" | ||
54 | BatteryStateEmpty -> "⤓" | ||
55 | BatteryStateFullyCharged -> "⤒" | ||
56 | _ -> "?" | ||
57 | battRate :: Maybe Double | ||
58 | battRate | rawRate < 0.1 = Nothing | ||
59 | | otherwise = Just rawRate | ||
60 | where rawRate = batteryEnergyRate info | ||
61 | in BWI{ seconds = battTime, percent = battPctNum, status = battStatus, rate = battRate } | ||
62 | |||
63 | -- | Given (maybe summarized) battery info and format: provides the string to display | ||
64 | formatBattInfo :: BatteryWidgetInfo -> String -> T.Text | ||
65 | formatBattInfo info fmt = | ||
66 | let tpl = newSTMP fmt | ||
67 | tpl' = tpl | ||
68 | & setManyAttrib [ ("percentage", printf "%.0f" $ percent info) | ||
69 | , ("status", status info) | ||
70 | ] | ||
71 | & setManyAttrib [ ("time", formatDuration <$> seconds info) | ||
72 | , ("rate", printf "%.0f" <$> rate info) | ||
73 | ] | ||
74 | in render tpl' | ||
75 | |||
76 | themeLoadFlags :: [IconLookupFlags] | ||
77 | themeLoadFlags = [IconLookupFlagsGenericFallback, IconLookupFlagsUseBuiltin] | ||
78 | |||
79 | batteryIconTooltipNew :: String -> TaffyIO Widget | ||
80 | batteryIconTooltipNew format = do | ||
81 | DisplayBatteryChanVar (chan, _) <- setupDisplayBatteryChanVar ["IconName", "State", "Percentage", "TimeToFull", "TimeToEmpty", "EnergyRate"] | ||
82 | ctx <- ask | ||
83 | liftIO $ do | ||
84 | image <- imageNew | ||
85 | styleCtx <- widgetGetStyleContext =<< toWidget image | ||
86 | defaultTheme <- iconThemeGetDefault | ||
87 | let getCurrentBatteryIconNameStringTooltip = do | ||
88 | info <- runReaderT getDisplayBatteryInfo ctx | ||
89 | let iconNameString = T.pack $ batteryIconName info | ||
90 | tooltip = formatBattInfo (getBatteryWidgetInfo info) format | ||
91 | return (iconNameString, tooltip) | ||
92 | extractPixbuf info = | ||
93 | fst <$> iconInfoLoadSymbolicForContext info styleCtx | ||
94 | setIconForSize size = do | ||
95 | (name, tooltip) <- getCurrentBatteryIconNameStringTooltip | ||
96 | widgetSetTooltipMarkup image $ Just tooltip | ||
97 | iconThemeLookupIcon defaultTheme name size themeLoadFlags >>= | ||
98 | traverse extractPixbuf >>= | ||
99 | traverse (scalePixbufToSize size OrientationHorizontal) | ||
100 | updateImage <- autoSizeImage image setIconForSize OrientationHorizontal | ||
101 | toWidget =<< channelWidgetNew image chan (const $ postGUIASync updateImage) | ||