about summary refs log tree commit diff
path: root/machines/profpatsch/taffybar.patch
blob: a93fca1ab4074f455ce93c497f2b816a32c95f91 (plain) (blame)
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
diff --git a/src/System/Taffybar/Battery.hs b/src/System/Taffybar/Battery.hs
index 5335eff..32c7efa 100644
--- a/src/System/Taffybar/Battery.hs
+++ b/src/System/Taffybar/Battery.hs
@@ -9,6 +9,7 @@
 -- more advanced features could be supported if there is interest.
 module System.Taffybar.Battery (
   batteryBarNew,
+  batteryIconNew,
   textBatteryNew,
   defaultBatteryConfig
   ) where
@@ -108,30 +109,22 @@ defaultBatteryConfig =
       | pct < 0.9 = (0.5, 0.5, 0.5)
       | otherwise = (0, 1, 0)
 
--- | A fancy graphical battery widget that represents the current
--- charge as a colored vertical bar.  There is also a textual
--- percentage readout next to the bar.
+-- | 
 batteryBarNew :: BarConfig -- ^ Configuration options for the bar display
-                 -> Double -- ^ Polling period in seconds
                  -> IO Widget
-batteryBarNew battCfg pollSeconds = do
+batteryBarNew battCfg = do
   battCtxt <- batteryContextNew
-  case battCtxt of
-    Nothing -> do
-      let lbl :: Maybe String
-          lbl = Just "No battery"
-      labelNew lbl >>= return . toWidget
-    Just ctxt -> do
-      -- This is currently pretty inefficient - each poll period it
-      -- queries the battery twice (once for the label and once for
-      -- the bar).
-      --
-      -- Converting it to combine the two shouldn't be hard.
-      b <- hBoxNew False 1
-      txt <- textBatteryNew "$percentage$%" pollSeconds
-      r <- newIORef ctxt
-      bar <- pollingBarNew battCfg pollSeconds (battPct r)
-      boxPackStart b bar PackNatural 0
-      boxPackStart b txt PackNatural 0
-      widgetShowAll b
-      return (toWidget b)
+  let noBat = toWidget <$> labelNew (Just "No battery" :: Maybe String)
+  maybe noBat (batteryIconNew battCfg) battCtxt
+
+-- | A fancy graphical battery widget that represents the current
+-- charge as a colored vertical bar.
+batteryIconNew :: BarConfig
+                  -> BatteryContext
+                  -> IO Widget
+batteryIconNew cfg ctxt = do
+    icon <- pollingBarNew cfg pollSeconds . battPct =<< newIORef ctxt
+    widgetShowAll icon
+    return icon
+      where
+        pollSeconds = 5
diff --git a/src/System/Taffybar/Widgets/PollingBar.hs b/src/System/Taffybar/Widgets/PollingBar.hs
index d30adaf..01f161c 100644
--- a/src/System/Taffybar/Widgets/PollingBar.hs
+++ b/src/System/Taffybar/Widgets/PollingBar.hs
@@ -16,6 +16,7 @@ import Control.Monad ( forever )
 import Graphics.UI.Gtk
 
 import System.Taffybar.Widgets.VerticalBar
+import Debug.Trace
 
 pollingBarNew :: BarConfig -> Double -> IO Double -> IO Widget
 pollingBarNew cfg pollSeconds action = do