From a0770266a89e5a5a7a9f4081aa18c42f5527692f Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 31 Oct 2026 18:46:38 -0700 Subject: [PATCH] refactor(mac): the workspace navigation commands are komorebi-core's The four navigation methods of the macOS window manager (the ones that were already the KDE text) call the functions of komorebi_core::command, the FocusLastWorkspace arm is `self.focus_last_workspace()` (a new method that calls the shared function; the macOS arm had the pointer preamble commented out, which is the host default), and WindowManager implements CommandHost by forwarding to its own methods. No behaviour change. Verified: fake-CC cargo clippy -p komorebi-macos -p komorebic-macos -p komorebi-client-macos -p komorebi-bar-macos --all-targets -D warnings: PASS (type-check only). Not run: the macOS tests (the Mac Mini). --- .../komorebi/src/process_command.rs | 222 ++++-------------- 1 file changed, 51 insertions(+), 171 deletions(-) diff --git a/komorebi-for-mac/komorebi/src/process_command.rs b/komorebi-for-mac/komorebi/src/process_command.rs index 6884c5c6..89ba9255 100644 --- a/komorebi-for-mac/komorebi/src/process_command.rs +++ b/komorebi-for-mac/komorebi/src/process_command.rs @@ -292,158 +292,27 @@ impl WindowManager { /// Focuses the next or previous workspace of the monitor that is focused, or of the monitor the pointer is on when its workspace is empty pub fn cycle_focus_workspace(&mut self, direction: CycleDirection) -> eyre::Result<()> { - // This is to ensure that even on an empty workspace on a secondary monitor, the - // secondary monitor where the cursor is focused will be used as the target for - // the workspace switch op - if let Some(monitor_idx) = self.monitor_idx_from_current_pos() - && monitor_idx != self.focused_monitor_idx() - && let Some(monitor) = self.monitors().get(monitor_idx) - && let Some(workspace) = monitor.focused_workspace() - && workspace.is_empty() - { - self.focus_monitor(monitor_idx)?; - } - - let focused_monitor = self.focused_monitor().ok_or_eyre("there is no monitor")?; - - let focused_workspace_idx = focused_monitor.focused_workspace_idx(); - let workspaces = focused_monitor.workspaces().len(); - - let workspace_idx = direction.next_idx( - focused_workspace_idx, - NonZeroUsize::new(workspaces).ok_or_eyre("there must be at least one workspace")?, - ); - - self.focus_workspace(workspace_idx)?; - Ok(()) + komorebi_core::cycle_focus_workspace(self, direction) } /// Focuses the next or previous empty workspace of the focused monitor, if it has one pub fn cycle_focus_empty_workspace(&mut self, direction: CycleDirection) -> eyre::Result<()> { - // TODO: figure out if we need to do this on macOS - // // This is to ensure that even on an empty workspace on a secondary monitor, the - // // secondary monitor where the cursor is focused will be used as the target for - // // the workspace switch op - // if let Some(monitor_idx) = self.monitor_idx_from_current_pos() { - // if monitor_idx != self.focused_monitor_idx() { - // if let Some(monitor) = self.monitors().get(monitor_idx) { - // if let Some(workspace) = monitor.focused_workspace() { - // if workspace.is_empty() { - // self.focus_monitor(monitor_idx)?; - // } - // } - // } - // } - // } - - let focused_monitor = self.focused_monitor().ok_or_eyre("there is no monitor")?; - - let focused_workspace_idx = focused_monitor.focused_workspace_idx(); - let workspaces = focused_monitor.workspaces().len(); - - let mut empty_workspaces = vec![]; - - for (idx, w) in focused_monitor.workspaces().iter().enumerate() { - if w.is_empty() { - empty_workspaces.push(idx); - } - } - - if !empty_workspaces.is_empty() { - let mut workspace_idx = direction.next_idx( - focused_workspace_idx, - NonZeroUsize::new(workspaces).ok_or_eyre("there must be at least one workspace")?, - ); - - while !empty_workspaces.contains(&workspace_idx) { - workspace_idx = direction.next_idx( - workspace_idx, - NonZeroUsize::new(workspaces) - .ok_or_eyre("there must be at least one workspace")?, - ); - } - - self.focus_workspace(workspace_idx)?; - } - Ok(()) + komorebi_core::cycle_focus_empty_workspace(self, direction) } /// Removes the focused workspace when it is empty, unnamed and not the monitor's only one, and focuses the one before it pub fn close_workspace(&mut self) -> eyre::Result<()> { - // TODO: figure out if we need to do this on macOS - // // This is to ensure that even on an empty workspace on a secondary monitor, the - // // secondary monitor where the cursor is focused will be used as the target for - // // the workspace switch op - // if let Some(monitor_idx) = self.monitor_idx_from_current_pos() { - // if monitor_idx != self.focused_monitor_idx() { - // if let Some(monitor) = self.monitors().get(monitor_idx) { - // if let Some(workspace) = monitor.focused_workspace() { - // if workspace.is_empty() { - // self.focus_monitor(monitor_idx)?; - // } - // } - // } - // } - // } - - let mut can_close = false; - - if let Some(monitor) = self.focused_monitor_mut() { - let focused_workspace_idx = monitor.focused_workspace_idx(); - let next_focused_workspace_idx = focused_workspace_idx.saturating_sub(1); - - if let Some(workspace) = monitor.focused_workspace() - && monitor.workspaces().len() > 1 - && workspace.containers().is_empty() - && workspace.floating_windows().is_empty() - && workspace.monocle_container.is_none() - && workspace.maximized_window.is_none() - && workspace.name.is_none() - { - can_close = true; - } - - if can_close - && monitor - .workspaces_mut() - .remove(focused_workspace_idx) - .is_some() - { - self.focus_workspace(next_focused_workspace_idx)?; - } - } - Ok(()) + komorebi_core::close_workspace(self) } /// Moves the focused container to the workspace that was focused before this one, and follows it when `follow` is set pub fn move_container_to_last_workspace(&mut self, follow: bool) -> eyre::Result<()> { - // This is to ensure that even on an empty workspace on a secondary monitor, the - // secondary monitor where the cursor is focused will be used as the target for - // the workspace switch op - if let Some(monitor_idx) = self.monitor_idx_from_current_pos() - && monitor_idx != self.focused_monitor_idx() - && let Some(monitor) = self.monitors().get(monitor_idx) - && let Some(workspace) = monitor.focused_workspace() - && workspace.is_empty() - { - self.focus_monitor(monitor_idx)?; - } + komorebi_core::move_container_to_last_workspace(self, follow) + } - let idx = self - .focused_monitor() - .ok_or_eyre("there is no monitor")? - .focused_workspace_idx(); - - if let Some(monitor) = self.focused_monitor_mut() - && let Some(last_focused_workspace) = monitor.last_focused_workspace - { - self.move_container_to_workspace(last_focused_workspace, follow, None)?; - } - - self.focused_monitor_mut() - .ok_or_eyre("there is no monitor")? - .last_focused_workspace = Option::from(idx); - Ok(()) + /// Focuses the workspace that was focused before this one + pub fn focus_last_workspace(&mut self) -> eyre::Result<()> { + komorebi_core::focus_last_workspace(self) } /// Looks for a window of the executable `exe` on every monitor and workspace, and focuses it @@ -744,38 +613,7 @@ impl WindowManager { self.focus_monitor(monitor_idx)?; } } - SocketMessage::FocusLastWorkspace => { - // TODO: figure out if we need to do this on macOS - // // This is to ensure that even on an empty workspace on a secondary monitor, the - // // secondary monitor where the cursor is focused will be used as the target for - // // the workspace switch op - // if let Some(monitor_idx) = self.monitor_idx_from_current_pos() { - // if monitor_idx != self.focused_monitor_idx() { - // if let Some(monitor) = self.monitors().get(monitor_idx) { - // if let Some(workspace) = monitor.focused_workspace() { - // if workspace.is_empty() { - // self.focus_monitor(monitor_idx)?; - // } - // } - // } - // } - // } - - let idx = self - .focused_monitor() - .ok_or_eyre("there is no monitor")? - .focused_workspace_idx(); - - if let Some(monitor) = self.focused_monitor_mut() - && let Some(last_focused_workspace) = monitor.last_focused_workspace - { - self.focus_workspace(last_focused_workspace)?; - } - - self.focused_monitor_mut() - .ok_or_eyre("there is no monitor")? - .last_focused_workspace = Option::from(idx); - } + SocketMessage::FocusLastWorkspace => self.focus_last_workspace()?, SocketMessage::FocusWorkspaceNumber(workspace_idx) => { if self.focused_workspace_idx().unwrap_or_default() != workspace_idx { self.focus_workspace(workspace_idx)?; @@ -1958,6 +1796,48 @@ pub fn read_commands_uds(wm: &Arc>, stream: UnixStream) -> komorebi_transport::read_commands::(wm, stream, &NoPolicy) } +/// The window manager as the shared commands of `komorebi_core::command` see it +impl komorebi_core::CommandHost for WindowManager { + type P = crate::workspace::MacosPlatform; + + fn monitors(&self) -> &std::collections::VecDeque { + WindowManager::monitors(self) + } + + fn focused_monitor(&self) -> Option<&crate::monitor::Monitor> { + WindowManager::focused_monitor(self) + } + + fn focused_monitor_mut(&mut self) -> Option<&mut crate::monitor::Monitor> { + WindowManager::focused_monitor_mut(self) + } + + fn focused_monitor_idx(&self) -> usize { + WindowManager::focused_monitor_idx(self) + } + + fn monitor_idx_from_current_pos(&mut self) -> Option { + WindowManager::monitor_idx_from_current_pos(self) + } + + fn focus_monitor(&mut self, idx: usize) -> eyre::Result<()> { + WindowManager::focus_monitor(self, idx) + } + + fn focus_workspace(&mut self, idx: usize) -> eyre::Result<()> { + WindowManager::focus_workspace(self, idx) + } + + fn move_container_to_workspace( + &mut self, + idx: usize, + follow: bool, + direction: Option, + ) -> eyre::Result<()> { + WindowManager::move_container_to_workspace(self, idx, follow, direction) + } +} + #[cfg(test)] mod tests { use crate::SocketMessage;