-
Notifications
You must be signed in to change notification settings - Fork 176
/
init.lua
189 lines (174 loc) · 6.56 KB
/
init.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
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
--- === ModalMgr ===
---
--- Modal keybindings environment management. Just an wrapper of `hs.hotkey.modal`.
---
--- Download: [https://github.com/Hammerspoon/Spoons/raw/master/Spoons/ModalMgr.spoon.zip](https://github.com/Hammerspoon/Spoons/raw/master/Spoons/ModalMgr.spoon.zip)
local obj = {}
obj.__index = obj
-- Metadata
obj.name = "ModalMgr"
obj.version = "1.0"
obj.author = "ashfinal <ashfinal@gmail.com>"
obj.homepage = "https://github.com/Hammerspoon/Spoons"
obj.license = "MIT - https://opensource.org/licenses/MIT"
obj.modal_tray = nil
obj.which_key = nil
obj.modal_list = {}
obj.active_list = {}
obj.supervisor = nil
function obj:init()
hsupervisor_keys = hsupervisor_keys or {{"cmd", "shift", "ctrl"}, "Q"}
obj.supervisor = hs.hotkey.modal.new(hsupervisor_keys[1], hsupervisor_keys[2], 'Initialize Modal Environment')
obj.supervisor:bind(hsupervisor_keys[1], hsupervisor_keys[2], "Reset Modal Environment", function() obj.supervisor:exit() end)
hshelp_keys = hshelp_keys or {{"alt", "shift"}, "/"}
obj.supervisor:bind(hshelp_keys[1], hshelp_keys[2], "Toggle Help Panel", function() obj:toggleCheatsheet({all=obj.supervisor}) end)
obj.modal_tray = hs.canvas.new({x = 0, y = 0, w = 0, h = 0})
obj.modal_tray:level(hs.canvas.windowLevels.tornOffMenu)
obj.modal_tray[1] = {
type = "circle",
action = "fill",
fillColor = {hex = "#FFFFFF", alpha = 0.7},
}
obj.which_key = hs.canvas.new({x = 0, y = 0, w = 0, h = 0})
obj.which_key:level(hs.canvas.windowLevels.tornOffMenu)
obj.which_key[1] = {
type = "rectangle",
action = "fill",
fillColor = {hex = "#EEEEEE", alpha = 0.95},
roundedRectRadii = {xRadius = 10, yRadius = 10},
}
end
--- ModalMgr:new(id)
--- Method
--- Create a new modal keybindings environment
---
--- Parameters:
--- * id - A string specifying ID of new modal keybindings
function obj:new(id)
obj.modal_list[id] = hs.hotkey.modal.new()
end
--- ModalMgr:toggleCheatsheet([idList], [force])
--- Method
--- Toggle the cheatsheet display of current modal environments's keybindings.
---
--- Parameters:
--- * iterList - An table specifying IDs of modal environments or active_list. Optional, defaults to all active environments.
--- * force - A optional boolean value to force show cheatsheet, defaults to `nil` (automatically).
function obj:toggleCheatsheet(iterList, force)
if obj.which_key:isShowing() and not force then
obj.which_key:hide()
else
local cscreen = hs.screen.mainScreen()
local cres = cscreen:fullFrame()
obj.which_key:frame({
x = cres.x + cres.w / 5,
y = cres.y + cres.h / 5,
w = cres.w / 5 * 3,
h = cres.h / 5 * 3
})
local keys_pool = {}
local tmplist = iterList or obj.active_list
for i, v in pairs(tmplist) do
if type(v) == "string" then
-- It appears to be idList
for _, m in ipairs(obj.modal_list[v].keys) do
table.insert(keys_pool, m.msg)
end
elseif type(i) == "string" then
-- It appears to be active_list
for _, m in pairs(v.keys) do
table.insert(keys_pool, m.msg)
end
end
end
for idx, val in ipairs(keys_pool) do
if idx % 2 == 1 then
obj.which_key[idx + 1] = {
type = "text",
text = keys_pool[idx],
textFont = "Courier-Bold",
textSize = 16,
textColor = {hex = "#2390FF", alpha = 1},
textAlignment = "left",
frame = {
x = tostring(40 / (cres.w / 5 * 3)),
y = tostring((30 + (idx - math.ceil(idx / 2)) * math.ceil((cres.h / 5 * 3 - 60) / #keys_pool) * 2) / (cres.h / 5 * 3)),
w = tostring((1 - 80 / (cres.w / 5 * 3)) / 2),
h = tostring(math.ceil((cres.h / 5 * 3 - 60) / #keys_pool) * 2 / (cres.h / 5 * 3))
}
}
else
obj.which_key[idx + 1] = {
type = "text",
text = keys_pool[idx],
textFont = "Courier-Bold",
textSize = 16,
textColor = {hex = "#2390FF"},
textAlignment = "right",
frame = {
x = "50%",
y = tostring((30 + (idx - math.ceil(idx / 2) - 1) * math.ceil((cres.h / 5 * 3 - 60) / #keys_pool) * 2) / (cres.h / 5 * 3)),
w = tostring((1 - 80 / (cres.w / 5 * 3)) / 2),
h = tostring(math.ceil((cres.h / 5 * 3 - 60) / #keys_pool) * 2 / (cres.h / 5 * 3))
}
}
end
end
obj.which_key:show()
end
end
--- ModalMgr:activate(idList, [trayColor], [showKeys])
--- Method
--- Activate all modal environment in `idList`.
---
--- Parameters:
--- * idList - An table specifying IDs of modal environments
--- * trayColor - An optional string (e.g. #000000) specifying the color of modalTray, defaults to `nil`.
--- * showKeys - A optional boolean value to show all available keybindings, defaults to `nil`.
function obj:activate(idList, trayColor, showKeys)
for _, val in ipairs(idList) do
obj.modal_list[val]:enter()
obj.active_list[val] = obj.modal_list[val]
end
if trayColor then
local cscreen = hs.screen.mainScreen()
local cres = cscreen:fullFrame()
local lcres = cscreen:absoluteToLocal(cres)
obj.modal_tray:frame(cscreen:localToAbsolute{
x = cres.w - 40,
y = cres.h - 40,
w = 20,
h = 20
})
obj.modal_tray[1].fillColor = {hex = trayColor, alpha = 0.7}
obj.modal_tray:show()
end
if showKeys then
obj:toggleCheatsheet(idList, true)
end
end
--- ModalMgr:deactivate(idList)
--- Method
--- Deactivate modal environments in `idList`.
---
--- Parameters:
--- * idList - An table specifying IDs of modal environments
function obj:deactivate(idList)
for _, val in ipairs(idList) do
obj.modal_list[val]:exit()
obj.active_list[val] = nil
end
obj.modal_tray:hide()
for i = 2, #obj.which_key do
obj.which_key:removeElement(2)
end
obj.which_key:hide()
end
--- ModalMgr:deactivateAll()
--- Method
--- Deactivate all active modal environments.
---
function obj:deactivateAll()
obj:deactivate(obj.active_list)
end
return obj