From b68d0a75eee7ebddd8f9d9c9ddd3bd3aa62dc705 Mon Sep 17 00:00:00 2001 From: Colton Padden Date: Tue, 24 Nov 2026 14:25:16 -0500 Subject: [PATCH 1/4] hammerspoon: animate the matte caffeine HUD --- hammerspoon/.hammerspoon/Makefile | 4 +- hammerspoon/.hammerspoon/init.lua | 13 +- hammerspoon/.hammerspoon/modules/caffeine.lua | 136 ++++++++++++++++-- .../.hammerspoon/modules/glass_toast.lua | 14 +- .../.hammerspoon/native/GlassToast.swift | 89 ++++++------ hammerspoon/.hammerspoon/native/README.md | 39 +++-- .../.hammerspoon/tests/test_glass_toast.lua | 49 +++++-- 7 files changed, 251 insertions(+), 93 deletions(-) diff --git a/hammerspoon/.hammerspoon/Makefile b/hammerspoon/.hammerspoon/Makefile index 16a373d8..b56cba3d 100644 --- a/hammerspoon/.hammerspoon/Makefile +++ b/hammerspoon/.hammerspoon/Makefile @@ -17,9 +17,9 @@ $(NATIVE_BINARIES) native/.build/test-glass-geometry: native/GlassBackground.swi xcrun swiftc $(SWIFT_FLAGS) -target "$$(uname -m)-apple-macosx13.0" -framework AppKit $^ -o $@.tmp mv -f $@.tmp $@ -# Visual preview only; does not change caffeine state. +# Visual preview only. preview-glass: native - native/.build/glass-toast enabled "Caffeine Enabled" 4 + native/.build/glass-toast compact-bottom "Muted" speaker.slash 4 test-glass-geometry: native/.build/test-glass-geometry native/.build/test-glass-geometry diff --git a/hammerspoon/.hammerspoon/init.lua b/hammerspoon/.hammerspoon/init.lua index 28a262e3..cf0a67d8 100644 --- a/hammerspoon/.hammerspoon/init.lua +++ b/hammerspoon/.hammerspoon/init.lua @@ -14,7 +14,7 @@ hs.alert.defaultStyle.fadeOutDuration = 2 require("modules.plugins") -- require("modules.alerts") -require("modules.caffeine") +require("modules.caffeine").bind(HYPER, "0", { position = "top" }) -- "top" or "bottom" local glass_toast = require("modules.glass_toast") local helpers = require("modules.helpers") @@ -98,6 +98,13 @@ end) -- The CLI itself is installed separately, so do not recheck/reinstall it on reload. require("hs.ipc") -glass_toast:show("reload", "Hammerspoon Reloaded", function() - helpers:show("Hammerspoon Reloaded") +local reload_title = "Hammerspoon Reloaded" +glass_toast:show({ + layout = "centered-card", + title = reload_title, + symbol = "arrow.clockwise", + detail = "Your configuration is ready.", + tint = "blue", +}, function() + helpers:show(reload_title) end) diff --git a/hammerspoon/.hammerspoon/modules/caffeine.lua b/hammerspoon/.hammerspoon/modules/caffeine.lua index b7fcff5a..32f19cde 100644 --- a/hammerspoon/.hammerspoon/modules/caffeine.lua +++ b/hammerspoon/.hammerspoon/modules/caffeine.lua @@ -1,12 +1,126 @@ -local glass_toast = require("modules.glass_toast") -local helpers = require("modules.helpers") - -hs.hotkey.bind(HYPER, "0", function() - hs.caffeinate.toggle("displayIdle") - local enabled = hs.caffeinate.get("displayIdle") - local title = enabled and "Caffeine Enabled" or "Caffeine Disabled" - local style = enabled and helpers.styles.success or helpers.styles.error - glass_toast:show(enabled and "enabled" or "disabled", title, function() - helpers:show(title, nil, style) +local obj = {} +local width, height = 200, 40 +local hold = 1.2 +local open_duration, close_duration = 0.21, 0.16 +local hud, timer +local progress, reduce_motion + +local function stop_timer() + if timer then + timer:stop() + timer = nil + end +end + +local function dismiss_hud() + stop_timer() + if hud then + hud:delete() + hud = nil + end +end + +local function system_reduces_motion() + local value = + hs.execute("/usr/bin/defaults read com.apple.universalaccess reduceMotion 2>/dev/null") + return value:match("^%s*1%s*$") ~= nil +end + +local function render(fraction) + progress = fraction + if reduce_motion then + hud:alpha(fraction) + return + end + + -- Width leads height so the island opens sideways before settling vertically. + -- The label fades in at a fixed font size once the silhouette can contain it. + local remaining = 1 - fraction + local w = width - (width - 34) * remaining ^ 3 + local h = height - (height - 14) * remaining ^ 2 + hud[1].frame = { x = (width - w) / 2, y = (height - h) / 2, w = w, h = h } + hud[1].roundedRectRadii = { xRadius = h / 2, yRadius = h / 2 } + local text_alpha = math.max(0, math.min(1, (fraction - 0.32) / 0.4)) + hud[2].textColor = { white = 1, alpha = text_alpha ^ 2 * (3 - 2 * text_alpha) } + hud:alpha(math.min(1, fraction * 5)) +end + +local function animate_to(target, duration, on_done) + stop_timer() + local start = progress + local seconds = duration * math.abs(target - start) + if seconds == 0 then + on_done() + return + end + local started = hs.timer.absoluteTime() + timer = hs.timer.doEvery(1 / 60, function() + local elapsed = (hs.timer.absoluteTime() - started) / 1e9 + local t = math.min(1, elapsed / seconds) + render(start + (target - start) * t) + if t == 1 then + stop_timer() + on_done() + end end) -end) +end + +-- Reuse the current HUD on quick toggles, even if it is still moving. +local function show_hud(title, position) + local screen = hs.screen.mainScreen() + if not screen then + dismiss_hud() + return + end + local frame = screen:frame() + local inset = math.min(96, frame.h * 0.12) + local x = frame.x + (frame.w - width) / 2 + local y = position == "top" and frame.y + 12 or frame.y + frame.h - inset - height + local previous = hud and hud:frame() + if previous and previous.x == x and previous.y == y then + hud[2].text = title + else + dismiss_hud() + reduce_motion = system_reduces_motion() + local font, size = ".AppleSystemUIFont", 14 + local text = hs.drawing.getTextDrawingSize(title, { font = font, size = size }) + hud = hs.canvas.new({ x = x, y = y, w = width, h = height }) + hud:appendElements({ + type = "rectangle", + action = "fill", + roundedRectRadii = { xRadius = height / 2, yRadius = height / 2 }, + fillColor = { white = 0.12 }, + }, { + type = "text", + text = title, + textFont = font, + textSize = size, + textColor = { white = 1 }, + textAlignment = "center", + frame = { x = 0, y = (height - text.h) / 2, w = width, h = math.ceil(text.h) }, + }) + render(0) + hud:level(hs.canvas.windowLevels.overlay):show() + end + + animate_to(1, reduce_motion and 0.15 or open_duration, function() + timer = hs.timer.doAfter(hold, function() + animate_to(0, reduce_motion and 0.18 or close_duration, dismiss_hud) + end) + end) +end + +function obj.bind(modifiers, key, options) + local position = (options or {}).position or "bottom" + assert( + position == "top" or position == "bottom", + 'Caffeine HUD position must be "top" or "bottom"' + ) + return hs.hotkey.bind(modifiers, key, function() + hs.caffeinate.toggle("displayIdle") + local enabled = hs.caffeinate.get("displayIdle") + show_hud(enabled and "Caffeine Enabled" or "Caffeine Disabled", position) + end) +end + +return obj diff --git a/hammerspoon/.hammerspoon/modules/glass_toast.lua b/hammerspoon/.hammerspoon/modules/glass_toast.lua index 24b49782..930e53e3 100644 --- a/hammerspoon/.hammerspoon/modules/glass_toast.lua +++ b/hammerspoon/.hammerspoon/modules/glass_toast.lua @@ -3,8 +3,9 @@ local task local log = hs.logger.new("glass_toast", "warning") -- Build explicitly with `make native`; never compile on the hotkey path. --- kind: "enabled", "disabled", or "reload". -function obj:show(kind, title, fallback) +-- options: layout ("compact-bottom" or "centered-card"), title, and SF Symbol name; +-- optional duration, detail (centered card only), and tint ("label" or "blue"). +function obj:show(options, fallback) -- Cancel before checking the binary so a fallback also replaces the old toast. local previous = task task = nil @@ -28,7 +29,14 @@ function obj:show(kind, title, fallback) log.w("Native toast failed: " .. tostring(stderr)) fallback() end - end, { kind, title, "2" }) + end, { + options.layout, + options.title, + options.symbol, + tostring(options.duration or 2), + options.detail or "", + options.tint or "label", + }) task = next_task if not next_task or not next_task:start() then task = nil diff --git a/hammerspoon/.hammerspoon/native/GlassToast.swift b/hammerspoon/.hammerspoon/native/GlassToast.swift index 0c72e8c0..af7c0b2d 100644 --- a/hammerspoon/.hammerspoon/native/GlassToast.swift +++ b/hammerspoon/.hammerspoon/native/GlassToast.swift @@ -16,35 +16,29 @@ final class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_ notification: Notification) { let args = Array(CommandLine.arguments.dropFirst()) - guard args.count <= 3 else { - fail("usage: glass-toast [enabled|disabled|reload] [title] [seconds]") + guard (3...6).contains(args.count) else { + fail("usage: glass-toast <symbol> [seconds] [detail] [tint]") + } + let compact: Bool + switch args[0] { + case "compact-bottom": compact = true + case "centered-card": compact = false + default: fail("unknown layout: \(args[0])") + } + let title = args[1] + let symbol = args[2] + let duration = args.count > 3 ? Double(args[3]) ?? .nan : 2 + let detailText = args.count > 4 ? args[4] : "" + guard !compact || detailText.isEmpty else { + fail("compact-bottom only supports one line of text") } - let kind = args.first ?? "enabled" - let symbol: String - let defaultTitle: String - let message: String let tint: NSColor - switch kind { - case "reload": - symbol = "arrow.clockwise" - defaultTitle = "Hammerspoon Reloaded" - message = "Your configuration is ready." - tint = .systemBlue - case "disabled": - symbol = "moon.zzz.fill" - defaultTitle = "Caffeine Disabled" - message = "Normal display sleep restored." - tint = .secondaryLabelColor - case "enabled": - symbol = "cup.and.saucer.fill" - defaultTitle = "Caffeine Enabled" - message = "Your display will stay awake." - tint = .systemGreen - default: - fail("unknown kind: \(kind)") + let tintName = args.count > 5 ? args[5] : "label" + switch tintName { + case "label": tint = .labelColor + case "blue": tint = .systemBlue + default: fail("unknown icon tint: \(tintName)") } - let title = args.count > 1 ? args[1] : defaultTitle - let duration = args.count > 2 ? Double(args[2]) ?? .nan : 2 guard duration.isFinite, (0.2...30).contains(duration) else { fail("duration must be between 0.2 and 30 seconds") } @@ -52,11 +46,21 @@ final class AppDelegate: NSObject, NSApplicationDelegate { fail("no screen available") } - let size = NSSize(width: 360, height: 64) + let heading = NSTextField(labelWithString: title) + heading.font = .systemFont(ofSize: 14, weight: .semibold) + heading.textColor = .labelColor + + let iconSize: CGFloat = compact ? 18 : 30 + let iconGap: CGFloat = compact ? 8 : 14 + let rowWidth = iconSize + iconGap + heading.intrinsicContentSize.width + let size = + compact + ? NSSize(width: max(200, ceil(rowWidth) + 32), height: 40) + : NSSize(width: 360, height: 64) let frame = screen.visibleFrame let rect = NSRect( x: frame.midX - size.width / 2, - y: frame.midY - size.height / 2, + y: compact ? frame.minY + min(96, frame.height * 0.12) : frame.midY - size.height / 2, width: size.width, height: size.height ) @@ -81,17 +85,18 @@ final class AppDelegate: NSObject, NSApplicationDelegate { let icon = NSImageView() icon.image = NSImage(systemSymbolName: symbol, accessibilityDescription: nil)? - .withSymbolConfiguration(NSImage.SymbolConfiguration(pointSize: 24, weight: .medium)) + .withSymbolConfiguration( + NSImage.SymbolConfiguration(pointSize: compact ? 16 : 24, weight: .medium)) icon.contentTintColor = tint icon.translatesAutoresizingMaskIntoConstraints = false - let heading = NSTextField(labelWithString: title) - heading.font = .systemFont(ofSize: 14, weight: .semibold) - heading.textColor = .labelColor - let detail = NSTextField(labelWithString: message) - detail.font = .systemFont(ofSize: 12) - detail.textColor = .secondaryLabelColor - let labels = NSStackView(views: [heading, detail]) + let labels = NSStackView(views: [heading]) + if !detailText.isEmpty { + let detail = NSTextField(labelWithString: detailText) + detail.font = .systemFont(ofSize: 12) + detail.textColor = .secondaryLabelColor + labels.addArrangedSubview(detail) + } labels.orientation = .vertical labels.alignment = .leading labels.spacing = 4 @@ -99,13 +104,15 @@ final class AppDelegate: NSObject, NSApplicationDelegate { content.addSubview(icon) content.addSubview(labels) NSLayoutConstraint.activate([ - icon.leadingAnchor.constraint(equalTo: content.leadingAnchor, constant: 22), + icon.leadingAnchor.constraint( + equalTo: content.leadingAnchor, constant: compact ? (size.width - rowWidth) / 2 : 22), icon.centerYAnchor.constraint(equalTo: content.centerYAnchor), - icon.widthAnchor.constraint(equalToConstant: 30), - icon.heightAnchor.constraint(equalToConstant: 30), - labels.leadingAnchor.constraint(equalTo: icon.trailingAnchor, constant: 14), + icon.widthAnchor.constraint(equalToConstant: iconSize), + icon.heightAnchor.constraint(equalToConstant: iconSize), + labels.leadingAnchor.constraint(equalTo: icon.trailingAnchor, constant: iconGap), labels.centerYAnchor.constraint(equalTo: content.centerYAnchor), - labels.trailingAnchor.constraint(lessThanOrEqualTo: content.trailingAnchor, constant: -22), + labels.trailingAnchor.constraint( + lessThanOrEqualTo: content.trailingAnchor, constant: compact ? -16 : -22), ]) self.panel = panel diff --git a/hammerspoon/.hammerspoon/native/README.md b/hammerspoon/.hammerspoon/native/README.md index 1c3dfe3d..f0edfd36 100644 --- a/hammerspoon/.hammerspoon/native/README.md +++ b/hammerspoon/.hammerspoon/native/README.md @@ -1,6 +1,6 @@ # Native glass toast prototype -A centered, click-through, non-activating AppKit panel for caffeine and reload feedback. +A click-through, non-activating AppKit toast with compact-bottom and centered layouts. `make native` also builds the interactive Launcher chooser; see [`Launcher.spoon`](../vendored/Launcher.spoon/README.md) for that UI and its API. Both helpers use `GlassBackground.swift` for the same native material. @@ -18,36 +18,34 @@ make native make preview-glass ``` -The preview does **not** change display-sleep prevention. To preview the other state: - -```sh -native/.build/glass-toast disabled "Caffeine Disabled" 4 -``` - To preview startup/reload feedback: ```sh -native/.build/glass-toast reload "Hammerspoon Reloaded" 4 +native/.build/glass-toast centered-card "Hammerspoon Reloaded" arrow.clockwise 4 \ + "Your configuration is ready." blue ``` -Arguments are kind (`enabled`, `disabled`, or `reload`), optional title, and optional hold -seconds (0.2–30; default 2). Invalid arguments exit nonzero with a diagnostic -so Hammerspoon can fall back to canvas. The panel centers on AppKit's main -screen, fades in and out, and exits. Hammerspoon replaces the previous process on rapid toggles. +The CLI takes `layout title symbol [seconds] [detail] [tint]`. The layouts are +`compact-bottom` (one line) and `centered-card` (optional detail). `symbol` is an +SF Symbol name, hold time is 0.2–30 seconds (default 2), and tint is `label` +(default) or `blue`. Callers choose the copy, symbol, and timing; the helper +only renders them. Invalid arguments exit nonzero with a diagnostic so +Hammerspoon can fall back to canvas. The panel fades in and out on AppKit's +main screen, then exits. The wrapper replaces the previous toast on a new request. This short-lived process is intentionally a prototype, not a persistent service. ## Hammerspoon integration Stow the `hammerspoon` package so `~/.hammerspoon/native` points here, build, -then reload Hammerspoon. Cmd+Ctrl+0 uses the native toast for caffeine; -startup/reload uses it with an `arrow.clockwise` SF Symbol and blue tint. -Other alerts still use the existing canvas renderer. The Lua wrapper falls back -to canvas if the binary is missing or fails to run; it never compiles on a hotkey. +then reload Hammerspoon. Startup/reload uses the native toast with an +`arrow.clockwise` SF Symbol and blue tint. Caffeine (Cmd+Ctrl+0) uses its own +matte canvas HUD and does not require the native helper. Reload feedback falls +back to canvas if the binary is missing or fails to run. The generated executable lives in ignored `native/.build/`. Rebuild after source changes; builds replace the executable atomically, and a binary is not distributed or committed. To disable the prototype, remove `native/.build/glass-toast`; -caffeine and reload will use the canvas again. +reload feedback will use the canvas again. ## Corner geometry @@ -60,7 +58,8 @@ for guidance on using regular glass for text-heavy overlays. `GlassGeometry` in `GlassBackground.swift` owns our chosen dimensions: -- Toast: 16-point outer radius, 360×64-point visible surface. The radius matches +- Toast: 16-point outer radius. `compact-bottom` uses a 200×40-point visible surface + (wider for longer titles as needed); `centered-card` uses 360×64 points. The radius matches AppKit's reported effective corners for a standard titled, resizable window on the current macOS host; it is not a universal value for every window style. - Launcher: 16-point outer radius, matching the toast and the measured window style. @@ -89,7 +88,7 @@ xcrun swift-format lint --strict native/*.swift tests/test_glass_geometry.swift ``` The wrapper tests use mock tasks and do not show UI or change caffeine state. -They cover native launch, replacement, fallback, stale callbacks, and reload -arguments. Preview each kind to check rendering and focus behavior on macOS. +They cover native arguments, launch, replacement, fallback, and stale callbacks. +Preview each layout to check rendering and focus behavior on macOS. The older-macOS visual-effect fallback requires separate visual testing on an older host. diff --git a/hammerspoon/.hammerspoon/tests/test_glass_toast.lua b/hammerspoon/.hammerspoon/tests/test_glass_toast.lua index 1492fe0a..cd4df593 100644 --- a/hammerspoon/.hammerspoon/tests/test_glass_toast.lua +++ b/hammerspoon/.hammerspoon/tests/test_glass_toast.lua @@ -46,18 +46,36 @@ function test.run() fallbacks = fallbacks + 1 end + local compact = { + layout = "compact-bottom", + title = "Muted", + symbol = "speaker.slash", + duration = 1.4, + } + local card = { + layout = "centered-card", + title = "Saved", + symbol = "checkmark", + detail = "Changes are ready.", + tint = "blue", + } + exists = false - toast:show("reload", "Hammerspoon Reloaded", fallback) + toast:show(card, fallback) assert(fallbacks == 1 and #tasks == 0, "missing binary should fall back") exists = true - toast:show("enabled", "Enabled", fallback) - assert(tasks[1].running and tasks[1].args[1] == "enabled", "should start native helper") + toast:show(compact, fallback) + assert(tasks[1].running, "should start native helper") + assert( + table.concat(tasks[1].args, "|") == "compact-bottom|Muted|speaker.slash|1.4||label", + "should pass compact layout, copy, symbol, duration, and default tint" + ) assert( tasks[1].path == "/test/native/.build/glass-toast", "should resolve config-relative binary" ) - toast:show("disabled", "Disabled", fallback) - assert(tasks[1].terminated and tasks[2].args[1] == "disabled", "should replace previous toast") + toast:show(card, fallback) + assert(tasks[1].terminated and tasks[2].running, "should replace previous toast") assert(fallbacks == 1, "termination of replaced toast must not fall back") tasks[1].callback(1, "", "stale failure") assert(fallbacks == 1, "late callback from replaced toast must be ignored") @@ -65,28 +83,33 @@ function test.run() tasks[2].callback(1, "", "failure") assert(fallbacks == 2, "helper failure should fall back") starts = false - toast:show("enabled", "Enabled", fallback) + toast:show(compact, fallback) assert(fallbacks == 3, "start failure should fall back") tasks[#tasks].callback(1, "", "late start failure") assert(fallbacks == 3, "start failure must not fall back twice") creates = false - toast:show("enabled", "Enabled", fallback) + toast:show(compact, fallback) assert(fallbacks == 4, "task creation failure should fall back") creates, starts = true, true - toast:show("reload", "Hammerspoon Reloaded", fallback) - assert(tasks[#tasks].args[1] == "reload", "should pass reload kind to helper") - assert(tasks[#tasks].args[2] == "Hammerspoon Reloaded", "should pass reload title to helper") + toast:show(card, fallback) + assert( + table.concat(tasks[#tasks].args, "|") + == "centered-card|Saved|checkmark|2|Changes are ready.|blue", + "should pass card layout, copy, symbol, default duration, detail, and tint" + ) tasks[#tasks].callback(0, "", "") assert(fallbacks == 4, "normal exit must not fall back") - toast:show("enabled", "Enabled", fallback) + toast:show(compact, fallback) local previous = tasks[#tasks] exists = false - toast:show("reload", "Hammerspoon Reloaded", fallback) + toast:show(card, fallback) assert(previous.terminated, "missing binary fallback should cancel the previous toast") assert(fallbacks == 5, "missing binary should fall back exactly once after cancellation") previous.callback(1, "", "late failure") assert(fallbacks == 5, "cancelled helper must not fall back again") - print("PASS: glass toast fallback, replacement, stale callback, reload, and normal exit tests") + print( + "PASS: glass toast arguments, fallback, replacement, stale callback, and normal exit tests" + ) end return test From 1e10e7ca771652d4e66d3fe1c49cdbc0f9c8efd4 Mon Sep 17 00:00:00 2001 From: Colton Padden <colton.padden@fastmail.com> Date: Tue, 24 Nov 2026 14:25:16 -0500 Subject: [PATCH 2/4] hammerspoon: watch Stow sources for config reloads --- hammerspoon/.hammerspoon/modules/plugins.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/hammerspoon/.hammerspoon/modules/plugins.lua b/hammerspoon/.hammerspoon/modules/plugins.lua index 9855e631..c00a1017 100644 --- a/hammerspoon/.hammerspoon/modules/plugins.lua +++ b/hammerspoon/.hammerspoon/modules/plugins.lua @@ -1,6 +1,12 @@ hs.loadSpoon("SpoonInstall") -spoon.SpoonInstall:andUse("ReloadConfiguration", { start = true }) +-- Stow links the config into $HOME. FSEvents does not follow those links into the checkout. +local init_path = hs.fs.pathToAbsolute(hs.configdir .. "/init.lua") +local source_dir = init_path and init_path:match("^(.+)/[^/]+$") or hs.configdir +spoon.SpoonInstall:andUse("ReloadConfiguration", { + config = { watch_paths = { source_dir } }, + start = true, +}) spoon.SpoonInstall:andUse("LookupSelection", { hotkeys = { lexicon = { HYPER, "d" } } }) From 1a7590aa6356b9eb49622bb5235cc7111cb8398e Mon Sep 17 00:00:00 2001 From: Colton Padden <colton.padden@fastmail.com> Date: Tue, 24 Nov 2026 14:28:34 -0500 Subject: [PATCH 3/4] hammerspoon: pull the island HUD from the top edge --- hammerspoon/.hammerspoon/modules/caffeine.lua | 93 ++++++++++++++++--- 1 file changed, 78 insertions(+), 15 deletions(-) diff --git a/hammerspoon/.hammerspoon/modules/caffeine.lua b/hammerspoon/.hammerspoon/modules/caffeine.lua index 32f19cde..507d5836 100644 --- a/hammerspoon/.hammerspoon/modules/caffeine.lua +++ b/hammerspoon/.hammerspoon/modules/caffeine.lua @@ -2,8 +2,10 @@ local obj = {} local width, height = 200, 40 local hold = 1.2 local open_duration, close_duration = 0.21, 0.16 +local top_gap = 20 +local matte = { white = 0.12 } local hud, timer -local progress, reduce_motion +local progress, reduce_motion, drip, label_height local function stop_timer() if timer then @@ -26,6 +28,51 @@ local function system_reduces_motion() return value:match("^%s*1%s*$") ~= nil end +local function smoothstep(t) + t = math.max(0, math.min(1, t)) + return t ^ 2 * (3 - 2 * t) +end + +-- Pull a curved neck between the screen edge and the capsule, then pinch it off. +-- The tether is drawn behind the capsule so their overlap reads as one shape. +local function render_tether(fraction, top, w, h) + local tail = 1 - smoothstep((fraction - 0.52) / 0.23) + local depth = (top + h / 2) * tail + if depth <= 0 or tail == 0 then + hud[1].action = "skip" + return + end + + local pinch = 1 - smoothstep((fraction - 0.23) / 0.37) + local center = width / 2 + local shoulder = (16 + 10 * pinch) * tail + local neck = (0.35 + 12 * pinch) * tail + local join = (0.35 + math.min(w * 0.3, 32) * pinch) * tail + local function point(x, y, cx1, cy1, cx2, cy2) + if cx1 then + return { + x = center + x, + y = y, + c1x = center + cx1, + c1y = cy1, + c2x = center + cx2, + c2y = cy2, + } + end + return { x = center + x, y = y } + end + hud[1].coordinates = { + point(-shoulder, 0), + point(shoulder, 0), + point(neck, depth * 0.48, neck, 0, neck, depth * 0.24), + point(join, depth, neck, depth * 0.78, join, depth * 0.88), + point(-join, depth), + point(-neck, depth * 0.48, -join, depth * 0.88, -neck, depth * 0.78), + point(-shoulder, 0, -neck, depth * 0.24, -neck, 0), + } + hud[1].action = "fill" +end + local function render(fraction) progress = fraction if reduce_motion then @@ -33,15 +80,19 @@ local function render(fraction) return end - -- Width leads height so the island opens sideways before settling vertically. - -- The label fades in at a fixed font size once the silhouette can contain it. + -- Width leads height, and the label always keeps its original font size. local remaining = 1 - fraction local w = width - (width - 34) * remaining ^ 3 local h = height - (height - 14) * remaining ^ 2 - hud[1].frame = { x = (width - w) / 2, y = (height - h) / 2, w = w, h = h } - hud[1].roundedRectRadii = { xRadius = h / 2, yRadius = h / 2 } - local text_alpha = math.max(0, math.min(1, (fraction - 0.32) / 0.4)) - hud[2].textColor = { white = 1, alpha = text_alpha ^ 2 * (3 - 2 * text_alpha) } + local top = (height - h) / 2 + if drip then + top = top_gap - (top_gap + 14) * remaining ^ 2 + render_tether(fraction, top, w, h) + hud[3].frame = { x = 0, y = top + (h - label_height) / 2, w = width, h = label_height } + end + hud[2].frame = { x = (width - w) / 2, y = top, w = w, h = h } + hud[2].roundedRectRadii = { xRadius = h / 2, yRadius = h / 2 } + hud[3].textColor = { white = 1, alpha = smoothstep((fraction - 0.32) / 0.4) } hud:alpha(math.min(1, fraction * 5)) end @@ -75,21 +126,31 @@ local function show_hud(title, position) local frame = screen:frame() local inset = math.min(96, frame.h * 0.12) local x = frame.x + (frame.w - width) / 2 - local y = position == "top" and frame.y + 12 or frame.y + frame.h - inset - height + local at_top = position == "top" + local y = at_top and frame.y or frame.y + frame.h - inset - height local previous = hud and hud:frame() if previous and previous.x == x and previous.y == y then - hud[2].text = title + hud[3].text = title else dismiss_hud() reduce_motion = system_reduces_motion() + drip = at_top and not reduce_motion + local gap = at_top and top_gap or 0 local font, size = ".AppleSystemUIFont", 14 - local text = hs.drawing.getTextDrawingSize(title, { font = font, size = size }) - hud = hs.canvas.new({ x = x, y = y, w = width, h = height }) + label_height = + math.ceil(hs.drawing.getTextDrawingSize(title, { font = font, size = size }).h) + hud = hs.canvas.new({ x = x, y = y, w = width, h = height + gap }) hud:appendElements({ + type = "segments", + action = "skip", + closed = true, + fillColor = matte, + }, { type = "rectangle", action = "fill", + frame = { x = 0, y = gap, w = width, h = height }, roundedRectRadii = { xRadius = height / 2, yRadius = height / 2 }, - fillColor = { white = 0.12 }, + fillColor = matte, }, { type = "text", text = title, @@ -97,15 +158,17 @@ local function show_hud(title, position) textSize = size, textColor = { white = 1 }, textAlignment = "center", - frame = { x = 0, y = (height - text.h) / 2, w = width, h = math.ceil(text.h) }, + frame = { x = 0, y = gap + (height - label_height) / 2, w = width, h = label_height }, }) render(0) hud:level(hs.canvas.windowLevels.overlay):show() end - animate_to(1, reduce_motion and 0.15 or open_duration, function() + local opening = reduce_motion and 0.15 or (drip and 0.25 or open_duration) + local closing = reduce_motion and 0.18 or (drip and 0.19 or close_duration) + animate_to(1, opening, function() timer = hs.timer.doAfter(hold, function() - animate_to(0, reduce_motion and 0.18 or close_duration, dismiss_hud) + animate_to(0, closing, dismiss_hud) end) end) end From 8c05789d2a453606c23335d954d8a10f2fb5fe2b Mon Sep 17 00:00:00 2001 From: Colton Padden <colton.padden@fastmail.com> Date: Tue, 24 Nov 2026 14:29:33 -0500 Subject: [PATCH 4/4] hammerspoon: share the island HUD with reload feedback --- hammerspoon/.hammerspoon/init.lua | 18 +- hammerspoon/.hammerspoon/modules/caffeine.lua | 176 +---------------- .../.hammerspoon/modules/island_hud.lua | 177 ++++++++++++++++++ hammerspoon/.hammerspoon/native/README.md | 16 +- 4 files changed, 191 insertions(+), 196 deletions(-) create mode 100644 hammerspoon/.hammerspoon/modules/island_hud.lua diff --git a/hammerspoon/.hammerspoon/init.lua b/hammerspoon/.hammerspoon/init.lua index cf0a67d8..6ad00168 100644 --- a/hammerspoon/.hammerspoon/init.lua +++ b/hammerspoon/.hammerspoon/init.lua @@ -14,10 +14,10 @@ hs.alert.defaultStyle.fadeOutDuration = 2 require("modules.plugins") -- require("modules.alerts") -require("modules.caffeine").bind(HYPER, "0", { position = "top" }) -- "top" or "bottom" +local hud_position = "top" -- "top" or "bottom" for caffeine and reload feedback +local island_hud = require("modules.island_hud") +require("modules.caffeine").bind(HYPER, "0", { position = hud_position }) -local glass_toast = require("modules.glass_toast") -local helpers = require("modules.helpers") local mouse_grid = require("modules.mouse_grid") local wm = require("modules.wm") @@ -78,6 +78,7 @@ mouse_grid.bind(HYPER, "g") -- Clean up overlays without replacing window-state persistence on reload. local save_window_state = hs.shutdownCallback hs.shutdownCallback = function() + island_hud.dismiss() mouse_grid.shutdown() require("modules.chooser").shutdown() if save_window_state then @@ -98,13 +99,4 @@ end) -- The CLI itself is installed separately, so do not recheck/reinstall it on reload. require("hs.ipc") -local reload_title = "Hammerspoon Reloaded" -glass_toast:show({ - layout = "centered-card", - title = reload_title, - symbol = "arrow.clockwise", - detail = "Your configuration is ready.", - tint = "blue", -}, function() - helpers:show(reload_title) -end) +island_hud.show("Hammerspoon Reloaded", hud_position) diff --git a/hammerspoon/.hammerspoon/modules/caffeine.lua b/hammerspoon/.hammerspoon/modules/caffeine.lua index 507d5836..1c250b66 100644 --- a/hammerspoon/.hammerspoon/modules/caffeine.lua +++ b/hammerspoon/.hammerspoon/modules/caffeine.lua @@ -1,177 +1,5 @@ +local island_hud = require("modules.island_hud") local obj = {} -local width, height = 200, 40 -local hold = 1.2 -local open_duration, close_duration = 0.21, 0.16 -local top_gap = 20 -local matte = { white = 0.12 } -local hud, timer -local progress, reduce_motion, drip, label_height - -local function stop_timer() - if timer then - timer:stop() - timer = nil - end -end - -local function dismiss_hud() - stop_timer() - if hud then - hud:delete() - hud = nil - end -end - -local function system_reduces_motion() - local value = - hs.execute("/usr/bin/defaults read com.apple.universalaccess reduceMotion 2>/dev/null") - return value:match("^%s*1%s*$") ~= nil -end - -local function smoothstep(t) - t = math.max(0, math.min(1, t)) - return t ^ 2 * (3 - 2 * t) -end - --- Pull a curved neck between the screen edge and the capsule, then pinch it off. --- The tether is drawn behind the capsule so their overlap reads as one shape. -local function render_tether(fraction, top, w, h) - local tail = 1 - smoothstep((fraction - 0.52) / 0.23) - local depth = (top + h / 2) * tail - if depth <= 0 or tail == 0 then - hud[1].action = "skip" - return - end - - local pinch = 1 - smoothstep((fraction - 0.23) / 0.37) - local center = width / 2 - local shoulder = (16 + 10 * pinch) * tail - local neck = (0.35 + 12 * pinch) * tail - local join = (0.35 + math.min(w * 0.3, 32) * pinch) * tail - local function point(x, y, cx1, cy1, cx2, cy2) - if cx1 then - return { - x = center + x, - y = y, - c1x = center + cx1, - c1y = cy1, - c2x = center + cx2, - c2y = cy2, - } - end - return { x = center + x, y = y } - end - hud[1].coordinates = { - point(-shoulder, 0), - point(shoulder, 0), - point(neck, depth * 0.48, neck, 0, neck, depth * 0.24), - point(join, depth, neck, depth * 0.78, join, depth * 0.88), - point(-join, depth), - point(-neck, depth * 0.48, -join, depth * 0.88, -neck, depth * 0.78), - point(-shoulder, 0, -neck, depth * 0.24, -neck, 0), - } - hud[1].action = "fill" -end - -local function render(fraction) - progress = fraction - if reduce_motion then - hud:alpha(fraction) - return - end - - -- Width leads height, and the label always keeps its original font size. - local remaining = 1 - fraction - local w = width - (width - 34) * remaining ^ 3 - local h = height - (height - 14) * remaining ^ 2 - local top = (height - h) / 2 - if drip then - top = top_gap - (top_gap + 14) * remaining ^ 2 - render_tether(fraction, top, w, h) - hud[3].frame = { x = 0, y = top + (h - label_height) / 2, w = width, h = label_height } - end - hud[2].frame = { x = (width - w) / 2, y = top, w = w, h = h } - hud[2].roundedRectRadii = { xRadius = h / 2, yRadius = h / 2 } - hud[3].textColor = { white = 1, alpha = smoothstep((fraction - 0.32) / 0.4) } - hud:alpha(math.min(1, fraction * 5)) -end - -local function animate_to(target, duration, on_done) - stop_timer() - local start = progress - local seconds = duration * math.abs(target - start) - if seconds == 0 then - on_done() - return - end - local started = hs.timer.absoluteTime() - timer = hs.timer.doEvery(1 / 60, function() - local elapsed = (hs.timer.absoluteTime() - started) / 1e9 - local t = math.min(1, elapsed / seconds) - render(start + (target - start) * t) - if t == 1 then - stop_timer() - on_done() - end - end) -end - --- Reuse the current HUD on quick toggles, even if it is still moving. -local function show_hud(title, position) - local screen = hs.screen.mainScreen() - if not screen then - dismiss_hud() - return - end - local frame = screen:frame() - local inset = math.min(96, frame.h * 0.12) - local x = frame.x + (frame.w - width) / 2 - local at_top = position == "top" - local y = at_top and frame.y or frame.y + frame.h - inset - height - local previous = hud and hud:frame() - if previous and previous.x == x and previous.y == y then - hud[3].text = title - else - dismiss_hud() - reduce_motion = system_reduces_motion() - drip = at_top and not reduce_motion - local gap = at_top and top_gap or 0 - local font, size = ".AppleSystemUIFont", 14 - label_height = - math.ceil(hs.drawing.getTextDrawingSize(title, { font = font, size = size }).h) - hud = hs.canvas.new({ x = x, y = y, w = width, h = height + gap }) - hud:appendElements({ - type = "segments", - action = "skip", - closed = true, - fillColor = matte, - }, { - type = "rectangle", - action = "fill", - frame = { x = 0, y = gap, w = width, h = height }, - roundedRectRadii = { xRadius = height / 2, yRadius = height / 2 }, - fillColor = matte, - }, { - type = "text", - text = title, - textFont = font, - textSize = size, - textColor = { white = 1 }, - textAlignment = "center", - frame = { x = 0, y = gap + (height - label_height) / 2, w = width, h = label_height }, - }) - render(0) - hud:level(hs.canvas.windowLevels.overlay):show() - end - - local opening = reduce_motion and 0.15 or (drip and 0.25 or open_duration) - local closing = reduce_motion and 0.18 or (drip and 0.19 or close_duration) - animate_to(1, opening, function() - timer = hs.timer.doAfter(hold, function() - animate_to(0, closing, dismiss_hud) - end) - end) -end function obj.bind(modifiers, key, options) local position = (options or {}).position or "bottom" @@ -182,7 +10,7 @@ function obj.bind(modifiers, key, options) return hs.hotkey.bind(modifiers, key, function() hs.caffeinate.toggle("displayIdle") local enabled = hs.caffeinate.get("displayIdle") - show_hud(enabled and "Caffeine Enabled" or "Caffeine Disabled", position) + island_hud.show(enabled and "Caffeine Enabled" or "Caffeine Disabled", position) end) end diff --git a/hammerspoon/.hammerspoon/modules/island_hud.lua b/hammerspoon/.hammerspoon/modules/island_hud.lua new file mode 100644 index 00000000..fbc2f266 --- /dev/null +++ b/hammerspoon/.hammerspoon/modules/island_hud.lua @@ -0,0 +1,177 @@ +local obj = {} +local width, height = 200, 40 +local hold = 1.2 +local open_duration, close_duration = 0.21, 0.16 +local top_gap = 20 +local matte = { white = 0.12 } +local hud, timer +local progress, reduce_motion, drip, label_height + +local function stop_timer() + if timer then + timer:stop() + timer = nil + end +end + +function obj.dismiss() + stop_timer() + if hud then + hud:delete() + hud = nil + end +end + +local function system_reduces_motion() + local value = + hs.execute("/usr/bin/defaults read com.apple.universalaccess reduceMotion 2>/dev/null") + return value:match("^%s*1%s*$") ~= nil +end + +local function smoothstep(t) + t = math.max(0, math.min(1, t)) + return t ^ 2 * (3 - 2 * t) +end + +-- Pull a curved neck between the screen edge and the capsule, then pinch it off. +-- The tether is drawn behind the capsule so their overlap reads as one shape. +local function render_tether(fraction, top, w, h) + local tail = 1 - smoothstep((fraction - 0.52) / 0.23) + local depth = (top + h / 2) * tail + if depth <= 0 or tail == 0 then + hud[1].action = "skip" + return + end + + local pinch = 1 - smoothstep((fraction - 0.23) / 0.37) + local center = width / 2 + local shoulder = (16 + 10 * pinch) * tail + local neck = (0.35 + 12 * pinch) * tail + local join = (0.35 + math.min(w * 0.3, 32) * pinch) * tail + local function point(x, y, cx1, cy1, cx2, cy2) + if cx1 then + return { + x = center + x, + y = y, + c1x = center + cx1, + c1y = cy1, + c2x = center + cx2, + c2y = cy2, + } + end + return { x = center + x, y = y } + end + hud[1].coordinates = { + point(-shoulder, 0), + point(shoulder, 0), + point(neck, depth * 0.48, neck, 0, neck, depth * 0.24), + point(join, depth, neck, depth * 0.78, join, depth * 0.88), + point(-join, depth), + point(-neck, depth * 0.48, -join, depth * 0.88, -neck, depth * 0.78), + point(-shoulder, 0, -neck, depth * 0.24, -neck, 0), + } + hud[1].action = "fill" +end + +local function render(fraction) + progress = fraction + if reduce_motion then + hud:alpha(fraction) + return + end + + -- Width leads height, and the label always keeps its original font size. + local remaining = 1 - fraction + local w = width - (width - 34) * remaining ^ 3 + local h = height - (height - 14) * remaining ^ 2 + local top = (height - h) / 2 + if drip then + top = top_gap - (top_gap + 14) * remaining ^ 2 + render_tether(fraction, top, w, h) + hud[3].frame = { x = 0, y = top + (h - label_height) / 2, w = width, h = label_height } + end + hud[2].frame = { x = (width - w) / 2, y = top, w = w, h = h } + hud[2].roundedRectRadii = { xRadius = h / 2, yRadius = h / 2 } + hud[3].textColor = { white = 1, alpha = smoothstep((fraction - 0.32) / 0.4) } + hud:alpha(math.min(1, fraction * 5)) +end + +local function animate_to(target, duration, on_done) + stop_timer() + local start = progress + local seconds = duration * math.abs(target - start) + if seconds == 0 then + on_done() + return + end + local started = hs.timer.absoluteTime() + timer = hs.timer.doEvery(1 / 60, function() + local elapsed = (hs.timer.absoluteTime() - started) / 1e9 + local t = math.min(1, elapsed / seconds) + render(start + (target - start) * t) + if t == 1 then + stop_timer() + on_done() + end + end) +end + +-- Reuse the current HUD for successive messages, even if it is still moving. +function obj.show(title, position) + assert(position == "top" or position == "bottom", 'HUD position must be "top" or "bottom"') + local screen = hs.screen.mainScreen() + if not screen then + obj.dismiss() + return + end + local frame = screen:frame() + local inset = math.min(96, frame.h * 0.12) + local x = frame.x + (frame.w - width) / 2 + local at_top = position == "top" + local y = at_top and frame.y or frame.y + frame.h - inset - height + local previous = hud and hud:frame() + if previous and previous.x == x and previous.y == y then + hud[3].text = title + else + obj.dismiss() + reduce_motion = system_reduces_motion() + drip = at_top and not reduce_motion + local gap = at_top and top_gap or 0 + local font, size = ".AppleSystemUIFont", 14 + label_height = + math.ceil(hs.drawing.getTextDrawingSize(title, { font = font, size = size }).h) + hud = hs.canvas.new({ x = x, y = y, w = width, h = height + gap }) + hud:appendElements({ + type = "segments", + action = "skip", + closed = true, + fillColor = matte, + }, { + type = "rectangle", + action = "fill", + frame = { x = 0, y = gap, w = width, h = height }, + roundedRectRadii = { xRadius = height / 2, yRadius = height / 2 }, + fillColor = matte, + }, { + type = "text", + text = title, + textFont = font, + textSize = size, + textColor = { white = 1 }, + textAlignment = "center", + frame = { x = 0, y = gap + (height - label_height) / 2, w = width, h = label_height }, + }) + render(0) + hud:level(hs.canvas.windowLevels.overlay):show() + end + + local opening = reduce_motion and 0.15 or (drip and 0.25 or open_duration) + local closing = reduce_motion and 0.18 or (drip and 0.19 or close_duration) + animate_to(1, opening, function() + timer = hs.timer.doAfter(hold, function() + animate_to(0, closing, obj.dismiss) + end) + end) +end + +return obj diff --git a/hammerspoon/.hammerspoon/native/README.md b/hammerspoon/.hammerspoon/native/README.md index f0edfd36..40745443 100644 --- a/hammerspoon/.hammerspoon/native/README.md +++ b/hammerspoon/.hammerspoon/native/README.md @@ -18,10 +18,10 @@ make native make preview-glass ``` -To preview startup/reload feedback: +To preview the centered layout: ```sh -native/.build/glass-toast centered-card "Hammerspoon Reloaded" arrow.clockwise 4 \ +native/.build/glass-toast centered-card "Changes Saved" checkmark 4 \ "Your configuration is ready." blue ``` @@ -36,16 +36,14 @@ This short-lived process is intentionally a prototype, not a persistent service. ## Hammerspoon integration -Stow the `hammerspoon` package so `~/.hammerspoon/native` points here, build, -then reload Hammerspoon. Startup/reload uses the native toast with an -`arrow.clockwise` SF Symbol and blue tint. Caffeine (Cmd+Ctrl+0) uses its own -matte canvas HUD and does not require the native helper. Reload feedback falls -back to canvas if the binary is missing or fails to run. +Caffeine (Cmd+Ctrl+0) and startup/reload feedback both use the matte canvas HUD +in `modules/island_hud.lua`, with a shared `hud_position` setting in `init.lua`. +Neither requires the native toast. The glass helper remains available for +standalone previews or callers that want either glass layout. The generated executable lives in ignored `native/.build/`. Rebuild after source changes; builds replace the executable atomically, and a binary is not distributed -or committed. To disable the prototype, remove `native/.build/glass-toast`; -reload feedback will use the canvas again. +or committed. ## Corner geometry