mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-20 07:31:39 +01:00
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.
This commit is contained in:
1 parent
c255772897
commit
f848c1d8f0
8 files changed
+121
-29
No files matched your search
@@ -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;
|
||||
|
||||
@@ -29,6 +29,8 @@ pub enum DefaultLayout {
|
||||
pub struct LayoutOptions {
|
||||
/// Options related to the Scrolling layout
|
||||
pub scrolling: Option<ScrollingLayoutOptions>,
|
||||
/// Options related to the Grid layout
|
||||
pub grid: Option<GridLayoutOptions>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq)]
|
||||
@@ -40,6 +42,13 @@ pub struct ScrollingLayoutOptions {
|
||||
pub center_focused_column: Option<bool>,
|
||||
}
|
||||
|
||||
#[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 {
|
||||
|
||||
@@ -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<LayoutOptions>,
|
||||
) -> Option<usize>;
|
||||
|
||||
fn is_valid_direction(
|
||||
@@ -14,30 +16,35 @@ pub trait Direction {
|
||||
op_direction: OperationDirection,
|
||||
idx: usize,
|
||||
count: usize,
|
||||
layout_options: Option<LayoutOptions>,
|
||||
) -> bool;
|
||||
fn up_index(
|
||||
&self,
|
||||
op_direction: Option<OperationDirection>,
|
||||
idx: usize,
|
||||
count: Option<usize>,
|
||||
layout_options: Option<LayoutOptions>,
|
||||
) -> usize;
|
||||
fn down_index(
|
||||
&self,
|
||||
op_direction: Option<OperationDirection>,
|
||||
idx: usize,
|
||||
count: Option<usize>,
|
||||
layout_options: Option<LayoutOptions>,
|
||||
) -> usize;
|
||||
fn left_index(
|
||||
&self,
|
||||
op_direction: Option<OperationDirection>,
|
||||
idx: usize,
|
||||
count: Option<usize>,
|
||||
layout_options: Option<LayoutOptions>,
|
||||
) -> usize;
|
||||
fn right_index(
|
||||
&self,
|
||||
op_direction: Option<OperationDirection>,
|
||||
idx: usize,
|
||||
count: Option<usize>,
|
||||
layout_options: Option<LayoutOptions>,
|
||||
) -> usize;
|
||||
}
|
||||
|
||||
@@ -47,32 +54,53 @@ impl Direction for DefaultLayout {
|
||||
op_direction: OperationDirection,
|
||||
idx: usize,
|
||||
count: usize,
|
||||
layout_options: Option<LayoutOptions>,
|
||||
) -> Option<usize> {
|
||||
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<LayoutOptions>,
|
||||
) -> 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<OperationDirection>,
|
||||
idx: usize,
|
||||
count: Option<usize>,
|
||||
layout_options: Option<LayoutOptions>,
|
||||
) -> 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<OperationDirection>,
|
||||
idx: usize,
|
||||
count: Option<usize>,
|
||||
layout_options: Option<LayoutOptions>,
|
||||
) -> 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<OperationDirection>,
|
||||
idx: usize,
|
||||
count: Option<usize>,
|
||||
layout_options: Option<LayoutOptions>,
|
||||
) -> 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<OperationDirection>,
|
||||
idx: usize,
|
||||
count: Option<usize>,
|
||||
layout_options: Option<LayoutOptions>,
|
||||
) -> 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<LayoutOptions>) -> 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<LayoutOptions>,
|
||||
) -> 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<OperationDirection>,
|
||||
idx: usize,
|
||||
count: Option<usize>,
|
||||
layout_options: Option<LayoutOptions>,
|
||||
) -> 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);
|
||||
|
||||
@@ -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<Axis>,
|
||||
idx: usize,
|
||||
len: NonZeroUsize,
|
||||
layout_options: Option<LayoutOptions>,
|
||||
) -> Option<usize> {
|
||||
layout.index_in_direction(self.flip(layout_flip), idx, len.get())
|
||||
layout.index_in_direction(self.flip(layout_flip), idx, len.get(), layout_options)
|
||||
}
|
||||
}
|
||||
@@ -1458,6 +1458,7 @@ impl WindowManager {
|
||||
columns: count.into(),
|
||||
center_focused_column: Default::default(),
|
||||
}),
|
||||
grid: None,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -225,6 +225,7 @@ impl Workspace {
|
||||
self.layout_flip,
|
||||
self.focused_container_idx(),
|
||||
len,
|
||||
self.layout_options,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+15
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user