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() };