From 4fc401cca80e5dd9555491c35ee106175c3d63a4 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 19 Sep 2025 09:49:25 -0700 Subject: [PATCH] feat(wm): implement layout cycling and flipping Another lift and shift from komorebi to add support for the CycleLayout and FlipLayout SocketMessage variants. Trying to get through all of the main workspace-related interactive commands. --- komorebi/src/core/default_layout.rs | 6 ++- komorebi/src/core/mod.rs | 2 + komorebi/src/process_command.rs | 2 + komorebi/src/window_manager.rs | 64 +++++++++++++++++++++++++++++ komorebic/src/main.rs | 14 +++++++ 5 files changed, 87 insertions(+), 1 deletion(-) diff --git a/komorebi/src/core/default_layout.rs b/komorebi/src/core/default_layout.rs index d545a9f3..ff85705d 100644 --- a/komorebi/src/core/default_layout.rs +++ b/komorebi/src/core/default_layout.rs @@ -4,8 +4,12 @@ use crate::core::rect::Rect; use clap::ValueEnum; use serde::Deserialize; use serde::Serialize; +use strum::Display; +use strum::EnumString; -#[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq, ValueEnum)] +#[derive( + Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq, Display, EnumString, ValueEnum, +)] pub enum DefaultLayout { BSP, Columns, diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index eef2f699..35890123 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -120,6 +120,8 @@ pub enum SocketMessage { CycleStack(CycleDirection), UnstackWindow, ChangeLayout(DefaultLayout), + CycleLayout(CycleDirection), + FlipLayout(Axis), TogglePause, ToggleMonocle, ToggleFloat, diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 622e6be0..0c9e69e3 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -98,7 +98,9 @@ impl WindowManager { SocketMessage::CycleStack(direction) => { self.cycle_container_window_in_direction(direction)?; } + 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)?, SocketMessage::TogglePause => { if self.is_paused { tracing::info!("resuming"); diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 0bb2d113..3c9bac4e 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -1169,4 +1169,68 @@ impl WindowManager { workspace.focus_container(target_idx); self.update_focused_workspace(self.mouse_follows_focus, true) } + + #[tracing::instrument(skip(self))] + pub fn flip_layout(&mut self, layout_flip: Axis) -> eyre::Result<()> { + let workspace = self.focused_workspace_mut()?; + + tracing::info!("flipping layout"); + + #[allow(clippy::match_same_arms)] + match workspace.layout_flip { + None => { + workspace.layout_flip = Option::from(layout_flip); + } + Some(current_layout_flip) => { + match current_layout_flip { + Axis::Horizontal => match layout_flip { + Axis::Horizontal => workspace.layout_flip = None, + Axis::Vertical => { + workspace.layout_flip = Option::from(Axis::HorizontalAndVertical) + } + Axis::HorizontalAndVertical => { + workspace.layout_flip = Option::from(Axis::HorizontalAndVertical) + } + }, + Axis::Vertical => match layout_flip { + Axis::Horizontal => { + workspace.layout_flip = Option::from(Axis::HorizontalAndVertical) + } + Axis::Vertical => workspace.layout_flip = None, + Axis::HorizontalAndVertical => { + workspace.layout_flip = Option::from(Axis::HorizontalAndVertical) + } + }, + Axis::HorizontalAndVertical => match layout_flip { + Axis::Horizontal => workspace.layout_flip = Option::from(Axis::Vertical), + Axis::Vertical => workspace.layout_flip = Option::from(Axis::Horizontal), + Axis::HorizontalAndVertical => workspace.layout_flip = None, + }, + }; + } + } + + self.update_focused_workspace(false, false) + } + + #[tracing::instrument(skip(self))] + pub fn cycle_layout(&mut self, direction: CycleDirection) -> eyre::Result<()> { + tracing::info!("cycling layout"); + + let workspace = self.focused_workspace_mut()?; + + match workspace.layout { + Layout::Default(current) => { + let new_layout = match direction { + CycleDirection::Previous => current.cycle_previous(), + CycleDirection::Next => current.cycle_next(), + }; + + tracing::info!("next layout: {new_layout}"); + workspace.layout = Layout::Default(new_layout); + } + } + + self.update_focused_workspace(self.mouse_follows_focus, false) + } } diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 96de658e..cc5d8a4e 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -56,7 +56,9 @@ macro_rules! gen_enum_subcommand_args { gen_enum_subcommand_args! { Focus: OperationDirection, Move: OperationDirection, + FlipLayout: Axis, ChangeLayout: DefaultLayout, + CycleLayout: CycleDirection, Stack: OperationDirection, CycleStack: CycleDirection, PromoteWindow: OperationDirection, @@ -138,6 +140,12 @@ enum SubCommand { /// Set the layout on the focused workspace #[clap(arg_required_else_help = true)] ChangeLayout(ChangeLayout), + /// Cycle between available layouts + #[clap(arg_required_else_help = true)] + CycleLayout(CycleLayout), + /// Flip the layout on the focused workspace + #[clap(arg_required_else_help = true)] + FlipLayout(FlipLayout), /// Promote the focused window to the top of the tree Promote, /// Promote the user focus to the top of the tree @@ -173,6 +181,12 @@ fn main() -> eyre::Result<()> { SubCommand::ChangeLayout(arg) => { send_message(&SocketMessage::ChangeLayout(arg.default_layout))?; } + SubCommand::CycleLayout(arg) => { + send_message(&SocketMessage::CycleLayout(arg.cycle_direction))?; + } + SubCommand::FlipLayout(arg) => { + send_message(&SocketMessage::FlipLayout(arg.axis))?; + } SubCommand::Stack(arg) => { send_message(&SocketMessage::StackWindow(arg.operation_direction))?; }