feat(wm): add global layout_defaults for per-layout default options

This commit migrates this feature as it was implemented in
LGUG2Z/komorebi@24c0ce0b1d by @CtByte.
This commit is contained in:
LGUG2Z committed 2026-05-03 11:04:20 -07:00
1 parent 7e54fed8de
commit 2b4760db1a
7 files changed
+143 -38

No files matched your search

Generated
+2 -2
View File
@@ -3159,7 +3159,7 @@ dependencies = [
[[package]]
name = "komorebi-layouts"
version = "0.1.41"
source = "git+https://github.com/LGUG2Z/komorebi?rev=d6b17bbc7c402c61fce765e2752040a6698c11a5#d6b17bbc7c402c61fce765e2752040a6698c11a5"
source = "git+https://github.com/LGUG2Z/komorebi?rev=24c0ce0b1df1b5d7d62c682a7e28b32980293883#24c0ce0b1df1b5d7d62c682a7e28b32980293883"
dependencies = [
"clap",
"color-eyre",
@@ -3175,7 +3175,7 @@ dependencies = [
[[package]]
name = "komorebi-themes"
version = "0.1.41"
source = "git+https://github.com/LGUG2Z/komorebi?rev=d6b17bbc7c402c61fce765e2752040a6698c11a5#d6b17bbc7c402c61fce765e2752040a6698c11a5"
source = "git+https://github.com/LGUG2Z/komorebi?rev=24c0ce0b1df1b5d7d62c682a7e28b32980293883#24c0ce0b1df1b5d7d62c682a7e28b32980293883"
dependencies = [
"base16-egui-themes",
"catppuccin-egui",
+2 -2
View File
@@ -13,8 +13,8 @@ members = [
version = "0.1.0"
[workspace.dependencies]
komorebi-themes = { git = "https://github.com/LGUG2Z/komorebi", rev = "d6b17bbc7c402c61fce765e2752040a6698c11a5" }
komorebi-layouts = { git = "https://github.com/LGUG2Z/komorebi", rev = "d6b17bbc7c402c61fce765e2752040a6698c11a5", features = [
komorebi-themes = { git = "https://github.com/LGUG2Z/komorebi", rev = "24c0ce0b1df1b5d7d62c682a7e28b32980293883" }
komorebi-layouts = { git = "https://github.com/LGUG2Z/komorebi", rev = "24c0ce0b1df1b5d7d62c682a7e28b32980293883", features = [
"darwin",
"schemars",
] }
+1
View File
@@ -22,6 +22,7 @@ pub use komorebi_layouts::DefaultLayout;
pub use komorebi_layouts::Direction;
pub use komorebi_layouts::GridLayoutOptions;
pub use komorebi_layouts::Layout;
pub use komorebi_layouts::LayoutDefaultEntry;
pub use komorebi_layouts::LayoutOptions;
pub use komorebi_layouts::MAX_RATIO;
pub use komorebi_layouts::MAX_RATIOS;
+4
View File
@@ -2,6 +2,8 @@
use crate::accessibility::error::AccessibilityError;
use crate::core::ApplicationIdentifier;
use crate::core::DefaultLayout;
use crate::core::LayoutDefaultEntry;
use crate::core::SocketMessage;
use crate::core::SubscribeOptions;
use crate::core::config_generation::IdWithIdentifier;
@@ -146,6 +148,8 @@ lazy_static! {
]));
static ref TITLELESS_APPLICATIONS: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
static ref UNMANAGED_WINDOW_IDS: Arc<Mutex<Vec<u32>>> = Arc::new(Mutex::new(vec![]));
pub static ref LAYOUT_DEFAULTS: Arc<Mutex<HashMap<DefaultLayout, LayoutDefaultEntry>>> =
Arc::new(Mutex::new(HashMap::new()));
}
pub static DEFAULT_WORKSPACE_PADDING: AtomicI32 = AtomicI32::new(5);
+1
View File
@@ -122,6 +122,7 @@ impl From<&WindowManager> for State {
layout_options: workspace.layout_options,
layout_rules: workspace.layout_rules.clone(),
layout_options_rules: workspace.layout_options_rules.clone(),
layout_defaults_cache: workspace.layout_defaults_cache.clone(),
layout_flip: workspace.layout_flip,
workspace_padding: workspace.workspace_padding,
container_padding: workspace.container_padding,
+31 -4
View File
@@ -8,6 +8,7 @@ use crate::DISPLAY_INDEX_PREFERENCES;
use crate::FLOATING_APPLICATIONS;
use crate::FLOATING_WINDOW_TOGGLE_ASPECT_RATIO;
use crate::IGNORE_IDENTIFIERS;
use crate::LAYOUT_DEFAULTS;
use crate::MANAGE_IDENTIFIERS;
use crate::REGEX_IDENTIFIERS;
use crate::TABBED_APPLICATIONS;
@@ -29,6 +30,7 @@ use crate::core::CrossBoundaryBehaviour;
use crate::core::DefaultLayout;
use crate::core::FloatingLayerBehaviour;
use crate::core::Layout;
use crate::core::LayoutDefaultEntry;
use crate::core::LayoutOptions;
use crate::core::MoveBehaviour;
use crate::core::OperationBehaviour;
@@ -542,6 +544,11 @@ pub struct StaticConfig {
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "schemars", schemars(extend("default" = DEFAULT_CONTAINER_PADDING)))]
pub default_container_padding: Option<i32>,
/// Per-layout default options and rules, keyed by layout name.
/// Applied as fallback when a workspace does not define its own layout_options or layout_options_rules.
/// If a workspace defines either setting, all global defaults for that layout are completely replaced.
#[serde(skip_serializing_if = "Option::is_none")]
pub layout_defaults: Option<HashMap<DefaultLayout, LayoutDefaultEntry>>,
/// Monitor and workspace configurations
#[serde(skip_serializing_if = "Option::is_none")]
pub monitors: Option<Vec<MonitorConfig>>,
@@ -762,6 +769,14 @@ impl From<&WindowManager> for StaticConfig {
default_container_padding: Option::from(
DEFAULT_CONTAINER_PADDING.load(Ordering::SeqCst),
),
layout_defaults: {
let guard = LAYOUT_DEFAULTS.lock();
if guard.is_empty() {
None
} else {
Some(guard.clone())
}
},
monitors: Option::from(monitors),
// window_hiding_behaviour: Option::from(*HIDING_BEHAVIOUR.lock()),
global_work_area_offset: value.work_area_offset,
@@ -871,6 +886,12 @@ impl StaticConfig {
DEFAULT_WORKSPACE_PADDING.store(workspace, Ordering::SeqCst);
}
if let Some(defaults) = &self.layout_defaults {
*LAYOUT_DEFAULTS.lock() = defaults.clone();
} else {
LAYOUT_DEFAULTS.lock().clear();
}
if let Some(border_width) = self.border_width {
border_manager::BORDER_WIDTH.store(border_width, Ordering::SeqCst);
}
@@ -1279,7 +1300,7 @@ impl StaticConfig {
monitor.update_workspaces_globals(offset);
for (j, ws) in monitor.workspaces_mut().iter_mut().enumerate() {
if let Some(workspace_config) = monitor_config.workspaces.get_mut(j) {
ws.load_static_config(workspace_config)?;
ws.load_static_config(workspace_config, value.layout_defaults.as_ref())?;
}
}
@@ -1354,7 +1375,10 @@ impl StaticConfig {
for (j, ws) in m.workspaces_mut().iter_mut().enumerate() {
if let Some(workspace_config) = monitor_config.workspaces.get(j) {
ws.load_static_config(workspace_config)?;
ws.load_static_config(
workspace_config,
value.layout_defaults.as_ref(),
)?;
}
}
@@ -1441,7 +1465,7 @@ impl StaticConfig {
for (j, ws) in monitor.workspaces_mut().iter_mut().enumerate() {
if let Some(workspace_config) = monitor_config.workspaces.get(j) {
ws.load_static_config(workspace_config)?;
ws.load_static_config(workspace_config, value.layout_defaults.as_ref())?;
}
}
@@ -1516,7 +1540,10 @@ impl StaticConfig {
for (j, ws) in m.workspaces_mut().iter_mut().enumerate() {
if let Some(workspace_config) = monitor_config.workspaces.get(j) {
ws.load_static_config(workspace_config)?;
ws.load_static_config(
workspace_config,
value.layout_defaults.as_ref(),
)?;
}
}
+102 -30
View File
@@ -5,6 +5,7 @@ use crate::core::CycleDirection;
use crate::core::DefaultLayout;
use crate::core::FloatingLayerBehaviour;
use crate::core::Layout;
use crate::core::LayoutDefaultEntry;
use crate::core::LayoutOptions;
use crate::core::OperationDirection;
use crate::core::Rect;
@@ -55,6 +56,10 @@ pub struct Workspace {
/// Sorted by threshold ascending at load time.
#[serde(default)]
pub layout_options_rules: Vec<(usize, LayoutOptions)>,
/// Cached per-layout defaults from the global `layout_defaults` config setting.
/// Pre-sorted at config load time; used as fallback when workspace has no overrides.
#[serde(skip)]
pub(crate) layout_defaults_cache: HashMap<DefaultLayout, CachedLayoutDefault>,
pub layout_flip: Option<Axis>,
pub workspace_padding: Option<i32>,
pub container_padding: Option<i32>,
@@ -120,6 +125,7 @@ impl Default for Workspace {
layout_options: None,
layout_rules: vec![],
layout_options_rules: vec![],
layout_defaults_cache: HashMap::new(),
globals: Default::default(),
layer: Default::default(),
tile: true,
@@ -160,8 +166,49 @@ pub struct WorkspaceGlobals {
pub window_hiding_position: WindowHidingPosition,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
/// Cached per-layout default options (pre-sorted rules) derived from the global `layout_defaults`.
pub(crate) struct CachedLayoutDefault {
pub layout_options: Option<LayoutOptions>,
/// Threshold-based rules, sorted by threshold ascending at load time
pub layout_options_rules: Vec<(usize, LayoutOptions)>,
}
/// Convert an optional HashMap of threshold-based layout options rules into a Vec sorted by
/// threshold ascending.
fn sorted_layout_options_rules(
rules: Option<&HashMap<usize, LayoutOptions>>,
) -> Vec<(usize, LayoutOptions)> {
match rules {
Some(rules) => {
let mut sorted: Vec<(usize, LayoutOptions)> =
rules.iter().map(|(t, o)| (*t, *o)).collect();
sorted.sort_by_key(|(t, _)| *t);
sorted
}
None => vec![],
}
}
/// Find the highest matching threshold rule for the given container count.
/// Rules must be sorted by threshold ascending.
fn resolve_threshold_match(
rules: &[(usize, LayoutOptions)],
container_count: usize,
) -> Option<LayoutOptions> {
rules
.iter()
.rev()
.find(|(threshold, _)| container_count >= *threshold)
.map(|(_, opts)| *opts)
}
impl Workspace {
pub fn load_static_config(&mut self, config: &WorkspaceConfig) -> eyre::Result<()> {
pub fn load_static_config(
&mut self,
config: &WorkspaceConfig,
layout_defaults: Option<&HashMap<DefaultLayout, LayoutDefaultEntry>>,
) -> eyre::Result<()> {
self.name = Option::from(config.name.clone());
self.container_padding = config.container_padding;
@@ -229,14 +276,8 @@ impl Workspace {
self.layout_options = config.layout_options;
// Load threshold-based layout options rules, sorted by threshold ascending
self.layout_options_rules = if let Some(rules) = &config.layout_options_rules {
let mut sorted_rules: Vec<(usize, LayoutOptions)> =
rules.iter().map(|(t, o)| (*t, *o)).collect();
sorted_rules.sort_by_key(|(t, _)| *t);
sorted_rules
} else {
vec![]
};
self.layout_options_rules =
sorted_layout_options_rules(config.layout_options_rules.as_ref());
tracing::debug!(
"Workspace '{}' loaded layout_options: {:?}, layout_options_rules: {} entries",
@@ -245,11 +286,62 @@ impl Workspace {
self.layout_options_rules.len(),
);
// Cache per-layout defaults from global layout_defaults, pre-sorting rules
self.layout_defaults_cache = if let Some(defaults) = layout_defaults {
defaults
.iter()
.map(|(layout, entry)| {
(
*layout,
CachedLayoutDefault {
layout_options: entry.layout_options,
layout_options_rules: sorted_layout_options_rules(
entry.layout_options_rules.as_ref(),
),
},
)
})
.collect()
} else {
HashMap::new()
};
self.workspace_config = Some(config.clone());
Ok(())
}
/// Compute effective layout options using the complete-replacement cascade:
///
/// If the workspace defines EITHER `layout_options` OR `layout_options_rules`,
/// it completely replaces the global `layout_defaults` for this layout.
/// Global defaults are only used when the workspace has NEITHER setting.
///
/// Within the effective source (workspace or global):
/// 1. Try threshold match from rules (highest matching threshold wins)
/// 2. If a rule matches -> use it (full replacement of base)
/// 3. Else -> use the base `layout_options`
fn effective_layout_options(&self) -> Option<LayoutOptions> {
let container_count = self.containers().len();
let has_workspace_overrides =
self.layout_options.is_some() || !self.layout_options_rules.is_empty();
let (effective_base, effective_rules): (Option<LayoutOptions>, &[(usize, LayoutOptions)]) =
if has_workspace_overrides {
(self.layout_options, &self.layout_options_rules)
} else {
match &self.layout {
Layout::Default(dl) => match self.layout_defaults_cache.get(dl) {
Some(entry) => (entry.layout_options, &entry.layout_options_rules),
None => (None, &[]),
},
}
};
resolve_threshold_match(effective_rules, container_count).or(effective_base)
}
#[tracing::instrument(skip(self))]
pub fn focus_container(&mut self, idx: usize) {
tracing::info!("focusing container");
@@ -1281,27 +1373,7 @@ impl Workspace {
window.set_position(&adjusted_work_area)?;
};
} else if !self.containers().is_empty() {
// Compute effective layout options:
// 1. If layout_options_rules has a matching threshold (container_count >= threshold),
// use the highest matching threshold's options (full replacement)
// 2. Otherwise, use the workspace base layout_options
let effective_layout_options = if !self.layout_options_rules.is_empty() {
let container_count = self.containers().len();
let mut matched = None;
for (threshold, opts) in &self.layout_options_rules {
if container_count >= *threshold {
matched = Some(*opts);
}
}
// If a rule matched, use it entirely (replaces base layout_options)
if matched.is_some() {
matched
} else {
self.layout_options
}
} else {
self.layout_options
};
let effective_layout_options = self.effective_layout_options();
tracing::debug!(
"Workspace '{}' update() - effective_layout_options: {:?} (base: {:?}, rules: {})",