diff --git a/Cargo.lock b/Cargo.lock index aebbddc9..11225018 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3557,6 +3557,7 @@ dependencies = [ "dunce", "fs-tail", "komorebi-client", + "komorebi-config", "lazy_static", "miette", "open", diff --git a/komorebi/src/core/asc.rs b/komorebi/src/core/asc.rs index 45d9fb59..e9547be0 100644 --- a/komorebi/src/core/asc.rs +++ b/komorebi/src/core/asc.rs @@ -1,137 +1,3 @@ -use crate::config_generation::ApplicationConfiguration; -use crate::config_generation::ApplicationOptions; -use crate::config_generation::MatchingRule; -use color_eyre::eyre; -use serde::Deserialize; -use serde::Serialize; -use std::collections::BTreeMap; -use std::ops::Deref; -use std::ops::DerefMut; -use std::path::PathBuf; - -#[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))] -#[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) -> eyre::Result { - let content = std::fs::read_to_string(pathbuf)?; - Ok(serde_json::from_str(&content)?) - } - - pub fn format(pathbuf: &PathBuf) -> eyre::Result { - Ok(serde_json::to_string_pretty(&Self::load(pathbuf)?)?) - } -} - -/// Rules that determine how an application is handled -#[derive(Clone, Debug, 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")] - pub transparency_ignore: Option>, - /// Rules to identify applications which minimize to the tray or have multiple windows - #[serde(skip_serializing_if = "Option::is_none")] - 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")] - pub layered: Option>, - /// Rules to identify applications which send the `EVENT_OBJECT_NAMECHANGE` event on launch - #[serde(skip_serializing_if = "Option::is_none")] - pub object_name_change: Option>, - /// Rules to identify applications which are slow to send initial event notifications - #[serde(skip_serializing_if = "Option::is_none")] - pub slow_application: 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: None, - manage: None, - floating: None, - transparency_ignore: None, - tray_and_multi_window: None, - layered: None, - object_name_change: None, - slow_application: None, - }; - - rules.ignore = entry.ignore_identifiers.clone(); - - if let Some(options) = &entry.options { - for opt in options { - match opt { - ApplicationOptions::ObjectNameChange => { - rules.object_name_change = - Some(vec![MatchingRule::Simple(entry.identifier.clone())]); - } - ApplicationOptions::Layered => { - rules.layered = - Some(vec![MatchingRule::Simple(entry.identifier.clone())]); - } - ApplicationOptions::TrayAndMultiWindow => { - rules.tray_and_multi_window = - Some(vec![MatchingRule::Simple(entry.identifier.clone())]); - } - ApplicationOptions::Force => { - rules.manage = - Some(vec![MatchingRule::Simple(entry.identifier.clone())]); - } - 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) - } -} +pub use komorebi_config::asc::ApplicationSpecificConfiguration; +pub use komorebi_config::asc::AscApplicationRules; +pub use komorebi_config::asc::AscApplicationRulesOrSchema; diff --git a/komorebi/src/core/config_generation.rs b/komorebi/src/core/config_generation.rs index 4a64d22a..73251aed 100644 --- a/komorebi/src/core/config_generation.rs +++ b/komorebi/src/core/config_generation.rs @@ -1,86 +1,15 @@ -use clap::ValueEnum; use color_eyre::eyre; use serde::Deserialize; use serde::Serialize; -use strum::Display; -use strum::EnumString; - -use super::ApplicationIdentifier; - -#[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) - ) - } -} +pub use komorebi_config::application_configuration::ApplicationConfiguration; +pub use komorebi_config::application_configuration::ApplicationOptions; 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; -#[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 => {} - } - } - } -} - #[derive(Clone, Debug, Serialize, Deserialize)] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub struct ApplicationConfigurationGenerator; diff --git a/komorebic/Cargo.toml b/komorebic/Cargo.toml index 82582036..e0e7343c 100644 --- a/komorebic/Cargo.toml +++ b/komorebic/Cargo.toml @@ -8,6 +8,7 @@ edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +komorebi-config = { path = "../crates/komorebi-config", default-features = false } komorebi-client = { path = "../komorebi-client", default-features = false } chrono = { workspace = true } @@ -38,7 +39,7 @@ shadow-rs = { workspace = true } [features] default = ["schemars"] -schemars = ["dep:schemars", "komorebi-client/default"] +schemars = ["dep:schemars", "komorebi-client/default", "komorebi-config/schemars"] [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(FALSE)'] } diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index e5fa9f8b..e925dd61 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -3471,9 +3471,10 @@ if (Get-Command Get-CimInstance -ErrorAction SilentlyContinue) { SubCommand::ApplicationSpecificConfigurationSchema => { #[cfg(feature = "schemars")] { - let asc = schemars::schema_for!(ApplicationSpecificConfiguration); - let schema = serde_json::to_string_pretty(&asc)?; - println!("{schema}"); + let schema = komorebi_config::schema::view::( + komorebi_config::schema::Platform::CURRENT, + ); + print!("{schema}"); } } SubCommand::NotificationSchema => {