From 407a011b5eb00efb97b08bd16144e5b692bf69ec Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Tue, 27 Oct 2026 18:44:40 -0700 Subject: [PATCH] fix(process_event): follow focus only while the app is still in front komorebi brings an app to the front with activateWithOptions and then hears about it from NSWorkspace and the accessibility API some time later, 0.8 to 1.7 s in a recording, by which time it has often brought another app to the front. The FocusChange handler took every such notification at its word, which had two effects: - focus moved back to the app that had had it, so Left, Left landed on the window in between rather than the one two along - the app of a window on a workspace that is not showing made the workspace reconciliator switch to that workspace, which undid a workspace change that had been asked for a moment earlier The handler now asks which app is in front and does not follow a notification about any other. The last notification that is followed is always about the app that really is in front, so the state ends up where the system is. Notifications about an app that is not in front still go through the rest of the handler, and nothing is ignored when the accessibility API will not say what is in front. Found in a recording of the installed build at db1c18b. That the notifications are late and that the OS front process had moved on was read off the OS trace; the change itself is tested only for the decision, and was not tried on a screen. --- komorebi/src/macos_api.rs | 17 ++++++++++++ komorebi/src/process_event.rs | 51 +++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/komorebi/src/macos_api.rs b/komorebi/src/macos_api.rs index e36cd10e..f09ffb06 100644 --- a/komorebi/src/macos_api.rs +++ b/komorebi/src/macos_api.rs @@ -447,6 +447,23 @@ impl MacosApi { } } + /// The process id of the application that is in front right now, as the accessibility API + /// says, or `None` if it will not say + pub fn frontmost_process_id() -> Option { + unsafe { + let syswide = AXUIElement::new_system_wide(); + let app = AccessibilityApi::copy_attribute_value::( + &syswide, + kAXFocusedApplicationAttribute, + )?; + + let mut pid = 0; + app.pid(NonNull::from_mut(&mut pid)); + + (pid != 0).then_some(pid) + } + } + pub fn cursor_pos() -> CGPoint { let source = CGEventSource::new(CGEventSourceStateID::HIDSystemState); let event = CGEvent::new(source.as_deref()); diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index f1878695..4e0280db 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -203,6 +203,19 @@ impl WindowManager { let mut tabbed_window = false; let mut needs_reconciliation = false; + // An application that komorebi brings to the front says so some time later, often + // after komorebi has moved on to another. Moving focus or workspace to follow that + // is moving it back to where it was, so it is only followed while the application + // is still the one in front. + let frontmost = MacosApi::frontmost_process_id(); + let stale = is_stale_focus_notification(frontmost, process_id); + if stale { + tracing::debug!( + "not following {notification} for process {process_id}: process {} is in front", + frontmost.unwrap_or_default() + ); + } + if let Some(window_id) = application.main_window_id() && let Some(element) = application.main_window() { @@ -406,7 +419,7 @@ impl WindowManager { should_switch_workspace_layer_to_tiling = false; } } - } else if is_on_current_workspace { + } else if is_on_current_workspace && !stale { workspace.focus_container_by_window(window_id)?; } @@ -429,7 +442,10 @@ impl WindowManager { } } - if !is_on_current_workspace && let Some((m_idx, w_idx)) = is_known { + if !stale + && !is_on_current_workspace + && let Some((m_idx, w_idx)) = is_known + { workspace_reconciliator::send_notification(m_idx, w_idx, event); needs_reconciliation = true; } @@ -1046,3 +1062,34 @@ impl WindowManager { Ok(()) } } + +/// Whether a notification that the application with this process id came to the front is out of +/// date, which it is if some other application is in front now. +/// +/// An application can be told to come to the front, and a while later tell komorebi that it did, +/// by which time another one has been told to, so what it says is no longer true. Nothing is out +/// of date if what is in front is not known, which leaves things as they were. +fn is_stale_focus_notification(frontmost: Option, process_id: i32) -> bool { + frontmost.is_some_and(|frontmost| frontmost != process_id) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn an_application_that_is_in_front_is_followed() { + assert!(!is_stale_focus_notification(Some(79469), 79469)); + } + + #[test] + fn an_application_that_is_not_in_front_any_more_is_not() { + // Beeper says that it came to the front after Slack has + assert!(is_stale_focus_notification(Some(96328), 79469)); + } + + #[test] + fn nothing_is_stale_when_it_is_not_known_what_is_in_front() { + assert!(!is_stale_focus_notification(None, 79469)); + } +}