From 15e6498a9ea955a6c4664d8f5dc93a03429eb931 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 30 Oct 2026 23:26:19 -0700 Subject: [PATCH] refactor(windows): use the placement types of komorebi-config core re-exports WindowPlacement, PlacementTarget, PlacementMatchingRules and InitialWindowPlacementRules from komorebi-config and loses the local definitions and impls (about 230 lines) with the imports they alone needed. PlacementTarget is now the crate's copy (the KDE one): it parses the same values (a name, a number, or numeric text) and only the wording of its error for a bad value differs; the schema text Windows checked in for it is part of the Windows view of komorebi-config (changes.rs), which printing already uses. Verified: Windows cargo clippy --workspace --locked --all-targets -D warnings: clean (tier 2). Not verified: Windows unit tests (cannot run). --- komorebi/src/core/mod.rs | 231 +-------------------------------------- 1 file changed, 4 insertions(+), 227 deletions(-) diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index 2ae7849f..020796f3 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -2,11 +2,8 @@ #![allow(clippy::missing_errors_doc, clippy::use_self, clippy::doc_markdown)] #![allow(deprecated)] // allow deprecated variants like HidingBehaviour::Hide to be used in derive macros -use clap::ValueEnum; use serde::Deserialize; use serde::Serialize; -use strum::Display; -use strum::EnumString; // Re-export everything from komorebi-layouts pub use komorebi_layouts::Arrangement; @@ -44,6 +41,10 @@ pub use komorebi_config::behaviour::FloatingLayerBehaviour; pub use komorebi_config::behaviour::Placement; pub use komorebi_config::behaviour::WindowContainerBehaviour; pub use komorebi_config::behaviour::WindowHandlingBehaviour; +pub use komorebi_config::placement::InitialWindowPlacementRules; +pub use komorebi_config::placement::PlacementMatchingRules; +pub use komorebi_config::placement::PlacementTarget; +pub use komorebi_config::placement::WindowPlacement; pub use komorebi_protocol::types::ApplicationIdentifier; pub use komorebi_protocol::types::BorderImplementation; pub use komorebi_protocol::types::BorderStyle; @@ -96,232 +97,8 @@ pub struct WindowManagementBehaviour { pub float_rule_placement: Placement, } -#[derive( - Clone, - Copy, - Debug, - Default, - Serialize, - Deserialize, - Display, - EnumString, - ValueEnum, - PartialEq, - Eq, - Hash, -)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -/// Placement strategy for new windows in a workspace -pub enum WindowPlacement { - /// Place the new window at the primary (largest) container position - Primary, - /// Place the new window at the secondary container position - Secondary, - /// Place the new window before the currently focused container - BeforeFocused, - /// Place the new window after the currently focused container (default behaviour) - #[default] - AfterFocused, - /// Place the new window at the end of the container list - Last, -} - -/// A target position for window placement, used as a key in `InitialWindowPlacementRules::Rules`. -/// -/// Can be either a `WindowPlacement` variant name (e.g. `"Primary"`, `"Last"`) -/// or a 1-based container index (e.g. `"1"`, `"3"`). -/// -/// NOTE: Integer indices are 1-based in the config for user-friendliness. -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub enum PlacementTarget { - /// A named placement strategy - Placement(WindowPlacement), - /// A 1-based container index - Index(usize), -} - -impl std::fmt::Display for PlacementTarget { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - PlacementTarget::Placement(p) => write!(f, "{p}"), - PlacementTarget::Index(i) => write!(f, "{i}"), - } - } -} - -impl std::str::FromStr for PlacementTarget { - type Err = String; - - fn from_str(s: &str) -> Result { - // Try parsing as a WindowPlacement variant first - if let Ok(placement) = ::from_str(s) { - return Ok(PlacementTarget::Placement(placement)); - } - // Then try parsing as a 1-based index - if let Ok(idx) = s.parse::() { - return Ok(PlacementTarget::Index(idx)); - } - Err(format!( - "'{s}' is not a valid WindowPlacement variant or integer" - )) - } -} - -impl PartialOrd for PlacementTarget { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} - // Written by hand because the (de)serialization is: a `WindowPlacement` name or a container index, // as a string or as an integer, never the externally tagged object a derive would describe -#[cfg(feature = "schemars")] -impl schemars::JsonSchema for PlacementTarget { - fn schema_name() -> std::borrow::Cow<'static, str> { - "PlacementTarget".into() - } - - fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema { - schemars::json_schema!({ - "description": "A target position for window placement, used as a key in `InitialWindowPlacementRules::Rules`. - -Can be either a `WindowPlacement` variant name (e.g. `\"Primary\"`, `\"Last\"`) -or a 1-based container index (e.g. `\"1\"`, `\"3\"`, or the integer `3`). - -NOTE: Integer indices are 1-based in the config for user-friendliness.", - "anyOf": [ - generator.subschema_for::(), - { - "description": "A 1-based container index as a string", - "type": "string", - "pattern": "^[0-9]+$" - }, - { - "description": "A 1-based container index", - "type": "integer", - "minimum": 0 - } - ] - }) - } -} - -impl Ord for PlacementTarget { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.to_string().cmp(&other.to_string()) - } -} - -impl Serialize for PlacementTarget { - fn serialize(&self, serializer: S) -> Result { - serializer.serialize_str(&self.to_string()) - } -} - -impl<'de> Deserialize<'de> for PlacementTarget { - fn deserialize>(deserializer: D) -> Result { - struct PlacementTargetVisitor; - - impl serde::de::Visitor<'_> for PlacementTargetVisitor { - type Value = PlacementTarget; - - fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.write_str("a WindowPlacement name or a non-negative container index") - } - - fn visit_str(self, s: &str) -> Result { - s.parse().map_err(E::custom) - } - - fn visit_u64(self, n: u64) -> Result { - usize::try_from(n) - .map(PlacementTarget::Index) - .map_err(E::custom) - } - - fn visit_i64(self, n: i64) -> Result { - usize::try_from(n) - .map(PlacementTarget::Index) - .map_err(E::custom) - } - } - - deserializer.deserialize_any(PlacementTargetVisitor) - } -} - -/// Configuration for initial window placement rules on a workspace. -/// -/// This can be specified in two forms in the JSON config: -/// - A placement target (string or integer) — applies the same placement to all windows. -/// Strings are `WindowPlacement` variant names (e.g. `"Primary"`, `"AfterFocused"`), -/// integers are 1-based container indices. -/// - A map of placement targets to matching rules — keys can be `WindowPlacement` variant names -/// (e.g. `"Primary"`, `"Secondary"`) or 1-based container indices (e.g. `"1"`, `"3"`). -/// Rules are evaluated in key order; the first matching rule determines placement. -/// -/// NOTE: Container indices in the config are 1-based for user-friendliness. -/// They are converted to 0-based internally during resolution. -/// -/// NOTE: This feature currently only applies when `WindowContainerBehaviour::Create` is active. -/// Future versions may support toggling this for `Append` mode as well. -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -#[serde(untagged)] -pub enum InitialWindowPlacementRules { - /// A single placement target applied to all new windows (string or integer in config) - Target(PlacementTarget), - /// A map of placement targets to matching rules. - /// Keys can be `WindowPlacement` variant names (e.g. `"Primary"`) or 1-based container indices (e.g. `"1"`). - /// Values can be: - /// - A single `IdWithIdentifier` object (simple rule) - /// - An array containing objects and/or arrays: - /// - Each object in the array is an independent simple rule (OR logic between entries) - /// - Each inner array is a composite rule where all conditions must match (AND logic) - /// - The outer array entries are evaluated with OR logic - /// - /// Rules are evaluated in key order; the first matching rule determines placement. - Rules(std::collections::BTreeMap), -} - -/// Matching rules for a placement target. -/// -/// Can be specified in JSON as: -/// - A single `IdWithIdentifier` object — one simple rule -/// - An array of `MatchingRule`s — multiple rules with OR logic between them -/// (each element can be a simple rule object or a composite rule array with AND logic) -/// -/// Examples: -/// ```json -/// // Single rule -/// { "kind": "Exe", "id": "chrome.exe", "matching_strategy": "Equals" } -/// -/// // Multiple rules (OR): chrome OR teams -/// [ -/// { "kind": "Exe", "id": "chrome.exe", "matching_strategy": "Equals" }, -/// { "kind": "Title", "id": "Microsoft Teams", "matching_strategy": "Equals" } -/// ] -/// -/// // Mixed: chrome OR (code.exe AND title contains "workspace") -/// [ -/// { "kind": "Exe", "id": "chrome.exe", "matching_strategy": "Equals" }, -/// [ -/// { "kind": "Exe", "id": "code.exe", "matching_strategy": "Equals" }, -/// { "kind": "Title", "id": "workspace", "matching_strategy": "Contains" } -/// ] -/// ] -/// ``` -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -#[serde(untagged)] -pub enum PlacementMatchingRules { - /// A single simple matching rule - Single(config_generation::IdWithIdentifier), - /// Multiple matching rules evaluated with OR logic. - /// Each entry is a `MatchingRule`: either a simple rule (object) or composite rule (array, AND logic). - Many(Vec), -} - #[cfg(test)] mod tests { use super::*;