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.
This commit is contained in:
LGUG2Z committed 2026-01-27 18:12:23 -08:00
1 parent b5215a2842
commit 1192a103d2
2 files changed
+41 -13

No files matched your search

+28 -9
View File
@@ -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;
}
}
}
}
+13 -4
View File
@@ -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()
};