Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

Refocus the previous window when the focused one closes without workspaces

oxydizer3948983cf5fa
diff --git a/src/toplevel/chrome_actions.zig b/src/toplevel/chrome_actions.zig
index 1f9b3ac..6661237 100644
--- a/src/toplevel/chrome_actions.zig
+++ b/src/toplevel/chrome_actions.zig
@@ -70,6 +70,7 @@ pub fn minimize(toplevel: *Toplevel) void {
toplevel.frame_tree.node.setEnabled(false);
const server = toplevel.server;
+ const held_focus = server.world.toplevels.first() == toplevel and toplevel.activated;
if (server.input.seat.keyboard_state.focused_surface) |focused| {
if (fromSurface(server, focused) == toplevel) server.input.seat.keyboardNotifyClearFocus();
}
@@ -101,7 +102,11 @@ pub fn minimize(toplevel: *Toplevel) void {
}
if (toplevel.tab_group != 0) @import("../window_tabs.zig").refreshVisibility(server);
- if (server.world.workspaces.active) @import("../workspaces.zig").focusFallback(server, toplevel.workspace_id);
+ if (server.world.workspaces.active) {
+ @import("../workspaces.zig").focusFallback(server, toplevel.workspace_id);
+ } else if (held_focus) {
+ @import("../workspaces.zig").focusFallback(server, null);
+ }
// Must run last: content_tree stays alive and rendering-ready while
// minimized (only the frame is hidden), so a window-capture source
// over it wouldn't otherwise notice — unlike a real close, nothing
diff --git a/src/toplevel/lifecycle.zig b/src/toplevel/lifecycle.zig
index e0fa737..e90806f 100644
--- a/src/toplevel/lifecycle.zig
+++ b/src/toplevel/lifecycle.zig
@@ -250,6 +250,7 @@ pub fn handleUnmapped(toplevel: *Toplevel) void {
toplevel.server.undo.applying = true;
defer toplevel.server.undo.applying = undo_applying;
const was_focused = toplevel.server.world.toplevels.first() == toplevel;
+ const was_activated = toplevel.activated;
const was_tab = toplevel.tab_group != 0;
@import("../auto_tiling.zig").unmapped(toplevel);
@import("../window_tabs.zig").rememberUnmap(toplevel);
@@ -312,7 +313,13 @@ pub fn handleUnmapped(toplevel: *Toplevel) void {
if (owner_toplevel.in_world) toplevel.server.world.focus(owner_toplevel);
}
}
- if (was_focused and toplevel.server.world.workspaces.active) @import("../workspaces.zig").focusFallback(toplevel.server, toplevel.workspace_id);
+ // Closing the active window hands the keyboard to the most recently
+ // focused survivor, on the canvas as on a workspace. `activated` is false
+ // while the desktop holds keyboard focus, which must not be taken away.
+ if (was_focused and was_activated) {
+ const workspaces = &toplevel.server.world.workspaces;
+ @import("../workspaces.zig").focusFallback(toplevel.server, if (workspaces.active) toplevel.workspace_id else null);
+ }
if (toplevel.server.ipc) |ipc| {
events.onWindowClosed(ipc, toplevel.id);
ipc.wait_mgr.checkAll();
diff --git a/tests/switcher.py b/tests/switcher.py
index 22bd891..04eb3a9 100644
--- a/tests/switcher.py
+++ b/tests/switcher.py
@@ -162,6 +162,18 @@ def run(scale, modifier=56, style='linear'):
ipc.pointer_button(0x110, False)
ipc.key(modifier, False)
assert focused() == accepted, 'dismissal must preserve focus'
+ # Closing or minimizing the focused window hands the keyboard
+ # to the previously focused one, without workspaces too.
+ def keyboard_window():
+ return next((w['id'] for w in ipc.get_windows() if w['is_focused']), None)
+ by_title = {w['title']: w['id'] for w in ipc.get_windows()}
+ live = [by_title[f'Window {i}'] for i in range(len(clients))]
+ for wid in live: ipc.action('focus_window', {'id': wid})
+ stop_process(clients.pop())
+ wait_for(lambda: len(ipc.get_windows()) == len(live) - 1, 'focused client teardown')
+ wait_for(lambda: keyboard_window() == live[-2], 'closing the focused window refocuses the previous one')
+ ipc.minimize(live[-2])
+ wait_for(lambda: keyboard_window() == live[-3], 'minimizing the focused window refocuses the previous one')
while len(clients) > 1:
stop_process(clients.pop())
wait_for(lambda: len(ipc.get_windows()) == 1, 'one remaining window')