fix(wm): preserve active tab when focusing tabbed applications

This commit ensures that when focusing back to a tabbed application, we
use the current main window element instead of the stored element which
may point to a stale tab, and we also update the stored window element
when the user switches tabs, so that borders can update correctly.
This commit is contained in:
LGUG2Z committed 2026-01-01 16:09:59 -08:00
1 parent f46fe659ac
commit 3a7758cd60
4 files changed
+100 -18

No files matched your search

+37
View File
@@ -81,6 +81,7 @@ pub struct Border {
pub observer: AccessibilityObserver,
pub tracking_element: AccessibilityUiElement,
pub tracking_window_id: u32,
pub process_id: i32,
pub monitor_idx: Option<usize>,
pub ns_window: NsWindow,
pub window_kind: WindowKind,
@@ -116,6 +117,7 @@ impl Border {
let mut border = Box::new(Self {
id: id.to_string(),
tracking_window_id,
process_id,
monitor_idx,
observer: observer.clone(),
tracking_element: element.clone(),
@@ -143,6 +145,41 @@ impl Border {
Ok(border)
}
pub fn update_tracking_element(
&mut self,
new_element: AccessibilityUiElement,
border_ptr: *mut c_void,
) {
let old_element = &self.tracking_element;
let notifications = [
kAXWindowMovedNotification,
kAXWindowResizedNotification,
kAXMainWindowChangedNotification,
];
// re-register observer notifications on the new element
if let Some(ref observer) = self.observer.0 {
for notification in &notifications {
let _ = AccessibilityApi::remove_notification_from_observer(
observer,
&old_element.0,
notification,
);
}
for notification in &notifications {
let _ = AccessibilityApi::add_notification_to_observer(
observer,
&new_element.0,
notification,
Some(border_ptr),
);
}
}
self.tracking_element = new_element;
}
pub fn update(&self) {
autoreleasepool(|_| {
let colour = Rgb::from(window_kind_colour(self.window_kind));
+12
View File
@@ -384,6 +384,12 @@ fn handle_notifications(
}
}
border.tracking_window_id = focused_window_id;
// Only update tracking element if same process (safe for tabbed apps)
if window.application.process_id == border.process_id {
let border_ptr =
std::ptr::addr_of_mut!(**border).cast::<std::ffi::c_void>();
border.update_tracking_element(window.element.clone(), border_ptr);
}
}
// Update the border's monitor idx in case it changed
@@ -499,6 +505,12 @@ fn handle_notifications(
}
}
border.tracking_window_id = window.id;
// Only update tracking element if same process (safe for tabbed apps)
if window.application.process_id == border.process_id {
let border_ptr =
std::ptr::addr_of_mut!(**border).cast::<std::ffi::c_void>();
border.update_tracking_element(window.element.clone(), border_ptr);
}
}
border.monitor_idx = Some(monitor_idx);
+32 -16
View File
@@ -213,10 +213,13 @@ impl WindowManager {
let tabbed_applications = TABBED_APPLICATIONS.lock();
if tabbed_applications.contains(&application_name) {
let mut first_tab_destroyed = false;
let mut container_idx_to_update = None;
for window in workspace.visible_windows() {
if let Some(window) = window
for (container_idx, container) in workspace.containers().iter().enumerate()
{
if let Some(window) = container.focused_window()
&& window.application.name().unwrap_or_default() == application_name
&& window.application.process_id == process_id
{
let tab_rect = MacosApi::window_rect(&element)?;
let main_rect = match MacosApi::window_rect(&window.element) {
@@ -227,18 +230,16 @@ impl WindowManager {
// this means we have closed the 1st tab, so we need this window object to be reaped
// and for the new 1st tab to be the key element of the window struct
if let Some(event) =
WindowManagerEvent::from_system_notification(
SystemNotification::Manual(
ManualNotification::ShowOnFocusChangeFirstTabDestroyed,
),
event.process_id(),
Some(window_id),
)
{
window_manager_event_listener::send_notification(
event,
);
}
WindowManagerEvent::from_system_notification(
SystemNotification::Manual(
ManualNotification::ShowOnFocusChangeFirstTabDestroyed,
),
event.process_id(),
Some(window_id),
)
{
window_manager_event_listener::send_notification(event);
}
first_tab_destroyed = true;
@@ -252,12 +253,27 @@ impl WindowManager {
};
if tab_rect == main_rect {
tracing::debug!("ignoring focus change for tabbed window");
tabbed_window = true;
if window.id != window_id {
// Tab changed - update stored element
container_idx_to_update = Some(container_idx);
tabbed_window = true;
}
// If window.id == window_id, proceed with normal focus handling
}
}
}
// Update the window element outside the immutable borrow
if let Some(container_idx) = container_idx_to_update {
if let Some(container) =
workspace.containers_mut().get_mut(container_idx)
&& let Some(window) = container.focused_window_mut()
{
window.id = window_id;
window.element = AccessibilityUiElement(element.clone());
}
}
if first_tab_destroyed {
self.reap_invalid_windows_for_application(process_id)?;
}
+19 -2
View File
@@ -7,6 +7,7 @@ use crate::LibraryError;
use crate::MANAGE_IDENTIFIERS;
use crate::PERMAIGNORE_CLASSES;
use crate::REGEX_IDENTIFIERS;
use crate::TABBED_APPLICATIONS;
use crate::WINDOW_RESTORE_POSITIONS;
use crate::accessibility::AccessibilityApi;
use crate::accessibility::action_constants::kAXPressAction;
@@ -748,10 +749,26 @@ impl Window {
let cf_boolean = CFBoolean::new(true);
let value = &**cf_boolean;
AccessibilityApi::set_attribute_cf_value(&self.element, kAXMainAttribute, value)?;
// For tabbed applications, use the current main window element instead of
// the stored element, which may point to a different tab
let tabbed_applications = TABBED_APPLICATIONS.lock();
let is_tabbed = tabbed_applications.contains(&self.application.name().unwrap_or_default());
drop(tabbed_applications);
let element_to_focus = if is_tabbed {
self.application
.main_window()
.map(AccessibilityUiElement)
.unwrap_or_else(|| self.element.clone())
} else {
self.element.clone()
};
AccessibilityApi::set_attribute_cf_value(&element_to_focus, kAXMainAttribute, value)?;
if mouse_follows_focus {
MacosApi::center_cursor_in_rect(&MacosApi::window_rect(&self.element)?.into())?
MacosApi::center_cursor_in_rect(&MacosApi::window_rect(&element_to_focus)?.into())?
}
Ok(())