-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule.lua
196 lines (158 loc) · 4.04 KB
/
module.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
190
191
192
193
194
195
196
local class = require("utils.class")
---@class Module: Class
---@operator call: Module
local Module = class.make_class("Module", class.Object)
---@alias SettingsItem {name: string, title: string, descr: string, control: string, default: any}
---@alias Action {name: string, title: string, descr: string, fn: function, default: string[]?}
---@alias HotkeySpec [string[], string]
---@param name string
---@param title string
---@param descr string
---@param settings_items SettingsItem[]
---@param actions Action[]
function Module:__init__(name, title, descr, settings_items, actions)
self.name = name
self.title = title
self.descr = descr
self.settings_schema = {
name=name,
title=title,
descr=descr,
items=settings_items,
}
self.actions = actions
self.loaded = false
self.started = false
self.hotkeys = {}
self.hyper_keys = {}
end
function Module:_check_loaded()
assert(self.loaded, "not loaded")
end
function Module:_check_started()
assert(self.started, "not started")
end
function Module:_check_loaded_and_started()
self:_check_loaded()
self:_check_started()
end
---@param settings SectionValues
function Module:load(settings)
assert(not self.loaded, "already loaded")
local core_modules = require("core_modules")
self.core_modules = core_modules
self:loadImpl(settings)
self.started = false
self.loaded = true
self:didLoad()
end
function Module:start()
assert(self.loaded, "not loaded")
assert(not self.started, "already started")
self:startImpl()
self:enableHotkeys()
self.started = true
self:didStart()
end
function Module:stop()
assert(self.loaded, "not loaded")
if not self.started then return end
self:disableHotkeys()
self:stopImpl()
self.started = false
self:didStop()
end
function Module:unload()
if not self.loaded then return end
self:stop()
self:unloadImpl()
self:unbindHotkeys()
self.loaded = false
self:didUnload()
end
function Module:loadImpl(settings) end
function Module:didLoad() end
function Module:startImpl() end
function Module:didStart() end
function Module:stopImpl() end
function Module:didStop() end
function Module:unloadImpl() end
function Module:didUnload() end
---@param spec table<string, HotkeySpec>
function Module:bindActionsHotkeys(spec)
if spec == nil then return end
local n_specs = 0
for _, _ in pairs(spec) do
n_specs = n_specs + 1
end
if n_specs == 0 then return end
self:unbindHotkeys()
for _, action in ipairs(self.actions) do
local s = spec[action.name]
if s == nil then goto continue end
local mods = s[1]
local key = s[2]
if mods == nil or #mods == 0 or key == nil then
print("warning: invalid hotkey spec for action: " .. action.name)
goto continue
end
local mods_contain_hyper = false
for _, mod in ipairs(mods) do
if mod == "hyper" then
mods_contain_hyper = true
break
end
end
if mods_contain_hyper then
if #mods ~= 1 then error("hyper key must be used alone") end
self:_bind_hyper_key(key, action.fn)
else
self:_bind_hotkey(mods, key, action.fn, true)
end
::continue::
end
end
---@param key string
---@param fn fun(...): nil
function Module:_bind_hyper_key(key, fn)
self.hyper_keys[key] = fn
if self.started then
self.core_modules.hyper:bind(key, fn)
end
end
---@param mods string[]
---@param key string
---@param fn fun(...): nil
---@param with_repeat boolean?
function Module:_bind_hotkey(mods, key, fn, with_repeat)
local hk = hs.hotkey.bind(mods, key, fn, nil, with_repeat and fn or nil)
if not self.started then
hk:disable()
end
table.insert(self.hotkeys, hk)
end
function Module:disableHotkeys()
for _, hk in ipairs(self.hotkeys) do
hk:disable()
end
for key, _ in pairs(self.hyper_keys) do
self.core_modules.hyper:unbind(key)
end
end
function Module:enableHotkeys()
for _, hk in ipairs(self.hotkeys) do
hk:enable()
end
for key, fn in pairs(self.hyper_keys) do
self.core_modules.hyper:bind(key, fn)
end
end
function Module:unbindHotkeys()
self:disableHotkeys()
for _, hk in ipairs(self.hotkeys) do
hk:delete()
end
self.hotkeys = {}
self.hyper_keys = {}
end
return Module