From f848c1d8f003a96b7769353b438cfc127fb8b510 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 17 Oct 2025 10:29:25 -0700 Subject: [PATCH] feat(wm): add opt to constrain grid layout by rows This commit adds a new LayoutOptions option, GridLayoutOptions, currently with a single configurable "rows" opt which can be use to constrain the grid by number of rows. --- komorebi/src/core/arrangement.rs | 16 +++- komorebi/src/core/default_layout.rs | 9 ++ komorebi/src/core/direction.rs | 102 +++++++++++++++++------ komorebi/src/core/operation_direction.rs | 4 +- komorebi/src/process_command.rs | 1 + komorebi/src/window_manager.rs | 2 + komorebi/src/workspace.rs | 1 + schema.json | 15 ++++ 8 files changed, 121 insertions(+), 29 deletions(-) diff --git a/komorebi/src/core/arrangement.rs b/komorebi/src/core/arrangement.rs index be877a4a..9396035b 100644 --- a/komorebi/src/core/arrangement.rs +++ b/komorebi/src/core/arrangement.rs @@ -536,14 +536,26 @@ impl Arrangement for DefaultLayout { let len = len as i32; - let num_cols = (len as f32).sqrt().ceil() as i32; + let row_constraint = layout_options.and_then(|o| o.grid.map(|g| g.rows)); + + let num_cols = if let Some(rows) = row_constraint { + ((len as f32) / (rows as f32)).ceil() as i32 + } else { + (len as f32).sqrt().ceil() as i32 + }; + let mut iter = layouts.iter_mut().enumerate().peekable(); for col in 0..num_cols { let iter_peek = iter.peek().map(|x| x.0).unwrap_or_default() as i32; let remaining_windows = len - iter_peek; let remaining_columns = num_cols - col; - let num_rows_in_this_col = remaining_windows / remaining_columns; + + let num_rows_in_this_col = if let Some(rows) = row_constraint { + (remaining_windows / remaining_columns).min(rows as i32) + } else { + remaining_windows / remaining_columns + }; let win_height = area.bottom / num_rows_in_this_col; let win_width = area.right / num_cols; diff --git a/komorebi/src/core/default_layout.rs b/komorebi/src/core/default_layout.rs index a363559e..264bb7e1 100644 --- a/komorebi/src/core/default_layout.rs +++ b/komorebi/src/core/default_layout.rs @@ -29,6 +29,8 @@ pub enum DefaultLayout { pub struct LayoutOptions { /// Options related to the Scrolling layout pub scrolling: Option, + /// Options related to the Grid layout + pub grid: Option, } #[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq)] @@ -40,6 +42,13 @@ pub struct ScrollingLayoutOptions { pub center_focused_column: Option, } +#[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +pub struct GridLayoutOptions { + /// Maximum number of rows per grid column + pub rows: usize, +} + impl DefaultLayout { pub fn leftmost_index(&self, len: usize) -> usize { match self { diff --git a/komorebi/src/core/direction.rs b/komorebi/src/core/direction.rs index 6df24716..39560cc9 100644 --- a/komorebi/src/core/direction.rs +++ b/komorebi/src/core/direction.rs @@ -1,4 +1,5 @@ use crate::core::default_layout::DefaultLayout; +use crate::core::default_layout::LayoutOptions; use crate::core::operation_direction::OperationDirection; pub trait Direction { @@ -7,6 +8,7 @@ pub trait Direction { op_direction: OperationDirection, idx: usize, count: usize, + layout_options: Option, ) -> Option; fn is_valid_direction( @@ -14,30 +16,35 @@ pub trait Direction { op_direction: OperationDirection, idx: usize, count: usize, + layout_options: Option, ) -> bool; fn up_index( &self, op_direction: Option, idx: usize, count: Option, + layout_options: Option, ) -> usize; fn down_index( &self, op_direction: Option, idx: usize, count: Option, + layout_options: Option, ) -> usize; fn left_index( &self, op_direction: Option, idx: usize, count: Option, + layout_options: Option, ) -> usize; fn right_index( &self, op_direction: Option, idx: usize, count: Option, + layout_options: Option, ) -> usize; } @@ -47,32 +54,53 @@ impl Direction for DefaultLayout { op_direction: OperationDirection, idx: usize, count: usize, + layout_options: Option, ) -> Option { match op_direction { OperationDirection::Left => { - if self.is_valid_direction(op_direction, idx, count) { - Option::from(self.left_index(Some(op_direction), idx, Some(count))) + if self.is_valid_direction(op_direction, idx, count, layout_options) { + Option::from(self.left_index( + Some(op_direction), + idx, + Some(count), + layout_options, + )) } else { None } } OperationDirection::Right => { - if self.is_valid_direction(op_direction, idx, count) { - Option::from(self.right_index(Some(op_direction), idx, Some(count))) + if self.is_valid_direction(op_direction, idx, count, layout_options) { + Option::from(self.right_index( + Some(op_direction), + idx, + Some(count), + layout_options, + )) } else { None } } OperationDirection::Up => { - if self.is_valid_direction(op_direction, idx, count) { - Option::from(self.up_index(Some(op_direction), idx, Some(count))) + if self.is_valid_direction(op_direction, idx, count, layout_options) { + Option::from(self.up_index( + Some(op_direction), + idx, + Some(count), + layout_options, + )) } else { None } } OperationDirection::Down => { - if self.is_valid_direction(op_direction, idx, count) { - Option::from(self.down_index(Some(op_direction), idx, Some(count))) + if self.is_valid_direction(op_direction, idx, count, layout_options) { + Option::from(self.down_index( + Some(op_direction), + idx, + Some(count), + layout_options, + )) } else { None } @@ -85,6 +113,7 @@ impl Direction for DefaultLayout { op_direction: OperationDirection, idx: usize, count: usize, + layout_options: Option, ) -> bool { if count < 2 { return false; @@ -97,7 +126,7 @@ impl Direction for DefaultLayout { Self::Rows | Self::HorizontalStack => idx != 0, Self::VerticalStack | Self::RightMainVerticalStack => idx != 0 && idx != 1, Self::UltrawideVerticalStack => idx > 2, - Self::Grid => !is_grid_edge(op_direction, idx, count), + Self::Grid => !is_grid_edge(op_direction, idx, count, layout_options), Self::Scrolling => false, }, OperationDirection::Down => match self { @@ -107,7 +136,7 @@ impl Direction for DefaultLayout { Self::VerticalStack | Self::RightMainVerticalStack => idx != 0 && idx != count - 1, Self::HorizontalStack => idx == 0, Self::UltrawideVerticalStack => idx > 1 && idx != count - 1, - Self::Grid => !is_grid_edge(op_direction, idx, count), + Self::Grid => !is_grid_edge(op_direction, idx, count, layout_options), Self::Scrolling => false, }, OperationDirection::Left => match self { @@ -117,7 +146,7 @@ impl Direction for DefaultLayout { Self::Rows => false, Self::HorizontalStack => idx != 0 && idx != 1, Self::UltrawideVerticalStack => idx != 1, - Self::Grid => !is_grid_edge(op_direction, idx, count), + Self::Grid => !is_grid_edge(op_direction, idx, count, layout_options), Self::Scrolling => idx != 0, }, OperationDirection::Right => match self { @@ -131,7 +160,7 @@ impl Direction for DefaultLayout { 2 => idx != 0, _ => idx < 2, }, - Self::Grid => !is_grid_edge(op_direction, idx, count), + Self::Grid => !is_grid_edge(op_direction, idx, count, layout_options), Self::Scrolling => idx != count - 1, }, } @@ -142,6 +171,7 @@ impl Direction for DefaultLayout { op_direction: Option, idx: usize, count: Option, + layout_options: Option, ) -> usize { match self { Self::BSP => { @@ -157,7 +187,7 @@ impl Direction for DefaultLayout { | Self::UltrawideVerticalStack | Self::RightMainVerticalStack => idx - 1, Self::HorizontalStack => 0, - Self::Grid => grid_neighbor(op_direction, idx, count), + Self::Grid => grid_neighbor(op_direction, idx, count, layout_options), Self::Scrolling => unreachable!(), } } @@ -167,6 +197,7 @@ impl Direction for DefaultLayout { op_direction: Option, idx: usize, count: Option, + layout_options: Option, ) -> usize { match self { Self::BSP @@ -176,7 +207,7 @@ impl Direction for DefaultLayout { | Self::RightMainVerticalStack => idx + 1, Self::Columns => unreachable!(), Self::HorizontalStack => 1, - Self::Grid => grid_neighbor(op_direction, idx, count), + Self::Grid => grid_neighbor(op_direction, idx, count, layout_options), Self::Scrolling => unreachable!(), } } @@ -186,6 +217,7 @@ impl Direction for DefaultLayout { op_direction: Option, idx: usize, count: Option, + layout_options: Option, ) -> usize { match self { Self::BSP => { @@ -204,7 +236,7 @@ impl Direction for DefaultLayout { 1 => unreachable!(), _ => 0, }, - Self::Grid => grid_neighbor(op_direction, idx, count), + Self::Grid => grid_neighbor(op_direction, idx, count, layout_options), Self::Scrolling => idx - 1, } } @@ -214,6 +246,7 @@ impl Direction for DefaultLayout { op_direction: Option, idx: usize, count: Option, + layout_options: Option, ) -> usize { match self { Self::BSP | Self::Columns | Self::HorizontalStack => idx + 1, @@ -225,7 +258,7 @@ impl Direction for DefaultLayout { 0 => 2, _ => unreachable!(), }, - Self::Grid => grid_neighbor(op_direction, idx, count), + Self::Grid => grid_neighbor(op_direction, idx, count, layout_options), Self::Scrolling => idx + 1, } } @@ -256,21 +289,32 @@ struct GridTouchingEdges { clippy::cast_precision_loss, clippy::cast_sign_loss )] -fn get_grid_item(idx: usize, count: usize) -> GridItem { - let num_cols = (count as f32).sqrt().ceil() as usize; +fn get_grid_item(idx: usize, count: usize, layout_options: Option) -> GridItem { + let row_constraint = layout_options.and_then(|o| o.grid.map(|g| g.rows)); + let num_cols = if let Some(rows) = row_constraint { + ((count as f32) / (rows as f32)).ceil() as i32 + } else { + (count as f32).sqrt().ceil() as i32 + }; + let mut iter = 0; for col in 0..num_cols { - let remaining_windows = count - iter; + let remaining_windows = (count - iter) as i32; let remaining_columns = num_cols - col; - let num_rows_in_this_col = remaining_windows / remaining_columns; + + let num_rows_in_this_col = if let Some(rows) = row_constraint { + (remaining_windows / remaining_columns).min(rows as i32) + } else { + remaining_windows / remaining_columns + }; for row in 0..num_rows_in_this_col { if iter == idx { return GridItem { state: GridItemState::Valid, - row: row + 1, - num_rows: num_rows_in_this_col, + row: (row + 1) as usize, + num_rows: num_rows_in_this_col as usize, touching_edges: GridTouchingEdges { left: col == 0, right: col == num_cols - 1, @@ -297,8 +341,13 @@ fn get_grid_item(idx: usize, count: usize) -> GridItem { } } -fn is_grid_edge(op_direction: OperationDirection, idx: usize, count: usize) -> bool { - let item = get_grid_item(idx, count); +fn is_grid_edge( + op_direction: OperationDirection, + idx: usize, + count: usize, + layout_options: Option, +) -> bool { + let item = get_grid_item(idx, count, layout_options); match item.state { GridItemState::Invalid => false, @@ -315,6 +364,7 @@ fn grid_neighbor( op_direction: Option, idx: usize, count: Option, + layout_options: Option, ) -> usize { let Some(op_direction) = op_direction else { return 0; @@ -324,11 +374,11 @@ fn grid_neighbor( return 0; }; - let item = get_grid_item(idx, count); + let item = get_grid_item(idx, count, layout_options); match op_direction { OperationDirection::Left => { - let item_from_prev_col = get_grid_item(idx - item.row, count); + let item_from_prev_col = get_grid_item(idx - item.row, count, layout_options); if item.touching_edges.up && item.num_rows != item_from_prev_col.num_rows { return idx - (item.num_rows - 1); diff --git a/komorebi/src/core/operation_direction.rs b/komorebi/src/core/operation_direction.rs index bb9d68eb..f29ca763 100644 --- a/komorebi/src/core/operation_direction.rs +++ b/komorebi/src/core/operation_direction.rs @@ -1,4 +1,5 @@ use crate::core::arrangement::Axis; +use crate::core::default_layout::LayoutOptions; use crate::core::direction::Direction; use clap::ValueEnum; use serde::Deserialize; @@ -53,7 +54,8 @@ impl OperationDirection { layout_flip: Option, idx: usize, len: NonZeroUsize, + layout_options: Option, ) -> Option { - layout.index_in_direction(self.flip(layout_flip), idx, len.get()) + layout.index_in_direction(self.flip(layout_flip), idx, len.get(), layout_options) } } diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 71b80552..19200fce 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -1458,6 +1458,7 @@ impl WindowManager { columns: count.into(), center_focused_column: Default::default(), }), + grid: None, }, }; diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 31538ac7..e38f2d2c 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -1082,6 +1082,7 @@ impl WindowManager { workspace.layout_flip, workspace.focused_container_idx(), len, + workspace.layout_options, ) .is_some(); @@ -1486,6 +1487,7 @@ impl WindowManager { workspace.layout_flip, focused_idx, len, + workspace.layout_options, ) .is_some() { diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index d1a6432f..9fc537bb 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -225,6 +225,7 @@ impl Workspace { self.layout_flip, self.focused_container_idx(), len, + self.layout_options, ) } diff --git a/schema.json b/schema.json index 43bbcf0a..442b7857 100644 --- a/schema.json +++ b/schema.json @@ -1452,6 +1452,21 @@ "description": "Layout-specific options (default: None)", "type": "object", "properties": { + "grid": { + "description": "Options related to the Grid layout", + "type": "object", + "required": [ + "rows" + ], + "properties": { + "rows": { + "description": "Maximum number of rows per grid column", + "type": "integer", + "format": "uint", + "minimum": 0.0 + } + } + }, "scrolling": { "description": "Options related to the Scrolling layout", "type": "object",