-
Notifications
You must be signed in to change notification settings - Fork 4
/
menubar_indicator.lua
132 lines (117 loc) · 3.87 KB
/
menubar_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
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
local as = require "hs.applescript"
local scr = require "hs.screen"
local draw = require "hs.drawing"
local geom = require "hs.geometry"
local col = draw.color
------- Functionality equivalent to ShowyEdge (https://pqrs.org/osx/ShowyEdge/index.html.en)
-- Global enable/disable
local enableIndicator = true
-- Display on all monitors or just the current one?
local allScreens = true
-- Specify 0.0-1.0 to specify a percentage of the height of the menu bar, larger values indicate a fixed height in pixels
local indicatorHeight = 1.0
-- transparency (1.0 - fully opaque)
local indicatorAlpha = 0.3
-- show the indicator in all spaces (this includes full-screen mode)
local indicatorInAllSpaces = true
---- Configuration of indicator colors
-- Each indicator is made of an arbitrary number of segments,
-- distributed evenly across the width of the screen. The table below
-- indicates the colors to use for a given keyboard layout. The index
-- is the name of the layout as it appears in the input source menu.
-- If a layout is not found, then the indicators are removed when that
-- layout is active.
local colors = {
-- Flag-like indicators
["Spanish"] = {col.green, col.white, col.red},
["German"] = {col.black, col.red, col.yellow},
-- Contrived example of programmatically-generated colors
-- ["U.S."] = (
-- function() res={}
-- for i = 0,10,1 do
-- table.insert(res, col.blue)
-- table.insert(res, col.white)
-- table.insert(res, col.red)
-- end
-- return res
-- end)(),
-- Solid colors
-- ["Spanish"] = {col.red},
-- ["German"] = {col.yellow},
}
----------------------------------------------------------------------
local ind = nil
function initIndicators()
if ind ~= nil then
delIndicators()
end
ind = {}
end
function delIndicators()
if ind ~= nil then
for i,v in ipairs(ind) do
if v ~= nil then
v:delete()
end
end
ind = nil
end
end
function getInputSource()
ok, result = as.applescript('tell application "System Events" to tell process "SystemUIServer" to get the value of the first menu bar item of menu bar 1 whose description is "text input"')
if ok then
return result
else
return nil
end
end
function drawIndicators(src)
initIndicators()
def = colors[src]
logger.df("Indicator definition for %s: %s", src, hs.inspect(def))
if def ~= nil then
if allScreens then
screens = scr.allScreens()
else
screens = { scr.mainScreen() }
end
for i,screen in ipairs(screens) do
local screeng = screen:fullFrame()
local width = screeng.w / #def
for i,v in ipairs(def) do
if indicatorHeight >= 0.0 and indicatorHeight <= 1.0 then
height = indicatorHeight*(screen:frame().y - screeng.y)
else
height = indicatorHeight
end
c = draw.rectangle(geom.rect(screeng.x+(width*(i-1)), screeng.y,
width, height))
c:setFillColor(v)
c:setFill(true)
c:setAlpha(indicatorAlpha)
c:setLevel(draw.windowLevels.overlay)
c:setStroke(false)
if indicatorInAllSpaces then
c:setBehavior(draw.windowBehaviors.canJoinAllSpaces)
end
c:show()
table.insert(ind, c)
end
end
else
logger.df("Removing indicators for %s because there is no color definitions for it.", src)
delIndicators()
end
end
function inputSourceChange()
source = getInputSource()
logger.df("New input source: " .. source)
drawIndicators(source)
end
if enableIndicator then
initIndicators()
-- Initial draw
drawIndicators(getInputSource())
-- Change whenever the input source changes
hs.keycodes.inputSourceChanged(inputSourceChange)
end