Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions hammerspoon/.hammerspoon/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions hammerspoon/.hammerspoon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ hs.alert.defaultStyle.fadeOutDuration = 2

require("modules.plugins")
-- require("modules.alerts")
require("modules.caffeine")
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")

Expand Down Expand Up @@ -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
Expand All @@ -98,6 +99,4 @@ 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")
end)
island_hud.show("Hammerspoon Reloaded", hud_position)
25 changes: 15 additions & 10 deletions hammerspoon/.hammerspoon/modules/caffeine.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
local glass_toast = require("modules.glass_toast")
local helpers = require("modules.helpers")
local island_hud = require("modules.island_hud")
local obj = {}

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)
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")
island_hud.show(enabled and "Caffeine Enabled" or "Caffeine Disabled", position)
end)
end)
end

return obj
14 changes: 11 additions & 3 deletions hammerspoon/.hammerspoon/modules/glass_toast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
177 changes: 177 additions & 0 deletions hammerspoon/.hammerspoon/modules/island_hud.lua
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 additions & 1 deletion hammerspoon/.hammerspoon/modules/plugins.lua
Original file line number Diff line number Diff line change
@@ -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" } } })

Expand Down
Loading
Loading