Repository navigation
Expand file tree
/
Copy pathinit.lua
More file actions
258 lines (226 loc) · 8.01 KB
/
Copy pathinit.lua
File metadata and controls
258 lines (226 loc) · 8.01 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
-- Zen Mode for macOS: a distraction-free mode for Hammerspoon
-- Author: Ryan Cabeen <cabeen@gmail.com>
-- Repository: https://github.com/cabeen/zen-mode
-- License: MIT (see LICENSE)
-- Snap windows instantly instead of Hammerspoon's choppy step-animation.
-- Note: this is a global Hammerspoon setting, so it affects any other
-- window-moving scripts in your config too. Zen Mode does its own animation.
hs.window.animationDuration = 0
local topMargin = 40
local bottomMargin = 40
local padding = -2
-- Corner rounding of the cutout. Should be at least the window's own corner
-- radius: too small leaks bright background at the corners, too large just
-- dims a sliver of the window corner. macOS 26 "Tahoe" windows are much
-- rounder (and vary per window); on older macOS ~10 is enough.
local cornerRadius = 24
local fillColor = {white = 0, alpha = 0.85}
local moveDuration = 0.3 -- seconds to glide the window to/from center; 0 = instant
local resizeDuration = 0.3 -- seconds to grow/shrink to full height; 0 = instant
local zenCanvas = nil
local zenWindow = nil
local originalFrame = nil
local escapeHotkey = nil
local zenTracker = nil
local zenAnimator = nil
local lastFrame = nil
local function stopTimers()
if zenTracker then
zenTracker:stop()
zenTracker = nil
end
if zenAnimator then
zenAnimator:stop()
zenAnimator = nil
end
end
-- The cutout rect, in canvas-local coordinates
local function cutoutFrame(f, base)
return {
x = f.x - base.x - padding,
y = f.y - base.y - padding,
w = f.w + (padding * 2),
h = f.h + (padding * 2)
}
end
-- Set the window frame, then fit the cutout to where the window actually
-- ended up: apps apply resizes late (and round to their own grid), and a
-- cutout that runs ahead of the window leaks bright background. Reading the
-- frame back means any residual lag errs dark instead of bright.
local function placeWindow(win, f)
win:setFrame(f, 0)
local actual = win:frame() or f
if zenCanvas then
zenCanvas[2].frame = cutoutFrame(actual, zenCanvas:frame())
end
return actual
end
local function easeInOutCubic(t)
if t < 0.5 then
return 4 * t ^ 3
end
return 1 - (-2 * t + 2) ^ 3 / 2
end
local function lerpFrame(a, b, t)
return {
x = a.x + (b.x - a.x) * t,
y = a.y + (b.y - a.y) * t,
w = a.w + (b.w - a.w) * t,
h = a.h + (b.h - a.h) * t
}
end
-- A frame of the given size, centered within the outer frame
local function centeredIn(outer, size)
return {
x = outer.x + (outer.w - size.w) / 2,
y = outer.y + (outer.h - size.h) / 2,
w = size.w,
h = size.h
}
end
-- Glide the window between frames while fading the backdrop, keeping the
-- cutout glued to the window the whole way, then call done()
local function animate(win, fromFrame, toFrame, alphaFrom, alphaTo, duration, done)
stopTimers()
local function finish()
if zenCanvas then zenCanvas:alpha(alphaTo) end
placeWindow(win, toFrame)
done()
end
if duration <= 0 then
finish()
return
end
local start = hs.timer.secondsSinceEpoch()
zenAnimator = hs.timer.doEvery(1 / 60, function()
local t = (hs.timer.secondsSinceEpoch() - start) / duration
if t >= 1 then
zenAnimator:stop()
zenAnimator = nil
finish()
return
end
local e = easeInOutCubic(t)
if zenCanvas then
zenCanvas:alpha(alphaFrom + (alphaTo - alphaFrom) * e)
end
placeWindow(win, lerpFrame(fromFrame, toFrame, e))
end)
end
-- Tear everything down once the exit transition has landed
local function finishExit()
if zenCanvas then
zenCanvas:delete()
zenCanvas = nil
end
zenWindow = nil
originalFrame = nil
lastFrame = nil
end
local function exitZenMode()
stopTimers()
-- Crucial: Give Cmd+Escape back to macOS!
if escapeHotkey then
escapeHotkey:disable()
end
if zenCanvas and zenWindow and originalFrame then
local current = zenWindow:frame()
if current then
-- Shrink back to the original size first, under the dark
-- backdrop, then glide home as the darkness lifts
local win = zenWindow
local alpha = zenCanvas:alpha()
local mid = centeredIn(current, originalFrame)
animate(win, current, mid, alpha, alpha, resizeDuration, function()
local settled = win:frame() or mid
animate(win, settled, originalFrame, alpha, 0, moveDuration, finishExit)
end)
return
end
end
finishExit()
end
-- Keep the cutout glued to the window if it is moved or resized
local function trackWindow()
if not (zenCanvas and zenWindow) then return end
local f = zenWindow:frame()
if not f or f.w == 0 then
-- Window closed or minimized; tear down without touching it
zenWindow = nil
exitZenMode()
return
end
if lastFrame and f == lastFrame then return end
lastFrame = f
zenCanvas[2].frame = cutoutFrame(f, zenCanvas:frame())
end
-- Create the Cmd+Escape hotkey, but DO NOT enable it yet.
-- Plain Escape is left alone so Vim and other Escape-heavy apps keep it.
escapeHotkey = hs.hotkey.new({"cmd"}, "escape", function()
exitZenMode()
end)
-- Bind to Option (alt) + Command (cmd) + Z
hs.hotkey.bind({"alt", "cmd"}, "z", function()
if zenCanvas then
-- If already active (or mid-transition), toggle it off
exitZenMode()
else
-- Toggle Zen Mode ON. Check that window operations actually work
-- before darkening anything: without Accessibility access the
-- blackout canvas would still appear, burying the very Settings
-- window (or permission prompt) the user needs to see. A functional
-- test beats asking the system, which can misreport the state.
local win = hs.window.focusedWindow()
local frame = win and win:frame()
if not (frame and frame.w > 0 and frame.h > 0) then
hs.alert.show(
"Zen Mode: no focusable window found.\n" ..
"If a window is focused, Hammerspoon may be missing\n" ..
"Accessibility access (System Settings → Privacy & Security).",
3
)
return
end
zenWindow = win
originalFrame = frame
local screen = win:screen()
local fullMax = screen:fullFrame()
local f = win:frame()
f.y = fullMax.y + topMargin
f.h = fullMax.h - topMargin - bottomMargin
f.x = fullMax.x + (fullMax.w / 2) - (f.w / 2)
zenCanvas = hs.canvas.new(fullMax)
zenCanvas:level(hs.canvas.windowLevels.cursor)
zenCanvas:appendElements(
{
type = "rectangle",
action = "fill",
fillColor = fillColor
},
{
type = "rectangle",
roundedRectRadii = {xRadius = cornerRadius, yRadius = cornerRadius},
action = "fill",
compositeRule = "clear",
frame = cutoutFrame(originalFrame, fullMax)
}
)
-- Start fully transparent, then fade in as the window glides.
-- Moves are cheap for apps to apply, resizes are not: glide the
-- window at its original size, and only grow to full height once
-- the backdrop is fully dark.
zenCanvas:alpha(0)
zenCanvas:show()
win:focus()
local mid = centeredIn(f, originalFrame)
animate(win, originalFrame, mid, 0, 1, moveDuration, function()
local settled = win:frame() or mid
animate(win, settled, f, 1, 1, resizeDuration, function()
lastFrame = win:frame() or f
zenTracker = hs.timer.doEvery(0.03, trackWindow)
end)
end)
-- Enable the Cmd+Escape intercept ONLY while Zen Mode is active
escapeHotkey:enable()
end
end)