diff --git a/komorebi-for-kde/Cargo.lock b/komorebi-for-kde/Cargo.lock index 7d2365fb..4e8a9d1a 100644 --- a/komorebi-for-kde/Cargo.lock +++ b/komorebi-for-kde/Cargo.lock @@ -2502,6 +2502,7 @@ dependencies = [ "futures-util", "hotwatch", "komorebi-collections", + "komorebi-config", "komorebi-hotkeys", "komorebi-layouts", "komorebi-protocol", @@ -2545,6 +2546,19 @@ dependencies = [ "serde", ] +[[package]] +name = "komorebi-config" +version = "0.1.42" +dependencies = [ + "komorebi-layouts", + "komorebi-protocol", + "komorebi-themes", + "schemars 1.2.3", + "serde", + "serde_json_lenient", + "strum", +] + [[package]] name = "komorebi-hotkeys" version = "0.1.0" diff --git a/komorebi-for-kde/Cargo.toml b/komorebi-for-kde/Cargo.toml index 1df4af7c..714f1b07 100644 --- a/komorebi-for-kde/Cargo.toml +++ b/komorebi-for-kde/Cargo.toml @@ -24,6 +24,7 @@ komorebi-layouts = { path = "../komorebi-layouts", features = [ # revision, whose layouts re-export it as `komorebi_layouts::scrolling::model` komorebi-scrolling = { path = "../komorebi-scrolling" } # the wire types shared with the other komorebi daemons +komorebi-config = { path = "../crates/komorebi-config", default-features = false, features = ["schemars"] } komorebi-protocol = { path = "../crates/komorebi-protocol", default-features = false, features = ["schemars"] } # the themes (and the palettes made from pictures), from the same upstream revision komorebi-themes = { path = "../komorebi-themes" } diff --git a/komorebi-for-kde/komorebi/Cargo.toml b/komorebi-for-kde/komorebi/Cargo.toml index 87e00f8a..2d497233 100644 --- a/komorebi-for-kde/komorebi/Cargo.toml +++ b/komorebi-for-kde/komorebi/Cargo.toml @@ -10,6 +10,7 @@ komorebi-themes = { workspace = true } komorebi-collections = { workspace = true } komorebi-layouts = { workspace = true } komorebi-scrolling = { workspace = true } +komorebi-config = { workspace = true } komorebi-protocol = { workspace = true } version = { path = "../version" } diff --git a/komorebi-for-kde/komorebi/src/core/config_generation.rs b/komorebi-for-kde/komorebi/src/core/config_generation.rs index 273eee18..03abb7ce 100644 --- a/komorebi-for-kde/komorebi/src/core/config_generation.rs +++ b/komorebi-for-kde/komorebi/src/core/config_generation.rs @@ -1,161 +1,9 @@ //! Rules that match an application by how it identifies itself. //! -//! A [`MatchingRule`] is the form every list of rules in the configuration takes: a single -//! [`IdWithIdentifier`], or a list of them that must all match. +//! The types are defined in `komorebi-config`, which `komorebi.json` is read with. -use serde::Deserialize; -use serde::Serialize; -use strum::Display; - -use super::ApplicationIdentifier; - -#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -#[serde(untagged)] -/// Rule for matching applications -pub enum MatchingRule { - /// Simple matching rule which must evaluate to true - Simple(IdWithIdentifier), - /// Composite matching rule where all conditions must evaluate to true - Composite(Vec), -} - -#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -/// Rule for assigning applications to a workspace -pub struct WorkspaceMatchingRule { - /// Target monitor index - pub monitor_index: usize, - /// Target workspace index - pub workspace_index: usize, - /// Matching rule for the application - pub matching_rule: MatchingRule, - /// Whether to apply the rule only when the application is initially launched - pub initial_only: bool, -} - -#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -/// Rule for matching applications -pub struct IdWithIdentifier { - /// Kind of identifier to target - pub kind: ApplicationIdentifier, - /// Target identifier - pub id: String, - #[serde(skip_serializing_if = "Option::is_none")] - /// Matching strategy to use - pub matching_strategy: Option, -} - -#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Display)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -/// Strategy for matching identifiers -pub enum MatchingStrategy { - /// Should not be used, only kept for backward compatibility - Legacy, - /// Equals - Equals, - /// Starts With - StartsWith, - /// Ends With - EndsWith, - /// Contains - Contains, - /// Regex - Regex, - /// Does not end with - DoesNotEndWith, - /// Does not start with - DoesNotStartWith, - /// Does not equal - DoesNotEqual, - /// Does not contain - DoesNotContain, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -pub struct IdWithIdentifierAndComment { - pub kind: ApplicationIdentifier, - pub id: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub comment: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub matching_strategy: Option, -} - -impl From for IdWithIdentifier { - fn from(value: IdWithIdentifierAndComment) -> Self { - Self { - kind: value.kind, - id: value.id.clone(), - matching_strategy: value.matching_strategy, - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn a_rule_that_is_an_object_is_simple() { - let rule: MatchingRule = serde_json::from_str( - r#"{ "kind": "Exe", "id": "foot", "matching_strategy": "Equals" }"#, - ) - .expect("parses"); - - assert_eq!( - rule, - MatchingRule::Simple(IdWithIdentifier { - kind: ApplicationIdentifier::Exe, - id: "foot".to_string(), - matching_strategy: Some(MatchingStrategy::Equals), - }) - ); - } - - #[test] - fn a_rule_that_is_a_list_is_composite() { - let rule: MatchingRule = serde_json::from_str( - r#"[{ "kind": "Exe", "id": "foot" }, { "kind": "Title", "id": "scratch", "matching_strategy": "StartsWith" }]"#, - ) - .expect("parses"); - - let MatchingRule::Composite(parts) = rule else { - panic!("a list of objects is a composite rule"); - }; - assert_eq!(parts.len(), 2); - assert_eq!( - parts[1].matching_strategy, - Some(MatchingStrategy::StartsWith) - ); - } - - #[test] - fn the_matching_strategy_is_left_out_when_there_is_none() { - let rule = IdWithIdentifier { - kind: ApplicationIdentifier::Class, - id: "firefox".to_string(), - matching_strategy: None, - }; - - let json = serde_json::to_string(&rule).expect("serializes"); - - assert_eq!(json, r#"{"kind":"Class","id":"firefox"}"#); - } - - #[test] - fn a_commented_rule_becomes_a_plain_one() { - let commented: IdWithIdentifierAndComment = serde_json::from_str( - r#"{ "kind": "Path", "id": "/usr/bin/kcalc", "comment": "a calculator" }"#, - ) - .expect("parses"); - - let plain = IdWithIdentifier::from(commented); - - assert_eq!(plain.kind, ApplicationIdentifier::Path); - assert_eq!(plain.id, "/usr/bin/kcalc"); - assert_eq!(plain.matching_strategy, None); - } -} +pub use komorebi_config::matching::IdWithIdentifier; +pub use komorebi_config::matching::IdWithIdentifierAndComment; +pub use komorebi_config::matching::MatchingRule; +pub use komorebi_config::matching::MatchingStrategy; +pub use komorebi_config::matching::WorkspaceMatchingRule;