-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
277 lines (230 loc) · 8.55 KB
/
Copy pathinit.lua
File metadata and controls
277 lines (230 loc) · 8.55 KB
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
-- Hammerspoon config
hs.window.animationDuration = 0
dictationNudge = {
enabled = true,
threshold = 25,
windowSeconds = 90,
cooldownSeconds = 30 * 60,
typed = 0,
windowStartedAt = 0,
lastShownAt = 0,
}
local textEntryKeys = {
space = true,
['return'] = true,
tab = true,
}
for char = string.byte('a'), string.byte('z') do
textEntryKeys[string.char(char)] = true
end
for digit = 0, 9 do
textEntryKeys[tostring(digit)] = true
end
for _, key in ipairs({ '-', '=', '[', ']', '\\', ';', "'", ',', '.', '/', '`' }) do
textEntryKeys[key] = true
end
local function maybeNudgeForDictation(event)
if not dictationNudge.enabled then
return false
end
local flags = event:getFlags()
if flags.cmd or flags.ctrl or flags.alt or flags.fn then
return false
end
local key = hs.keycodes.map[event:getKeyCode()]
if not textEntryKeys[key] then
return false
end
local now = hs.timer.secondsSinceEpoch()
if now - dictationNudge.windowStartedAt > dictationNudge.windowSeconds then
dictationNudge.typed = 0
dictationNudge.windowStartedAt = now
end
if dictationNudge.windowStartedAt == 0 then
dictationNudge.windowStartedAt = now
end
dictationNudge.typed = dictationNudge.typed + 1
if dictationNudge.typed >= dictationNudge.threshold and now - dictationNudge.lastShownAt > dictationNudge.cooldownSeconds then
dictationNudge.lastShownAt = now
dictationNudge.typed = 0
dictationNudge.windowStartedAt = now
hs.alert.show('press fn to dictate', 3)
end
return false
end
dictationNudge.eventtap = hs.eventtap.new({ hs.eventtap.event.types.keyDown }, maybeNudgeForDictation):start()
local ratios = {
side = { 0.50, 0.67, 0.75, 0.25, 0.33 },
vertical = { 0.50, 0.67, 0.75 },
corner = { 0.50, 0.67 },
}
local function unit(position, ratio)
if position == 'left' then
return { x = 0.00, y = 0.00, w = ratio, h = 1.00 }
elseif position == 'right' then
return { x = 1.00 - ratio, y = 0.00, w = ratio, h = 1.00 }
elseif position == 'top' then
return { x = 0.00, y = 0.00, w = 1.00, h = ratio }
elseif position == 'bottom' then
return { x = 0.00, y = 1.00 - ratio, w = 1.00, h = ratio }
elseif position == 'topleft' then
return { x = 0.00, y = 0.00, w = ratio, h = ratio }
elseif position == 'topright' then
return { x = 1.00 - ratio, y = 0.00, w = ratio, h = ratio }
elseif position == 'bottomleft' then
return { x = 0.00, y = 1.00 - ratio, w = ratio, h = ratio }
elseif position == 'bottomright' then
return { x = 1.00 - ratio, y = 1.00 - ratio, w = ratio, h = ratio }
end
end
local function nearlyEqual(a, b)
return math.abs(a - b) < 0.01
end
local function sameUnit(a, b)
return nearlyEqual(a.x, b.x) and nearlyEqual(a.y, b.y) and nearlyEqual(a.w, b.w) and nearlyEqual(a.h, b.h)
end
local function currentUnit(window)
local frame = window:frame()
local screenFrame = window:screen():frame()
return {
x = (frame.x - screenFrame.x) / screenFrame.w,
y = (frame.y - screenFrame.y) / screenFrame.h,
w = frame.w / screenFrame.w,
h = frame.h / screenFrame.h,
}
end
local function bindCycle(mods, key, position, cycleRatios)
hs.hotkey.bind(mods, key, function()
local window = hs.window.focusedWindow()
if not window then return end
local activeUnit = currentUnit(window)
local nextIndex = 1
for index, ratio in ipairs(cycleRatios) do
if sameUnit(activeUnit, unit(position, ratio)) then
nextIndex = (index % #cycleRatios) + 1
break
end
end
window:move(unit(position, cycleRatios[nextIndex]), nil, true)
end)
end
local maximum = { x = 0.00, y = 0.00, w = 1.00, h = 1.00 }
local resizeStep = 0.15
local function moveFocusedWindow(targetUnit)
local window = hs.window.focusedWindow()
if window then window:move(targetUnit, nil, true) end
end
local function resizeSnappedWindow(delta)
local window = hs.window.focusedWindow()
if not window then return end
local activeUnit = currentUnit(window)
local targetUnit = nil
if sameUnit(activeUnit, maximum) and delta > 0 then
targetUnit = {
x = resizeStep,
y = activeUnit.y,
w = 1.00 - resizeStep,
h = activeUnit.h,
}
elseif nearlyEqual(activeUnit.x, 0.00) then
targetUnit = {
x = 0.00,
y = activeUnit.y,
w = math.max(resizeStep, math.min(1.00, activeUnit.w + delta)),
h = activeUnit.h,
}
elseif nearlyEqual(activeUnit.x + activeUnit.w, 1.00) then
local nextWidth = math.max(resizeStep, math.min(1.00, activeUnit.w - delta))
targetUnit = {
x = 1.00 - nextWidth,
y = activeUnit.y,
w = nextWidth,
h = activeUnit.h,
}
end
if targetUnit then window:move(targetUnit, nil, true) end
end
mash = { 'ctrl', 'option', 'cmd' }
hs.hotkey.bind(mash, 'l', function() moveFocusedWindow(unit('right', 0.50)) end)
hs.hotkey.bind(mash, 'h', function() moveFocusedWindow(unit('left', 0.50)) end)
hs.hotkey.bind(mash, '.', function() resizeSnappedWindow(resizeStep) end)
hs.hotkey.bind(mash, ',', function() resizeSnappedWindow(-resizeStep) end)
bindCycle(mash, 'k', 'top', ratios.vertical)
bindCycle(mash, 'j', 'bottom', ratios.vertical)
bindCycle(mash, 'u', 'topleft', ratios.corner)
hs.hotkey.bind(mash, 'i', function() hs.application.launchOrFocus('Linear') end)
bindCycle(mash, 'o', 'bottomright', ratios.corner)
bindCycle(mash, 'p', 'bottomleft', ratios.corner)
hs.hotkey.bind(mash, 'm', function()
local window = hs.window.focusedWindow()
if window then window:move(maximum, nil, true) end
end)
local function logVivaldiShortcut(message)
hs.printf('[vivaldi-shortcuts] %s', message)
end
local function focusVivaldiWorkspaceTab(label, workspace, tab)
logVivaldiShortcut(string.format('%s requested; target workspace=%s tab=%s', label, workspace, tab or 'none'))
hs.application.launchOrFocus('Vivaldi')
logVivaldiShortcut('Called hs.application.launchOrFocus("Vivaldi")')
hs.timer.doAfter(0.05, function()
logVivaldiShortcut(string.format('Sending Vivaldi workspace shortcut ctrl+shift+%s', workspace))
hs.eventtap.keyStroke({ 'ctrl', 'shift' }, tostring(workspace))
if tab then
hs.timer.doAfter(0.05, function()
logVivaldiShortcut(string.format('Sending Vivaldi tab shortcut cmd+%s', tab))
hs.eventtap.keyStroke({ 'cmd' }, tostring(tab))
end)
else
logVivaldiShortcut(string.format('%s is workspace-only; skipping tab shortcut', label))
end
end)
end
local vivaldiShortcuts = {
{ key = 'g', label = 'Gmail', workspace = 2, tab = 1 },
{ key = 's', label = 'SMS', workspace = 2, tab = 3 },
{ key = 'y', label = 'Jellyfin', workspace = 3, tab = 2 },
{ key = 'v', label = 'Video workspace', workspace = 3 },
{ key = 'b', label = 'Airbnb workspace', workspace = 4 },
{ key = '4', label = 'Money', workspace = 5 },
{ key = 'd', label = 'DayZ', workspace = 6 },
}
for _, shortcut in ipairs(vivaldiShortcuts) do
hs.hotkey.bind(mash, shortcut.key, function()
logVivaldiShortcut(string.format('Global shortcut ctrl+option+cmd+%s fired', shortcut.key))
focusVivaldiWorkspaceTab(shortcut.label, shortcut.workspace, shortcut.tab)
end)
end
hs.hotkey.bind(mash, 'n', function()
-- Get the focused window, its window frame dimensions, its screen frame dimensions,
-- and the next screen's frame dimensions.
local focusedWindow = hs.window.focusedWindow()
local focusedScreenFrame = focusedWindow:screen():frame()
local nextScreenFrame = focusedWindow:screen():next():frame()
local windowFrame = focusedWindow:frame()
-- Calculate the coordinates of the window frame in the next screen and retain aspect ratio
windowFrame.x = ((((windowFrame.x - focusedScreenFrame.x) / focusedScreenFrame.w) * nextScreenFrame.w) + nextScreenFrame.x)
windowFrame.y = ((((windowFrame.y - focusedScreenFrame.y) / focusedScreenFrame.h) * nextScreenFrame.h) + nextScreenFrame.y)
windowFrame.h = ((windowFrame.h / focusedScreenFrame.h) * nextScreenFrame.h)
windowFrame.w = ((windowFrame.w / focusedScreenFrame.w) * nextScreenFrame.w)
-- Set the focused window's new frame dimensions
focusedWindow:setFrame(windowFrame)
end)
function focus_app_tab(app, name)
return function()
hs.osascript.javascript([[
(function() {
var brave = Application(']] .. app .. [[');
brave.activate();
for (win of brave.windows()) {
var tabIndex =
win.tabs().findIndex(tab => tab.name().match(/]] .. name .. [[/));
if (tabIndex != -1) {
win.activeTabIndex = (tabIndex + 1);
win.index = 1;
}
}
})();
]])
end
end
-- hs.hotkey.bind({"alt", "cmd"}, "Left", focus_app_tab('Google Chrome', '.*Inbox.*'));