-
Notifications
You must be signed in to change notification settings - Fork 13
/
mode-indicator.lua
80 lines (67 loc) · 2.6 KB
/
mode-indicator.lua
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
--- === ModeIndicator ===
---
--- Mode indicator implementation that displays the current Ki mode in a menubar item
---
local ModeIndicator = {}
local spoonPath = hs.spoons.scriptPath()
ModeIndicator.timer = nil
ModeIndicator.isDefault = true
ModeIndicator.menubar = hs.menubar.new()
--- ModeIndicator.isDarkMode
--- Variable
--- A boolean value indicating whether the menu bar style is in dark mode. This value will be determined automatically.
ModeIndicator.isDarkMode = false
-- Text styles
local smallMenlo = { name = "Menlo", size = 9 }
local darkColor = { red = 0.5, blue = 0.5, green = 0.5 }
local darkTextStyle = { color = darkColor, font = smallMenlo }
local lightColor = { red = 0.8, blue = 0.8, green = 0.8 }
local lightTextStyle = { color = lightColor, font = smallMenlo }
--- ModeIndicator:show(mode)
--- Method
--- Shows a text display on center of the menu bar to indicate the current mode
---
--- Parameters:
--- * `mode` - a string value containing the current mode (i.e., `"normal"`, `"entity"`, etc.)
---
--- Returns:
--- * None
function ModeIndicator:show(mode)
-- Interrupt fade-out timer if running
if self.timer and self.timer:running() then
self.timer:stop()
end
-- Determine based on dark mode status which style is faded or normal
local fadedTextStyle = self.isDarkMode and lightTextStyle or darkTextStyle
local normalTextStyle = self.isDarkMode and darkTextStyle or lightTextStyle
-- Stylize text and update title in menu bar
local text = "-- "..mode:upper().." --"
local textElement = hs.styledtext.new(text, fadedTextStyle)
self.menubar:setTitle(textElement)
-- Dim desktop mode text after some timeout
if mode == "desktop" then
self.timer = hs.timer.doAfter(0.5, function()
self.menubar:setTitle(hs.styledtext.new(text, normalTextStyle))
end)
end
end
-- Handle streaming task output
function ModeIndicator:handleTaskOutput(_, stdout)
if stdout == "Dark" then
self.isDarkMode = true
return false
end
return true
end
-- Initialize mode indicator with desktop mode
function ModeIndicator:initialize()
self:show("desktop")
end
-- Start swift task in the background to retrieve the dark mode status
local swiftBin = "/usr/bin/swift"
local swiftFile = spoonPath.."bin/print-dark-mode.swift"
local taskDoneCallback = function(...) return ModeIndicator:initialize(...) end
local streamingCallback = function(...) return ModeIndicator:handleTaskOutput(...) end
local getDarkModeTask = hs.task.new(swiftBin, taskDoneCallback, streamingCallback, { swiftFile })
getDarkModeTask:start()
return ModeIndicator