fix(wm): prevent ws reconciliation when closing last ws window

When the last window on a workspace is closed, macOS automatically
activates another app (often on a different workspace). This app's focus
event triggers workspace reconciliation, switching the user away from
the now-empty workspace they (presumably) intended to stay on.

The issue is a race condition: by the time we try to activate Finder to
prevent the unwanted focus change, the other app's activation event has
already been queued and will still get processed.

This commit enusres that we skip reconciliation when the current
workspace is empty; this indicates the user just closed the last window
and should stay on that workspace, not follow workspace focus to
wherever macOS arbitrarily activates an app.

Additionally, we activate Finder when a workspace becomes empty to match
native macOS behavior (clicking on empty desktop activates Finder
without opening a window).
This commit is contained in:
LGUG2Z committed 2026-01-12 13:39:52 -08:00
1 parent 7c1ab9a6cc
commit 08794ba317
3 files changed
+51

No files matched your search

+21
View File
@@ -459,4 +459,25 @@ impl MacosApi {
pub fn left_mouse_button_is_pressed() -> bool {
NSEvent::pressedMouseButtons() & 0x1 != 0
}
pub fn activate_finder() {
use objc2_app_kit::NSApplicationActivationOptions;
DispatchQueue::main().exec_sync(move || {
let workspace = NSWorkspace::sharedWorkspace();
let apps = workspace.runningApplications();
for app in apps.iter() {
if let Some(bundle_id) = app.bundleIdentifier()
&& bundle_id.to_string() == "com.apple.finder"
{
// Activate without opening a new window
app.activateWithOptions(NSApplicationActivationOptions::empty());
return;
}
}
tracing::warn!("could not find running Finder application");
});
}
}
+11
View File
@@ -683,6 +683,17 @@ impl WindowManager {
self.reap_invalid_windows_for_application(process_id)?;
}
// if the workspace is now empty (last window was closed), activate Finder
// instead of letting an app on another workspace take focus (technically
// the other app will take focus first, but this ensures that _eventually_
// i.e. quicker than the user can recognize, Finder will be the focused app)
if self.focused_workspace()?.containers().is_empty() {
tracing::debug!(
"workspace is now empty, activating Finder to prevent unwanted workspace switch"
);
MacosApi::activate_finder();
}
self.update_focused_workspace(false, false)?;
}
WindowManagerEvent::Unmanage(_, _, window_id) => {
+19
View File
@@ -104,6 +104,25 @@ pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result
let updated_pair = (notification.monitor_idx, notification.workspace_idx);
if focused_pair != updated_pair {
// don't switch workspaces if the current workspace is empty
// this happens when the user just closed the last window on a workspace
// and the last focused application, usually on another workspace, takes focus
// and in doing so triggers the reconciliator
if let Ok(workspace) = wm.focused_workspace()
&& workspace.containers().is_empty()
{
tracing::debug!(
"current workspace is empty (user closed last window), not reconciling to prevent unwanted workspace switch"
);
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64;
LAST_RECONCILIATION.store(now, Ordering::SeqCst);
RECONCILIATION_IN_PROGRESS.store(false, Ordering::Relaxed);
continue;
}
tracing::info!("reconciliating workspace");
wm.focus_monitor(notification.monitor_idx)?;
let mouse_follows_focus = wm.mouse_follows_focus;