diff --git a/Cargo.lock b/Cargo.lock index 6c66057a..e2a6c09d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -539,6 +539,7 @@ dependencies = [ "objc2-foundation", "parking_lot", "pastey", + "regex", "serde", "serde_json_lenient", "serde_with", @@ -983,6 +984,18 @@ dependencies = [ "syn", ] +[[package]] +name = "regex" +version = "1.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23d7fd106d8c02486a8d64e778353d1cffe08ce79ac2e82f540c86d0facf6912" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + [[package]] name = "regex-automata" version = "0.4.10" diff --git a/dependencies.json b/dependencies.json index 8739a57f..6ce3c607 100644 --- a/dependencies.json +++ b/dependencies.json @@ -64,6 +64,7 @@ "pin-project-lite 0.2.16 registry+https://github.com/rust-lang/crates.io-index", "proc-macro2 1.0.101 registry+https://github.com/rust-lang/crates.io-index", "quote 1.0.40 registry+https://github.com/rust-lang/crates.io-index", + "regex 1.11.2 registry+https://github.com/rust-lang/crates.io-index", "regex-automata 0.4.10 registry+https://github.com/rust-lang/crates.io-index", "regex-syntax 0.8.6 registry+https://github.com/rust-lang/crates.io-index", "rustc-demangle 0.1.26 registry+https://github.com/rust-lang/crates.io-index", @@ -103,6 +104,7 @@ [ "addr2line 0.24.2 registry+https://github.com/rust-lang/crates.io-index", "adler2 2.0.1 registry+https://github.com/rust-lang/crates.io-index", + "aho-corasick 1.1.3 registry+https://github.com/rust-lang/crates.io-index", "anstream 0.6.20 registry+https://github.com/rust-lang/crates.io-index", "anstyle 1.0.11 registry+https://github.com/rust-lang/crates.io-index", "anstyle-parse 0.2.7 registry+https://github.com/rust-lang/crates.io-index", @@ -170,6 +172,7 @@ "pin-project-lite 0.2.16 registry+https://github.com/rust-lang/crates.io-index", "proc-macro2 1.0.101 registry+https://github.com/rust-lang/crates.io-index", "quote 1.0.40 registry+https://github.com/rust-lang/crates.io-index", + "regex 1.11.2 registry+https://github.com/rust-lang/crates.io-index", "regex-automata 0.4.10 registry+https://github.com/rust-lang/crates.io-index", "regex-syntax 0.8.6 registry+https://github.com/rust-lang/crates.io-index", "rustc-demangle 0.1.26 registry+https://github.com/rust-lang/crates.io-index", @@ -216,6 +219,7 @@ [ "Unlicense", [ + "aho-corasick 1.1.3 registry+https://github.com/rust-lang/crates.io-index", "memchr 2.7.5 registry+https://github.com/rust-lang/crates.io-index" ] ], diff --git a/komorebi/Cargo.toml b/komorebi/Cargo.toml index ff957cc5..31005369 100644 --- a/komorebi/Cargo.toml +++ b/komorebi/Cargo.toml @@ -13,11 +13,12 @@ lazy_static = { workspace = true } objc2 = "0.6" objc2-app-kit = "0.3" objc2-application-services = "0.3" -objc2-foundation = "0.3" objc2-core-foundation = { version = "0.3", features = ["CFDictionary", "CFString"] } objc2-core-graphics = { version = "0.3", features = ["CGWindow", "alloc"] } +objc2-foundation = "0.3" parking_lot = "0.12" pastey = { workspace = true } +regex = "1" serde = { workspace = true } serde_json = { workspace = true } serde_with = "3" diff --git a/komorebi/src/core/asc.rs b/komorebi/src/core/asc.rs new file mode 100644 index 00000000..dd4e1414 --- /dev/null +++ b/komorebi/src/core/asc.rs @@ -0,0 +1,134 @@ +use crate::core::config_generation::ApplicationConfiguration; +use crate::core::config_generation::ApplicationOptions; +use crate::core::config_generation::MatchingRule; +use color_eyre::Result; +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)] +pub struct ApplicationSpecificConfiguration(pub BTreeMap); + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[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, Serialize, Deserialize)] +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) + } +} diff --git a/komorebi/src/core/config_generation.rs b/komorebi/src/core/config_generation.rs new file mode 100644 index 00000000..3dfbe2fd --- /dev/null +++ b/komorebi/src/core/config_generation.rs @@ -0,0 +1,99 @@ +use clap::ValueEnum; +use serde::Deserialize; +use serde::Serialize; +use strum::Display; +use strum::EnumString; + +use super::ApplicationIdentifier; + +#[derive(Clone, Copy, Debug, Serialize, Deserialize, Display, EnumString, ValueEnum)] +#[strum(serialize_all = "snake_case")] +#[serde(rename_all = "snake_case")] +pub enum ApplicationOptions { + ObjectNameChange, + Layered, + TrayAndMultiWindow, + Force, + BorderOverflow, +} + +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +#[serde(untagged)] +pub enum MatchingRule { + Simple(IdWithIdentifier), + Composite(Vec), +} + +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +pub struct WorkspaceMatchingRule { + pub monitor_index: usize, + pub workspace_index: usize, + pub matching_rule: MatchingRule, + pub initial_only: bool, +} + +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +pub struct IdWithIdentifier { + pub kind: ApplicationIdentifier, + pub id: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub matching_strategy: Option, +} + +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Display)] +pub enum MatchingStrategy { + Legacy, + Equals, + StartsWith, + EndsWith, + Contains, + Regex, + DoesNotEndWith, + DoesNotStartWith, + DoesNotEqual, + DoesNotContain, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +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, + } + } +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +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/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index 74e32962..aea9818e 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -11,6 +11,8 @@ use strum::Display; use strum::EnumString; pub mod arrangement; +pub mod asc; +pub mod config_generation; pub mod cycle_direction; pub mod default_layout; pub mod direction; @@ -133,6 +135,20 @@ pub enum CrossBoundaryBehaviour { Monitor, } +#[derive( + Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Display, EnumString, ValueEnum, +)] +pub enum ApplicationIdentifier { + #[serde(alias = "exe")] + Exe, + #[serde(alias = "class")] + Class, + #[serde(alias = "title")] + Title, + #[serde(alias = "path")] + Path, +} + #[serde_with::serde_as] #[derive(Clone, Debug, Serialize, Deserialize, Display)] #[serde(tag = "type", content = "content")] diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index 2e31192d..e0dd07d6 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -1,6 +1,10 @@ #![warn(clippy::all)] use crate::accessibility::error::AccessibilityError; +use crate::core::ApplicationIdentifier; +use crate::core::config_generation::IdWithIdentifier; +use crate::core::config_generation::MatchingRule; +use crate::core::config_generation::MatchingStrategy; use crate::core_graphics::error::CoreGraphicsError; use crate::window::AspectRatio; use crate::window::PredefinedAspectRatio; @@ -17,6 +21,7 @@ use objc2_core_foundation::CGPoint; use objc2_core_foundation::CGRect; use objc2_core_foundation::CGSize; use parking_lot::Mutex; +use regex::Regex; use std::collections::HashMap; use std::ops::Deref; use std::path::PathBuf; @@ -29,10 +34,10 @@ use std::sync::atomic::AtomicI32; pub mod ring; pub mod accessibility; -mod app_kit_notification_constants; +pub mod app_kit_notification_constants; pub mod application; pub mod ax_event_listener; -mod container; +pub mod container; pub mod core; pub mod core_graphics; pub mod display_reconfiguration_listener; @@ -73,6 +78,36 @@ lazy_static! { static ref WINDOW_RESTORE_POSITIONS: Arc>> = Arc::new(Mutex::new(HashMap::new())); pub static ref UPDATE_MONITOR_WORK_AREAS: Arc = Arc::new(AtomicBool::new(false)); + static ref REGEX_IDENTIFIERS: Arc>> = + Arc::new(Mutex::new(HashMap::new())); + static ref MANAGE_IDENTIFIERS: Arc>> = Arc::new(Mutex::new(vec![])); + static ref IGNORE_IDENTIFIERS: Arc>> = Arc::new(Mutex::new(vec![ + MatchingRule::Simple(IdWithIdentifier { + kind: ApplicationIdentifier::Class, + id: String::from("OPContainerClass"), + matching_strategy: Option::from(MatchingStrategy::Equals), + }), + MatchingRule::Simple(IdWithIdentifier { + kind: ApplicationIdentifier::Class, + id: String::from("IHWindowClass"), + matching_strategy: Option::from(MatchingStrategy::Equals), + }), + MatchingRule::Simple(IdWithIdentifier { + kind: ApplicationIdentifier::Exe, + id: String::from("komorebi-bar.exe"), + matching_strategy: Option::from(MatchingStrategy::Equals), + }) + ])); + static ref SESSION_FLOATING_APPLICATIONS: Arc>> = + Arc::new(Mutex::new(Vec::new())); + static ref FLOATING_APPLICATIONS: Arc>> = + Arc::new(Mutex::new(vec![MatchingRule::Simple(IdWithIdentifier { + kind: ApplicationIdentifier::Exe, + id: String::from("komorebi-shortcuts.exe"), + matching_strategy: Option::from(MatchingStrategy::Equals), + })])); + static ref PERMAIGNORE_CLASSES: Arc>> = + Arc::new(Mutex::new(vec!["Chrome_RenderWidgetHostHWND".to_string(),])); } pub static DEFAULT_WORKSPACE_PADDING: AtomicI32 = AtomicI32::new(5);