From 89c729e7313b0c3b9737f6695b20ac9f6f660df8 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 30 Oct 2026 22:31:40 -0700 Subject: [PATCH] refactor(config): move asc and the application configuration data into komorebi-config ApplicationSpecificConfiguration, AscApplicationRulesOrSchema and AscApplicationRules are code-identical in the trees except that AscApplicationRules differs in its rule kinds: five Windows-only kinds, titleless and tabbed on KDE and macOS. The crate holds the union (declaration order floating-first as before; the platform-only fields carry x-komorebi-platforms, so a view prunes them) and, for the Windows From> conversion that the orphan rule keeps next to its types, ApplicationOptions and ApplicationConfiguration (data only: the YAML generator stays in the Windows daemon). The crate gains the color-eyre dependency. The oracle test builds the view of ApplicationSpecificConfiguration for Windows, KDE and macOS and compares it with each tree's checked-in schema.asc.json byte for byte: all three are identical. This is the first proof that per-platform views of one union reproduce the checked-in files exactly (D17/D18); the three trees still use their own copies. Verified: native cargo test -p komorebi-config: 4 unit + 6 schemas passed, 0 failed (tier 1). --- Cargo.lock | 1 + crates/komorebi-config/Cargo.toml | 1 + .../src/application_configuration.rs | 84 +++++++++++ crates/komorebi-config/src/asc.rs | 139 ++++++++++++++++++ crates/komorebi-config/src/lib.rs | 2 + crates/komorebi-config/src/schema.rs | 2 +- crates/komorebi-config/tests/schemas.rs | 21 +++ komorebi-for-kde/Cargo.lock | 1 + komorebi-for-mac/Cargo.lock | 1 + 9 files changed, 251 insertions(+), 1 deletion(-) create mode 100644 crates/komorebi-config/src/application_configuration.rs create mode 100644 crates/komorebi-config/src/asc.rs diff --git a/Cargo.lock b/Cargo.lock index baf496e2..aebbddc9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3445,6 +3445,7 @@ name = "komorebi-config" version = "0.1.42" dependencies = [ "clap", + "color-eyre", "komorebi-layouts", "komorebi-protocol", "komorebi-themes", diff --git a/crates/komorebi-config/Cargo.toml b/crates/komorebi-config/Cargo.toml index 36c724de..999e93be 100644 --- a/crates/komorebi-config/Cargo.toml +++ b/crates/komorebi-config/Cargo.toml @@ -10,6 +10,7 @@ komorebi-protocol = { path = "../komorebi-protocol", default-features = false } komorebi-themes = { path = "../../komorebi-themes" } clap = { workspace = true } +color-eyre = { workspace = true } serde = { workspace = true } serde_json = { workspace = true, features = ["preserve_order"] } strum = { workspace = true } diff --git a/crates/komorebi-config/src/application_configuration.rs b/crates/komorebi-config/src/application_configuration.rs new file mode 100644 index 00000000..b54ffd77 --- /dev/null +++ b/crates/komorebi-config/src/application_configuration.rs @@ -0,0 +1,84 @@ +//! The entries that the Windows `fetch-app-specific-configuration` +//! generator reads, and the options that turn into rules. +//! +//! The generator itself (it reads YAML) stays in the Windows daemon; the data types are here so +//! that the conversion into `ApplicationSpecificConfiguration` can be a `From` impl next to it. + +use clap::ValueEnum; +use komorebi_protocol::types::ApplicationIdentifier; +use serde::Deserialize; +use serde::Serialize; +use strum::Display; +use strum::EnumString; + +use crate::matching::IdWithIdentifier; +use crate::matching::MatchingRule; +use crate::matching::MatchingStrategy; + +#[derive(Clone, Copy, Debug, Serialize, Deserialize, Display, EnumString, ValueEnum)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[strum(serialize_all = "snake_case")] +#[serde(rename_all = "snake_case")] +pub enum ApplicationOptions { + ObjectNameChange, + Layered, + TrayAndMultiWindow, + Force, + BorderOverflow, +} + +impl ApplicationOptions { + #[must_use] + pub fn raw_cfgen(&self, kind: &ApplicationIdentifier, id: &str) -> String { + match self { + ApplicationOptions::ObjectNameChange => { + format!("komorebic.exe identify-object-name-change-application {kind} \"{id}\"",) + } + ApplicationOptions::Layered => { + format!("komorebic.exe identify-layered-application {kind} \"{id}\"",) + } + ApplicationOptions::TrayAndMultiWindow => { + format!("komorebic.exe identify-tray-application {kind} \"{id}\"",) + } + ApplicationOptions::Force => { + format!("komorebic.exe manage-rule {kind} \"{id}\"") + } + ApplicationOptions::BorderOverflow => { + unreachable!("deprecated"); + } + } + } + + #[must_use] + pub fn cfgen(&self, kind: &ApplicationIdentifier, id: &str) -> String { + format!( + "RunWait('{}', , \"Hide\")", + ApplicationOptions::raw_cfgen(self, kind, id) + ) + } +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +pub struct ApplicationConfiguration { + pub name: String, + pub identifier: IdWithIdentifier, + #[serde(skip_serializing_if = "Option::is_none")] + pub options: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(alias = "float_identifiers")] + pub ignore_identifiers: Option>, +} + +impl ApplicationConfiguration { + pub fn populate_default_matching_strategies(&mut self) { + if self.identifier.matching_strategy.is_none() { + match self.identifier.kind { + ApplicationIdentifier::Exe | ApplicationIdentifier::Path => { + self.identifier.matching_strategy = Option::from(MatchingStrategy::Equals); + } + ApplicationIdentifier::Class | ApplicationIdentifier::Title => {} + } + } + } +} diff --git a/crates/komorebi-config/src/asc.rs b/crates/komorebi-config/src/asc.rs new file mode 100644 index 00000000..a2bedac6 --- /dev/null +++ b/crates/komorebi-config/src/asc.rs @@ -0,0 +1,139 @@ +//! The application specific configuration file (`app_specific_configuration_path`): rules, by +//! application, for the windows komorebi ignores, manages or floats. + +use std::collections::BTreeMap; +use std::ops::Deref; +use std::ops::DerefMut; +use std::path::PathBuf; + +use color_eyre::Result; +use serde::Deserialize; +use serde::Serialize; + +use crate::application_configuration::ApplicationConfiguration; +use crate::application_configuration::ApplicationOptions; +use crate::matching::MatchingRule; + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +pub struct ApplicationSpecificConfiguration(pub BTreeMap); + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[allow(clippy::large_enum_variant)] // the rules are the common case; the enum is a map value +#[serde(untagged)] +pub enum AscApplicationRulesOrSchema { + AscApplicationRules(AscApplicationRules), + Schema(String), +} + +impl Deref for ApplicationSpecificConfiguration { + type Target = BTreeMap; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for ApplicationSpecificConfiguration { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl ApplicationSpecificConfiguration { + pub fn load(pathbuf: &PathBuf) -> Result { + let content = std::fs::read_to_string(pathbuf)?; + Ok(serde_json::from_str(&content)?) + } + + pub fn format(pathbuf: &PathBuf) -> Result { + Ok(serde_json::to_string_pretty(&Self::load(pathbuf)?)?) + } +} + +/// Rules that determine how an application is handled +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +pub struct AscApplicationRules { + /// Rules to ignore specific windows + #[serde(skip_serializing_if = "Option::is_none")] + pub ignore: Option>, + /// Rules to forcibly manage specific windows + #[serde(skip_serializing_if = "Option::is_none")] + pub manage: Option>, + /// Rules to manage specific windows as floating windows + #[serde(skip_serializing_if = "Option::is_none")] + pub floating: Option>, + /// Rules to ignore specific windows from the transparency feature + #[serde(skip_serializing_if = "Option::is_none")] + #[cfg_attr(feature = "schemars", schemars(extend("x-komorebi-platforms" = ["windows"])))] + pub transparency_ignore: Option>, + /// Rules to identify applications which minimize to the tray or have multiple windows + #[serde(skip_serializing_if = "Option::is_none")] + #[cfg_attr(feature = "schemars", schemars(extend("x-komorebi-platforms" = ["windows"])))] + pub tray_and_multi_window: Option>, + /// Rules to identify applications which have the `WS_EX_LAYERED` Extended Window Style + #[serde(skip_serializing_if = "Option::is_none")] + #[cfg_attr(feature = "schemars", schemars(extend("x-komorebi-platforms" = ["windows"])))] + pub layered: Option>, + /// Rules to identify applications which send the `EVENT_OBJECT_NAMECHANGE` event on launch + #[serde(skip_serializing_if = "Option::is_none")] + #[cfg_attr(feature = "schemars", schemars(extend("x-komorebi-platforms" = ["windows"])))] + pub object_name_change: Option>, + /// Rules to identify applications which are slow to send initial event notifications + #[serde(skip_serializing_if = "Option::is_none")] + #[cfg_attr(feature = "schemars", schemars(extend("x-komorebi-platforms" = ["windows"])))] + pub slow_application: Option>, + /// Rules to identify applications which are title-less - only accepts Simple Exe rules + #[serde(skip_serializing_if = "Option::is_none")] + #[cfg_attr(feature = "schemars", schemars(extend("x-komorebi-platforms" = ["kde", "macos"])))] + pub titleless: Option>, + /// Rules to identify applications which are title-less - only accepts Simple Exe rules + #[serde(skip_serializing_if = "Option::is_none")] + #[cfg_attr(feature = "schemars", schemars(extend("x-komorebi-platforms" = ["kde", "macos"])))] + pub tabbed: Option>, +} + +impl From> for ApplicationSpecificConfiguration { + fn from(value: Vec) -> Self { + let mut map = BTreeMap::new(); + + for entry in &value { + let key = entry.name.clone(); + let mut rules = AscApplicationRules { + ignore: entry.ignore_identifiers.clone(), + ..AscApplicationRules::default() + }; + + if let Some(options) = &entry.options { + for opt in options { + let rule = Some(vec![MatchingRule::Simple(entry.identifier.clone())]); + match opt { + ApplicationOptions::ObjectNameChange => rules.object_name_change = rule, + ApplicationOptions::Layered => rules.layered = rule, + ApplicationOptions::TrayAndMultiWindow => { + rules.tray_and_multi_window = rule + } + ApplicationOptions::Force => rules.manage = rule, + ApplicationOptions::BorderOverflow => {} + } + } + } + + if rules.ignore.is_some() + || rules.manage.is_some() + || rules.floating.is_some() + || rules.transparency_ignore.is_some() + || rules.tray_and_multi_window.is_some() + || rules.layered.is_some() + || rules.object_name_change.is_some() + || rules.slow_application.is_some() + { + map.insert(key, AscApplicationRulesOrSchema::AscApplicationRules(rules)); + } + } + + Self(map) + } +} diff --git a/crates/komorebi-config/src/lib.rs b/crates/komorebi-config/src/lib.rs index a479a397..fab57d38 100644 --- a/crates/komorebi-config/src/lib.rs +++ b/crates/komorebi-config/src/lib.rs @@ -11,6 +11,8 @@ pub mod schema; pub mod animation; +pub mod application_configuration; +pub mod asc; pub mod aspect_ratio; pub mod behaviour; pub mod matching; diff --git a/crates/komorebi-config/src/schema.rs b/crates/komorebi-config/src/schema.rs index 5fdac050..108617d7 100644 --- a/crates/komorebi-config/src/schema.rs +++ b/crates/komorebi-config/src/schema.rs @@ -6,7 +6,7 @@ //! [`view`] drops what the platform does not read, strips the `x-komorebi-` markers and //! removes the definitions nothing refers to any more. -use komorebi_protocol::platforms::Platform; +pub use komorebi_protocol::platforms::Platform; use schemars::JsonSchema; use serde_json::Map; use serde_json::Value; diff --git a/crates/komorebi-config/tests/schemas.rs b/crates/komorebi-config/tests/schemas.rs index 11780bea..e9b72ad5 100644 --- a/crates/komorebi-config/tests/schemas.rs +++ b/crates/komorebi-config/tests/schemas.rs @@ -195,3 +195,24 @@ fn the_aspect_ratio_is_the_definition_of_every_tree() { assert_same_definitions::(&all(), &["AspectRatio", "PredefinedAspectRatio"]); } + +/// The platform views of the union schema of the applications file are byte-identical to the +/// three checked-in `schema.asc.json`. +#[test] +fn the_applications_schema_is_a_view_of_one_union() { + use komorebi_config::asc::ApplicationSpecificConfiguration; + use komorebi_protocol::platforms::Platform; + + for (tree, platform) in [ + (&TREES[0], Platform::Windows), + (&TREES[1], Platform::Kde), + (&TREES[2], Platform::MacOs), + ] { + let view = komorebi_config::schema::view::(platform); + assert!( + view == checked_in(tree, "schema.asc.json"), + "{}: the view of the applications schema differs from schema.asc.json", + tree.name + ); + } +} diff --git a/komorebi-for-kde/Cargo.lock b/komorebi-for-kde/Cargo.lock index d690a983..cf1873d2 100644 --- a/komorebi-for-kde/Cargo.lock +++ b/komorebi-for-kde/Cargo.lock @@ -2551,6 +2551,7 @@ name = "komorebi-config" version = "0.1.42" dependencies = [ "clap", + "color-eyre", "komorebi-layouts", "komorebi-protocol", "komorebi-themes", diff --git a/komorebi-for-mac/Cargo.lock b/komorebi-for-mac/Cargo.lock index c21f4eea..aba2bd4f 100644 --- a/komorebi-for-mac/Cargo.lock +++ b/komorebi-for-mac/Cargo.lock @@ -3261,6 +3261,7 @@ name = "komorebi-config" version = "0.1.42" dependencies = [ "clap", + "color-eyre", "komorebi-layouts", "komorebi-protocol", "komorebi-themes",