From 078bdc1666dcfb518796d6ea0e249163c1867af6 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 31 Oct 2026 10:01:11 -0700 Subject: [PATCH] refactor(windows): focus goes through backend() Switch the 40 Window::focus(mouse_follows)? call sites in process_event, process_command, workspace, window_manager, focus_manager and the stackbar click to backend().focus(WindowId::from_hwnd(..), mouse_follows). WindowsBackend::focus runs Window::focus, so the deferral while a window slides in, the already-foreground shortcut and the cursor centring are unchanged. animation/transition.rs:534 stays on Window::focus (it is the code Window::focus defers to). docs/dev/backend-switch.md records what is switched. BEHAVIOUR: a dead window now fails early with Error::WindowGone (before: the Win32 error from raise_and_focus_window) and a failed focus of a window that died meanwhile reports WindowGone; every caller uses ? or ignores the result. e2e gate needed: commands, focus, focus_follows_mouse, stacking, workspaces, lifecycle. Verified: Windows clippy -D warnings via tools/fast-check PASS; e2e not run. --- docs/dev/backend-switch.md | 17 ++++++++ komorebi/src/focus_manager.rs | 5 ++- komorebi/src/process_command.rs | 19 +++++---- komorebi/src/process_event.rs | 16 ++++++-- komorebi/src/stackbar_manager/stackbar.rs | 6 ++- komorebi/src/window_manager.rs | 50 ++++++++++++++--------- komorebi/src/workspace.rs | 28 +++++++++---- 7 files changed, 100 insertions(+), 41 deletions(-) create mode 100644 docs/dev/backend-switch.md diff --git a/docs/dev/backend-switch.md b/docs/dev/backend-switch.md new file mode 100644 index 00000000..1bf88630 --- /dev/null +++ b/docs/dev/backend-switch.md @@ -0,0 +1,17 @@ +# Windows: which call sites go through `backend()` + +Step 8 of the unified-binaries plan. `komorebi::windows_backend::backend()` returns the shared +`komorebi_backend::Backend` (a stateless `WindowsBackend` over `WindowsApi` and `Window`). The daemon +switches one cluster at a time; each switch is its own revision marked BEHAVIOUR. + +What every switch changes, for the Windows daemon: `WindowsBackend` first asks `IsWindow` and fails +with `Error::WindowGone` for a dead window, and a failure of a window that died meanwhile is reported +as `WindowGone` (the message text changes: it was the Win32 error). Call sites use `?` or ignore the +result, none inspects the error. + +| Cluster | Operation | Status | Sites | Left on `Window`/`WindowsApi`, and why | e2e needed | +|---|---|---|---|---|---| +| focus | `focus(id, mouse_follows)` | switched | process_event (4), process_command (3), workspace (7), window_manager (24), focus_manager, stackbar | `animation/transition.rs:534` calls `Window::focus` from inside the slide-in code that `Window::focus` itself defers to (it would recurse through the backend for no gain) | commands, focus, focus_follows_mouse, stacking, workspaces, lifecycle | +| frame / position | `set_rect`, `window_rect` | not switched | | `Window::set_position` has the `top` flag, animation/slide-in/live-resize guards that the backend `set_rect` (always `top = false`) cannot carry | | +| hide / show | `hide`, `restore`, `minimize`, `unminimize`, `raise`, `close` | not switched | | | | +| queries | `window`, `foreground_window`, `cursor_pos`, `monitors`, `monitor_from_point` | not switched | | | | diff --git a/komorebi/src/focus_manager.rs b/komorebi/src/focus_manager.rs index e6055ff8..d6e3bf98 100644 --- a/komorebi/src/focus_manager.rs +++ b/komorebi/src/focus_manager.rs @@ -7,8 +7,9 @@ use std::ops::Deref; use std::sync::Arc; use std::sync::OnceLock; -use crate::Window; use crate::WindowManager; +use crate::windows_backend::backend; +use komorebi_backend::WindowId; pub struct Notification(isize); @@ -65,7 +66,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result for notification in receiver { let mouse_follows_focus = wm.lock().mouse_follows_focus; - let _ = Window::from(*notification).focus(mouse_follows_focus); + let _ = backend().focus(WindowId::from_hwnd(*notification), mouse_follows_focus); } Ok(()) diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 7362499b..a3fd6d66 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -93,9 +93,11 @@ use crate::window::RuleDebug; use crate::window::Window; use crate::window_manager::WindowManager; use crate::windows_api::WindowsApi; +use crate::windows_backend::backend; use crate::winevent_listener; use crate::workspace::WorkspaceLayer; use crate::workspace::WorkspaceWindowLocation; +use komorebi_backend::WindowId; use stackbar_manager::STACKBAR_FOCUSED_TEXT_COLOUR; use stackbar_manager::STACKBAR_LABEL; use stackbar_manager::STACKBAR_MODE; @@ -283,7 +285,10 @@ impl WindowManager { if let Some(window) = &mut self.focused_workspace_mut()?.maximized_window { - window.focus(self.mouse_follows_focus)?; + backend().focus( + WindowId::from_hwnd(window.hwnd), + self.mouse_follows_focus, + )?; } } WorkspaceWindowLocation::Container(container_idx, window_idx) => { @@ -300,7 +305,10 @@ impl WindowManager { .floating_windows_mut() .get_mut(window_idx) { - window.focus(self.mouse_follows_focus)?; + backend().focus( + WindowId::from_hwnd(window.hwnd), + self.mouse_follows_focus, + )?; } } } @@ -1366,7 +1374,7 @@ impl WindowManager { } }; if let Some(window) = to_focus { - window.focus(mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?; } } SocketMessage::Stop => { @@ -2283,10 +2291,7 @@ if (!(Get-Process komorebi-bar -ErrorAction SilentlyContinue)) } } -pub fn read_commands_uds( - wm: &Arc>, - stream: UnixStream, -) -> eyre::Result<()> { +pub fn read_commands_uds(wm: &Arc>, stream: UnixStream) -> eyre::Result<()> { komorebi_transport::read_commands::(wm, stream, &WindowsConnectionPolicy) } diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index 86813656..bb011c40 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -8,6 +8,8 @@ use parking_lot::Mutex; use crate::core::Rect; use crate::core::WindowContainerBehaviour; +use crate::windows_backend::backend; +use komorebi_backend::WindowId; use crate::CURRENT_VIRTUAL_DESKTOP; use crate::DefaultLayout; @@ -301,7 +303,7 @@ impl WindowManager { match event { WindowManagerEvent::Raise(window) => { - window.focus(false)?; + backend().focus(WindowId::from_hwnd(window.hwnd), false)?; self.has_pending_raise_op = false; } WindowManagerEvent::Destroy(_, window) | WindowManagerEvent::Unmanage(window) => { @@ -435,7 +437,7 @@ impl WindowManager { if let Some(monocle) = &workspace.monocle_container { if let Some(window) = monocle.focused_window() { - window.focus(false)?; + backend().focus(WindowId::from_hwnd(window.hwnd), false)?; } } else { workspace.focus_container_by_window(window.hwnd)?; @@ -621,7 +623,10 @@ impl WindowManager { // first send the `FocusChange` event and only the `Show` event after // and we will be focusing the desktop on the `FocusChange` event since // it is still empty. - window.focus(self.mouse_follows_focus)?; + backend().focus( + WindowId::from_hwnd(window.hwnd), + self.mouse_follows_focus, + )?; } } @@ -812,7 +817,10 @@ impl WindowManager { self.update_focused_workspace(false, false)?; // Make sure to give focus to the moved window again - window.focus(self.mouse_follows_focus)?; + backend().focus( + WindowId::from_hwnd(window.hwnd), + self.mouse_follows_focus, + )?; } } else if window_management_behaviour.float_override { workspace.floating_windows_mut().push_back(window); diff --git a/komorebi/src/stackbar_manager/stackbar.rs b/komorebi/src/stackbar_manager/stackbar.rs index 68543ff2..71c208a4 100644 --- a/komorebi/src/stackbar_manager/stackbar.rs +++ b/komorebi/src/stackbar_manager/stackbar.rs @@ -18,7 +18,9 @@ use crate::stackbar_manager::STACKBAR_TAB_WIDTH; use crate::stackbar_manager::STACKBAR_UNFOCUSED_TEXT_COLOUR; use crate::stackbar_manager::STACKBARS_CONTAINERS; use crate::windows_api; +use crate::windows_backend::backend; use crossbeam_utils::atomic::AtomicConsume; +use komorebi_backend::WindowId; use std::os::windows::ffi::OsStrExt; use std::sync::atomic::Ordering; use std::sync::mpsc; @@ -370,7 +372,9 @@ impl Stackbar { // Restore the window corresponding to the tab we have clicked window.restore_with_border(false); - if let Err(err) = window.focus(false) { + if let Err(err) = + backend().focus(WindowId::from_hwnd(window.hwnd), false) + { tracing::error!( "stackbar WMLBUTTONDOWN focus error: hwnd {} ({})", *window, diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index a4e6502c..15d02218 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -48,6 +48,8 @@ use crate::core::Sizing; use crate::core::WindowContainerBehaviour; use crate::core::WindowManagementBehaviour; use crate::core::config_generation::MatchingRule; +use crate::windows_backend::backend; +use komorebi_backend::WindowId; use crate::CrossBoundaryBehaviour; use crate::DATA_DIR; @@ -1129,17 +1131,17 @@ impl WindowManager { if follow_focus { if let Some(window) = self.focused_workspace()?.maximized_window { if trigger_focus { - window.focus(self.mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?; } } else if let Some(container) = &self.focused_workspace()?.monocle_container { if let Some(window) = container.focused_window() && trigger_focus { - window.focus(self.mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?; } } else if let Ok(window) = self.focused_window_mut() { if trigger_focus { - window.focus(self.mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?; } } else { let desktop_window = Window::from(WindowsApi::desktop_window()?); @@ -1179,7 +1181,7 @@ impl WindowManager { && let Ok(window) = self.focused_window_mut() && trigger_focus { - window.focus(self.mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?; } } } @@ -1995,7 +1997,7 @@ impl WindowManager { if let Some(idx) = target_idx { focused_workspace.floating_windows.focus(idx); if let Some(window) = focused_workspace.floating_windows().get(idx) { - window.focus(mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?; } return Ok(()); } @@ -2055,11 +2057,11 @@ impl WindowManager { if let Ok(focused_workspace) = self.focused_workspace_mut() { if let Some(window) = focused_workspace.maximized_window { - window.focus(mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?; cross_monitor_monocle_or_max = true; } else if let Some(monocle) = &focused_workspace.monocle_container { if let Some(window) = monocle.focused_window() { - window.focus(mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?; cross_monitor_monocle_or_max = true; } } else if focused_workspace.layer == WorkspaceLayer::Tiling { @@ -2111,12 +2113,15 @@ impl WindowManager { } } else if ws.layer == WorkspaceLayer::Floating && !ws.floating_windows().is_empty() { if let Some(window) = ws.focused_floating_window() { - window.focus(self.mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?; } } else { ws.layer = WorkspaceLayer::Tiling; if let Ok(focused_window) = self.focused_window() { - focused_window.focus(self.mouse_follows_focus)?; + backend().focus( + WindowId::from_hwnd(focused_window.hwnd), + self.mouse_follows_focus, + )?; } } } @@ -2258,11 +2263,12 @@ impl WindowManager { if let Ok(focused_workspace) = self.focused_workspace_mut() { if let Some(window) = focused_workspace.maximized_window { - window.focus(mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?; cross_monitor_monocle_or_max = true; } else if let Some(monocle) = &focused_workspace.monocle_container { if let Some(window) = monocle.focused_window() { - window.focus(mouse_follows_focus)?; + backend() + .focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?; cross_monitor_monocle_or_max = true; } } else if focused_workspace.layer == WorkspaceLayer::Tiling { @@ -2320,12 +2326,15 @@ impl WindowManager { } } else if ws.layer == WorkspaceLayer::Floating && !ws.floating_windows().is_empty() { if let Some(window) = ws.focused_floating_window() { - window.focus(self.mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?; } } else { ws.layer = WorkspaceLayer::Tiling; if let Ok(focused_window) = self.focused_window() { - focused_window.focus(self.mouse_follows_focus)?; + backend().focus( + WindowId::from_hwnd(focused_window.hwnd), + self.mouse_follows_focus, + )?; } } } @@ -2634,7 +2643,7 @@ impl WindowManager { if let Some(idx) = target_idx && let Some(window) = focused_workspace.floating_windows().get(idx) { - window.focus(mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?; } Ok(()) @@ -2674,7 +2683,8 @@ impl WindowManager { } else if monocle_next { self.toggle_monocle()?; } else { - self.focused_window_mut()?.focus(self.mouse_follows_focus)?; + let hwnd = self.focused_window_mut()?.hwnd; + backend().focus(WindowId::from_hwnd(hwnd), self.mouse_follows_focus)?; } Ok(()) @@ -2734,7 +2744,7 @@ impl WindowManager { container.load_focused_window(); if let Some(window) = container.focused_window() { - window.focus(self.mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?; } self.update_focused_workspace(self.mouse_follows_focus, true) @@ -2771,7 +2781,7 @@ impl WindowManager { container.load_focused_window(); if let Some(window) = container.focused_window() { - window.focus(self.mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?; } self.update_focused_workspace(self.mouse_follows_focus, true) @@ -2804,7 +2814,7 @@ impl WindowManager { container.load_focused_window(); if let Some(window) = container.focused_window() { - window.focus(self.mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?; } self.update_focused_workspace(self.mouse_follows_focus, true) @@ -2948,7 +2958,7 @@ impl WindowManager { if changed_focus && let Some(container) = workspace.focused_container_mut() { container.load_focused_window(); if let Some(window) = container.focused_window() { - window.focus(self.mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?; } } @@ -3110,7 +3120,7 @@ impl WindowManager { if toggle_float_placement.should_center() { window.center(&work_area, toggle_float_placement.should_resize())?; } - window.focus(self.mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?; Ok(()) } diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index 9b2bd4c7..48e43ddf 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -46,8 +46,10 @@ use crate::static_config::WorkspaceConfig; use crate::window::Window; use crate::window::WindowDetails; use crate::windows_api::WindowsApi; +use crate::windows_backend::backend; use color_eyre::eyre; use color_eyre::eyre::OptionExt; +use komorebi_backend::WindowId; use komorebi_scrolling::ScrollingSpace; use komorebi_themes::Base16ColourPalette; use komorebi_themes::KomorebiThemeCustom as Custom; @@ -553,7 +555,7 @@ impl Workspace { && let Some(window) = container.focused_window() { container.restore(); - window.focus(mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?; return self.apply_wallpaper(hmonitor, monitor_wp); } @@ -583,18 +585,30 @@ impl Workspace { // when switching to a workspace if let Some(window) = to_focus { if self.maximized_window.is_none() && matches!(self.layer, WorkspaceLayer::Tiling) { - window.focus(mouse_follows_focus)?; + backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?; } else if let Some(maximized_window) = self.maximized_window { maximized_window.restore(); - maximized_window.focus(mouse_follows_focus)?; + backend().focus( + WindowId::from_hwnd(maximized_window.hwnd), + mouse_follows_focus, + )?; } else if let Some(floating_window) = self.focused_floating_window() { - floating_window.focus(mouse_follows_focus)?; + backend().focus( + WindowId::from_hwnd(floating_window.hwnd), + mouse_follows_focus, + )?; } } else if let Some(maximized_window) = self.maximized_window { maximized_window.restore(); - maximized_window.focus(mouse_follows_focus)?; + backend().focus( + WindowId::from_hwnd(maximized_window.hwnd), + mouse_follows_focus, + )?; } else if let Some(floating_window) = self.focused_floating_window() { - floating_window.focus(mouse_follows_focus)?; + backend().focus( + WindowId::from_hwnd(floating_window.hwnd), + mouse_follows_focus, + )?; } self.apply_wallpaper(hmonitor, monitor_wp) @@ -1219,7 +1233,7 @@ impl Workspace { } else { container.load_focused_window(); if let Some(window) = container.focused_window() { - window.focus(false)?; + backend().focus(WindowId::from_hwnd(window.hwnd), false)?; } }