diff --git a/hammerspoon/.hammerspoon/Makefile b/hammerspoon/.hammerspoon/Makefile index b5709592..16a373d8 100644 --- a/hammerspoon/.hammerspoon/Makefile +++ b/hammerspoon/.hammerspoon/Makefile @@ -1,5 +1,5 @@ .DEFAULT_GOAL := test -.PHONY: test format native preview-glass test-glass-geometry +.PHONY: test format native preview-glass test-glass-geometry test-mouse-grid NATIVE_BINARIES := native/.build/glass-toast native/.build/native-chooser SWIFT_FLAGS ?= -O @@ -24,7 +24,10 @@ preview-glass: native test-glass-geometry: native/.build/test-glass-geometry native/.build/test-glass-geometry -test: test-glass-geometry +test-mouse-grid: + hs -c "assert(dofile(hs.configdir .. '/tests/test_mouse_grid.lua').run())" + +test: test-glass-geometry test-mouse-grid hs -c "dofile(hs.configdir .. '/tests/test_chooser.lua').run()" hs -c "dofile(hs.configdir .. '/tests/test_launcher.lua').run()" hs -c "dofile(hs.configdir .. '/tests/test_glass_toast.lua').run()" diff --git a/hammerspoon/.hammerspoon/docs/mouse-grid.md b/hammerspoon/.hammerspoon/docs/mouse-grid.md new file mode 100644 index 00000000..807d3e2a --- /dev/null +++ b/hammerspoon/.hammerspoon/docs/mouse-grid.md @@ -0,0 +1,17 @@ +# Mouse grid + +Press **⌘⌃G** over the window you want to control. The overlay starts on the part of that window on its current display. The left-hand letter keys map to the grid like this on QWERTY: + +```text +Q W E +A S D +Z X C +``` + +- **Q/W/E**, **A/S/D**, and **Z/X/C** zoom into the corresponding cell (no Shift needed). At small sizes the drawing stays large enough to read, while the outline shows the actual selection. Choose one more letter on that smallest grid to switch to a crosshair. +- In crosshair mode, the same letters nudge by one point in the corresponding direction: **W/A/X/D** move up/left/down/right, and **Q/E/Z/C** move diagonally. **S** stays put. Hold a movement key to keep moving, or add **Shift** for ten-point steps. You can move anywhere inside the original window area, not just inside the last cell. +- **Return** left-clicks the crosshair and closes the grid. +- **Space** moves the pointer there without clicking and closes the grid. Use this to reveal Slack's hover controls, then reopen the grid to pick one. +- **Esc** or **⌘⌃G** cancels. Reopen the grid to start over. The grid also cancels if the target window loses focus, moves, or resizes. + +This works with coordinates, not app controls. It won't track content moving inside the window (such as when Slack scrolls), and a floating window or menu can cover the point you're aiming at. For a window spanning displays, the grid covers only the portion on that window's current display. Change the entry shortcut in `init.lua`. diff --git a/hammerspoon/.hammerspoon/init.lua b/hammerspoon/.hammerspoon/init.lua index e7f0ca4e..28a262e3 100644 --- a/hammerspoon/.hammerspoon/init.lua +++ b/hammerspoon/.hammerspoon/init.lua @@ -18,6 +18,7 @@ require("modules.caffeine") local glass_toast = require("modules.glass_toast") local helpers = require("modules.helpers") +local mouse_grid = require("modules.mouse_grid") local wm = require("modules.wm") -- Configure applications that should be ignored by window management @@ -72,9 +73,12 @@ wm.config.layouts = { wm:init() --- Preserve window-state persistence while closing the native chooser on reload. +mouse_grid.bind(HYPER, "g") + +-- Clean up overlays without replacing window-state persistence on reload. local save_window_state = hs.shutdownCallback hs.shutdownCallback = function() + mouse_grid.shutdown() require("modules.chooser").shutdown() if save_window_state then save_window_state() diff --git a/hammerspoon/.hammerspoon/modules/mouse_grid.lua b/hammerspoon/.hammerspoon/modules/mouse_grid.lua new file mode 100644 index 00000000..3c1fcf99 --- /dev/null +++ b/hammerspoon/.hammerspoon/modules/mouse_grid.lua @@ -0,0 +1,284 @@ +local M = {} +local modal, toggle, session +local cell_keys = { "q", "w", "e", "a", "s", "d", "z", "x", "c" } + +local function intersection(a, b) + local x, y = math.max(a.x, b.x), math.max(a.y, b.y) + local w = math.min(a.x + a.w, b.x + b.w) - x + local h = math.min(a.y + a.h, b.y + b.h) - y + if w > 0 and h > 0 then + return { x = x, y = y, w = w, h = h } + end +end + +local function same_frame(a, b) + return a.x == b.x and a.y == b.y and a.w == b.w and a.h == b.h +end + +local function target() + local win = hs.window.focusedWindow() + local app = win and win:application() + if not app or not app:isFrontmost() then + return + end + local screen, id = win:screen(), win:id() + if not screen or not id then + return + end + local frame = win:frame() + local bounds = win:isFullScreen() and screen:fullFrame() or screen:frame() + local visible = intersection(frame, bounds) + if visible then + return { id = id, frame = frame, visible = visible } + end +end + +local function cell_direction(index) + return (index - 1) % 3 - 1, math.floor((index - 1) / 3) - 1 +end + +local function cell(rect, index) + local dx, dy = cell_direction(index) + local w, h = rect.w / 3, rect.h / 3 + return { + x = rect.x + (dx + 1) * w, + y = rect.y + (dy + 1) * h, + w = w, + h = h, + } +end + +local function clamp_offset(value, limit) + return math.max(0, math.min(value, limit)) +end + +local function display_rect(rect, bounds) + -- Enlarge the drawing, not the region the keys and pointer act on. + local w = math.min(bounds.w, math.max(rect.w, 78)) + local h = math.min(bounds.h, math.max(rect.h, 72)) + return { + x = clamp_offset(rect.x + (rect.w - w) / 2, bounds.w - w), + y = clamp_offset(rect.y + (rect.h - h) / 2, bounds.h - h), + w = w, + h = h, + } +end + +local function center(rect) + return { x = rect.x + rect.w / 2, y = rect.y + rect.h / 2 } +end + +local function aim_point() + return session.nudge_point or center(session.rect) +end + +local function overlay_elements(rect, bounds, aim, nudging) + local elements = {} + local white = { white = 1 } + local black = { white = 0 } + local function box(frame, fill, stroke) + elements[#elements + 1] = { + type = "rectangle", + action = stroke and (fill and "strokeAndFill" or "stroke") or "fill", + frame = frame, + fillColor = fill, + strokeColor = stroke, + } + end + + local function arm(dx, dy, w, h) + box({ x = aim.x + dx - 1, y = aim.y + dy - 1, w = w + 2, h = h + 2 }, black) + box({ x = aim.x + dx, y = aim.y + dy, w = w, h = h }, white) + end + if nudging then + arm(-8, -1, 16, 2) + arm(-1, -8, 2, 16) + return elements + end + + local grid = display_rect(rect, bounds) + box(grid, { red = 0.12, green = 0.34, blue = 0.65, alpha = 0.24 }, white) + for index in ipairs(cell_keys) do + box(cell(grid, index), nil, { white = 1, alpha = 0.5 }) + end + if not same_frame(grid, rect) then + box(rect, nil, white) + end + -- Leave room for the centered S without obscuring the real aim point. + arm(-15, -1, 4, 2) + arm(11, -1, 4, 2) + arm(-1, -14, 2, 4) + arm(-1, 10, 2, 4) + for index, key in ipairs(cell_keys) do + local area = cell(grid, index) + local cx, cy = area.x + area.w / 2, area.y + area.h / 2 + box({ x = cx - 10, y = cy - 9, w = 20, h = 18 }, { white = 0, alpha = 0.85 }) + elements[#elements + 1] = { + type = "text", + text = key:upper(), + textSize = 13, + textColor = white, + textAlignment = "center", + frame = { x = cx - 10, y = cy - 8, w = 20, h = 16 }, + } + end + return elements +end + +local function render() + local elements = overlay_elements( + session.rect, + session.target.visible, + aim_point(), + session.nudge_point ~= nil + ) + session.canvas:replaceElements(elements) +end + +local function cancel() + if not session then + return + end + local closing = session + session = nil + closing.timer:stop() + modal:exit() + closing.canvas:delete() +end + +local function validate_target() + if not session then + return false + end + local now, original = target(), session.target + if + now + and now.id == original.id + and same_frame(now.frame, original.frame) + and same_frame(now.visible, original.visible) + then + return true + end + cancel() + return false +end + +local function refine(index) + if not session or session.nudge_point or not validate_target() then + return + end + local rect = session.rect + local grid = display_rect(rect, session.target.visible) + session.rect = cell(rect, index) + if grid.w > rect.w or grid.h > rect.h then + -- Take one last letter selection from the smallest labeled grid, then pan the aim freely. + session.nudge_point = center(session.rect) + end + render() +end + +local function nudge(dx, dy) + if (dx == 0 and dy == 0) or not session or not session.nudge_point or not validate_target() then + return + end + local aim, bounds = session.nudge_point, session.target.visible + local x = clamp_offset(aim.x + dx, bounds.w - 1) + local y = clamp_offset(aim.y + dy, bounds.h - 1) + if x ~= aim.x or y ~= aim.y then + aim.x, aim.y = x, y + render() + end +end + +local function finish(click) + if not validate_target() then + return + end + local aim, origin = aim_point(), session.target.visible + local point = { x = origin.x + aim.x, y = origin.y + aim.y } + cancel() + if click then + hs.eventtap.leftClick(point) + else + hs.mouse.absolutePosition(point) + -- A warp alone may not notify the app to update hover UI. + local event = hs.eventtap.event + event.newMouseEvent(event.types.mouseMoved, point):post() + end +end + +local function start() + local focused = target() + if not focused then + return + end + local canvas = hs.canvas.new(focused.visible) + if not canvas then + return + end + canvas:level(hs.canvas.windowLevels.overlay) + canvas:behaviorAsLabels({ "moveToActiveSpace", "fullScreenAuxiliary" }) + canvas:clickActivating(false) + session = { + target = focused, + canvas = canvas, + rect = { x = 0, y = 0, w = focused.visible.w, h = focused.visible.h }, + timer = hs.timer.doEvery(0.15, validate_target), + } + render() + canvas:show() + modal:enter() +end + +function M.bind(mods, key) + M.shutdown() + toggle = hs.hotkey.bind(mods, key, function() + if session then + cancel() + else + start() + end + end) + if not toggle then + return + end + modal = hs.hotkey.modal.new() + for index, key in ipairs(cell_keys) do + local dx, dy = cell_direction(index) + local function step() + nudge(dx, dy) + end + local function fast_step() + nudge(dx * 10, dy * 10) + end + modal:bind({}, key, function() + if session and session.nudge_point then + step() + else + refine(index) + end + end, nil, step) + modal:bind({ "shift" }, key, fast_step, nil, fast_step) + end + modal:bind({}, "escape", cancel) + modal:bind({}, "return", function() + finish(true) + end) + modal:bind({}, "space", function() + finish(false) + end) +end + +function M.shutdown() + cancel() + if modal then + modal:delete() + modal = nil + end + if toggle then + toggle:delete() + toggle = nil + end +end + +return M diff --git a/hammerspoon/.hammerspoon/tests/test_mouse_grid.lua b/hammerspoon/.hammerspoon/tests/test_mouse_grid.lua new file mode 100644 index 00000000..3f5663c9 --- /dev/null +++ b/hammerspoon/.hammerspoon/tests/test_mouse_grid.lua @@ -0,0 +1,297 @@ +local M = {} + +function M.run(path) + local keys, trigger, tick, canvas_frame, display, click, move, hover, labels, label_frames + local active, showing, polling, frontmost = false, false, false, true + local renders = 0 + local repeats + local id = 42 + local window_frame = { x = -1000, y = 10, w = 1000, h = 810 } + local screen_frame = { x = -900, y = 10, w = 900, h = 900 } + local canvas = { + level = function() end, + behaviorAsLabels = function() end, + clickActivating = function() end, + replaceElements = function(_, elements) + renders = renders + 1 + display = elements[1].frame + labels, label_frames = {}, {} + for _, element in ipairs(elements) do + if element.type == "text" then + label_frames[#label_frames + 1] = element.frame + labels[#labels + 1] = element.text + end + end + end, + show = function() + showing = true + end, + delete = function() + showing = false + end, + } + local win = { + id = function() + return id + end, + application = function() + return { + isFrontmost = function() + return frontmost + end, + } + end, + frame = function() + return window_frame + end, + isFullScreen = function() + return false + end, + screen = function() + return { + frame = function() + return screen_frame + end, + } + end, + } + local function assert_closed() + assert( + not active and not showing and not polling, + "grid must release its keys, canvas, and timer" + ) + end + local fake_hs = { + window = { + focusedWindow = function() + return win + end, + }, + canvas = { + windowLevels = { overlay = 1 }, + new = function(frame) + canvas_frame = frame + return canvas + end, + }, + hotkey = { + bind = function(_, _, fn) + trigger = fn + return { + delete = function() + trigger = nil + end, + } + end, + modal = { + new = function() + keys, repeats = {}, {} + return { + bind = function(_, mods, key, fn, _, repeat_fn) + local chord = mods[1] and mods[1] .. "+" .. key or key + keys[chord], repeats[chord] = fn, repeat_fn + end, + enter = function() + active = true + end, + exit = function() + active = false + end, + delete = function() + keys = nil + end, + } + end, + }, + }, + timer = { + doEvery = function(_, fn) + tick, polling = fn, true + return { + stop = function() + polling = false + end, + } + end, + }, + mouse = { + absolutePosition = function(point) + assert_closed() + move = point + end, + }, + eventtap = { + leftClick = function(point) + assert_closed() + click = point + end, + event = { + types = { mouseMoved = 1 }, + newMouseEvent = function(kind, point) + assert(kind == 1) + return { + post = function() + hover = point + end, + } + end, + }, + }, + } + path = path or hs.configdir .. "/modules/mouse_grid.lua" + local env = setmetatable({ hs = fake_hs }, { __index = _G }) + local load_module = assert(loadfile(path, "t", env)) + if setfenv then -- Also runnable with LuaJIT outside Hammerspoon. + setfenv(load_module, env) + end + local grid = load_module() + grid.bind({ "cmd", "ctrl" }, "g") + + local function assert_label_centers() + assert(table.concat(labels) == "QWEASDZXC", "display letters in grid order") + for index, frame in ipairs(label_frames) do + local col, row = (index - 1) % 3, math.floor((index - 1) / 3) + assert( + math.abs(frame.x + frame.w / 2 - (display.x + (col + 0.5) * display.w / 3)) < 0.001 + ) + assert( + math.abs(frame.y + frame.h / 2 - (display.y + (row + 0.5) * display.h / 3)) < 0.001 + ) + end + end + + trigger() + assert(active and showing and polling) + assert(canvas_frame.x == -900 and canvas_frame.w == 900, "clip to the current display") + assert_label_centers() + assert(not keys.delete, "Backspace is not a grid command") + keys.z() + keys.c() + assert(display.x == 200 and display.y == 720, "repeated keys refine the current cell") + keys["return"]() + assert( + click.x == -650 and click.y == 775, + "click the refined cell's center in screen coordinates" + ) + + click = nil + trigger() + keys.e() + keys.space() + assert( + not click and move.x == -150 and move.y == 145 and hover == move, + "move and deliver hover without clicking" + ) + + trigger() + keys.s() + keys.s() + keys.s() + assert(display.w == 78 and display.h == 72, "enlarge the small grid itself") + assert( + display.x + display.w / 2 == 450 and display.y + display.h / 2 == 405, + "center it over the selection" + ) + assert_label_centers() + keys.c() + keys["return"]() + assert( + math.abs(click.x - (-450 + 100 / 9)) < 0.001 and click.y == 425, + "refine the real region, not the drawing" + ) + click = nil + + trigger() + for _ = 1, 3 do + keys.x() + end + assert_label_centers() + local grid_renders = renders + repeats.x() + keys["shift+x"]() + assert(renders == grid_renders, "held or shifted letters do not zoom the labeled grid") + assert(not keys.right, "arrows are not grid bindings") + keys.x() + assert(#labels == 0, "the final letter switches to a crosshair") + local final_renders = renders + keys.s() + repeats.s() + assert(renders == final_renders, "the center key leaves the crosshair in place") + keys.x() + keys.e() + keys.d() + for _ = 1, 20 do + repeats.d() + end + keys["shift+a"]() + keys.w() + repeats["shift+w"]() + assert(active and showing and #labels == 0, "movement does not reopen the labeled grid") + keys["return"]() + assert( + math.abs(click.x + 438) < 0.001 and math.abs(click.y - 804) < 0.001, + string.format( + "nudge beyond the final cell and click there (got %.3f, %.3f)", + click.x, + click.y + ) + ) + click = nil + + trigger() + for _ = 1, 4 do + keys.z() + end + for _ = 1, 100 do + repeats["shift+z"]() + end + local edge_renders = renders + keys.z() + assert(renders == edge_renders, "nudging at a window edge does not redraw") + keys.space() + assert( + move.x == -900 and move.y == 819 and hover == move, + "stop nudging at the display-clipped window edge" + ) + + trigger() + assert( + display.w == canvas_frame.w and display.h == canvas_frame.h, + "reopen at the full window after nudging" + ) + assert_label_centers() + keys.escape() + assert_closed() + + trigger() + frontmost = false + tick() + assert_closed() + trigger() + assert_closed() + frontmost = true + + trigger() + id = id + 1 + keys["return"]() + assert_closed() + assert(not click, "never click after focus changes, even between timer ticks") + + trigger() + window_frame = { x = -1000, y = 10, w = 950, h = 810 } + keys["return"]() + assert_closed() + assert(not click, "never click after a resize") + + trigger() + trigger() + assert_closed() + trigger() + grid.shutdown() + assert_closed() + assert(not trigger and not keys, "shutdown unbinds both the entry shortcut and the modal") + print("Mouse grid tests passed") + return true +end + +return M