From 0da55d8709bd18197dd6c79f82ed4e0763a16efe Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 31 Oct 2026 18:44:56 -0700 Subject: [PATCH] refactor(mac): match window rules with the shared komorebi-config::rules macOS' should_act and should_act_individual become thin wrappers over komorebi_config::rules (RuleQuirks::UNIX: a `.exe` in a rule id is ignored, as before). Signatures and the trait methods in workspace.rs that call them are unchanged. The old bodies were byte-identical to KDE's, which the shared tests (crates/komorebi-config/tests/rule_matching.rs) now cover. No behaviour change intended. Verified: cargo clippy --target aarch64-apple-darwin -p komorebi -p komorebi-macos -p komorebic-macos -p komorebi-client-macos -p komorebi-bar-macos --all-targets -D warnings with the fake CC: clean. Not run: any macOS test (type-check only on Linux); the Mini must run the suite on this revision. --- komorebi-for-mac/komorebi/src/window.rs | 325 +++--------------------- 1 file changed, 30 insertions(+), 295 deletions(-) diff --git a/komorebi-for-mac/komorebi/src/window.rs b/komorebi-for-mac/komorebi/src/window.rs index 895b6673..92a29da4 100644 --- a/komorebi-for-mac/komorebi/src/window.rs +++ b/komorebi-for-mac/komorebi/src/window.rs @@ -39,12 +39,10 @@ use crate::animation::tracks::Verdict; use crate::application::Application; use crate::arrival; use crate::cf_dictionary_value; -use crate::core::ApplicationIdentifier; use crate::core::Rect; use crate::core::WindowHidingPosition; use crate::core::config_generation::IdWithIdentifier; use crate::core::config_generation::MatchingRule; -use crate::core::config_generation::MatchingStrategy; use crate::core_graphics::CoreGraphicsApi; use crate::hidden_frame; use crate::is_visible_on_another_display; @@ -59,6 +57,9 @@ use crate::window_manager_event_listener; use color_eyre::eyre; pub use komorebi_config::aspect_ratio::AspectRatio; pub use komorebi_config::aspect_ratio::PredefinedAspectRatio; +use komorebi_config::rules; +use komorebi_config::rules::RuleQuirks; +use komorebi_config::rules::RuleSubject; use objc2::__framework_prelude::Retained; use objc2_app_kit::NSApplicationActivationOptions; use objc2_app_kit::NSRunningApplication; @@ -1824,7 +1825,9 @@ fn window_is_eligible( true } -#[allow(clippy::cognitive_complexity, clippy::too_many_lines)] +/// Returns the last of `identifiers` that matches a window with this title, executable, classes +/// and path, if one does (the shared evaluation, [`komorebi_config::rules::should_act`], with a +/// `.exe` in a rule id ignored) pub fn should_act( title: &str, exe_name: &str, @@ -1833,44 +1836,22 @@ pub fn should_act( identifiers: &[MatchingRule], regex_identifiers: &HashMap, ) -> Option { - let mut matching_rule = None; - for rule in identifiers { - match rule { - MatchingRule::Simple(identifier) => { - if should_act_individual( - title, - exe_name, - classes, - path, - identifier, - regex_identifiers, - ) { - matching_rule = Some(rule.clone()); - }; - } - MatchingRule::Composite(identifiers) => { - let mut composite_results = vec![]; - for identifier in identifiers { - composite_results.push(should_act_individual( - title, - exe_name, - classes, - path, - identifier, - regex_identifiers, - )); - } - - if composite_results.iter().all(|&x| x) { - matching_rule = Some(rule.clone()); - } - } - } - } - - matching_rule + rules::should_act( + &RuleSubject { + title, + exe_name, + classes, + path, + }, + RuleQuirks::UNIX, + identifiers, + regex_identifiers, + ) } +/// Returns true if and only if the condition `identifier` holds for a window with this title, +/// executable, classes and path (the shared evaluation, +/// [`komorebi_config::rules::should_act_individual`], with a `.exe` in the id ignored) pub fn should_act_individual( title: &str, exe_name: &str, @@ -1879,263 +1860,17 @@ pub fn should_act_individual( identifier: &IdWithIdentifier, regex_identifiers: &HashMap, ) -> bool { - let mut should_act = false; - - let mut identifier = identifier.clone(); - identifier.id = identifier.id.replace(".exe", ""); - - match identifier.matching_strategy { - None | Some(MatchingStrategy::Legacy) => match identifier.kind { - ApplicationIdentifier::Title => { - if title.starts_with(&identifier.id) || title.ends_with(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Class => { - for class in classes { - if class.starts_with(&identifier.id) || class.ends_with(&identifier.id) { - should_act = true; - } - } - } - ApplicationIdentifier::Exe => { - if exe_name.eq(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Path => { - if path.eq(&identifier.id) { - should_act = true; - } - } + rules::should_act_individual( + &RuleSubject { + title, + exe_name, + classes, + path, }, - Some(MatchingStrategy::Equals) => match identifier.kind { - ApplicationIdentifier::Title => { - if title.eq(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Class => { - for class in classes { - if class.eq(&identifier.id) { - should_act = true; - } - } - } - ApplicationIdentifier::Exe => { - if exe_name.eq(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Path => { - if path.eq(&identifier.id) { - should_act = true; - } - } - }, - Some(MatchingStrategy::DoesNotEqual) => match identifier.kind { - ApplicationIdentifier::Title => { - if !title.eq(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Class => { - for class in classes { - if !class.eq(&identifier.id) { - should_act = true; - } - } - } - ApplicationIdentifier::Exe => { - if !exe_name.eq(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Path => { - if !path.eq(&identifier.id) { - should_act = true; - } - } - }, - Some(MatchingStrategy::StartsWith) => match identifier.kind { - ApplicationIdentifier::Title => { - if title.starts_with(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Class => { - for class in classes { - if class.starts_with(&identifier.id) { - should_act = true; - } - } - } - ApplicationIdentifier::Exe => { - if exe_name.starts_with(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Path => { - if path.starts_with(&identifier.id) { - should_act = true; - } - } - }, - Some(MatchingStrategy::DoesNotStartWith) => match identifier.kind { - ApplicationIdentifier::Title => { - if !title.starts_with(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Class => { - for class in classes { - if !class.starts_with(&identifier.id) { - should_act = true; - } - } - } - ApplicationIdentifier::Exe => { - if !exe_name.starts_with(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Path => { - if !path.starts_with(&identifier.id) { - should_act = true; - } - } - }, - Some(MatchingStrategy::EndsWith) => match identifier.kind { - ApplicationIdentifier::Title => { - if title.ends_with(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Class => { - for class in classes { - if class.ends_with(&identifier.id) { - should_act = true; - } - } - } - ApplicationIdentifier::Exe => { - if exe_name.ends_with(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Path => { - if path.ends_with(&identifier.id) { - should_act = true; - } - } - }, - Some(MatchingStrategy::DoesNotEndWith) => match identifier.kind { - ApplicationIdentifier::Title => { - if !title.ends_with(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Class => { - for class in classes { - if !class.ends_with(&identifier.id) { - should_act = true; - } - } - } - ApplicationIdentifier::Exe => { - if !exe_name.ends_with(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Path => { - if !path.ends_with(&identifier.id) { - should_act = true; - } - } - }, - Some(MatchingStrategy::Contains) => match identifier.kind { - ApplicationIdentifier::Title => { - if title.contains(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Class => { - for class in classes { - if class.contains(&identifier.id) { - should_act = true; - } - } - } - ApplicationIdentifier::Exe => { - if exe_name.contains(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Path => { - if path.contains(&identifier.id) { - should_act = true; - } - } - }, - Some(MatchingStrategy::DoesNotContain) => match identifier.kind { - ApplicationIdentifier::Title => { - if !title.contains(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Class => { - for class in classes { - if !class.contains(&identifier.id) { - should_act = true; - } - } - } - ApplicationIdentifier::Exe => { - if !exe_name.contains(&identifier.id) { - should_act = true; - } - } - ApplicationIdentifier::Path => { - if !path.contains(&identifier.id) { - should_act = true; - } - } - }, - Some(MatchingStrategy::Regex) => match identifier.kind { - ApplicationIdentifier::Title => { - if let Some(re) = regex_identifiers.get(&identifier.id) - && re.is_match(title) - { - should_act = true; - } - } - ApplicationIdentifier::Class => { - if let Some(re) = regex_identifiers.get(&identifier.id) { - for class in classes { - if re.is_match(class) { - should_act = true; - } - } - } - } - ApplicationIdentifier::Exe => { - if let Some(re) = regex_identifiers.get(&identifier.id) - && re.is_match(exe_name) - { - should_act = true; - } - } - ApplicationIdentifier::Path => { - if let Some(re) = regex_identifiers.get(&identifier.id) - && re.is_match(path) - { - should_act = true; - } - } - }, - } - - should_act + RuleQuirks::UNIX, + identifier, + regex_identifiers, + ) } #[cfg(test)]