From 1192a103d208f0b9eb1c9163cc381893a38f89d1 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Tue, 27 Jan 2026 18:11:48 -0800 Subject: [PATCH] fix(wm): distinguish separate windows from tabs for tabbed apps When Finder is configured as a tabbed application, opening multiple separate Finder windows (not tabs within the same window) prevented focusing between those windows using komorebic focus commands. The tab detection logic uses geometry comparison (tab_rect == main_rect) to determine if a window event represents a tab change. This fails when separate windows from the same application have identical geometry after tiling, causing them to be incorrectly treated as tabs within a single window. This commit adds window ID verification as an additional requirement for tab detection. For Show events, both geometry AND window IDs must match for the event to be classified as a tab (preventing new container creation). For FocusChange events, we verify the stored element's window ID matches the container's stored ID to confirm legitimate tab switches within the same window. The geometry check is preserved to maintain existing tab detection, but now requires window ID confirmation to prevent false positives when separate windows happen to share identical positions and dimensions. Additionally, this commit fixes the Window::focus() function for tabbed applications to prefer using the stored element when valid, only falling back to main_window() when the element is invalid (tab was closed). This allows focusing specific windows even for tabbed applications instead of always focusing the main/frontmost window. --- komorebi/src/process_event.rs | 37 ++++++++++++++++++++++++++--------- komorebi/src/window.rs | 17 ++++++++++++---- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index a08735cb..3b321105 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -252,10 +252,21 @@ impl WindowManager { Err(error) => return Err(error.into()), }; + // Check if this is truly a tab change within the same window + // by verifying the stored element's window ID matches the container's window ID if tab_rect == main_rect && window.id != window_id { - // Tab changed - update stored element - container_idx_to_update = Some(container_idx); - tabbed_window = true; + // Additional check: verify the stored element belongs to this container + // This prevents treating separate windows as tabs when they have matching geometry + if let Ok(stored_window_id) = + AccessibilityApi::window_id(&window.element.0) + && stored_window_id == window.id + { + // All checks pass: stored element matches container ID = true tab change + container_idx_to_update = Some(container_idx); + tabbed_window = true; + } + // If stored_window_id != window.id, this is a stale element or different window + // Don't set tabbed_window=true, allow normal focus handling } // If window.id == window_id, proceed with normal focus handling } @@ -468,12 +479,20 @@ impl WindowManager { let tabbed_applications = TABBED_APPLICATIONS.lock(); if tabbed_applications.contains(&application_name) { - for window in workspace.visible_windows().iter().flatten() { - if window.application.name().unwrap_or_default() == application_name { - let tab_rect = MacosApi::window_rect(element)?; - let main_rect = MacosApi::window_rect(&window.element)?; - if tab_rect == main_rect { - tabbed_window = true; + // Get the window_id for the new window + if let Ok(new_window_id) = AccessibilityApi::window_id(element) { + for window in workspace.visible_windows().iter().flatten() { + if window.application.name().unwrap_or_default() == application_name + { + let tab_rect = MacosApi::window_rect(element)?; + let main_rect = MacosApi::window_rect(&window.element)?; + // BOTH conditions must be true: + // 1. Geometry matches (original check - preserves existing behavior) + // 2. Window IDs match (new check - prevents false positives) + if tab_rect == main_rect && window.id == new_window_id { + tabbed_window = true; + break; + } } } } diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index 2f5360d1..48bd0f70 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -758,10 +758,19 @@ impl Window { drop(tabbed_applications); let element_to_focus = if is_tabbed { - self.application - .main_window() - .map(AccessibilityUiElement) - .unwrap_or_else(|| self.element.clone()) + // For tabbed apps, we still need to check if the stored element is valid + // If valid, use it directly (allows focusing specific windows) + // If invalid (tab was closed), fall back to main_window() for the active tab + match AccessibilityApi::window_id(&self.element.0) { + Ok(_) => self.element.clone(), // Element is valid, use it + Err(_) => { + // Element is invalid (tab closed), get current main window + self.application + .main_window() + .map(AccessibilityUiElement) + .unwrap_or_else(|| self.element.clone()) + } + } } else { self.element.clone() };