From fcc4da32b9d1e081570836adf8038db8c4e5aaf7 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 31 Oct 2026 15:34:01 -0700 Subject: [PATCH] refactor(kde,mac): window_manager.rs asks window::foreground_window_id() for the focus Both daemons' window_manager.rs read the focused window's id the same way: `foreground_window_id()` from their own window.rs (KDE's is the backend query it already had there; macOS gets the one-line wrapper of MacosApi::foreground_window_id). The inline `backend().foreground_window() .and_then(WindowId::to_u32)` chains on KDE and the `MacosApi::` calls on macOS go, which is the first step to the same text in the two files (the Windows one is not touched). No behaviour change. Verified: KDE cargo test -p komorebi-linux (lite env): 664 passed, 0 failed; fake-CC cargo clippy -p komorebi-macos -p komorebic-macos -p komorebi-client-macos -p komorebi-bar-macos --all-targets -D warnings: PASS (type-check only). Not run: the macOS tests (the Mac Mini), VM suites. --- .../komorebi/src/window_manager.rs | 55 +++++-------------- komorebi-for-mac/komorebi/src/window.rs | 5 ++ .../komorebi/src/window_manager.rs | 31 +++++------ 3 files changed, 35 insertions(+), 56 deletions(-) diff --git a/komorebi-for-kde/komorebi/src/window_manager.rs b/komorebi-for-kde/komorebi/src/window_manager.rs index 86776110..2d952b57 100644 --- a/komorebi-for-kde/komorebi/src/window_manager.rs +++ b/komorebi-for-kde/komorebi/src/window_manager.rs @@ -48,9 +48,6 @@ //! //! [`listen_for_commands`]: crate::process_command::listen_for_commands -use crate::monitor::MonitorExt; -use crate::scrolling::WorkspaceScrolling; -use crate::workspace::WorkspaceExt; use crate::DATA_DIR; use crate::HIDDEN_WINDOWS; use crate::LibraryError; @@ -80,21 +77,24 @@ use crate::core::config_generation::MatchingRule; use crate::desktop; use crate::lockable_sequence::Lockable; use crate::monitor::Monitor; +use crate::monitor::MonitorExt; use crate::ring::Ring; +use crate::scrolling::WorkspaceScrolling; use crate::static_config::StaticConfig; use crate::static_config::StaticConfigExt; use crate::window::Window; +use crate::window::foreground_window_id; use crate::window::should_act_individual; use crate::window_manager_event::WindowManagerEvent; use crate::window_manager_event_listener; use crate::workspace::Workspace; +use crate::workspace::WorkspaceExt; use crate::workspace::WorkspaceLayer; use color_eyre::eyre; use color_eyre::eyre::OptionExt; use color_eyre::eyre::bail; use crossbeam_channel::Receiver; use hotwatch::Hotwatch; -use komorebi_backend::WindowId; use parking_lot::Mutex; use std::collections::HashMap; use std::collections::HashSet; @@ -324,10 +324,7 @@ impl WindowManager { let len = focused_workspace.floating_windows().len(); if len > 1 { - let focused_window_id = backend() - .foreground_window() - .and_then(WindowId::to_u32) - .ok_or_eyre("no foreground window")?; + let focused_window_id = foreground_window_id().ok_or_eyre("no foreground window")?; let focused_rect = Window::from(focused_window_id).rect()?; match direction { @@ -800,10 +797,7 @@ impl WindowManager { let focused_workspace = self.focused_workspace()?; let delta = self.resize_delta; - let focused_window_id = backend() - .foreground_window() - .and_then(WindowId::to_u32) - .ok_or_eyre("no foreground window")?; + let focused_window_id = foreground_window_id().ok_or_eyre("no foreground window")?; for window in focused_workspace.floating_windows().iter() { if window.id == focused_window_id { let mut rect = window.rect()?; @@ -1706,10 +1700,7 @@ impl WindowManager { /// - The workspace cannot be tiled again. #[tracing::instrument(skip(self))] pub fn toggle_float(&mut self, force_float: bool) -> eyre::Result<()> { - let window_id = backend() - .foreground_window() - .and_then(WindowId::to_u32) - .ok_or_eyre("no foreground window")?; + let window_id = foreground_window_id().ok_or_eyre("no foreground window")?; let workspace = self.focused_workspace_mut()?; if workspace.monocle_container.is_some() { @@ -1816,10 +1807,8 @@ impl WindowManager { match workspace.layer { WorkspaceLayer::Floating => { let workspace = self.focused_workspace()?; - let focused_window_id = backend() - .foreground_window() - .and_then(WindowId::to_u32) - .ok_or_eyre("no foreground window")?; + let focused_window_id = + foreground_window_id().ok_or_eyre("no foreground window")?; let border_offset = 0; let border_width = 0; @@ -2476,10 +2465,7 @@ impl WindowManager { eyre::bail!("cannot move native maximized window to another monitor or workspace"); } - let foreground_window_id = backend() - .foreground_window() - .and_then(WindowId::to_u32) - .ok_or_eyre("no foreground window")?; + let foreground_window_id = foreground_window_id().ok_or_eyre("no foreground window")?; let floating_window_index = workspace .floating_windows() .iter() @@ -2772,10 +2758,7 @@ impl WindowManager { let len = focused_workspace.floating_windows().len(); if len > 1 { - let focused_window_id = backend() - .foreground_window() - .and_then(WindowId::to_u32) - .ok_or_eyre("no foreground window")?; + let focused_window_id = foreground_window_id().ok_or_eyre("no foreground window")?; for (idx, window) in focused_workspace.floating_windows().iter().enumerate() { if window.id == focused_window_id { match direction { @@ -3276,10 +3259,8 @@ impl WindowManager { OperationBehaviour::NoOp ) { let workspace = self.focused_workspace()?; - let focused_hwnd = backend() - .foreground_window() - .and_then(WindowId::to_u32) - .ok_or_eyre("there is no foreground window")?; + let focused_hwnd = + foreground_window_id().ok_or_eyre("there is no foreground window")?; if !workspace.contains_managed_window(focused_hwnd) { bail!("ignoring commands while active window is not managed by komorebi"); } @@ -3810,10 +3791,7 @@ impl WindowManager { /// The compositor reports no focused window. #[tracing::instrument(skip(self))] pub fn manage_focused_window(&mut self) -> eyre::Result<()> { - let window_id = backend() - .foreground_window() - .and_then(WindowId::to_u32) - .ok_or_eyre("there is no foreground window")?; + let window_id = foreground_window_id().ok_or_eyre("there is no foreground window")?; UNMANAGED_WINDOW_IDS.lock().retain(|id| *id != window_id); @@ -3833,10 +3811,7 @@ impl WindowManager { /// The compositor reports no focused window. #[tracing::instrument(skip(self))] pub fn unmanage_focused_window(&mut self) -> eyre::Result<()> { - let window_id = backend() - .foreground_window() - .and_then(WindowId::to_u32) - .ok_or_eyre("there is no foreground window")?; + let window_id = foreground_window_id().ok_or_eyre("there is no foreground window")?; window_manager_event_listener::send_notification(WindowManagerEvent::Unmanage( Window::from(window_id), diff --git a/komorebi-for-mac/komorebi/src/window.rs b/komorebi-for-mac/komorebi/src/window.rs index 7d1dd991..0684803c 100644 --- a/komorebi-for-mac/komorebi/src/window.rs +++ b/komorebi-for-mac/komorebi/src/window.rs @@ -551,6 +551,11 @@ impl schemars::JsonSchema for Window { } } +/// The id of the window that has the focus, if any window does +pub fn foreground_window_id() -> Option { + MacosApi::foreground_window_id() +} + impl Window { pub fn new( element: CFRetained, diff --git a/komorebi-for-mac/komorebi/src/window_manager.rs b/komorebi-for-mac/komorebi/src/window_manager.rs index 2f3f0471..a1aba496 100644 --- a/komorebi-for-mac/komorebi/src/window_manager.rs +++ b/komorebi-for-mac/komorebi/src/window_manager.rs @@ -1,4 +1,3 @@ -use crate::monitor::MonitorExt; use crate::AccessibilityUiElement; use crate::CoreFoundationRunLoop; use crate::DATA_DIR; @@ -41,6 +40,7 @@ use crate::current_space_id; use crate::lockable_sequence::Lockable; use crate::macos_api::MacosApi; use crate::monitor::Monitor; +use crate::monitor::MonitorExt; use crate::pan::Pan; use crate::ring::Ring; use crate::scrolling::WorkspaceScrolling; @@ -52,6 +52,7 @@ use crate::window::AdhocWindow; use crate::window::Snapping; use crate::window::Window; use crate::window::dragged_window; +use crate::window::foreground_window_id; use crate::window::live_resize_begins; use crate::window::live_resize_ends; use crate::window::should_act_individual; @@ -267,8 +268,7 @@ impl WindowManager { let len = focused_workspace.floating_windows().len(); if len > 1 { - let focused_window_id = - MacosApi::foreground_window_id().ok_or_eyre("no foreground window")?; + let focused_window_id = foreground_window_id().ok_or_eyre("no foreground window")?; let focused_rect = Rect::from(MacosApi::window_rect( MacosApi::foreground_window() .ok_or_eyre("no foreground window")? @@ -735,8 +735,7 @@ impl WindowManager { let focused_workspace = self.focused_workspace()?; let delta = self.resize_delta; - let focused_window_id = - MacosApi::foreground_window_id().ok_or_eyre("no foreground window")?; + let focused_window_id = foreground_window_id().ok_or_eyre("no foreground window")?; for window in focused_workspace.floating_windows().iter() { if window.id == focused_window_id { let mut rect = Rect::from(window.window_rect()?); @@ -1548,7 +1547,8 @@ impl WindowManager { let hiding_position = self .focused_monitor() .ok_or_eyre("there is no monitor")? - .extra.window_hiding_position; + .extra + .window_hiding_position; let workspace = self.focused_workspace_mut()?; workspace.new_monocle_container()?; @@ -1584,7 +1584,8 @@ impl WindowManager { let hiding_position = self .focused_monitor() .ok_or_eyre("there is no monitor")? - .extra.window_hiding_position; + .extra + .window_hiding_position; self.focused_workspace_mut()? .cycle_monocle_container(direction)?; @@ -1598,7 +1599,7 @@ impl WindowManager { #[tracing::instrument(skip(self))] pub fn toggle_float(&mut self, force_float: bool) -> eyre::Result<()> { - let window_id = MacosApi::foreground_window_id().ok_or_eyre("no foreground window")?; + let window_id = foreground_window_id().ok_or_eyre("no foreground window")?; let workspace = self.focused_workspace_mut()?; if workspace.monocle_container.is_some() { @@ -1674,7 +1675,7 @@ impl WindowManager { WorkspaceLayer::Floating => { let workspace = self.focused_workspace()?; let focused_window_id = - MacosApi::foreground_window_id().ok_or_eyre("no foreground window")?; + foreground_window_id().ok_or_eyre("no foreground window")?; let border_offset = 0; let border_width = 0; @@ -2570,8 +2571,7 @@ impl WindowManager { eyre::bail!("cannot move native maximized window to another monitor or workspace"); } - let foreground_window_id = - MacosApi::foreground_window_id().ok_or_eyre("no foreground window")?; + let foreground_window_id = foreground_window_id().ok_or_eyre("no foreground window")?; let floating_window_index = workspace .floating_windows() .iter() @@ -2836,8 +2836,7 @@ impl WindowManager { let len = focused_workspace.floating_windows().len(); if len > 1 { - let focused_window_id = - MacosApi::foreground_window_id().ok_or_eyre("no foreground window")?; + let focused_window_id = foreground_window_id().ok_or_eyre("no foreground window")?; for (idx, window) in focused_workspace.floating_windows().iter().enumerate() { if window.id == focused_window_id { match direction { @@ -3270,7 +3269,7 @@ impl WindowManager { ) { let workspace = self.focused_workspace()?; let focused_hwnd = - MacosApi::foreground_window_id().ok_or_eyre("there is no foreground window")?; + foreground_window_id().ok_or_eyre("there is no foreground window")?; if !workspace.contains_managed_window(focused_hwnd) { bail!("ignoring commands while active window is not managed by komorebi"); } @@ -3675,7 +3674,7 @@ impl WindowManager { #[tracing::instrument(skip(self))] pub fn manage_focused_window(&mut self) -> eyre::Result<()> { let element = MacosApi::foreground_window().ok_or_eyre("there is no foreground window")?; - let window_id = MacosApi::foreground_window_id(); + let window_id = foreground_window_id(); if let Some(process_id) = AdhocWindow::process_id(&element) && let Some(event) = WindowManagerEvent::from_system_notification( SystemNotification::Manual(ManualNotification::Manage), @@ -3696,7 +3695,7 @@ impl WindowManager { #[tracing::instrument(skip(self))] pub fn unmanage_focused_window(&mut self) -> eyre::Result<()> { let element = MacosApi::foreground_window().ok_or_eyre("there is no foreground window")?; - let window_id = MacosApi::foreground_window_id(); + let window_id = foreground_window_id(); if let Some(process_id) = AdhocWindow::process_id(&element) && let Some(event) = WindowManagerEvent::from_system_notification( SystemNotification::Manual(ManualNotification::Unmanage),