From 565c6cd6f88b0bbe06edbee0db67b3f95acbd939 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 19 Sep 2025 11:55:37 -0700 Subject: [PATCH] feat(wm): implement remaining cycle and stack cmds This commit implements the CycleFocusWindow, CycleMoveWindow, StackAll, UnstackAll, FocusStackWindow and CycleStackIndex SocketMessage variants. Largely a code migration from komorebi. --- komorebi/src/core/mod.rs | 8 +- komorebi/src/process_command.rs | 31 ++++ komorebi/src/window_manager.rs | 251 ++++++++++++++++++++++++++++++++ komorebi/src/workspace.rs | 25 ++++ komorebic/src/main.rs | 34 +++++ 5 files changed, 348 insertions(+), 1 deletion(-) diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index d5db0cfe..74e32962 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -140,9 +140,15 @@ pub enum SocketMessage { // Window / Container Commands FocusWindow(OperationDirection), MoveWindow(OperationDirection), + CycleFocusWindow(CycleDirection), + CycleMoveWindow(CycleDirection), StackWindow(OperationDirection), - CycleStack(CycleDirection), UnstackWindow, + CycleStack(CycleDirection), + CycleStackIndex(CycleDirection), + FocusStackWindow(usize), + StackAll, + UnstackAll, ResizeWindowEdge(OperationDirection, Sizing), ResizeWindowAxis(Axis, Sizing), MoveContainerToLastWorkspace, diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index c11e625a..aae233ca 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -99,11 +99,42 @@ impl WindowManager { } } } + SocketMessage::CycleFocusWindow(direction) => { + let focused_workspace = self.focused_workspace()?; + match focused_workspace.layer { + WorkspaceLayer::Tiling => { + self.focus_container_in_cycle_direction(direction)?; + } + WorkspaceLayer::Floating => { + self.focus_floating_window_in_cycle_direction(direction)?; + } + } + } + SocketMessage::CycleMoveWindow(direction) => { + self.move_container_in_cycle_direction(direction)?; + } + SocketMessage::StackWindow(direction) => self.add_window_to_container(direction)?, SocketMessage::UnstackWindow => self.remove_window_from_container()?, + SocketMessage::StackAll => self.stack_all()?, + SocketMessage::UnstackAll => self.unstack_all(true)?, SocketMessage::CycleStack(direction) => { self.cycle_container_window_in_direction(direction)?; } + SocketMessage::CycleStackIndex(direction) => { + self.cycle_container_window_index_in_direction(direction)?; + } + SocketMessage::FocusStackWindow(idx) => { + // In case you are using this command on a bar on a monitor + // different from the currently focused one, you'd want that + // monitor to be focused so that the FocusStackWindow happens + // on the monitor with the bar you just pressed. + if let Some(monitor_idx) = self.monitor_idx_from_current_pos() { + self.focus_monitor(monitor_idx)?; + } + self.focus_container_window(idx)?; + } + SocketMessage::FlipLayout(layout_flip) => self.flip_layout(layout_flip)?, SocketMessage::ChangeLayout(layout) => self.change_workspace_layout_default(layout)?, SocketMessage::CycleLayout(direction) => self.cycle_layout(direction)?, diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index a80b2dd9..696d4554 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -1600,4 +1600,255 @@ impl WindowManager { .ok_or_else(|| eyre!("there is no monitor"))? .update_focused_workspace(offset) } + + #[tracing::instrument(skip(self))] + pub fn focus_container_in_cycle_direction( + &mut self, + direction: CycleDirection, + ) -> eyre::Result<()> { + tracing::info!("focusing container"); + // let mut maximize_next = false; + let mut monocle_next = false; + + let mouse_follows_focus = self.mouse_follows_focus; + + if self.focused_workspace_mut()?.maximized_window.is_some() { + // TODO: maximized window stuff + // maximize_next = true; + // self.unmaximize_window()?; + } + + if self.focused_workspace_mut()?.monocle_container.is_some() { + monocle_next = true; + self.monocle_off()?; + } + + let workspace = self.focused_workspace_mut()?; + + let new_idx = workspace + .new_idx_for_cycle_direction(direction) + .ok_or_else(|| eyre!("this is not a valid direction from the current position"))?; + + workspace.focus_container(new_idx); + + // if maximize_next { + // self.toggle_maximize()?; + // } else + if monocle_next { + self.toggle_monocle()?; + } else { + self.focused_window_mut()?.focus(mouse_follows_focus)?; + } + + Ok(()) + } + + #[tracing::instrument(skip(self))] + pub fn move_container_in_cycle_direction( + &mut self, + direction: CycleDirection, + ) -> eyre::Result<()> { + let workspace = self.focused_workspace_mut()?; + if workspace.is_focused_window_monocle_or_maximized()? { + return Err(eyre!( + "ignoring command while active window is in monocle mode or maximized" + )); + } + + tracing::info!("moving container"); + + let current_idx = workspace.focused_container_idx(); + let new_idx = workspace + .new_idx_for_cycle_direction(direction) + .ok_or_else(|| eyre!("this is not a valid direction from the current position"))?; + + workspace.swap_containers(current_idx, new_idx); + workspace.focus_container(new_idx); + self.update_focused_workspace(self.mouse_follows_focus, true) + } + + #[tracing::instrument(skip(self))] + pub fn focus_floating_window_in_cycle_direction( + &mut self, + direction: CycleDirection, + ) -> eyre::Result<()> { + let mouse_follows_focus = self.mouse_follows_focus; + let focused_workspace = self.focused_workspace()?; + + let mut target_idx = None; + let len = focused_workspace.floating_windows().len(); + + if len > 1 { + let focused_window_id = + MacosApi::foreground_window_id().ok_or(eyre!("no foreground window"))?; + for (idx, window) in focused_workspace.floating_windows().iter().enumerate() { + if window.id == focused_window_id { + match direction { + CycleDirection::Previous => { + if idx == 0 { + target_idx = Some(len - 1) + } else { + target_idx = Some(idx - 1) + } + } + CycleDirection::Next => { + if idx == len - 1 { + target_idx = Some(0) + } else { + target_idx = Some(idx - 1) + } + } + } + } + } + + if target_idx.is_none() { + target_idx = Some(0); + } + } + + if let Some(idx) = target_idx + && let Some(window) = focused_workspace.floating_windows().get(idx) + { + window.focus(mouse_follows_focus)?; + } + + Ok(()) + } + + #[tracing::instrument(skip(self))] + pub fn cycle_container_window_index_in_direction( + &mut self, + direction: CycleDirection, + ) -> eyre::Result<()> { + tracing::info!("cycling container window index"); + + let mouse_follows_focus = self.mouse_follows_focus; + + let container = + if let Some(container) = &mut self.focused_workspace_mut()?.monocle_container { + container + } else { + self.focused_container_mut()? + }; + + let len = NonZeroUsize::new(container.windows().len()) + .ok_or_else(|| eyre!("there must be at least one window in a container"))?; + + if len.get() == 1 { + return Err(eyre!("there is only one window in this container")); + } + + let current_idx = container.focused_window_idx(); + let next_idx = direction.next_idx(current_idx, len); + container.windows_mut().swap(current_idx, next_idx); + + container.focus_window(next_idx); + container.load_focused_window()?; + + if let Some(window) = container.focused_window() { + window.focus(mouse_follows_focus)?; + } + + self.update_focused_workspace(mouse_follows_focus, true) + } + + #[tracing::instrument(skip(self))] + pub fn focus_container_window(&mut self, idx: usize) -> eyre::Result<()> { + tracing::info!("focusing container window at index {idx}"); + + let mouse_follows_focus = self.mouse_follows_focus; + + let container = + if let Some(container) = &mut self.focused_workspace_mut()?.monocle_container { + container + } else { + self.focused_container_mut()? + }; + + let len = NonZeroUsize::new(container.windows().len()) + .ok_or_else(|| eyre!("there must be at least one window in a container"))?; + + if len.get() == 1 && idx != 0 { + return Err(eyre!("there is only one window in this container")); + } + + if container.windows().get(idx).is_none() { + return Err(eyre!("there is no window in this container at index {idx}")); + } + + container.focus_window(idx); + container.load_focused_window()?; + + if let Some(window) = container.focused_window() { + window.focus(mouse_follows_focus)?; + } + + self.update_focused_workspace(mouse_follows_focus, true) + } + + #[tracing::instrument(skip(self))] + pub fn stack_all(&mut self) -> eyre::Result<()> { + self.unstack_all(false)?; + + tracing::info!("stacking all windows on workspace"); + + let workspace = self.focused_workspace_mut()?; + + let mut focused_window_id = None; + if let Some(container) = workspace.focused_container() + && let Some(window) = container.focused_window() + { + focused_window_id = Some(window.id); + } + + workspace.focus_container(workspace.containers().len().saturating_sub(1)); + while workspace.focused_container_idx() > 0 { + workspace.move_window_to_container(0)?; + workspace.focus_container(workspace.containers().len().saturating_sub(1)); + } + + if let Some(window_id) = focused_window_id { + workspace.focus_container_by_window(window_id)?; + } + + self.update_focused_workspace(self.mouse_follows_focus, true) + } + + #[tracing::instrument(skip(self))] + pub fn unstack_all(&mut self, update_workspace: bool) -> eyre::Result<()> { + tracing::info!("unstacking all windows in container"); + + let workspace = self.focused_workspace_mut()?; + + let mut focused_window_id = None; + if let Some(container) = workspace.focused_container() + && let Some(window) = container.focused_window() + { + focused_window_id = Some(window.id); + } + + let initial_focused_container_index = workspace.focused_container_idx(); + let mut focused_container = workspace.focused_container().cloned(); + + while let Some(focused) = &focused_container { + if focused.windows().len() > 1 { + workspace.new_container_for_focused_window()?; + workspace.focus_container(initial_focused_container_index); + focused_container = workspace.focused_container().cloned(); + } else { + focused_container = None; + } + } + + if let Some(window_id) = focused_window_id { + workspace.focus_container_by_window(window_id)?; + } + + if update_workspace { + self.update_focused_workspace(self.mouse_follows_focus, true)?; + } + + Ok(()) + } } diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index 14a1ace0..89f31601 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -2,6 +2,7 @@ use crate::container::Container; use crate::core::FloatingLayerBehaviour; use crate::core::WindowContainerBehaviour; use crate::core::arrangement::Axis; +use crate::core::cycle_direction::CycleDirection; use crate::core::default_layout::DefaultLayout; use crate::core::default_layout::LayoutOptions; use crate::core::layout::Layout; @@ -135,6 +136,13 @@ impl Workspace { ) } + pub fn new_idx_for_cycle_direction(&self, direction: CycleDirection) -> Option { + Option::from(direction.next_idx( + self.focused_container_idx(), + NonZeroUsize::new(self.containers().len())?, + )) + } + pub fn container_idx_for_window(&self, window_id: u32) -> Option { let mut idx = None; for (i, x) in self.containers().iter().enumerate() { @@ -304,6 +312,23 @@ impl Workspace { self.focus_container(focused_idx.saturating_sub(1)); } + pub fn is_focused_window_monocle_or_maximized(&self) -> eyre::Result { + let window_id = MacosApi::foreground_window_id().ok_or(eyre!("no foreground window"))?; + if let Some(window) = &self.maximized_window + && window_id == window.id + { + return Ok(true); + } + + if let Some(container) = &self.monocle_container + && container.contains_window(window_id) + { + return Ok(true); + } + + Ok(false) + } + pub fn is_empty(&self) -> bool { self.containers().is_empty() && self.maximized_window.is_none() diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 0ec69b77..22ea0408 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -185,11 +185,27 @@ enum SubCommand { /// Stack the focused window in the specified direction #[clap(arg_required_else_help = true)] Stack(Stack), + /// Change focus to the window in the specified cycle direction + #[clap(arg_required_else_help = true)] + CycleFocus(CycleFocus), + /// Move the focused window in the specified cycle direction + #[clap(arg_required_else_help = true)] + CycleMove(CycleMove), /// Unstack the focused window Unstack, /// Cycle the focused stack in the specified cycle direction #[clap(arg_required_else_help = true)] CycleStack(CycleStack), + /// Cycle the index of the focused window in the focused stack in the specified cycle direction + #[clap(arg_required_else_help = true)] + CycleStackIndex(CycleStackIndex), + /// Focus the specified window index in the focused stack + #[clap(arg_required_else_help = true)] + FocusStackWindow(FocusStackWindow), + /// Stack all windows on the focused workspace + StackAll, + /// Unstack all windows in the focused container + UnstackAll, /// Resize the focused window in the specified direction #[clap(arg_required_else_help = true)] #[clap(alias = "resize")] @@ -334,6 +350,12 @@ fn main() -> eyre::Result<()> { SubCommand::Move(arg) => { send_message(&SocketMessage::MoveWindow(arg.operation_direction))?; } + SubCommand::CycleFocus(arg) => { + send_message(&SocketMessage::CycleFocusWindow(arg.cycle_direction))?; + } + SubCommand::CycleMove(arg) => { + send_message(&SocketMessage::CycleMoveWindow(arg.cycle_direction))?; + } SubCommand::TogglePause => { send_message(&SocketMessage::TogglePause)?; } @@ -349,12 +371,24 @@ fn main() -> eyre::Result<()> { SubCommand::Stack(arg) => { send_message(&SocketMessage::StackWindow(arg.operation_direction))?; } + SubCommand::StackAll => { + send_message(&SocketMessage::StackAll)?; + } SubCommand::Unstack => { send_message(&SocketMessage::UnstackWindow)?; } + SubCommand::UnstackAll => { + send_message(&SocketMessage::UnstackAll)?; + } + SubCommand::FocusStackWindow(arg) => { + send_message(&SocketMessage::FocusStackWindow(arg.target))?; + } SubCommand::CycleStack(arg) => { send_message(&SocketMessage::CycleStack(arg.cycle_direction))?; } + SubCommand::CycleStackIndex(arg) => { + send_message(&SocketMessage::CycleStackIndex(arg.cycle_direction))?; + } SubCommand::FocusWorkspace(arg) => { send_message(&SocketMessage::FocusWorkspaceNumber(arg.target))?; }