Files
scripts/hypr/cursor_follow.lua
T
adamfandClaude Opus 5.5 017d537138 Fix Kando global shortcut ID for the SUPER+A pie menu
Kando registers its shortcut under its desktop file name, so the ID is
menu.kando.Kando:popups, not kando:popups.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-26 15:36:06 +00:00

464 lines
14 KiB
Lua

-- cursor_follow: keep a floating window glued next to the mouse cursor (Hyprland Lua config)
-- Picture-in-Picture windows follow automatically; SUPER+Z pauses following, and pressed again over
-- another window makes that one follow instead (or over the paused one, resumes it);
-- SUPER+SHIFT+Z stops; SUPER+CTRL+Z toggles a bigger window centered
-- above the cursor; SUPER+ALT+S/A/D play/pause and seek the video (via playerctl);
-- SUPER+ALT+Up/Down widen/narrow the gap to the cursor; SUPER+ALT+Right/Left grow/shrink the window;
-- SUPER+ALT+Z lists these keybindings.
-- SUPER+A opens a Kando pie menu of popups (popup.sh) that open beside the cursor; the keyboard ones
-- hold focus while they follow it and stop following once Enter is pressed in them, so they can be scrolled.
-- Load from hyprland.lua with: require("cursor_follow")
local M = {
cfg = {
width = 480, -- size applied when a window starts following
height = 270,
gap = 24, -- px between the cursor hotspot and the window
gap_step = 8, -- px added/removed per gap key press
margin = 8, -- keep this far from monitor edges
dir = { x = 1, y = -1 }, -- starting side of the cursor (right, above); flips at screen edges
centered_scale = 1.25, -- window size multiplier while centered above the cursor
size_step = 1.1, -- window size multiplier per grow key press (shrink divides by it)
min_width = 160, -- smallest width the shrink key goes down to
interval = 8, -- ms between cursor polls
player = "firefox", -- MPRIS player for the video keys (Waterfox shows up as firefox)
seek = 10, -- seconds skipped by the seek keys
-- SUPER+A menu popups by window class: size, and whether it follows the cursor until Enter is
-- pressed in it (keyboard popups) or is only placed beside the cursor (ones used with the mouse)
popups = {
["popup-ai"] = { 640, 420, follow = true },
["popup-term"] = { 640, 360, follow = true },
-- the receipt printer page as a Chromium app, served locally or by the website
["chrome-127.0.0.1__-Default"] = { 480, 720 },
["chrome-www.adam-french.co.uk__receipt_-Default"] = { 480, 720 },
["org.pulseaudio.pavucontrol"] = { 640, 480 },
},
},
target = nil, -- address of the followed window
paused = false,
centered = false, -- window centered horizontally above the cursor instead of beside it
}
local cfg = M.cfg
local last_x, last_y
-- current side of the cursor per axis; stays flipped until the window hits the opposite edge
local side = { x = cfg.dir.x, y = cfg.dir.y }
local timer
-- marks the followed window so it can be picked back up after a config reload
local tag = "cursor-follow"
-- marks it as centered too, so the mode survives a reload
local centered_tag = "cursor-follow-centered"
-- window that was following before a popup took over, picked back up when the popup closes
local resume_after_popup
-- sticky notes, kept in the scratchpad
local sticky_class = "xpad"
-- the SUPER+A pie menu's full-screen window (Kando's settings window has the same class)
local menu_class, menu_title = "menu.kando.Kando", "Kando Menu"
local function selector()
return "address:" .. M.target
end
-- the popup spec of a following SUPER+A popup
local function follow_popup(w)
local p = w and cfg.popups[w.class]
return p and p.follow and p
end
-- focus follows the mouse and the followed window is never under the cursor, so a keyboard
-- popup holds keyboard focus while it follows
local function lock_focus(on)
if follow_popup(M.target and hl.get_window(selector())) then
hl.dispatch(hl.dsp.window.set_prop({ window = selector(), prop = "stay_focused", value = on and "1" or "0" }))
end
end
local function set_tag(name, on)
hl.dispatch(hl.dsp.window.tag({ window = selector(), tag = (on and "+" or "-") .. name }))
end
local function resize(x, y)
hl.dispatch(hl.dsp.window.resize({ window = selector(), x = math.floor(x + 0.5), y = math.floor(y + 0.5) }))
end
-- centered mode always starts with the window above the cursor
local function reset_side()
side.x, side.y = cfg.dir.x, M.centered and -1 or cfg.dir.y
end
-- Keep a window of `size` starting at `p` within [lo, hi] minus the margin
local function clamp(p, size, lo, hi)
p = math.max(lo + cfg.margin, math.min(p, hi - cfg.margin - size))
return math.floor(p + 0.5)
end
-- Position along one axis: on side `dir` of the cursor, flipped if it would leave the monitor
-- and the other side has room. Returns the position and the (possibly flipped) side.
local function axis(cur, size, lo, hi, dir)
local after, before = cur + cfg.gap, cur - cfg.gap - size
local fits_after, fits_before = after + size <= hi - cfg.margin, before >= lo + cfg.margin
if dir > 0 and not fits_after and fits_before then
dir = -1
elseif dir < 0 and not fits_before and fits_after then
dir = 1
end
return clamp(dir > 0 and after or before, size, lo, hi), dir
end
function M.detach()
timer:set_enabled(false)
if M.target then
set_tag(tag, false)
set_tag(centered_tag, false)
lock_focus(false)
end
M.target, M.paused = nil, false
last_x, last_y = nil, nil
end
-- Position for a window of `size` beside the cursor, on the sides in `s` (flipped in place at
-- the monitor edges), or centered above it
local function beside_cursor(size, s, centered)
local c, m = hl.get_cursor_pos(), hl.get_monitor_at_cursor()
if not c or not m then
return
end
local mw, mh = m.width / m.scale, m.height / m.scale
local x, y
if centered then
x = clamp(c.x - size.x / 2, size.x, m.x, m.x + mw)
else
x, s.x = axis(c.x, size.x, m.x, m.x + mw, s.x)
end
y, s.y = axis(c.y, size.y, m.y, m.y + mh, s.y)
return x, y
end
local function place()
local w = M.target and hl.get_window(selector())
-- stop once the window is closed or tiled again
if not w or not w.floating then
M.detach()
return
end
local x, y = beside_cursor(w.size, side, M.centered)
if not x or (x == last_x and y == last_y) then
return
end
last_x, last_y = x, y
hl.dispatch(hl.dsp.window.move({ window = selector(), x = x, y = y }))
end
timer = hl.timer(place, { timeout = cfg.interval, type = "repeat" })
timer:set_enabled(false)
-- keep_size: picking a window back up after a reload, so leave its size alone
function M.attach(w, keep_size)
if M.target then
M.detach()
end
M.target = w.address
reset_side()
local sel = selector()
set_tag(tag, true)
set_tag(centered_tag, M.centered)
hl.dispatch(hl.dsp.window.float({ window = sel, action = "enable" }))
hl.dispatch(hl.dsp.window.pin({ window = sel, action = "enable" }))
-- moves would otherwise animate and trail behind the cursor
hl.dispatch(hl.dsp.window.set_prop({ window = sel, prop = "no_anim", value = "1" }))
if not keep_size then
local s = M.centered and cfg.centered_scale or 1
resize(cfg.width * s, cfg.height * s)
end
lock_focus(true)
timer:set_enabled(true)
end
function M.toggle_pause()
M.paused = not M.paused
last_x, last_y = nil, nil
timer:set_enabled(not M.paused)
lock_focus(not M.paused)
end
function M.change_gap(d)
cfg.gap = math.max(0, cfg.gap + d)
end
function M.toggle_centered()
M.centered = not M.centered
reset_side()
local w = M.target and hl.get_window(selector())
if w then
set_tag(centered_tag, M.centered)
local s = M.centered and cfg.centered_scale or 1 / cfg.centered_scale
resize(w.size.x * s, w.size.y * s)
end
end
-- Grow (f > 1) or shrink the followed window, keeping its aspect ratio and keeping it between
-- min_width and the monitor width. Later windows start at the new size too.
function M.scale(f)
local w = M.target and hl.get_window(selector())
if not w then
return
end
local m = hl.get_monitor_at_cursor()
local max = m and m.width / m.scale - 2 * cfg.margin or math.huge
f = math.max(cfg.min_width / w.size.x, math.min(f, max / w.size.x))
cfg.width, cfg.height = cfg.width * f, cfg.height * f
resize(w.size.x * f, w.size.y * f)
end
---------------
---- RULES ----
---------------
local pip_title = "Picture-in-Picture"
hl.window_rule({
name = "cursor-follow-pip",
match = { title = "^(" .. pip_title .. ")$" },
float = true,
pin = true,
size = cfg.width .. " " .. cfg.height,
no_anim = true,
no_initial_focus = true,
keep_aspect_ratio = true,
})
for class, p in pairs(cfg.popups) do
hl.window_rule({
name = "cursor-follow-" .. class,
match = { class = "^(" .. class .. ")$" },
float = true,
pin = p.follow,
size = p[1] .. " " .. p[2],
no_anim = true,
})
end
hl.window_rule({
name = "cursor-follow-sticky",
match = { class = "^(" .. sticky_class .. ")$" },
workspace = "special:scratchpad silent",
float = true,
})
-- Kando draws the pie menu in a transparent window covering the screen
hl.window_rule({
name = "kando",
match = { class = menu_class, title = menu_title },
no_blur = true,
opaque = true,
move = { 0, 0 },
rounding = 0,
size = { "100%", "100%" },
border_size = 0,
no_anim = true,
float = true,
pin = true,
})
----------------
---- EVENTS ----
----------------
local function maybe_attach_pip(w)
if w.title == pip_title and w.address ~= M.target then
M.attach(w)
end
end
-- keyboard popups follow the cursor; the others are only placed beside it
local function open_popup(w)
local p = cfg.popups[w.class]
if not p then
return
end
if p.follow then
if w.address ~= M.target then
resume_after_popup = M.target
M.attach(w, true)
end
return
end
local x, y = beside_cursor({ x = p[1], y = p[2] }, { x = cfg.dir.x, y = cfg.dir.y }, false)
if x then
hl.dispatch(hl.dsp.window.move({ window = "address:" .. w.address, x = x, y = y }))
end
end
-- The pie menu needs the keyboard (it selects when SUPER is released) and would otherwise have
-- a following popup chase the cursor across it, so a following popup stays where it is from here on
local function open_menu(w)
if w.class ~= menu_class or w.title ~= menu_title then
return
end
if M.target and not M.paused and follow_popup(hl.get_window(selector())) then
M.toggle_pause()
-- stay_focused may already have kept focus from the menu as it mapped
hl.dispatch(hl.dsp.focus({ window = "address:" .. w.address }))
end
end
hl.on("window.open", maybe_attach_pip)
hl.on("window.open", open_popup)
hl.on("window.open", open_menu)
-- browsers can set the PiP title after the window maps
hl.on("window.title", maybe_attach_pip)
hl.on("window.close", function(w)
if w.address == M.target then
M.detach()
-- hand following back to the window the popup took over from
local prev = follow_popup(w) and resume_after_popup and hl.get_window("address:" .. resume_after_popup)
if prev and prev.floating then
M.attach(prev, true)
end
end
end)
-- an unpinned window stays on one workspace (SUPER+CTRL+N unpins it to send it away), so stop following it
hl.on("window.pin", function(w)
if w.address == M.target and not w.pinned then
M.detach()
end
end)
-- Enter in a following popup sends what was typed, so stop following and let the answer be
-- scrolled with the mouse; non_consuming still passes the key on to the popup
hl.bind("Return", function()
local w = M.target and not M.paused and hl.get_active_window()
if w and w.address == M.target and follow_popup(w) then
M.toggle_pause()
end
end, { non_consuming = true })
------------------
---- KEYBINDS ----
------------------
local keys -- { keys, description, action, bind options }, listed by M.help
local function player(args)
return hl.dsp.exec_cmd("playerctl -p " .. cfg.player .. " " .. args)
end
function M.help()
local lines = {}
for _, k in ipairs(keys) do
lines[#lines + 1] = k[1] .. ": " .. k[2]
end
hl.exec_cmd("notify-send -t 10000 'cursor follow' '" .. table.concat(lines, "\n") .. "'")
end
-- show or hide the sticky notes in the scratchpad, moving any pads that are elsewhere into it
function M.toggle_sticky()
local pads = false
for _, w in ipairs(hl.get_windows()) do
if w.class == sticky_class then
pads = true
if not w.workspace or w.workspace.name ~= "special:scratchpad" then
hl.dispatch(
hl.dsp.window.move({
window = "address:" .. w.address,
workspace = "special:scratchpad",
follow = false,
})
)
end
end
end
if not pads then
hl.exec_cmd("xpad --show")
end
hl.dispatch(hl.dsp.workspace.toggle_special("scratchpad"))
end
-- Pause the following window. Once paused, the window under the cursor (focus follows the mouse)
-- follows instead, leaving the paused one where it is, or the paused one resumes if it's under the cursor.
function M.follow_or_pause()
local w = hl.get_active_window()
if M.target and (not M.paused or not w or w.address == M.target) then
M.toggle_pause()
elseif w then
-- popups keep their own size
M.attach(w, cfg.popups[w.class] ~= nil)
end
end
keys = {
{ "SUPER + Z", "pause following; when paused, the window under the cursor follows instead", M.follow_or_pause },
{ "SUPER + SHIFT + Z", "stop following", M.detach },
{ "SUPER + CTRL + Z", "toggle a bigger window centered above the cursor", M.toggle_centered },
{ "SUPER + ALT + S", "play/pause the video", player("play-pause") },
{ "SUPER + ALT + A", "skip back " .. cfg.seek .. "s", player("position " .. cfg.seek .. "-"), { repeating = true } },
{ "SUPER + ALT + D", "skip forward " .. cfg.seek .. "s", player("position " .. cfg.seek .. "+"), { repeating = true } },
{
"SUPER + ALT + up",
"widen the gap to the cursor",
function()
M.change_gap(cfg.gap_step)
end,
{ repeating = true },
},
{
"SUPER + ALT + down",
"narrow the gap to the cursor",
function()
M.change_gap(-cfg.gap_step)
end,
{ repeating = true },
},
{
"SUPER + ALT + right",
"make the window bigger",
function()
M.scale(cfg.size_step)
end,
{ repeating = true },
},
{
"SUPER + ALT + left",
"make the window smaller",
function()
M.scale(1 / cfg.size_step)
end,
{ repeating = true },
},
{ "SUPER + ALT + Z", "show these keybindings", M.help },
{ "SUPER + A", "pie menu of popups: hold SUPER, flick toward one, release", hl.dsp.global("menu.kando.Kando:popups") },
}
for _, k in ipairs(keys) do
hl.bind(k[1], k[3], k[4])
end
-- A config reload starts this module from scratch while the followed window stays pinned,
-- so it would tag along across workspaces but stand still. Pick it back up.
local resumed = hl.get_windows({ tag = tag })[1]
if not resumed then
for _, w in ipairs(hl.get_windows()) do
if w.title == pip_title then
resumed = w
break
end
end
end
if resumed then
M.centered = hl.get_windows({ tag = centered_tag })[1] ~= nil
M.attach(resumed, true)
-- popups spend most of their life with Enter already pressed
if follow_popup(resumed) then
M.toggle_pause()
end
end
-- the pie menu runs in the background so it opens instantly; its launcher defaults to XWayland
hl.on("hyprland.start", function()
hl.exec_cmd("env KANDO_OZONE_PLATFORM=wayland kando")
end)
return M