-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse_grid.lua
More file actions
1606 lines (1469 loc) · 59.3 KB
/
Copy pathmouse_grid.lua
File metadata and controls
1606 lines (1469 loc) · 59.3 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Mouse Grid: keyboard-driven mouse control (Mouseless-style)
-- Tap left Cmd alone to show a full-screen hint grid. Type a cell's two
-- characters, then a subgrid key (or Space for cell center) to click there.
-- Modifiers on the final key: Shift = right click, Ctrl = double click,
-- Alt = move only, Cmd = arm drag (next selection drops). Backspace undoes
-- one level, Tab moves to the next screen, "," enters scroll mode, Esc exits.
-- Nudge: HOLD the final key instead of tapping it, then use arrows or
-- h/j/k/l to move the cursor in small steps (Shift = bigger); releasing the
-- held key executes the action at the nudged position.
-- Tap left Alt alone for free mode: h/j/k/l move the cursor smoothly
-- (Shift = fast, Ctrl = slow), Space clicks (Shift = right, Ctrl = double,
-- Cmd = drag toggle), m/,/.// scroll, Esc or idle timeout exits.
-- Double-tap left Cmd for hints mode (Shortcat-style): actionable UI
-- elements of the focused window are scanned via the Accessibility API and
-- labelled; typing a label clicks the element (same final-key modifiers as
-- the grid). Space enters search: type the element's text to filter, Tab
-- cycles matches, Enter acts. Backspace un-types, Esc exits.
local M = {}
local etypes = hs.eventtap.event.types
local eprops = hs.eventtap.event.properties
local kmap = hs.keycodes.map
local KEYCODE_LEFT_CMD = 55
local KEYCODE_LEFT_ALT = 58
local MAGIC = 0x4D475244 -- tags self-synthesized events so taps ignore them
-- Config defaults (overridden via M.init(cfg))
local config = {
firstChars = "abcdefghijklmnopqrstuvwxyz", -- rows (a-z, top to bottom)
secondChars = "qwertasdfgzxcvb", -- columns (keyboard rows, left to right)
subgridKeys = { "qwert", "asdfg", "zxcvb" }, -- 5x3 subgrid, spatial layout
tapTimeout = 0.25, -- max seconds for a modifier tap to trigger
scrollKey = ",",
scrollStep = 40, -- pixels per scroll keypress (Shift = x5)
nudgeStep = 2, -- pixels per nudge keypress while holding the final key (Shift = x5)
dragSteps = 5, -- interpolation steps for drag movement
freeSpeed = 600, -- free mode cursor speed, px/sec
freeFastMultiplier = 4, -- free mode speed with Shift held
freeSlowMultiplier = 0.25, -- free mode speed with Ctrl held
freeIdleTimeout = 10, -- seconds of inactivity before free mode exits
hintChars = "fjdkslaghrueiwoqp", -- hints mode label alphabet (home-row first)
doubleTapWindow = 0.3, -- max seconds between two Cmd taps for hints mode
hintsMaxDepth = 60, -- AX traversal depth limit (browser trees are deep)
hintsMaxElements = 400, -- cap on rendered hints (also stops the search early)
hintsRescanDelay = 0.2, -- delay before the one-shot rescan after an AX fixup
hintsMinElements = 5, -- fewer first-pass results than this triggers the rescan
hintsRoles = { -- AX roles considered actionable (set-style, overridable)
AXButton = true, AXLink = true, AXMenuItem = true, AXMenuItemCheckbox = true,
AXCheckBox = true, AXRadioButton = true, AXPopUpButton = true, AXComboBox = true,
AXTextField = true, AXTextArea = true, AXMenuButton = true,
AXDisclosureTriangle = true, AXSlider = true, AXCell = true,
},
enhancedUIApps = { -- Chromium: needs AXEnhancedUserInterface for web content
["com.google.Chrome"] = true, ["company.thebrowser.Browser"] = true,
["com.brave.Browser"] = true, ["com.microsoft.edgemac"] = true,
["org.chromium.Chromium"] = true, ["com.vivaldi.Vivaldi"] = true,
},
electronApps = { -- Electron: AXManualAccessibility (no window-manager side effects)
["com.tinyspeck.slackmacgap"] = true, ["com.hnc.Discord"] = true,
["com.microsoft.VSCode"] = true, ["notion.id"] = true,
},
colors = {
background = { red = 0, green = 0, blue = 0, alpha = 0.3 },
gridLine = { white = 1, alpha = 0.25 },
label = { white = 1, alpha = 0.9 },
labelBackground = { red = 0, green = 0, blue = 0, alpha = 0.55 },
rowHighlight = { red = 1, green = 0.8, blue = 0.2, alpha = 0.25 },
subBackground = { red = 0.1, green = 0.1, blue = 0.15, alpha = 0.3 },
subLabel = { red = 1, green = 0.85, blue = 0.3, alpha = 1 },
badge = { red = 1, green = 0.3, blue = 0.3, alpha = 0.9 },
hintBackground = { red = 1, green = 0.85, blue = 0.3, alpha = 0.95 },
hintLabel = { red = 0.1, green = 0.1, blue = 0.1, alpha = 1 },
hintTyped = { red = 0.8, green = 0.1, blue = 0.1, alpha = 1 },
hintDimAlpha = 0.15, -- alpha multiplier for hints filtered out by typing
freeGlow = { red = 1, green = 0.85, blue = 0.3, alpha = 0.5 },
},
}
-- Private state
local activationTap = nil
local modalTap = nil
local gridCanvas = nil -- currently shown grid canvas (owned by canvasCache)
local focusCanvas = nil -- per-keystroke feedback canvas
local canvasCache = {} -- screen id -> { frame, canvas }
local screenWatcher = nil
local currentScreen = nil
local mode = "idle" -- idle | first | second | sub | scroll | free
local selRow, selCol = nil, nil
local dragOrigin = nil -- global point while a grid drag is armed
local nudgeKey = nil -- subgrid char (or "space") held for nudging
local tapPending = nil -- { kc, downAt } while a tap-candidate modifier is held
-- Hints mode state
local hintsTap = nil -- dedicated modal eventtap while hints are up
local hintsCanvas = nil -- hint-badge canvas
local hints = {} -- array of { label, frame (global), cx, cy }
local hintsTyped = "" -- label prefix typed so far
local hintsQuery = nil -- search text while the search sub-mode is active (nil = label mode)
local hintsSelIdx = 1 -- selected match while searching (Tab cycles)
local hintsSearch = nil -- in-flight elementSearch object (supports :cancel())
local hintsScanGen = 0 -- generation counter: drops stale async callbacks
local hintsWinFrame = nil -- focused window AXFrame at scan time (clip rect)
local hintsNotice = nil -- status string shown in the toast instead of tips
local hintsAXFixup = nil -- { axApp, attr } to restore on exit
local hintsRescanTimer = nil -- one-shot rescan timer after an AX fixup
local hintsRescanned = false -- only rescan once per session
local lastCmdTapAt = 0 -- when the last lone-Cmd tap completed (double-tap detect)
-- Free mode state
local freeTap = nil
local freeCanvas = nil -- badge canvas while free mode is active
local freeGlowCanvas = nil -- screen-edge glow canvas while free mode is active
local freeHeld = {} -- set: movement key char -> true
local freeButtonHeld = false -- left button held by free-mode drag toggle
local freeMoveTimer, freeIdleTimer = nil, nil
local freeLastTick = 0
local freeRemX, freeRemY = 0, 0 -- fractional pixel remainders
local freeBadgeScreenId = nil
-- Forward declarations (must precede every function that references them)
local cellRect, cellCenter, subCellPoint, buildGridElements, getGridCanvas
local postMouse, postClick, doClick, armDrag, finishDrag, cancelDrag
local enterScrollMode, exitScrollMode, handleScrollKey, postScroll
local pointOnScreen, clampToScreens, freeMoveTick, startFreeMove, stopFreeMove
local updateFreeBadge, updateFreeGlow, resetFreeIdle, toggleFreeDrag
local enterFreeMode, exitFreeMode, toggleFreeMode
local freeKeyImpl, handleFreeKey
local performAction, drawFocus, addBadge, showOnScreen, moveToNextScreen
local beginNudge, endNudgeCancel
local showOverlay, hideOverlay, toggleOverlay
local modalKeyImpl, handleModalKey, onlyFlag, handleActivationEvent
local generateHintLabels, hintsRoot, applyAXFixup, restoreAXFixup
local startHintsScan, onHintsScanResults, drawHints
local armHintsDrag, performHintsAction
local enterHintsMode, exitHintsMode, toggleHints
local hintsKeyImpl, handleHintsKey
local dismissActiveOverlay
-- Tap-to-activate specs: actions are closures so the forward-declared
-- locals resolve at call time, not at table construction
local tapSpecs = {
[KEYCODE_LEFT_CMD] = {
flag = "cmd",
action = function() toggleOverlay() end,
doubleAction = function() toggleHints() end, -- two taps within doubleTapWindow
},
[KEYCODE_LEFT_ALT] = { flag = "alt", action = function() toggleFreeMode() end },
}
-- Geometry: cell rects are in canvas-local coordinates; click points are
-- global (fullFrame offset applied), so they land correctly on any monitor.
cellRect = function(row, col)
local f = currentScreen:fullFrame()
local cw = f.w / #config.secondChars
local ch = f.h / #config.firstChars
return { x = (col - 1) * cw, y = (row - 1) * ch, w = cw, h = ch }
end
cellCenter = function(row, col)
local f = currentScreen:fullFrame()
local r = cellRect(row, col)
return { x = f.x + r.x + r.w / 2, y = f.y + r.y + r.h / 2 }
end
subCellPoint = function(row, col, sr, sc)
local f = currentScreen:fullFrame()
local r = cellRect(row, col)
local sw = r.w / #config.subgridKeys[1]
local sh = r.h / #config.subgridKeys
return {
x = f.x + r.x + (sc - 0.5) * sw,
y = f.y + r.y + (sr - 0.5) * sh,
}
end
buildGridElements = function(frame)
local rows, cols = #config.firstChars, #config.secondChars
local cw, ch = frame.w / cols, frame.h / rows
local elems = {}
elems[#elems + 1] = {
type = "rectangle", action = "fill",
fillColor = config.colors.background,
frame = { x = 0, y = 0, w = frame.w, h = frame.h },
}
for c = 1, cols - 1 do
elems[#elems + 1] = {
type = "segments", action = "stroke",
strokeColor = config.colors.gridLine, strokeWidth = 0.5,
coordinates = { { x = c * cw, y = 0 }, { x = c * cw, y = frame.h } },
}
end
for r = 1, rows - 1 do
elems[#elems + 1] = {
type = "segments", action = "stroke",
strokeColor = config.colors.gridLine, strokeWidth = 0.5,
coordinates = { { x = 0, y = r * ch }, { x = frame.w, y = r * ch } },
}
end
local fs = math.max(9, math.min(ch * 0.4, cw * 0.55))
-- Menlo advance width is ~0.6em; pad so the pill hugs the two letters
local bw, bh = fs * 0.6 * 2 + 8, fs * 1.4
for r = 1, rows do
for c = 1, cols do
elems[#elems + 1] = {
type = "rectangle", action = "fill",
fillColor = config.colors.labelBackground,
roundedRectRadii = { xRadius = 3, yRadius = 3 },
frame = {
x = (c - 1) * cw + (cw - bw) / 2,
y = (r - 1) * ch + (ch - bh) / 2,
w = bw, h = bh,
},
}
elems[#elems + 1] = {
type = "text",
text = config.firstChars:sub(r, r) .. config.secondChars:sub(c, c),
textSize = fs, textFont = "Menlo",
textColor = config.colors.label, textAlignment = "center",
frame = {
x = (c - 1) * cw,
y = (r - 1) * ch + (ch - fs * 1.25) / 2,
w = cw, h = fs * 1.5,
},
}
end
end
return elems
end
getGridCanvas = function(screen)
local id = tostring(screen:id())
local f = screen:fullFrame()
local cached = canvasCache[id]
if cached and cached.frame.x == f.x and cached.frame.y == f.y
and cached.frame.w == f.w and cached.frame.h == f.h then
return cached.canvas
end
if cached then cached.canvas:delete() end
local canvas = hs.canvas.new(f)
canvas:level(hs.canvas.windowLevels.screenSaver)
canvas:behavior({ "canJoinAllSpaces", "stationary" })
-- Single bulk install: never append elements one by one (slow with ~270)
canvas:appendElements(table.unpack(buildGridElements(f)))
canvasCache[id] = { frame = { x = f.x, y = f.y, w = f.w, h = f.h }, canvas = canvas }
print("mouse_grid: built grid canvas for screen " .. id)
return canvas
end
-- Mouse event synthesis
postMouse = function(evType, point, clickState)
local ev = hs.eventtap.event.newMouseEvent(evType, point)
-- Clear flags so a still-held Shift/Ctrl/Cmd from the final keystroke
-- doesn't turn this into a modified click
ev:setFlags({})
ev:setProperty(eprops.eventSourceUserData, MAGIC)
if clickState then ev:setProperty(eprops.mouseEventClickState, clickState) end
ev:post()
end
postClick = function(point, kind)
if kind == "right" then
postMouse(etypes.rightMouseDown, point)
postMouse(etypes.rightMouseUp, point)
elseif kind == "double" then
postMouse(etypes.leftMouseDown, point, 1)
postMouse(etypes.leftMouseUp, point, 1)
postMouse(etypes.leftMouseDown, point, 2)
postMouse(etypes.leftMouseUp, point, 2)
else
postMouse(etypes.leftMouseDown, point, 1)
postMouse(etypes.leftMouseUp, point, 1)
end
print(string.format("mouse_grid: %s click at %.0f,%.0f", kind, point.x, point.y))
end
doClick = function(point, kind)
dismissActiveOverlay()
-- Small delay so the overlay is gone before the click lands
hs.timer.doAfter(0.05, function()
hs.mouse.absolutePosition(point)
postClick(point, kind)
end)
end
armDrag = function(point)
hs.mouse.absolutePosition(point)
postMouse(etypes.leftMouseDown, point, 1)
dragOrigin = point
selRow, selCol = nil, nil
mode = "first"
drawFocus()
print(string.format("mouse_grid: drag armed at %.0f,%.0f", point.x, point.y))
end
finishDrag = function(point)
local origin = dragOrigin
dragOrigin = nil
dismissActiveOverlay()
hs.timer.doAfter(0.05, function()
for i = 1, config.dragSteps do
local t = i / config.dragSteps
local p = {
x = origin.x + (point.x - origin.x) * t,
y = origin.y + (point.y - origin.y) * t,
}
hs.mouse.absolutePosition(p)
postMouse(etypes.leftMouseDragged, p)
hs.timer.usleep(20000)
end
postMouse(etypes.leftMouseUp, point, 1)
print(string.format("mouse_grid: dropped at %.0f,%.0f", point.x, point.y))
end)
end
cancelDrag = function()
if not dragOrigin then return end
postMouse(etypes.leftMouseUp, dragOrigin, 1)
dragOrigin = nil
print("mouse_grid: drag cancelled")
end
-- Scroll mode
enterScrollMode = function()
mode = "scroll"
if gridCanvas then gridCanvas:hide() end
drawFocus()
end
exitScrollMode = function()
mode = "first"
if gridCanvas then
gridCanvas:show()
if focusCanvas then focusCanvas:orderAbove(gridCanvas) end
end
drawFocus()
end
postScroll = function(h, v)
local ev = hs.eventtap.event.newScrollEvent({ h, v }, {}, "pixel")
ev:setProperty(eprops.eventSourceUserData, MAGIC)
ev:post()
end
handleScrollKey = function(ch, kc, flags)
local step = config.scrollStep * (flags.shift and 5 or 1)
local h, v = 0, 0
if ch == "j" or kc == kmap.down then v = -step
elseif ch == "k" or kc == kmap.up then v = step
elseif ch == "h" or kc == kmap.left then h = step
elseif ch == "l" or kc == kmap.right then h = -step
else return end
postScroll(h, v)
end
-- Free mode: relative cursor movement, no overlay
local FREE_MOVE = { h = true, j = true, k = true, l = true }
-- Scroll keys matched by physical keycode (the four keys right of N), so
-- they work on any layout: "m,./" on US, "m,.-" on Spanish ISO, etc.
local FREE_SCROLL_KEYCODES = { 46, 43, 47, 44 } -- left, up, down, right
local FREE_SCROLL = {
[46] = { 1, 0 }, [43] = { 0, 1 }, [47] = { 0, -1 }, [44] = { -1, 0 },
}
local FREE_DRAG_TIPS = "FREE · DRAG · hjkl move · Space drops · Esc cancels"
local function freeTipsLabel()
local keys = ""
for _, kc in ipairs(FREE_SCROLL_KEYCODES) do
local ch = kmap[kc]
keys = keys .. ((type(ch) == "string" and #ch == 1) and ch or "?")
end
return "FREE · hjkl move (⇧ fast ⌃ slow) · Space click (⇧ right ⌃ dbl ⌘ drag) · "
.. keys .. " scroll · Esc"
end
pointOnScreen = function(x, y)
for _, s in ipairs(hs.screen.allScreens()) do
local f = s:fullFrame()
if x >= f.x and x < f.x + f.w and y >= f.y and y < f.y + f.h then
return s
end
end
return nil
end
clampToScreens = function(nx, ny, ox, oy)
-- Accept the new point if it's on some screen; otherwise slide along
-- edges (x-only / y-only) so L-shaped layouts don't trap the cursor
local s = pointOnScreen(nx, ny)
if s then return nx, ny, s end
s = pointOnScreen(nx, oy)
if s then return nx, oy, s end
s = pointOnScreen(ox, ny)
if s then return ox, ny, s end
return ox, oy, pointOnScreen(ox, oy)
end
freeMoveTick = function()
local now = hs.timer.secondsSinceEpoch()
local dt = math.min(now - freeLastTick, 0.05) -- clamp stalls
freeLastTick = now
local dx = (freeHeld.l and 1 or 0) - (freeHeld.h and 1 or 0)
local dy = (freeHeld.j and 1 or 0) - (freeHeld.k and 1 or 0)
if dx == 0 and dy == 0 then return end
if dx ~= 0 and dy ~= 0 then dx, dy = dx * 0.7071, dy * 0.7071 end
local mods = hs.eventtap.checkKeyboardModifiers()
local mult = (mods.shift and config.freeFastMultiplier)
or (mods.ctrl and config.freeSlowMultiplier) or 1
-- Re-read each tick: drag events or the user may have moved the cursor
local pos = hs.mouse.absolutePosition()
local fx = dx * config.freeSpeed * mult * dt + freeRemX
local fy = dy * config.freeSpeed * mult * dt + freeRemY
local ix = fx >= 0 and math.floor(fx) or math.ceil(fx)
local iy = fy >= 0 and math.floor(fy) or math.ceil(fy)
freeRemX, freeRemY = fx - ix, fy - iy
local nx, ny, scr = clampToScreens(pos.x + ix, pos.y + iy, pos.x, pos.y)
local p = { x = nx, y = ny }
hs.mouse.absolutePosition(p)
if freeButtonHeld then postMouse(etypes.leftMouseDragged, p) end
if scr and scr:id() ~= freeBadgeScreenId then updateFreeBadge() end
resetFreeIdle() -- holding a movement key counts as activity
end
startFreeMove = function()
if freeMoveTimer then return end
freeLastTick = hs.timer.secondsSinceEpoch()
freeRemX, freeRemY = 0, 0
freeMoveTimer = hs.timer.doEvery(1 / 60, freeMoveTick)
end
stopFreeMove = function()
if freeMoveTimer then freeMoveTimer:stop(); freeMoveTimer = nil end
end
-- Soft glow along the screen edges so free mode is obvious at a glance.
-- hs.canvas has no real blur, so layer inset rounded-rect strokes with a
-- quadratic alpha falloff to fake it.
updateFreeGlow = function(scr)
if freeGlowCanvas then freeGlowCanvas:delete() end
local f = scr:fullFrame()
local g = config.colors.freeGlow
freeGlowCanvas = hs.canvas.new(f)
freeGlowCanvas:level(hs.canvas.windowLevels.screenSaver)
freeGlowCanvas:behavior({ "canJoinAllSpaces", "stationary" })
local layers, sw = 6, 5
-- Radius shrinks with the inset so every layer's corner arc shares one
-- center; a fixed radius would leave √2-spaced gaps between the rings
-- at the corners
local baseRadius = (layers - 1) * sw + 5
local elems = {}
for i = 0, layers - 1 do
local inset = i * sw + sw / 2
local r = baseRadius - i * sw
elems[#elems + 1] = {
type = "rectangle", action = "stroke",
strokeColor = { red = g.red, green = g.green, blue = g.blue,
alpha = (g.alpha or 1) * (1 - i / layers) ^ 2 },
strokeWidth = sw,
roundedRectRadii = { xRadius = r, yRadius = r },
frame = { x = inset, y = inset, w = f.w - 2 * inset, h = f.h - 2 * inset },
}
end
freeGlowCanvas:appendElements(table.unpack(elems))
freeGlowCanvas:show()
end
updateFreeBadge = function()
local scr = hs.mouse.getCurrentScreen() or hs.screen.mainScreen()
freeBadgeScreenId = scr:id()
updateFreeGlow(scr)
local f = scr:fullFrame()
local label = freeButtonHeld and FREE_DRAG_TIPS or freeTipsLabel()
local w = math.min(f.w - 40, 34 + utf8.len(label) * 8.7)
local h = 38
if freeCanvas then freeCanvas:delete() end
freeCanvas = hs.canvas.new({ x = f.x + (f.w - w) / 2, y = f.y + f.h - 76, w = w, h = h })
freeCanvas:level(hs.canvas.windowLevels.screenSaver)
freeCanvas:behavior({ "canJoinAllSpaces", "stationary" })
freeCanvas:appendElements(
{
type = "rectangle", action = "fill",
fillColor = { red = 0.06, green = 0.06, blue = 0.1, alpha = 0.9 },
roundedRectRadii = { xRadius = 8, yRadius = 8 },
frame = { x = 0, y = 0, w = w, h = h },
},
{
type = "text", text = label, textSize = 15,
textColor = { white = 1, alpha = 0.95 }, textAlignment = "center",
frame = { x = 0, y = 9, w = w, h = 22 },
}
)
freeCanvas:show()
end
resetFreeIdle = function()
if freeIdleTimer then freeIdleTimer:setNextTrigger(config.freeIdleTimeout) end
end
toggleFreeDrag = function(pos)
if freeButtonHeld then
postMouse(etypes.leftMouseUp, pos, 1)
freeButtonHeld = false
print("mouse_grid: free drag dropped")
else
postMouse(etypes.leftMouseDown, pos, 1)
freeButtonHeld = true
print("mouse_grid: free drag started")
end
updateFreeBadge()
end
enterFreeMode = function()
mode = "free"
freeHeld = {}
freeButtonHeld = false
freeRemX, freeRemY = 0, 0
freeTap:start()
updateFreeBadge()
freeIdleTimer = hs.timer.doAfter(config.freeIdleTimeout, function()
print("mouse_grid: free mode idle timeout")
exitFreeMode()
end)
print("mouse_grid: free mode on")
end
exitFreeMode = function()
if freeButtonHeld then -- never leave the system with a stuck button
postMouse(etypes.leftMouseUp, hs.mouse.absolutePosition(), 1)
freeButtonHeld = false
end
stopFreeMove()
if freeIdleTimer then freeIdleTimer:stop(); freeIdleTimer = nil end
if freeTap then freeTap:stop() end
if freeCanvas then freeCanvas:delete(); freeCanvas = nil end
if freeGlowCanvas then freeGlowCanvas:delete(); freeGlowCanvas = nil end
freeHeld = {}
freeBadgeScreenId = nil
mode = "idle"
print("mouse_grid: free mode off")
end
toggleFreeMode = function()
if mode == "free" then
exitFreeMode()
elseif mode == "hints" then -- switch hints -> free
cancelDrag()
exitHintsMode()
enterFreeMode()
elseif mode ~= "idle" then -- grid is up: switch to free mode
cancelDrag()
hideOverlay()
enterFreeMode()
else
enterFreeMode()
end
end
freeKeyImpl = function(ev)
-- Consumed keys never reach the activation tap, so cancel pending taps
-- here too (same reason as modalKeyImpl)
tapPending = nil
lastCmdTapAt = 0
local isDown = ev:getType() == etypes.keyDown
local kc = ev:getKeyCode()
local flags = ev:getFlags()
local ch = kmap[kc]
if type(ch) ~= "string" or #ch ~= 1 then
ch = ev:getCharacters(true)
end
ch = ch and ch:lower() or ""
resetFreeIdle()
if FREE_MOVE[ch] then
if isDown then
freeHeld[ch] = true
startFreeMove()
else
freeHeld[ch] = nil
if not next(freeHeld) then stopFreeMove() end
end
return
end
if not isDown then return end -- everything below acts on keyDown only
local repeating = (ev:getProperty(eprops.keyboardEventAutorepeat) or 0) ~= 0
if kc == kmap.escape then
if not repeating then exitFreeMode() end
return
end
if kc == kmap.space then
if flags.fn then return "pass" end -- fn+Space is the STT toggle; don't swallow it
if repeating then return end -- no machine-gun clicks
local pos = hs.mouse.absolutePosition()
if flags.cmd or freeButtonHeld then
-- Cmd+Space toggles the button; any Space while held drops it
toggleFreeDrag(pos)
elseif flags.shift then
postClick(pos, "right")
elseif flags.ctrl then
postClick(pos, "double")
else
postClick(pos, "left")
end
return
end
local sv = FREE_SCROLL[kc] -- keycode-matched: layout-independent
if sv then -- autorepeat-driven, like grid scroll mode
local step = config.scrollStep * (flags.shift and 5 or 1)
postScroll(sv[1] * step, sv[2] * step)
end
end
handleFreeKey = function(ev)
local ok, result = pcall(freeKeyImpl, ev)
if not ok then
-- A live error here would leave the tap consuming the keyboard
print("mouse_grid: error in free key handler: " .. tostring(result))
pcall(exitFreeMode)
return true
end
if result == "pass" then return false end -- let the event reach other taps (e.g. STT)
return true -- consume everything else while free mode is active
end
-- Hints mode: Shortcat-style element hints. The focused window's
-- accessibility tree is searched (async, so big browser trees don't
-- beachball Hammerspoon) for actionable roles; each hit gets a short label
-- drawn at its top-left. Typing a label filters live; completing one acts
-- at the element's center with the same final-key modifiers as the grid.
local HINTS_TIPS = "type label · Space search · ⇧ right ⌃ dbl ⌥ move ⌘ drag · Bksp undo · Esc"
-- Prefix-free label set (Vimium-style trie expansion): pop the oldest
-- (shortest) label and replace it with its children — removing the parent
-- is what keeps the set prefix-free, so an exact match can fire immediately
-- even while longer labels exist
generateHintLabels = function(n)
local chars = {}
for i = 1, #config.hintChars do chars[i] = config.hintChars:sub(i, i) end
local queue, head = {}, 1
for _, c in ipairs(chars) do queue[#queue + 1] = c end
while #queue - head + 1 < n do
local parent = queue[head]
head = head + 1
for _, c in ipairs(chars) do queue[#queue + 1] = parent .. c end
end
local labels = {}
for i = head, math.min(#queue, head + n - 1) do labels[#labels + 1] = queue[i] end
return labels
end
-- Chromium hides web content from the AX tree unless AXEnhancedUserInterface
-- is set; Electron uses AXManualAccessibility instead. Enabled only while
-- hints mode is active (AXEnhancedUserInterface is the VoiceOver flag and is
-- known to glitch window snapping), and only for known bundle IDs.
applyAXFixup = function(app)
hintsAXFixup = nil
if not app then return end
local bundle = app:bundleID()
local attr = (config.enhancedUIApps[bundle] and "AXEnhancedUserInterface")
or (config.electronApps[bundle] and "AXManualAccessibility")
if not attr then return end
local axApp = hs.axuielement.applicationElement(app)
if not axApp then return end
local ok, prev = pcall(axApp.attributeValue, axApp, attr)
if ok and prev == true then return end -- already on: nothing to restore
pcall(axApp.setAttributeValue, axApp, attr, true)
hintsAXFixup = { axApp = axApp, attr = attr }
print("mouse_grid: enabled " .. attr .. " on " .. tostring(bundle))
end
restoreAXFixup = function()
if not hintsAXFixup then return end
pcall(hintsAXFixup.axApp.setAttributeValue, hintsAXFixup.axApp, hintsAXFixup.attr, false)
hintsAXFixup = nil
end
hintsRoot = function(app)
if not app then return nil end
local axApp = hs.axuielement.applicationElement(app)
if not axApp then return nil end
local ok, win = pcall(axApp.attributeValue, axApp, "AXFocusedWindow")
if ok and win then return win end
local w = app:focusedWindow()
return w and hs.axuielement.windowElement(w) or nil
end
-- Searchable text for an element (Shortcat-style search sub-mode)
local elementText = function(el)
local parts = {}
for _, attr in ipairs({ "AXTitle", "AXDescription", "AXValue", "AXPlaceholderValue" }) do
local ok, v = pcall(el.attributeValue, el, attr)
if ok and type(v) == "string" and #v > 0 then
parts[#parts + 1] = v:sub(1, 200)
end
end
return table.concat(parts, " "):lower()
end
-- Indices into `hints` matching the current search query (plain substring)
local hintsMatchIndices = function()
local q = hintsQuery or ""
local out = {}
for i, h in ipairs(hints) do
if q == "" or (h.text and h.text:find(q, 1, true)) then
out[#out + 1] = i
end
end
return out
end
drawHints = function()
if not hintsCanvas then return end
local f = currentScreen:fullFrame()
local elems = {}
local bg, fg, typedColor = config.colors.hintBackground,
config.colors.hintLabel, config.colors.hintTyped
local cw, bh = 7, 16 -- Menlo 11pt advance width, badge height
local matchList = hintsQuery and hintsMatchIndices() or nil
if matchList then
-- Search sub-mode: outline matches instead of labels (typing goes
-- to the query); the Tab-selected match gets the accent outline
local sel = matchList[math.min(math.max(hintsSelIdx, 1), math.max(#matchList, 1))]
for _, i in ipairs(matchList) do
local h = hints[i]
local isSel = (i == sel)
elems[#elems + 1] = {
type = "rectangle", action = "stroke",
strokeColor = isSel and config.colors.badge or bg,
strokeWidth = isSel and 2.5 or 1.5,
roundedRectRadii = { xRadius = 3, yRadius = 3 },
frame = { x = h.frame.x - f.x, y = h.frame.y - f.y,
w = h.frame.w, h = h.frame.h },
}
end
local label = string.format("%sSEARCH · \"%s\" · %d hits · ⇥ next · ⏎ act (⇧ right ⌃ dbl ⌥ move ⌘ drag) · Esc back",
dragOrigin and "DRAG · " or "", hintsQuery, #matchList)
if hintsNotice then label = "HINTS · " .. hintsNotice end
addBadge(elems, f, label)
hintsCanvas:replaceElements(table.unpack(elems))
return
end
for _, h in ipairs(hints) do
local matched = h.label:sub(1, #hintsTyped) == hintsTyped
local alpha = matched and 1 or config.colors.hintDimAlpha
local bw = 8 + #h.label * cw
local bx = math.max(0, math.min(h.frame.x - f.x, f.w - bw))
local by = math.max(0, math.min(h.frame.y - f.y, f.h - bh))
elems[#elems + 1] = {
type = "rectangle", action = "fill",
fillColor = { red = bg.red, green = bg.green, blue = bg.blue,
alpha = (bg.alpha or 1) * alpha },
roundedRectRadii = { xRadius = 3, yRadius = 3 },
frame = { x = bx, y = by, w = bw, h = bh },
}
local tx = bx + 4
if matched and #hintsTyped > 0 then -- typed prefix in accent color
elems[#elems + 1] = {
type = "text", text = h.label:sub(1, #hintsTyped),
textSize = 11, textFont = "Menlo", textColor = typedColor,
frame = { x = tx, y = by + 1.5, w = #hintsTyped * cw + 2, h = 14 },
}
tx = tx + #hintsTyped * cw
end
local rest = matched and h.label:sub(#hintsTyped + 1) or h.label
if #rest > 0 then
elems[#elems + 1] = {
type = "text", text = rest,
textSize = 11, textFont = "Menlo",
textColor = { red = fg.red, green = fg.green, blue = fg.blue,
alpha = (fg.alpha or 1) * alpha },
frame = { x = tx, y = by + 1.5, w = #rest * cw + 4, h = 14 },
}
end
end
local label
if hintsNotice then
label = "HINTS · " .. hintsNotice
elseif hintsSearch then
label = "HINTS · scanning…"
else
label = (dragOrigin and "HINTS · DRAG · " or "HINTS · ") .. HINTS_TIPS
end
addBadge(elems, f, label)
hintsCanvas:replaceElements(table.unpack(elems))
end
onHintsScanResults = function(results)
-- Clip to focused window ∩ current screen: drops offscreen and
-- scrolled-out elements that still report a frame
local f = currentScreen:fullFrame()
local clip = { x = f.x, y = f.y, w = f.w, h = f.h }
local wf = hintsWinFrame
if wf then
local x1, y1 = math.max(clip.x, wf.x), math.max(clip.y, wf.y)
local x2 = math.min(clip.x + clip.w, wf.x + wf.w)
local y2 = math.min(clip.y + clip.h, wf.y + wf.h)
if x2 > x1 and y2 > y1 then
clip = { x = x1, y = y1, w = x2 - x1, h = y2 - y1 }
end
end
local list, seen = {}, {}
for i = 1, #results do
local el = results[i]
-- pcall everything: AX elements die mid-flight when apps mutate their UI
local ok, fr = pcall(el.attributeValue, el, "AXFrame")
if ok and type(fr) == "table" and fr.w and fr.w > 2 and fr.h > 2 then
local cx, cy = fr.x + fr.w / 2, fr.y + fr.h / 2
if cx >= clip.x and cx < clip.x + clip.w
and cy >= clip.y and cy < clip.y + clip.h then
-- Dedupe by rounded frame: kills nested AXCell duplicates
local key = math.floor(fr.x) .. ":" .. math.floor(fr.y)
.. ":" .. math.floor(fr.w) .. ":" .. math.floor(fr.h)
if not seen[key] then
seen[key] = true
list[#list + 1] = {
frame = { x = fr.x, y = fr.y, w = fr.w, h = fr.h },
cx = cx, cy = cy,
text = elementText(el), -- for the search sub-mode
}
end
end
end
end
-- Chromium/Electron populate the web subtree lazily after the AX
-- attribute flips, so a thin first pass gets one delayed rescan
if #list < config.hintsMinElements and hintsAXFixup and not hintsRescanned then
hintsRescanned = true
hintsRescanTimer = hs.timer.doAfter(config.hintsRescanDelay, function()
hintsRescanTimer = nil
if mode == "hints" then
startHintsScan(hs.application.frontmostApplication())
drawHints()
end
end)
return
end
table.sort(list, function(a, b) -- reading order, so labels are predictable
if a.frame.y ~= b.frame.y then return a.frame.y < b.frame.y end
return a.frame.x < b.frame.x
end)
while #list > config.hintsMaxElements do list[#list] = nil end
local labels = generateHintLabels(#list)
hints = {}
for i, item in ipairs(list) do
hints[i] = { label = labels[i], frame = item.frame,
cx = item.cx, cy = item.cy, text = item.text }
end
hintsTyped = ""
hintsSelIdx = 1
if #hints == 0 then
hintsNotice = "no clickable elements found"
local gen = hintsScanGen
hs.timer.doAfter(1.2, function()
if gen == hintsScanGen and mode == "hints" then exitHintsMode() end
end)
else
hintsNotice = nil
end
drawHints()
print(string.format("mouse_grid: hints found %d elements", #hints))
end
startHintsScan = function(app)
hintsScanGen = hintsScanGen + 1
local gen = hintsScanGen
local root = hintsRoot(app)
if not root then
hintsNotice = "no focusable window"
drawHints()
hs.timer.doAfter(1.2, function()
if gen == hintsScanGen and mode == "hints" then exitHintsMode() end
end)
return
end
local okF, wf = pcall(root.attributeValue, root, "AXFrame")
hintsWinFrame = (okF and type(wf) == "table") and wf or nil
local roles = config.hintsRoles
hintsSearch = root:elementSearch(
function(_, results)
if gen ~= hintsScanGen or mode ~= "hints" then return end -- stale
hintsSearch = nil
local ok, err = pcall(onHintsScanResults, results)
if not ok then
print("mouse_grid: hints scan error: " .. tostring(err))
pcall(exitHintsMode)
end
end,
function(el) -- criteria: actionable role?
local ok, role = pcall(el.attributeValue, el, "AXRole")
return ok and role ~= nil and roles[role] == true
end,
{ count = config.hintsMaxElements, depth = config.hintsMaxDepth }
)
end
armHintsDrag = function(point)
hs.mouse.absolutePosition(point)
postMouse(etypes.leftMouseDown, point, 1)
dragOrigin = point
hintsTyped = "" -- full hint set comes back for picking the drop target
if hintsQuery then hintsQuery = "" end -- stay in search, clear the query
hintsSelIdx = 1
drawHints()
print(string.format("mouse_grid: drag armed at %.0f,%.0f (hints)", point.x, point.y))
end
performHintsAction = function(point, flags)
if not dragOrigin and flags.cmd then
armHintsDrag(point)
else
-- finishDrag/doClick/alt-move all dismiss via dismissActiveOverlay
performAction(point, flags)
end
end
enterHintsMode = function()
if mode == "free" then
exitFreeMode()
elseif mode ~= "idle" then
cancelDrag()
hideOverlay()
end
local app = hs.application.frontmostApplication()
-- Anchor to the screen of the window being scanned, not the cursor's —
-- they can differ, and a wrong clip rect rejects every scanned element
local focusedWindow = hs.window.focusedWindow()
currentScreen = (focusedWindow and focusedWindow:screen())
or hs.mouse.getCurrentScreen() or hs.screen.mainScreen()
mode = "hints"
hints = {}
hintsTyped = ""
hintsQuery = nil
hintsSelIdx = 1
hintsNotice = nil
hintsRescanned = false
applyAXFixup(app)
hintsCanvas = hs.canvas.new(currentScreen:fullFrame())
hintsCanvas:level(hs.canvas.windowLevels.screenSaver)
hintsCanvas:behavior({ "canJoinAllSpaces", "stationary" })
hintsCanvas:show()
hintsTap:start()
startHintsScan(app)
drawHints() -- "scanning…" badge until results arrive
print("mouse_grid: hints mode on (" .. (app and app:name() or "?") .. ")")
end
-- Idempotent: also called from pcall error paths and async timers
exitHintsMode = function()
hintsScanGen = hintsScanGen + 1 -- invalidate in-flight callbacks
if hintsSearch then pcall(hintsSearch.cancel, hintsSearch); hintsSearch = nil end
if hintsRescanTimer then hintsRescanTimer:stop(); hintsRescanTimer = nil end
if hintsTap then hintsTap:stop() end
if hintsCanvas then hintsCanvas:delete(); hintsCanvas = nil end
restoreAXFixup()
hints = {}
hintsTyped = ""
hintsQuery = nil
hintsSelIdx = 1
hintsNotice = nil
hintsWinFrame = nil
-- dragOrigin intentionally untouched: finishDrag reads it after dismissal
-- and Esc runs its own cancelDrag()
mode = "idle"
print("mouse_grid: hints mode off")
end
toggleHints = function()
if mode == "hints" then
cancelDrag()
exitHintsMode()
else
enterHintsMode()
end
end