From 8dfc70e6d23a6578f9aae52fb678f4793e976fb9 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Mon, 15 Dec 2025 20:25:35 -0800 Subject: [PATCH] fix(wm): consume more appkit events and make border manager reconcile focus This commit ensures that NSWorkspaceDidActivateApplicationNotification and NSWorkspaceDidDeactivateApplicationNotification get emitted not just for the loginwindow but for all apps with a valid name. This helps us to catch events from windows which don't emit AXWindowCreated consistently like Mail, Calendar, etc. Additionally, if a window has been closed with cmd+w but the application is still active, the border manager will pick a window container to mark as the new focused window container, and call window.focus() on the selected window to align the focus with the focused border color. --- komorebi/src/border_manager/mod.rs | 48 +++++++++++++---- komorebi/src/monitor_reconciliator.rs | 2 +- komorebi/src/notification_center_listener.rs | 56 +++++++++++++++----- komorebi/src/process_command.rs | 2 +- komorebi/src/process_event.rs | 4 +- komorebi/src/reaper.rs | 3 +- 6 files changed, 85 insertions(+), 30 deletions(-) diff --git a/komorebi/src/border_manager/mod.rs b/komorebi/src/border_manager/mod.rs index dd913d54..34a0250b 100644 --- a/komorebi/src/border_manager/mod.rs +++ b/komorebi/src/border_manager/mod.rs @@ -54,7 +54,9 @@ lazy_static! { } pub enum Notification { - Update(Option, Option), + // NOTE: bool should only ever be set to true by the reaper + // TODO: eventually the bool should be a source enum so we can match on emitters + Update(Option, Option, bool), ForceUpdate, } @@ -86,9 +88,13 @@ pub fn window_border(window_id: u32) -> Option { }) } -pub fn send_notification(element: Option, window_id: Option) { +pub fn send_notification( + element: Option, + window_id: Option, + reaper: bool, +) { if event_tx() - .try_send(Notification::Update(element, window_id)) + .try_send(Notification::Update(element, window_id, reaper)) .is_err() { tracing::warn!("channel is full; dropping notification") @@ -200,7 +206,7 @@ fn handle_notifications( tracing::info!("listening"); let receiver = event_rx(); - event_tx().send(Notification::Update(None, None))?; + event_tx().send(Notification::Update(None, None, false))?; let mut previous_snapshot = Ring::default(); let mut previous_pending_move_op = None; @@ -232,7 +238,7 @@ fn handle_notifications( drop(state); let should_process_notification = match notification { - Notification::Update(_, notification_window_id) => { + Notification::Update(_, notification_window_id, reaper) => { let mut should_process_notification = true; if monitors == previous_snapshot @@ -285,7 +291,7 @@ fn handle_notifications( } if !should_process_notification - && let Some(Notification::Update(_, ref previous_window_id)) = + && let Some(Notification::Update(_, ref previous_window_id, false)) = previous_notification && previous_window_id.unwrap_or_default() != notification_window_id.unwrap_or_default() @@ -293,13 +299,17 @@ fn handle_notifications( should_process_notification = true; } + if reaper { + should_process_notification = true; + } + should_process_notification } Notification::ForceUpdate => true, }; if !should_process_notification { - // tracing::debug!("monitor state matches latest snapshot, skipping notification"); + tracing::debug!("monitor state matches latest snapshot, skipping notification"); continue 'receiver; } @@ -444,10 +454,18 @@ fn handle_notifications( } }; - let new_focus_state = if idx != ws.focused_container_idx() - || monitor_idx != focused_monitor_idx - || window.id != foreground_window - { + // this happens if we cmd+w a window, but the app is still active with no windows + let ignore_foreground_window = foreground_window == 0; + + let focus_state_condition = if ignore_foreground_window { + idx != ws.focused_container_idx() || monitor_idx != focused_monitor_idx + } else { + idx != ws.focused_container_idx() + || monitor_idx != focused_monitor_idx + || window.id != foreground_window + }; + + let new_focus_state = if focus_state_condition { if c.locked { WindowKind::UnfocusedLocked } else { @@ -459,6 +477,14 @@ fn handle_notifications( WindowKind::Single }; + // if we are in the cmd+w situation, we wanna make sure the window we're gonna + // set a focused border on actually has keyboard focus + if ignore_foreground_window + && matches!(new_focus_state, WindowKind::Single | WindowKind::Stack) + { + window.focus(false)?; + } + border.window_kind = new_focus_state; if border.tracking_window_id != window.id { diff --git a/komorebi/src/monitor_reconciliator.rs b/komorebi/src/monitor_reconciliator.rs index 847c5c4c..559346a5 100644 --- a/komorebi/src/monitor_reconciliator.rs +++ b/komorebi/src/monitor_reconciliator.rs @@ -569,7 +569,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result )?; } - border_manager::send_notification(None, None); + border_manager::send_notification(None, None, false); } Ok(()) diff --git a/komorebi/src/notification_center_listener.rs b/komorebi/src/notification_center_listener.rs index 317ee939..8df9be4a 100644 --- a/komorebi/src/notification_center_listener.rs +++ b/komorebi/src/notification_center_listener.rs @@ -78,23 +78,51 @@ define_class! { tracing::debug!("notification: {}, process: {process_id}", notif.name()); match AppKitWorkspaceNotification::from_str(¬if.name().to_string()) { Ok(AppKitWorkspaceNotification::NSWorkspaceDidActivateApplicationNotification) => { - if application.is_some_and(|app| app.name().unwrap_or_default() == "loginwindow") { - window_manager_event_listener::send_notification( - WindowManagerEvent::ScreenLock( - SystemNotification::AppKitWorkspace(AppKitWorkspaceNotification::NSWorkspaceDidDeactivateApplicationNotification), - process_id - ) - ); + if let Some(app) = &application { + match app.name().unwrap_or_default().as_str() { + "loginwindow" => { + window_manager_event_listener::send_notification( + WindowManagerEvent::ScreenLock( + SystemNotification::AppKitWorkspace(AppKitWorkspaceNotification::NSWorkspaceDidActivateApplicationNotification), + process_id + ) + ); + } + app if !app.is_empty() => { + if let Some(event) = WindowManagerEvent::from_system_notification( + SystemNotification::AppKitWorkspace(AppKitWorkspaceNotification::NSWorkspaceDidActivateApplicationNotification), + process_id, + window_id, + ) { + window_manager_event_listener::send_notification(event); + } + } + _ => {}, + } } } Ok(AppKitWorkspaceNotification::NSWorkspaceDidDeactivateApplicationNotification) => { - if application.is_some_and(|app| app.name().unwrap_or_default() == "loginwindow") { - window_manager_event_listener::send_notification( - WindowManagerEvent::ScreenUnlock( - SystemNotification::AppKitWorkspace(AppKitWorkspaceNotification::NSWorkspaceDidDeactivateApplicationNotification), - process_id - ) - ); + if let Some(app) = &application { + match app.name().unwrap_or_default().as_str() { + "loginwindow" => { + window_manager_event_listener::send_notification( + WindowManagerEvent::ScreenUnlock( + SystemNotification::AppKitWorkspace(AppKitWorkspaceNotification::NSWorkspaceDidDeactivateApplicationNotification), + process_id + ) + ); + } + app if !app.is_empty() => { + if let Some(event) = WindowManagerEvent::from_system_notification( + SystemNotification::AppKitWorkspace(AppKitWorkspaceNotification::NSWorkspaceDidDeactivateApplicationNotification), + process_id, + window_id, + ) { + window_manager_event_listener::send_notification(event); + } + } + _ => {}, + } } } Ok(notification) => { diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index d4f13b7a..c29bb256 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -1757,7 +1757,7 @@ impl WindowManager { initial_state.has_been_modified(self.as_ref()), )?; - border_manager::send_notification(None, None); + border_manager::send_notification(None, None, false); if matches!(message, SocketMessage::Theme(_)) { tracing::trace!("processed command: {message}"); diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index a4d2f6fc..9b9918ff 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -114,7 +114,7 @@ impl WindowManager { && let Some(current_space_id) = current_space_id() { if *space_id == current_space_id { - border_manager::send_notification(None, None); + border_manager::send_notification(None, None, false); } else { border_manager::destroy_all_borders()?; } @@ -976,7 +976,7 @@ impl WindowManager { initial_state.has_been_modified(self.as_ref()), )?; - border_manager::send_notification(window_element, window_id); + border_manager::send_notification(window_element, window_id, false); Ok(()) } diff --git a/komorebi/src/reaper.rs b/komorebi/src/reaper.rs index d7fa74e3..17771e06 100644 --- a/komorebi/src/reaper.rs +++ b/komorebi/src/reaper.rs @@ -107,10 +107,11 @@ fn handle_notifications(wm: Arc>) -> color_eyre::Result<()> } } + // If an invalid window was cleaned up, we update the workspace if should_update { // If an invalid window was cleaned up, we update the workspace wm.update_focused_workspace(false, false)?; - border_manager::send_notification(None, Some(window_id)); + border_manager::send_notification(None, Some(window_id), true); } } ReaperNotification::MouseUpKeyUp => {