From 5bfbaf5f8a403f4ddf7f76b2f692bfb13151a872 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Tue, 27 Oct 2026 14:04:41 -0700 Subject: [PATCH] fix(window): recognise our own move and resize notifications The moves and resizes the animator makes are reported back to us like any others. They used to be avoided by removing and re-adding the AX observers around every animation, which cost four AX calls a window and still let some through. Recognise them instead: a notification for any window of an app we are moving, or have moved in the last 300ms, is an echo and is swallowed. With the mouse down it is only taken for somebody grabbing the window, and the animation let go of, if the window's origin is nowhere we put it, so that apps which snap or clamp their size are not mistaken for a drag. Once an app has had a window animated, the move and resize notifications that arrive while the mouse is up are also throttled per window, so that an app that will not stop reporting cannot fill the event channel and cost the window manager real events such as focus changes. Borders no longer ask a window where it is while it is being animated, as the animator moves them. A drag cancels the animation of the window being dragged, and what is known about a process is forgotten when it terminates, as a new process may be given the same id. --- komorebi/src/border_manager/border.rs | 7 +++ komorebi/src/process_event.rs | 18 +++++++ komorebi/src/window.rs | 67 +++++++++++++++++++-------- 3 files changed, 73 insertions(+), 19 deletions(-) diff --git a/komorebi/src/border_manager/border.rs b/komorebi/src/border_manager/border.rs index e3f8114e..efb43459 100644 --- a/komorebi/src/border_manager/border.rs +++ b/komorebi/src/border_manager/border.rs @@ -5,6 +5,7 @@ use crate::accessibility::AccessibilityApi; use crate::accessibility::notification_constants::kAXMainWindowChangedNotification; use crate::accessibility::notification_constants::kAXWindowMovedNotification; use crate::accessibility::notification_constants::kAXWindowResizedNotification; +use crate::animation::animator; use crate::border_manager::BORDER_OFFSET; use crate::border_manager::BORDER_OFFSET_ADJUSTMENT; use crate::border_manager::BORDER_WIDTH; @@ -68,6 +69,12 @@ unsafe extern "C-unwind" fn border_observer_callback( return; } + // while a window is being animated the animator moves the border along with it, a + // frame at a time, without having to ask the window where it is + if animator::is_animating(border.tracking_window_id) { + return; + } + if let Ok(rect) = MacosApi::window_rect(&border.tracking_element) { border.update(); border.ns_window.window.setFrame_display_animate( diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index d84e1f8d..b386352e 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -7,9 +7,12 @@ use crate::TABBED_APPLICATIONS; use crate::UNMANAGED_WINDOW_IDS; use crate::WORKSPACE_MATCHING_RULES; use crate::accessibility::AccessibilityApi; +use crate::accessibility::enhanced_ui; use crate::accessibility::error::AccessibilityApiError; use crate::accessibility::error::AccessibilityError; use crate::accessibility::notification_constants::AccessibilityNotification; +use crate::animation::animator; +use crate::app_kit_notification_constants::AppKitWorkspaceNotification; use crate::border_manager; use crate::core::DefaultLayout; use crate::core::Layout; @@ -718,6 +721,16 @@ impl WindowManager { } } WindowManagerEvent::Destroy(notification, process_id) => { + // a new process may be given this id, and must not inherit what we know about it + if matches!( + notification, + SystemNotification::AppKitWorkspace( + AppKitWorkspaceNotification::NSWorkspaceDidTerminateApplicationNotification + ) + ) { + enhanced_ui::forget(process_id); + } + // some apps like Discord in all of their relentless Electron slop stupidity // hijack CMD+W to send a HIDE instead of a close (i.e. the equivalent of pressing CMD+H) let mut should_force_reap = false; @@ -807,6 +820,9 @@ impl WindowManager { } } WindowManagerEvent::MoveStart(_, _, window_id) => { + // somebody is dragging the window, so we must stop animating it somewhere else + animator::cancel(window_id); + if self.pending_move_op.is_none() && self.pending_resize_op.is_none() { let monitor_idx = self.focused_monitor_idx(); let workspace_idx = self @@ -1026,6 +1042,8 @@ impl WindowManager { } } WindowManagerEvent::ResizeStart(_, _, window_id) => { + animator::cancel(window_id); + // AXWindowResized fires again and again through a drag; only the first frame is // the one the drag started from let already_recorded = matches!( diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index a95a0bcc..21bf609b 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -36,6 +36,7 @@ use crate::animation::animator::Journey; use crate::animation::animator::WindowHandle; use crate::animation::prefix::AnimationPrefix; use crate::animation::step::MotionSpec; +use crate::animation::tracks::Verdict; use crate::application::Application; use crate::cf_dictionary_value; use crate::core::ApplicationIdentifier; @@ -112,33 +113,61 @@ unsafe extern "C-unwind" fn window_observer_callback( _context: *mut c_void, ) { unsafe { + let window_id = AccessibilityApi::window_id(element.as_ref()).ok(); + let notification = AccessibilityNotification::from_str(¬ification.as_ref().to_string()); + + let mut process_id = 0; + element.as_ref().pid(NonNull::from_mut(&mut process_id)); + + // the moves and resizes the animator makes are reported back to us like any others, and + // must not be mistaken for somebody moving a window + if let Ok( + moved_or_resized @ (AccessibilityNotification::AXWindowMoved + | AccessibilityNotification::AXWindowResized), + ) = notification + && let Some(window_id) = window_id + { + let mouse_is_down = MacosApi::left_mouse_button_is_pressed(); + + let verdict = + animator::notification_verdict(window_id, process_id, mouse_is_down, || { + MacosApi::window_rect(element.as_ref()).ok().map(Rect::from) + }); + + if verdict == Verdict::Suppress { + return; + } + + // an app that will not stop reporting must not be allowed to fill the event channel + if !animator::admit_notification( + window_id, + process_id, + moved_or_resized == AccessibilityNotification::AXWindowResized, + mouse_is_down, + ) { + return; + } + } + let name = AccessibilityApi::copy_attribute_value::(element.as_ref(), kAXTitleAttribute) .map(|s| s.to_string()); if let Some(name) = name && !name.is_empty() + && let Ok(notification) = notification + && let Some(event) = WindowManagerEvent::from_system_notification( + SystemNotification::Accessibility(notification), + process_id, + window_id, + ) { - let mut process_id = 0; - element.as_ref().pid(NonNull::from_mut(&mut process_id)); + tracing::debug!( + "notification: {}, process: {process_id}, name: \"{name}\"", + notification, + ); - let window_id = AccessibilityApi::window_id(element.as_ref()).ok(); - - if let Ok(notification) = - AccessibilityNotification::from_str(¬ification.as_ref().to_string()) - && let Some(event) = WindowManagerEvent::from_system_notification( - SystemNotification::Accessibility(notification), - process_id, - window_id, - ) - { - tracing::debug!( - "notification: {}, process: {process_id}, name: \"{name}\"", - notification, - ); - - window_manager_event_listener::send_notification(event); - } + window_manager_event_listener::send_notification(event); } } }