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.
This commit is contained in:
LGUG2Z committed 2025-12-15 20:31:24 -08:00
1 parent 73705136f0
commit 8dfc70e6d2
6 files changed
+85 -30

No files matched your search

+37 -11
View File
@@ -54,7 +54,9 @@ lazy_static! {
}
pub enum Notification {
Update(Option<AccessibilityUiElement>, Option<u32>),
// 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<AccessibilityUiElement>, Option<u32>, bool),
ForceUpdate,
}
@@ -86,9 +88,13 @@ pub fn window_border(window_id: u32) -> Option<BorderInfo> {
})
}
pub fn send_notification(element: Option<AccessibilityUiElement>, window_id: Option<u32>) {
pub fn send_notification(
element: Option<AccessibilityUiElement>,
window_id: Option<u32>,
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 {
+1 -1
View File
@@ -569,7 +569,7 @@ pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result
)?;
}
border_manager::send_notification(None, None);
border_manager::send_notification(None, None, false);
}
Ok(())
+42 -14
View File
@@ -78,23 +78,51 @@ define_class! {
tracing::debug!("notification: {}, process: {process_id}", notif.name());
match AppKitWorkspaceNotification::from_str(&notif.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) => {
+1 -1
View File
@@ -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}");
+2 -2
View File
@@ -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(())
}
+2 -1
View File
@@ -107,10 +107,11 @@ fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> 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 => {