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