From 9ba821bdd8ba160e61b000c508d90628b6d26369 Mon Sep 17 00:00:00 2001 From: Marcelo Alcantara Date: Thu, 8 Oct 2026 12:42:24 +1000 Subject: [PATCH] Keep the focused workspace in front when a monitor goes away Brings back the workspace refocus split out of #14431 so it gets its own review: when a monitor is unplugged, the workspace the user was viewing stays in front on the remaining monitor instead of whatever Hyprland picks. --- default/hypr/monitor-removal.lua | 88 ++++++++ default/hypr/omarchy.lua | 1 + test/shell.d/hyprland-monitor-removal-test.sh | 188 ++++++++++++++++++ 3 files changed, 277 insertions(+) create mode 100644 default/hypr/monitor-removal.lua create mode 100755 test/shell.d/hyprland-monitor-removal-test.sh diff --git a/default/hypr/monitor-removal.lua b/default/hypr/monitor-removal.lua new file mode 100644 index 000000000000..deb94a05a7aa --- /dev/null +++ b/default/hypr/monitor-removal.lua @@ -0,0 +1,88 @@ +-- Unplugging a monitor, or the lid disabling the laptop panel, keeps you on the +-- workspace you were on. Hyprland first warps focus to whichever monitor it +-- lists first and only then moves the removed monitor's workspaces over, so the +-- one you were on lands hidden behind that monitor's own. Measured on Hyprland +-- 0.56.2 (CMonitor::onDisconnect). +-- +-- monitor.focused fires before Hyprland records the new focus, so the monitor +-- still reported as active is the one being left. The workspace it showed is +-- kept until the event loop next turns, which the removal that caused the warp +-- finishes well before: a monitor.removed inside that window brings it back. + +local left_behind = nil +local generation = 0 +local moves = 0 +local restoring = false + +local function selector(ws) + if not ws or type(ws.id) ~= "number" then + return nil + end + + -- Named workspaces have negative ids, which a selector reads as relative. + if ws.id > 0 then + return tostring(ws.id) + end + return "name:" .. ws.name +end + +local function later(fn) + hl.timer(fn, { timeout = 1, type = "oneshot" }) +end + +hl.on("monitor.focused", function(monitor) + if restoring then + return + end + + generation = generation + 1 + moves = moves + 1 + left_behind = nil + + local leaving = hl.get_active_monitor() + if not leaving or not monitor or leaving.name == monitor.name then + return + end + + -- An open scratchpad already comes along to the front of the next monitor. + if hl.get_active_special_workspace(leaving) then + return + end + + left_behind = selector(hl.get_active_workspace(leaving)) + local seen = generation + later(function() + if generation == seen then + left_behind = nil + end + end) +end) + +-- Only counts towards giving way to the user once the removal is over: the +-- workspaces it moves between monitors fire this too, before monitor.removed. +hl.on("workspace.active", function() + if not restoring then + moves = moves + 1 + end +end) + +hl.on("monitor.removed", function() + local workspace = left_behind + left_behind = nil + if not workspace then + return + end + + -- Let the removal finish first, and give way to any focus or workspace change + -- since. + local seen = moves + later(function() + if moves ~= seen or selector(hl.get_active_workspace()) == workspace or not hl.get_workspace(workspace) then + return + end + + restoring = true + pcall(hl.dispatch, hl.dsp.focus({ workspace = workspace })) + restoring = false + end) +end) diff --git a/default/hypr/omarchy.lua b/default/hypr/omarchy.lua index abc7c4e0888d..6586024d8a62 100644 --- a/default/hypr/omarchy.lua +++ b/default/hypr/omarchy.lua @@ -16,6 +16,7 @@ end require("default.hypr.envs") require("default.hypr.looknfeel") require("default.hypr.qconsole") +require("default.hypr.monitor-removal") require("default.hypr.input") require("default.hypr.windows") diff --git a/test/shell.d/hyprland-monitor-removal-test.sh b/test/shell.d/hyprland-monitor-removal-test.sh new file mode 100755 index 000000000000..2478d89f3f1b --- /dev/null +++ b/test/shell.d/hyprland-monitor-removal-test.sh @@ -0,0 +1,188 @@ +#!/bin/bash + +source "$(dirname "${BASH_SOURCE[0]}")/base-test.sh" + +require_command lua + +grep -Fx 'require("default.hypr.monitor-removal")' "$ROOT/default/hypr/omarchy.lua" >/dev/null || + fail "the Omarchy Hyprland config loads the monitor removal handler" +pass "the Omarchy Hyprland config loads the monitor removal handler" + +# Replays the event order of Hyprland 0.56.2's CMonitor::onDisconnect: focus is +# warped to the first remaining monitor, and monitor.focused fires while the +# monitor being left is still the active one (FocusState.cpp rawMonitorFocus). +# The removed monitor's workspaces then move over hidden and monitor.removed +# fires, all in one event-loop turn. hl.timer callbacks only run once that turn +# is over. +OMARCHY_PATH="$ROOT" lua - <<'LUA' || fail "a monitor removal keeps the focused workspace in front" +local handlers, timers, dispatched = {}, {}, {} +local monitors, focused, workspaces, after_dispatch + +local function reset() + monitors = { + ["eDP-1"] = { name = "eDP-1", active = { id = 1, name = "1" } }, + ["USB-2"] = { name = "USB-2", active = { id = 5, name = "5" } }, + } + focused = "eDP-1" + workspaces = { ["1"] = true, ["5"] = true, ["name:notes"] = true } + dispatched = {} + after_dispatch = nil +end + +-- Like rawMonitorFocus, which returns early when the monitor is unchanged. +local function focus_moves(to, assign_first) + if focused == to then return end + if assign_first then focused = to end + handlers["monitor.focused"]({ name = to }) + focused = to +end + +local function switch_workspace(id) + monitors[focused].active = { id = id, name = tostring(id) } + workspaces[tostring(id)] = true + handlers["workspace.active"](monitors[focused].active) +end + +hl = { + on = function(event, callback) + handlers[event] = callback + end, + timer = function(callback, opts) + assert(opts.type == "oneshot" and opts.timeout > 0) + table.insert(timers, callback) + end, + get_active_monitor = function() + return monitors[focused] + end, + get_active_workspace = function(monitor) + return (monitor or monitors[focused]).active + end, + get_active_special_workspace = function(monitor) + return (monitor or monitors[focused]).special + end, + get_workspace = function(selector) + return workspaces[selector] and {} or nil + end, + dispatch = function(action) + table.insert(dispatched, action.workspace) + -- Focusing a workspace on another monitor moves monitor focus too. + focus_moves("eDP-1") + if after_dispatch then after_dispatch() end + end, + dsp = { + focus = function(args) + return args + end, + }, +} + +local function turn_ends() + while #timers > 0 do + local pending = timers + timers = {} + for _, callback in ipairs(pending) do + callback() + end + end +end + +-- Unplug USB-2 the way Hyprland does: the warp goes to eDP-1, first in its +-- monitor list, wherever focus was. +local function unplug(opts) + opts = opts or {} + local was_on_it = focused == "USB-2" + focus_moves("eDP-1", opts.assign_first) + if was_on_it then + -- Moving the active workspace off leaves the dying monitor a placeholder. + handlers["workspace.active"]({ id = 2, name = "2" }) + end + monitors["USB-2"] = nil + handlers["monitor.removed"]({ name = "USB-2" }) + if opts.then_focus then + focus_moves(opts.then_focus) + end + if opts.then_switch then + switch_workspace(opts.then_switch) + end + turn_ends() +end + +dofile(os.getenv("OMARCHY_PATH") .. "/default/hypr/bootstrap.lua") +require("default.hypr.monitor-removal") + +reset() +focus_moves("USB-2"); turn_ends() +unplug() +assert(#dispatched == 1 and dispatched[1] == "5", "the workspace you were on comes to the front of the built-in panel") + +reset() +unplug() +assert(#dispatched == 0, "unplugging a display you were not on changes nothing") + +reset() +focus_moves("USB-2"); turn_ends() +focus_moves("eDP-1"); turn_ends() +unplug() +assert(#dispatched == 0, "an earlier focus change does not switch the workspace later") + +reset() +monitors["HDMI-A-1"] = { name = "HDMI-A-1", active = { id = 3, name = "3" } } +focus_moves("USB-2"); turn_ends() +unplug({ then_focus = "HDMI-A-1" }) +assert(#dispatched == 0, "a focus change after the removal wins over the restore") + +reset() +focus_moves("USB-2"); turn_ends() +unplug({ then_switch = 9 }) +assert(#dispatched == 0, "a workspace switch after the removal wins over the restore") + +reset() +monitors["HDMI-A-1"] = { name = "HDMI-A-1", active = { id = 7, name = "7" } } +workspaces["7"] = true +focus_moves("HDMI-A-1"); turn_ends() +unplug() +assert(#dispatched == 1 and dispatched[1] == "7", "unplugging another display leaves focus on the third one") + +reset() +workspaces["5"] = nil +focus_moves("USB-2"); turn_ends() +unplug() +assert(#dispatched == 0, "an empty workspace, gone once hidden, is not recreated") + +reset() +monitors["USB-2"].special = { id = -98, name = "special:scratchpad" } +focus_moves("USB-2"); turn_ends() +unplug() +assert(#dispatched == 0, "an open scratchpad is left in front") + +reset() +monitors["USB-2"].active = { id = -1337, name = "notes" } +focus_moves("USB-2"); turn_ends() +unplug() +assert(#dispatched == 1 and dispatched[1] == "name:notes", "a named workspace is focused by name") + +-- The restore's own focus change does not arm another one: here it leaves a +-- third monitor, which is then removed in the same turn. +reset() +monitors["HDMI-A-1"] = { name = "HDMI-A-1", active = { id = 3, name = "3" } } +workspaces["3"] = true +focus_moves("USB-2"); turn_ends() +focus_moves("eDP-1") +monitors["USB-2"] = nil +handlers["monitor.removed"]({ name = "USB-2" }) +focused = "HDMI-A-1" +after_dispatch = function() + monitors["HDMI-A-1"] = nil + handlers["monitor.removed"]({ name = "HDMI-A-1" }) +end +turn_ends() +assert(#dispatched == 1 and dispatched[1] == "5", "restoring focus does not queue a second restore") + +-- A Hyprland that records focus before announcing it leaves nothing to restore +-- from, so the handler stays out of the way rather than guessing. +reset() +focus_moves("USB-2"); turn_ends() +unplug({ assign_first = true }) +assert(#dispatched == 0, "a changed event order degrades to Hyprland's own behaviour") +LUA +pass "a monitor removal keeps the focused workspace in front"