From de630fc70225aa5854824fbf344bd3c7c66ff598 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 31 Oct 2026 15:10:21 -0700 Subject: [PATCH] fix(mac): register a window when its observer is attached, not when it is constructed BEHAVIOUR (what is registered, and when; nothing reads the registry yet except the unused MacosBackend). Review findings 1 (cost) and 6 (wrong observer). Window::new created a fresh AXObserver and registered the window each time, so one event registered up to three constructions on the event thread under the window manager lock (the attach-check, the re-check and the managed one), each replacing the last, and the managed window's attached observer (the one window.observe put on the run loop) was replaced by the third throwaway's unattached one; window_by_title at start-up made n x n of them for an application with n windows. Registration now happens in Window::observe, after add_observer_to_run_loop succeeded: a Window made only to ask an app something is never observed and never registered, the registry holds the observer that is really attached, and a window is registered once (process_event.rs after should_manage, and macos_api.rs at start-up). The decision is documented in ax_registry.rs in the docs revision of this stack. Verified: macOS fake-CC type-check (tools/fast-check) PASS. Not verified: any run. Verified on the Mini: not yet. Hand checks: H3 (start-up with a many-window app: time to ready), H7 (native tabs). --- komorebi-for-mac/komorebi/src/window.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/komorebi-for-mac/komorebi/src/window.rs b/komorebi-for-mac/komorebi/src/window.rs index 3699d077..c3788456 100644 --- a/komorebi-for-mac/komorebi/src/window.rs +++ b/komorebi-for-mac/komorebi/src/window.rs @@ -559,18 +559,13 @@ impl Window { Some(window_observer_callback), )?; - let window = Self { + Ok(Self { id: AccessibilityApi::window_id(&element)?, element: AccessibilityUiElement(element), application, observer: AccessibilityObserver(Some(observer)), details: None, - }; - - // so that the window can be found again from its id alone - crate::ax_registry::register(&window); - - Ok(window) + }) } /// The handles that act on this window, as the registry keeps them @@ -627,7 +622,13 @@ impl Window { NOTIFICATIONS, run_loop, refcon, - ) + )?; + + // the registry holds the window whose observer is attached: a Window that was only made to + // ask an app about a window is never observed, so it is never registered + crate::ax_registry::register(self); + + Ok(()) } /// Hide the window in the corner of the display that it is on