fix(wm): process notifications from destroyed windows

When closing a window with CMD+W, the AXUIElementDestroyed notification
has an empty title. We need to add an is_destroyed override for these
AXUIElementDestroyed notifications without a title so that they can be
processed.

This commit also fixes a race condition exposed by the above fix where
the reaper thread sometimes removes the window before the reap function
tries to remove it during event processing. When this happens, we can
just ignore the error and continue instead of propagating it up and
interrupting the event processing loop until the user's next manual
interaction with another window.
This commit is contained in:
LGUG2Z committed 2025-12-31 16:13:24 -08:00
1 parent 49bdf41bfc
commit 05cd7832e8
2 files changed
+14 -7

No files matched your search

+12 -6
View File
@@ -58,18 +58,23 @@ unsafe extern "C-unwind" fn application_observer_callback(
_context: *mut c_void,
) {
unsafe {
let notification_str = notification.as_ref().to_string();
let name =
AccessibilityApi::copy_attribute_value::<CFString>(element.as_ref(), kAXTitleAttribute)
.map(|s| s.to_string());
if let Some(name) = name
&& !name.is_empty()
{
// AXUIElementDestroyed fires when the element is already gone,
// so we can't get the title - but we still need to process it
let is_destroyed = matches!(
AccessibilityNotification::from_str(&notification_str),
Ok(AccessibilityNotification::AXUIElementDestroyed)
);
if is_destroyed || name.as_ref().is_some_and(|n| !n.is_empty()) {
let mut process_id = 0;
element.as_ref().pid(NonNull::from_mut(&mut process_id));
if let Ok(notification) =
AccessibilityNotification::from_str(&notification.as_ref().to_string())
if let Ok(notification) = AccessibilityNotification::from_str(&notification_str)
&& let Some(event) = WindowManagerEvent::from_system_notification(
SystemNotification::Accessibility(notification),
process_id,
@@ -77,7 +82,8 @@ unsafe extern "C-unwind" fn application_observer_callback(
)
{
tracing::debug!(
"notification: {notification}, process: {process_id}, name: \"{name}\"",
"notification: {notification}, process: {process_id}, name: \"{}\"",
name.as_deref().unwrap_or("<destroyed>")
);
window_manager_event_listener::send_notification(event);
+2 -1
View File
@@ -449,7 +449,8 @@ impl Workspace {
}
for window_id in &invalid_window_ids {
self.remove_window(*window_id)?;
// Ignore errors - window may have already been removed by the reaper thread
let _ = self.remove_window(*window_id);
}
for window_id in &invalid_floating_window_ids {