From c1cf94b23ffeb80f79207868b8c2807e6d746c41 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 19 Sep 2025 09:33:54 -0700 Subject: [PATCH] feat(wm): implement resizing and retiling This commit migrates the code from komorebi required to implement the ResizeWindowEdge, ResizeWindowAxis, Retile and RetileWithResizeDimensions SocketMessage variants, as well as the related komorebic commands to emit those SocketMessages. --- komorebi-client/src/lib.rs | 2 + komorebi/src/core/arrangement.rs | 8 +- komorebi/src/core/mod.rs | 8 +- komorebi/src/process_command.rs | 67 ++++++++++ komorebi/src/window_manager.rs | 215 +++++++++++++++++++++++++++++++ komorebic/src/main.rs | 36 ++++++ 6 files changed, 331 insertions(+), 5 deletions(-) diff --git a/komorebi-client/src/lib.rs b/komorebi-client/src/lib.rs index aae61b29..2a0febd2 100644 --- a/komorebi-client/src/lib.rs +++ b/komorebi-client/src/lib.rs @@ -1,5 +1,7 @@ pub use komorebi::DATA_DIR; +pub use komorebi::core::Sizing; pub use komorebi::core::SocketMessage; +pub use komorebi::core::arrangement::Axis; pub use komorebi::core::cycle_direction::CycleDirection; pub use komorebi::core::default_layout::DefaultLayout; pub use komorebi::core::operation_direction::OperationDirection; diff --git a/komorebi/src/core/arrangement.rs b/komorebi/src/core/arrangement.rs index c256ec1d..c4915e06 100644 --- a/komorebi/src/core/arrangement.rs +++ b/komorebi/src/core/arrangement.rs @@ -1,10 +1,12 @@ -use std::num::NonZeroUsize; - use crate::core::default_layout::DefaultLayout; use crate::core::default_layout::LayoutOptions; use crate::core::rect::Rect; +use clap::ValueEnum; use serde::Deserialize; use serde::Serialize; +use std::num::NonZeroUsize; +use strum::Display; +use strum::EnumString; pub trait Arrangement { #[allow(clippy::too_many_arguments)] @@ -567,7 +569,7 @@ impl Arrangement for DefaultLayout { } } -#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Copy, Debug, Serialize, Deserialize, Display, EnumString, ValueEnum, PartialEq)] pub enum Axis { Horizontal, Vertical, diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index 6cb01d82..c916bd81 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -1,3 +1,4 @@ +use crate::core::arrangement::Axis; use crate::core::cycle_direction::CycleDirection; use crate::core::default_layout::DefaultLayout; use crate::core::operation_direction::OperationDirection; @@ -18,12 +19,11 @@ pub mod operation_direction; pub mod pathext; pub mod rect; -#[derive(Clone, Copy, Debug, Serialize, Deserialize)] +#[derive(Clone, Copy, Debug, Serialize, Deserialize, Display, EnumString, ValueEnum)] pub enum Sizing { Increase, Decrease, } - #[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq)] pub struct WindowManagementBehaviour { /// The current WindowContainerBehaviour to be used @@ -127,6 +127,10 @@ pub enum SocketMessage { FocusWorkspaceNumber(usize), MoveContainerToWorkspaceNumber(usize), SendContainerToWorkspaceNumber(usize), + ResizeWindowEdge(OperationDirection, Sizing), + ResizeWindowAxis(Axis, Sizing), + Retile, + RetileWithResizeDimensions, } impl SocketMessage { diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 01f7b26b..b775eaf6 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -1,4 +1,6 @@ use crate::core::SocketMessage; +use crate::core::arrangement::Axis; +use crate::core::operation_direction::OperationDirection; use crate::core::rect::Rect; use crate::macos_api::MacosApi; use crate::window_manager::WindowManager; @@ -222,6 +224,71 @@ impl WindowManager { window.focus(mouse_follows_focus)?; } } + SocketMessage::ResizeWindowEdge(direction, sizing) => { + self.resize_window(direction, sizing, self.resize_delta, true)?; + } + SocketMessage::ResizeWindowAxis(axis, sizing) => { + match axis { + Axis::Horizontal => { + self.resize_window( + OperationDirection::Left, + sizing, + self.resize_delta, + false, + )?; + self.resize_window( + OperationDirection::Right, + sizing, + self.resize_delta, + false, + )?; + } + Axis::Vertical => { + self.resize_window( + OperationDirection::Up, + sizing, + self.resize_delta, + false, + )?; + self.resize_window( + OperationDirection::Down, + sizing, + self.resize_delta, + false, + )?; + } + Axis::HorizontalAndVertical => { + self.resize_window( + OperationDirection::Left, + sizing, + self.resize_delta, + false, + )?; + self.resize_window( + OperationDirection::Right, + sizing, + self.resize_delta, + false, + )?; + self.resize_window( + OperationDirection::Up, + sizing, + self.resize_delta, + false, + )?; + self.resize_window( + OperationDirection::Down, + sizing, + self.resize_delta, + false, + )?; + } + } + + self.update_focused_workspace(false, false)?; + } + SocketMessage::Retile => self.retile_all(false)?, + SocketMessage::RetileWithResizeDimensions => self.retile_all(true)?, } Ok(()) diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 2f440fb0..5346fedc 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -5,7 +5,10 @@ use crate::accessibility::AccessibilityApi; use crate::application::Application; use crate::container::Container; use crate::core::Placement; +use crate::core::Sizing; use crate::core::WindowManagementBehaviour; +use crate::core::arrangement::Arrangement; +use crate::core::arrangement::Axis; use crate::core::cycle_direction::CycleDirection; use crate::core::default_layout::DefaultLayout; use crate::core::layout::Layout; @@ -920,4 +923,216 @@ impl WindowManager { let workspace = self.focused_workspace_mut()?; workspace.new_container_for_floating_window() } + + #[tracing::instrument(skip(self))] + pub fn resize_window( + &mut self, + direction: OperationDirection, + sizing: Sizing, + delta: i32, + update: bool, + ) -> eyre::Result<()> { + let mouse_follows_focus = self.mouse_follows_focus; + let mut focused_monitor_work_area = self.focused_monitor_work_area()?; + let workspace = self.focused_workspace_mut()?; + + match workspace.layer { + WorkspaceLayer::Floating => { + let workspace = self.focused_workspace()?; + let focused_window_id = + MacosApi::foreground_window_id().ok_or(eyre!("no foreground window"))?; + + let border_offset = 0; + let border_width = 0; + focused_monitor_work_area.left += border_offset; + focused_monitor_work_area.left += border_width; + focused_monitor_work_area.top += border_offset; + focused_monitor_work_area.top += border_width; + focused_monitor_work_area.right -= border_offset * 2; + focused_monitor_work_area.right -= border_width * 2; + focused_monitor_work_area.bottom -= border_offset * 2; + focused_monitor_work_area.bottom -= border_width * 2; + + for window in workspace.floating_windows().iter() { + if window.id == focused_window_id { + let mut rect = Rect::from(MacosApi::window_rect(&window.element)?); + match (direction, sizing) { + (OperationDirection::Left, Sizing::Increase) => { + if rect.left - delta < focused_monitor_work_area.left { + rect.left = focused_monitor_work_area.left; + } else { + rect.left -= delta; + } + } + (OperationDirection::Left, Sizing::Decrease) => { + rect.left += delta; + } + (OperationDirection::Right, Sizing::Increase) => { + if rect.left + rect.right + delta * 2 + > focused_monitor_work_area.left + + focused_monitor_work_area.right + { + rect.right = focused_monitor_work_area.left + + focused_monitor_work_area.right + - rect.left; + } else { + rect.right += delta * 2; + } + } + (OperationDirection::Right, Sizing::Decrease) => { + rect.right -= delta * 2; + } + (OperationDirection::Up, Sizing::Increase) => { + if rect.top - delta < focused_monitor_work_area.top { + rect.top = focused_monitor_work_area.top; + } else { + rect.top -= delta; + } + } + (OperationDirection::Up, Sizing::Decrease) => { + rect.top += delta; + } + (OperationDirection::Down, Sizing::Increase) => { + if rect.top + rect.bottom + delta * 2 + > focused_monitor_work_area.top + + focused_monitor_work_area.bottom + { + rect.bottom = focused_monitor_work_area.top + + focused_monitor_work_area.bottom + - rect.top; + } else { + rect.bottom += delta * 2; + } + } + (OperationDirection::Down, Sizing::Decrease) => { + rect.bottom -= delta * 2; + } + } + + window.set_position(&rect)?; + + if mouse_follows_focus { + MacosApi::center_cursor_in_rect(&rect)?; + } + + break; + } + } + } + WorkspaceLayer::Tiling => { + match workspace.layout { + Layout::Default(layout) => { + tracing::info!("resizing window"); + let len = NonZeroUsize::new(workspace.containers().len()) + .ok_or_else(|| eyre!("there must be at least one container"))?; + let focused_idx = workspace.focused_container_idx(); + let focused_idx_resize = workspace + .resize_dimensions + .get(focused_idx) + .ok_or_else(|| { + eyre!("there is no resize adjustment for this container") + })?; + + if direction + .destination( + workspace.layout.as_boxed_direction().as_ref(), + workspace.layout_flip, + focused_idx, + len, + ) + .is_some() + { + let unaltered = layout.calculate( + &focused_monitor_work_area, + len, + workspace.container_padding, + workspace.layout_flip, + &[], + workspace.focused_container_idx(), + workspace.layout_options, + &workspace.latest_layout, + ); + + let mut direction = direction; + + // We only ever want to operate on the unflipped Rect positions when resizing, then we + // can flip them however they need to be flipped once the resizing has been done + if let Some(flip) = workspace.layout_flip { + match flip { + Axis::Horizontal => { + if matches!(direction, OperationDirection::Left) + || matches!(direction, OperationDirection::Right) + { + direction = direction.opposite(); + } + } + Axis::Vertical => { + if matches!(direction, OperationDirection::Up) + || matches!(direction, OperationDirection::Down) + { + direction = direction.opposite(); + } + } + Axis::HorizontalAndVertical => direction = direction.opposite(), + } + } + + let resize = layout.resize( + unaltered + .get(focused_idx) + .ok_or_else(|| eyre!("there is no last layout"))?, + focused_idx_resize, + direction, + sizing, + delta, + ); + + workspace.resize_dimensions[focused_idx] = resize; + + return if update { + self.update_focused_workspace(false, false) + } else { + Ok(()) + }; + } + + tracing::warn!("cannot resize container in this direction"); + } + } + } + } + + Ok(()) + } + + #[tracing::instrument(skip(self))] + pub fn retile_all(&mut self, preserve_resize_dimensions: bool) -> eyre::Result<()> { + let offset = self.work_area_offset; + + for monitor in self.monitors_mut() { + let offset = if monitor.work_area_offset.is_some() { + monitor.work_area_offset + } else { + offset + }; + + let focused_workspace_idx = monitor.focused_workspace_idx(); + monitor.update_workspace_globals(focused_workspace_idx, offset); + + let workspace = monitor + .focused_workspace_mut() + .ok_or_else(|| eyre!("there is no workspace"))?; + + // Reset any resize adjustments if we want to force a retile + if !preserve_resize_dimensions { + for resize in &mut workspace.resize_dimensions { + *resize = None; + } + } + + workspace.update()?; + } + + Ok(()) + } } diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index b789ae4d..dce01bc8 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -2,10 +2,12 @@ use clap::Parser; use color_eyre::eyre; +use komorebi_client::Axis; use komorebi_client::CycleDirection; use komorebi_client::DefaultLayout; use komorebi_client::OperationDirection; use komorebi_client::PathExt; +use komorebi_client::Sizing; use komorebi_client::SocketMessage; use komorebi_client::send_message; use lazy_static::lazy_static; @@ -78,6 +80,22 @@ gen_target_subcommand_args! { SendToWorkspace } +#[derive(Parser)] +struct Resize { + #[clap(value_enum)] + edge: OperationDirection, + #[clap(value_enum)] + sizing: Sizing, +} + +#[derive(Parser)] +struct ResizeAxis { + #[clap(value_enum)] + axis: Axis, + #[clap(value_enum)] + sizing: Sizing, +} + #[derive(Parser)] #[clap(author, about, version)] struct Opts { @@ -101,6 +119,13 @@ enum SubCommand { /// Cycle the focused stack in the specified cycle direction #[clap(arg_required_else_help = true)] CycleStack(CycleStack), + /// Resize the focused window in the specified direction + #[clap(arg_required_else_help = true)] + #[clap(alias = "resize")] + ResizeEdge(Resize), + /// Resize the focused window or primary column along the specified axis + #[clap(arg_required_else_help = true)] + ResizeAxis(ResizeAxis), /// Toggle the paused state for all window tiling TogglePause, /// Toggle monocle mode for the focused container @@ -121,6 +146,8 @@ enum SubCommand { /// Send the focused window to the specified workspace #[clap(arg_required_else_help = true)] SendToWorkspace(SendToWorkspace), + /// Force the retiling of all managed windows + Retile, } fn main() -> eyre::Result<()> { @@ -166,6 +193,15 @@ fn main() -> eyre::Result<()> { SubCommand::ToggleWorkspaceLayer => { send_message(&SocketMessage::ToggleWorkspaceLayer)?; } + SubCommand::ResizeEdge(arg) => { + send_message(&SocketMessage::ResizeWindowEdge(arg.edge, arg.sizing))?; + } + SubCommand::ResizeAxis(arg) => { + send_message(&SocketMessage::ResizeWindowAxis(arg.axis, arg.sizing))?; + } + SubCommand::Retile => { + send_message(&SocketMessage::Retile)?; + } } Ok(())