feat(cli): new layout-ratios command

This commit is largely lift and shift of LGUG2Z/komorebi@c165172:

The implementation adds a new layout-ratio CLI command to komorebi that
allows users to dynamically set column and row ratios for layouts at
runtime via komorebic layout-ratio --columns 0.3 0.4 --rows 0.5.

A public validate_ratios function was added to komorebi-layouts that
clamps ratio values between 0.1 and 0.9, truncates when the cumulative
sum would reach or exceed 1.0, and limits to a maximum of 5 ratios. This
function is shared between config file deserialization and the new CLI
command, ensuring consistent validation behavior.

The SocketMessage enum in komorebi/src/core/mod.rs has a new
LayoutRatios variant. The handler in process_command.rs uses the shared
validate_ratios function to process the ratios before applying them to
the focused workspace's layout options.

When the CLI command is called without any arguments, it prints a
helpful message instead of returning an error.
This commit is contained in:
LGUG2Z committed 2026-02-13 17:03:55 -08:00
1 parent 0e120a7b35
commit 9087d68742
8 files changed
+155 -79

No files matched your search

Generated
+1 -1
View File
@@ -3091,7 +3091,7 @@ dependencies = [
[[package]]
name = "komorebi-layouts"
version = "0.1.40"
source = "git+https://github.com/LGUG2Z/komorebi?rev=98122bd9d4aabbff927a29228acf6c8a0246c509#98122bd9d4aabbff927a29228acf6c8a0246c509"
source = "git+https://github.com/LGUG2Z/komorebi?rev=c165172b5a636f383739a99dc51a35fe4a3a77ef#c165172b5a636f383739a99dc51a35fe4a3a77ef"
dependencies = [
"clap",
"color-eyre",
+1 -1
View File
@@ -14,7 +14,7 @@ version = "0.1.0"
[workspace.dependencies]
komorebi-themes = { git = "https://github.com/LGUG2Z/komorebi", rev = "98122bd9d4aabbff927a29228acf6c8a0246c509" }
komorebi-layouts = { git = "https://github.com/LGUG2Z/komorebi", rev = "98122bd9d4aabbff927a29228acf6c8a0246c509", features = [
komorebi-layouts = { git = "https://github.com/LGUG2Z/komorebi", rev = "c165172b5a636f383739a99dc51a35fe4a3a77ef", features = [
"darwin",
"schemars",
] }
+2
View File
@@ -30,6 +30,7 @@ pub use komorebi_layouts::OperationDirection;
pub use komorebi_layouts::Rect;
pub use komorebi_layouts::ScrollingLayoutOptions;
pub use komorebi_layouts::Sizing;
pub use komorebi_layouts::validate_ratios;
// Keep local modules that aren't in komorebi-layouts
pub mod animation;
@@ -315,6 +316,7 @@ pub enum SocketMessage {
ChangeLayout(DefaultLayout),
CycleLayout(CycleDirection),
ScrollingLayoutColumns(NonZeroUsize),
LayoutRatios(Option<Vec<f32>>, Option<Vec<f32>>),
// ChangeLayoutCustom(#[serde_as(as = "ResolvedPathBuf")] PathBuf),
FlipLayout(Axis),
ToggleWorkspaceWindowContainerBehaviour,
+29
View File
@@ -0,0 +1,29 @@
use crate::core::Rect;
use crate::core_graphics::CoreGraphicsApi;
use crate::window::WindowBounds;
use objc2_core_foundation::CGRect;
/// Mac-specific extensions for the Rect type from komorebi-layouts
pub trait RectExt {
/// Check if this rectangle contains another rectangle
fn contains(&self, other: &Rect) -> bool;
}
impl RectExt for Rect {
fn contains(&self, other: &Rect) -> bool {
let self_cg: CGRect = self.into();
let other_cg: CGRect = other.into();
CoreGraphicsApi::contains_rect(other_cg, self_cg)
}
}
impl From<WindowBounds> for Rect {
fn from(value: WindowBounds) -> Self {
Self {
left: value.x as i32,
top: value.y as i32,
right: value.width as i32,
bottom: value.height as i32,
}
}
}
+24
View File
@@ -36,6 +36,7 @@ use crate::core::config_generation::IdWithIdentifier;
use crate::core::config_generation::MatchingRule;
use crate::core::config_generation::MatchingStrategy;
use crate::core::config_generation::WorkspaceMatchingRule;
use crate::core_graphics::CoreGraphicsApi;
use crate::current_space_id;
use crate::macos_api::MacosApi;
@@ -1521,6 +1522,29 @@ impl WindowManager {
focused_workspace.layout_options = Some(options);
self.update_focused_workspace(false, false)?;
}
SocketMessage::LayoutRatios(ref columns, ref rows) => {
use crate::core::validate_ratios;
let focused_workspace = self.focused_workspace_mut()?;
let mut options = focused_workspace.layout_options.unwrap_or(LayoutOptions {
scrolling: None,
grid: None,
column_ratios: None,
row_ratios: None,
});
if let Some(cols) = columns {
options.column_ratios = Some(validate_ratios(cols));
}
if let Some(rws) = rows {
options.row_ratios = Some(validate_ratios(rws));
}
focused_workspace.layout_options = Some(options);
self.update_focused_workspace(false, false)?;
}
SocketMessage::NamedWorkspaceLayoutRule(ref workspace, at_container_count, layout) => {
if let Some((monitor_idx, workspace_idx)) =
self.monitor_workspace_index_by_name(workspace)
+21
View File
@@ -621,6 +621,16 @@ struct ScrollingLayoutColumns {
count: NonZeroUsize,
}
#[derive(Parser)]
struct LayoutRatios {
/// Column width ratios (space-separated values between 0.1 and 0.9)
#[clap(short, long, num_args = 1..)]
columns: Option<Vec<f32>>,
/// Row height ratios (space-separated values between 0.1 and 0.9)
#[clap(short, long, num_args = 1..)]
rows: Option<Vec<f32>>,
}
#[derive(Parser)]
struct Border {
#[clap(value_enum)]
@@ -969,6 +979,8 @@ enum SubCommand {
/// Set the number of visible columns for the Scrolling layout on the focused workspace
#[clap(arg_required_else_help = true)]
ScrollingLayoutColumns(ScrollingLayoutColumns),
/// Set the layout column and row ratios for the focused workspace
LayoutRatios(LayoutRatios),
// /// Load a custom layout from file for the focused workspace
// #[clap(hide = true)]
// #[clap(arg_required_else_help = true)]
@@ -2337,6 +2349,15 @@ exit 1
SubCommand::ScrollingLayoutColumns(args) => {
send_message(&SocketMessage::ScrollingLayoutColumns(args.count))?;
}
SubCommand::LayoutRatios(args) => {
if args.columns.is_none() && args.rows.is_none() {
println!(
"No ratios provided, nothing to change. Use --columns or --rows to specify ratios."
);
} else {
send_message(&SocketMessage::LayoutRatios(args.columns, args.rows))?;
}
}
SubCommand::ClearWorkspaceLayoutRules(args) => {
send_message(&SocketMessage::ClearWorkspaceLayoutRules(
args.monitor,
+22 -22
View File
@@ -976,6 +976,17 @@ in
);
default = null;
};
accent = lib.mkOption {
type = (
lib.types.nullOr (
lib.types.oneOf [
(lib.types.nullOr catppuccinValue)
(lib.types.nullOr base16Value)
]
)
);
default = null;
};
colours = lib.mkOption {
type = (
lib.types.nullOr (
@@ -1051,28 +1062,6 @@ in
);
default = null;
};
auto_select_fill = lib.mkOption {
type = (
lib.types.nullOr (
lib.types.oneOf [
(lib.types.nullOr catppuccinValue)
(lib.types.nullOr base16Value)
]
)
);
default = null;
};
accent = lib.mkOption {
type = (
lib.types.nullOr (
lib.types.oneOf [
(lib.types.nullOr catppuccinValue)
(lib.types.nullOr base16Value)
]
)
);
default = null;
};
name = lib.mkOption {
type = (
lib.types.nullOr (
@@ -1359,6 +1348,17 @@ in
);
default = null;
};
auto_select_fill = lib.mkOption {
type = (
lib.types.nullOr (
lib.types.oneOf [
(lib.types.nullOr catppuccinValue)
(lib.types.nullOr base16Value)
]
)
);
default = null;
};
};
}
)
+55 -55
View File
@@ -788,17 +788,6 @@ in
]
);
};
floating_border = lib.mkOption {
type = (
lib.types.nullOr (
lib.types.oneOf [
(lib.types.nullOr catppuccinValue)
(lib.types.nullOr base16Value)
]
)
);
default = null;
};
name = lib.mkOption {
type = (
lib.types.nullOr (
@@ -1107,50 +1096,6 @@ in
);
default = null;
};
monocle_border = lib.mkOption {
type = (
lib.types.nullOr (
lib.types.oneOf [
(lib.types.nullOr catppuccinValue)
(lib.types.nullOr base16Value)
]
)
);
default = null;
};
unfocused_border = lib.mkOption {
type = (
lib.types.nullOr (
lib.types.oneOf [
(lib.types.nullOr catppuccinValue)
(lib.types.nullOr base16Value)
]
)
);
default = null;
};
unfocused_locked_border = lib.mkOption {
type = (
lib.types.nullOr (
lib.types.oneOf [
(lib.types.nullOr catppuccinValue)
(lib.types.nullOr base16Value)
]
)
);
default = null;
};
bar_accent = lib.mkOption {
type = (
lib.types.nullOr (
lib.types.oneOf [
(lib.types.nullOr catppuccinValue)
(lib.types.nullOr base16Value)
]
)
);
default = null;
};
colours = lib.mkOption {
type = (
lib.types.nullOr (
@@ -1226,6 +1171,61 @@ in
);
default = null;
};
floating_border = lib.mkOption {
type = (
lib.types.nullOr (
lib.types.oneOf [
(lib.types.nullOr catppuccinValue)
(lib.types.nullOr base16Value)
]
)
);
default = null;
};
monocle_border = lib.mkOption {
type = (
lib.types.nullOr (
lib.types.oneOf [
(lib.types.nullOr catppuccinValue)
(lib.types.nullOr base16Value)
]
)
);
default = null;
};
bar_accent = lib.mkOption {
type = (
lib.types.nullOr (
lib.types.oneOf [
(lib.types.nullOr catppuccinValue)
(lib.types.nullOr base16Value)
]
)
);
default = null;
};
unfocused_locked_border = lib.mkOption {
type = (
lib.types.nullOr (
lib.types.oneOf [
(lib.types.nullOr catppuccinValue)
(lib.types.nullOr base16Value)
]
)
);
default = null;
};
unfocused_border = lib.mkOption {
type = (
lib.types.nullOr (
lib.types.oneOf [
(lib.types.nullOr catppuccinValue)
(lib.types.nullOr base16Value)
]
)
);
default = null;
};
};
}
)