-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaffybar.hs
287 lines (261 loc) · 11.6 KB
/
taffybar.hs
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE ImplicitParams #-}
import Control.Monad ( void )
import Control.Monad.Trans.Class
import Control.Monad.Trans.Reader ( ReaderT(runReaderT)
, ask
)
import Data.GI.Base
import qualified Data.GI.Gtk as Gtk
import qualified Data.Text as T
import qualified GI.Gdk as Gdk
import GI.Gdk ( EventButton )
import System.Process ( spawnProcess )
import System.Taffybar.Context ( Context
, TaffyIO
, runX11Def
, subscribeToPropertyEvents
, unsubscribe
)
import System.Taffybar.Hooks ( NetworkInfoChan
( NetworkInfoChan
)
, getNetworkChan
)
import System.Taffybar.Information.CPU
( cpuLoad )
import System.Taffybar.Information.Memory
( MemoryInfo(..)
, parseMeminfo
)
import System.Taffybar.Information.Network
( sumSpeeds )
import System.Taffybar.Information.X11DesktopInfo
( getAtom
, readAsString
, sendCommandEvent
)
import System.Taffybar.SimpleConfig ( SimpleTaffyConfig(..)
, StrutSize(..)
, defaultSimpleTaffyConfig
, simpleTaffybar
)
import System.Taffybar.Util ( liftReader
, postGUIASync
)
import System.Taffybar.Widget ( ClockConfig(..)
, WindowsConfig(..)
, Workspace(..)
, WorkspacesConfig(..)
, defaultClockConfig
, defaultNetFormat
, defaultWindowsConfig
, defaultWorkspacesConfig
, networkGraphNew
, showInfo
, sniTrayNew
, textClockNewWith
, truncatedGetActiveLabel
, truncatedGetMenuLabel
, widgetSetClassGI
, windowsNew
, workspacesNew
)
import System.Taffybar.Widget.Generic.ChannelWidget
( channelWidgetNew )
import System.Taffybar.Widget.Generic.Graph
( GraphConfig(..)
, GraphDirection(..)
, defaultGraphConfig
)
import System.Taffybar.Widget.Generic.PollingBar
( BarConfig
( barBackgroundColor
, barBorderColor
)
, defaultBarConfig
, pollingBarNew
)
import System.Taffybar.Widget.Generic.PollingGraph
( pollingGraphNew )
mkRGBA :: Fractional a => (a, a, a, a) -> (a, a, a, a)
mkRGBA (r, g, b, a) = (r / 255, g / 255, b / 255, a)
blue, green, magenta, yellow1, yellow2 :: (Double, Double, Double, Double)
blue = mkRGBA (33, 150, 243, 1)
green = mkRGBA (77, 189, 140, 1)
magenta = mkRGBA (198, 120, 221, 1)
yellow1 = mkRGBA (236, 190, 123, 1)
yellow2 = mkRGBA (254, 204, 83, 1)
-- red = mkRGBA (255, 108, 107, 1)
colorGradient :: Double -> (Double, Double, Double)
colorGradient t
| t < 0.5
= ( (77 + (255 - 77) / 0.5 * t) / 255.0
, (189 + (181 - 189) / 0.5 * t) / 255.0
, (85 + (107 - 85) / 0.5 * t) / 255.0
)
| otherwise
= (1.0, (181 + (107 - 181) / 0.5 * (t - 0.5)) / 255.0, 107 / 255.0)
readTemp :: IO Double
readTemp = (/ 100000.0) . read <$> readFile
"/sys/class/thermal/thermal_zone2/temp"
cpuCallback :: IO [Double]
cpuCallback = do
(_, systemLoad, totalLoad) <- cpuLoad
return [totalLoad, systemLoad]
memCallback :: IO [Double]
memCallback = do
mi <- parseMeminfo
return [memoryUsedRatio mi]
myGraphConfig :: GraphConfig
myGraphConfig = defaultGraphConfig
{ graphBackgroundColor = (0.1, 0.1, 0.1, 0.3)
, graphBorderWidth = 0
, graphDirection = RIGHT_TO_LEFT
, graphWidth = 75
}
buttonOverlay :: Gtk.Widget -> T.Text -> IO () -> TaffyIO Gtk.Widget
buttonOverlay widget label action = lift $ do
btn <- new Gtk.Button []
_ <-
on btn #enterNotifyEvent
$ const
$ set ?self [#label := label]
>> return True
_ <-
on btn #leaveNotifyEvent
$ const
$ set ?self [#label := ""]
>> return True
_ <- on btn #buttonPressEvent $ \eButton -> do
eType <- get eButton #type
case eType of
Gdk.EventType2buttonPress -> action >> return False
_ -> return True
overlay <- new Gtk.Overlay []
_ <- #add overlay widget
_ <- #addOverlay overlay btn
_ <- #showAll overlay
Gtk.toWidget overlay
xLayoutProp :: String
xLayoutProp = "_XMONAD_CURRENT_LAYOUT"
dispatchButtonEvent :: Context -> EventButton -> IO Bool
dispatchButtonEvent context btn = do
pressType <- get btn #type
buttonNumber <- get btn #button
case pressType of
Gdk.EventTypeButtonPress -> case buttonNumber of
1 -> runReaderT (runX11Def () (switch (1 :: Integer))) context
>> return True
2 -> spawnProcess "xmonadctl" ["default-layout"] >> return True
3 -> spawnProcess "xdotool" ["key", "Super+Shift+grave"]
>> return True
_ -> return False
_ -> return False
where
switch n = do
cmd <- getAtom xLayoutProp
sendCommandEvent cmd (fromIntegral n)
main :: IO ()
main = do
let sep = do
sep' <- new
Gtk.Label
[ #label
:= "<span size=\"larger\" weight=\"bold\" color=\"#6b6d70\">|</span>"
, #useMarkup := True
]
#show sep'
Gtk.toWidget sep'
workspaces = workspacesNew defaultWorkspacesConfig
{ widgetGap = 2
, maxIcons = Just 3
, showWorkspaceFn = (/=) "NSP" . workspaceName
, urgentWorkspaceState = True
}
layout = do
ctx <- ask
label <- new Gtk.Label []
_ <- widgetSetClassGI label "layout-label"
let callback _ = liftReader postGUIASync $ do
layout' <- runX11Def "" $ readAsString Nothing xLayoutProp
let markup = T.pack layout'
Gtk.labelSetMarkup label markup
subscription <- subscribeToPropertyEvents [xLayoutProp] callback
do
ebox <- new
Gtk.EventBox
[ #tooltipMarkup
:= "left click:\tnext layout\nmiddle click:\tdefault layout\nright click:\ttoggle float"
]
_ <- #add ebox label
_ <- on ebox #buttonPressEvent $ dispatchButtonEvent ctx
_ <- #showAll ebox
_ <- on ebox #unrealize $ flip runReaderT ctx $ unsubscribe
subscription
Gtk.toWidget ebox
title = windowsNew defaultWindowsConfig
{ getMenuLabel = truncatedGetMenuLabel 80
, getActiveLabel = truncatedGetActiveLabel 80
}
clock =
textClockNewWith defaultClockConfig { clockFormatString = "%R" }
cpu = do
cpuGraph <- pollingGraphNew
myGraphConfig { graphDataColors = [green, magenta] }
0.5
cpuCallback
buttonOverlay
cpuGraph
"cpu"
(void $ spawnProcess
"/usr/bin/alacritty"
$ words "--class AlacrittyFloat,AlacrittyFloat -o window.dimensions.columns=200 -o window.dimensions.lines=40 -e htop --sort-key PERCENT_CPU"
)
mem = do
memGraph <- pollingGraphNew
myGraphConfig { graphDataColors = [blue] }
1
memCallback
buttonOverlay
memGraph
"mem"
(void $ spawnProcess
"/usr/bin/alacritty"
$ words "--class AlacrittyFloat,AlacrittyFloat -o window.dimensions.columns=200 -o window.dimensions.lines=40 -e htop --sort-key PERCENT_MEM"
)
network = do
netGraph <- networkGraphNew
myGraphConfig { graphDataColors = [yellow1, yellow2] }
Nothing
NetworkInfoChan chan <- getNetworkChan
network' <- buttonOverlay
netGraph
"net"
(void $ spawnProcess "nm-connection-editor" [])
_ <- channelWidgetNew network' chan $ \speedInfo ->
let (up, down) = sumSpeeds $ map snd speedInfo
tooltipString = showInfo
defaultNetFormat
3
(fromRational down, fromRational up)
in postGUIASync
$ set network' [#tooltipMarkup := tooltipString]
return network'
temp = pollingBarNew
((defaultBarConfig colorGradient)
{ barBorderColor = (0.1, 0.1, 0.1)
, barBackgroundColor = const (0.1, 0.1, 0.1)
}
)
0.5
readTemp
simpleTaffybar defaultSimpleTaffyConfig
{ barHeight = ScreenRatio $ 1 / 36
, barPadding = 6
, widgetSpacing = 4
, startWidgets = [workspaces, sep, layout, sep, title]
, centerWidgets = [clock]
, endWidgets = [sniTrayNew, sep, temp, network, mem, cpu]
}