diff --git a/komorebi/src/macos_api.rs b/komorebi/src/macos_api.rs index dfe13e08..a9b38c52 100644 --- a/komorebi/src/macos_api.rs +++ b/komorebi/src/macos_api.rs @@ -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"); + }); + } } diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index d368510c..a08735cb 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -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) => { diff --git a/komorebi/src/workspace_reconciliator.rs b/komorebi/src/workspace_reconciliator.rs index add059ee..c5ffda21 100644 --- a/komorebi/src/workspace_reconciliator.rs +++ b/komorebi/src/workspace_reconciliator.rs @@ -104,6 +104,25 @@ pub fn handle_notifications(wm: Arc>) -> 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;