From 7178609daf7b65f1a57871af0050b4c263ad4801 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Tue, 23 Sep 2025 19:33:29 -0700 Subject: [PATCH] feat(cli): implement a bunch of rule cmds This commit implements a whole bunch of commands around runtime manipulation of ignore rules, manage rules and workspace rules. Mostly lifted straight from komorebi. --- komorebi-client/src/lib.rs | 1 + komorebi/src/core/mod.rs | 20 ++-- komorebi/src/process_command.rs | 202 ++++++++++++++++++++++++++++++++ komorebic/src/main.rs | 193 +++++++++++++++++++++++++----- 4 files changed, 377 insertions(+), 39 deletions(-) diff --git a/komorebi-client/src/lib.rs b/komorebi-client/src/lib.rs index 1043b01e..f5e02391 100644 --- a/komorebi-client/src/lib.rs +++ b/komorebi-client/src/lib.rs @@ -1,4 +1,5 @@ pub use komorebi::DATA_DIR; +pub use komorebi::core::ApplicationIdentifier; pub use komorebi::core::Sizing; pub use komorebi::core::SocketMessage; pub use komorebi::core::StateQuery; diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index 1d24faf3..fc268b3a 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -333,20 +333,20 @@ pub enum SocketMessage { // WorkspaceWorkAreaOffset(usize, usize, Rect), ToggleWindowBasedWorkAreaOffset, // ResizeDelta(i32), - // InitialWorkspaceRule(ApplicationIdentifier, String, usize, usize), - // InitialNamedWorkspaceRule(ApplicationIdentifier, String, String), - // WorkspaceRule(ApplicationIdentifier, String, usize, usize), - // NamedWorkspaceRule(ApplicationIdentifier, String, String), - // ClearWorkspaceRules(usize, usize), - // ClearNamedWorkspaceRules(String), - // ClearAllWorkspaceRules, - // EnforceWorkspaceRules, + InitialWorkspaceRule(ApplicationIdentifier, String, usize, usize), + InitialNamedWorkspaceRule(ApplicationIdentifier, String, String), + WorkspaceRule(ApplicationIdentifier, String, usize, usize), + NamedWorkspaceRule(ApplicationIdentifier, String, String), + ClearWorkspaceRules(usize, usize), + ClearNamedWorkspaceRules(String), + ClearAllWorkspaceRules, + EnforceWorkspaceRules, SessionFloatRule, SessionFloatRules, ClearSessionFloatRules, #[serde(alias = "FloatRule")] - // IgnoreRule(ApplicationIdentifier, String), - // ManageRule(ApplicationIdentifier, String), + IgnoreRule(ApplicationIdentifier, String), + ManageRule(ApplicationIdentifier, String), // IdentifyObjectNameChangeApplication(ApplicationIdentifier, String), // IdentifyTrayApplication(ApplicationIdentifier, String), // IdentifyLayeredApplication(ApplicationIdentifier, String), diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 7384fcd5..394fc79a 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -1,5 +1,8 @@ use crate::FLOATING_APPLICATIONS; +use crate::IGNORE_IDENTIFIERS; +use crate::MANAGE_IDENTIFIERS; use crate::SESSION_FLOATING_APPLICATIONS; +use crate::WORKSPACE_MATCHING_RULES; use crate::build; use crate::core::ApplicationIdentifier; use crate::core::MoveBehaviour; @@ -10,6 +13,7 @@ use crate::core::arrangement::Axis; use crate::core::config_generation::IdWithIdentifier; use crate::core::config_generation::MatchingRule; use crate::core::config_generation::MatchingStrategy; +use crate::core::config_generation::WorkspaceMatchingRule; use crate::core::layout::Layout; use crate::core::operation_direction::OperationDirection; use crate::core::rect::Rect; @@ -957,6 +961,204 @@ impl WindowManager { floating_applications.retain(|r| !session_floating_applications.contains(r)); session_floating_applications.clear() } + SocketMessage::InitialWorkspaceRule(identifier, ref id, monitor_idx, workspace_idx) => { + let mut workspace_rules = WORKSPACE_MATCHING_RULES.lock(); + let workspace_matching_rule = WorkspaceMatchingRule { + monitor_index: monitor_idx, + workspace_index: workspace_idx, + matching_rule: MatchingRule::Simple(IdWithIdentifier { + kind: identifier, + id: id.to_string(), + matching_strategy: Some(MatchingStrategy::Legacy), + }), + initial_only: true, + }; + + if !workspace_rules.contains(&workspace_matching_rule) { + workspace_rules.push(workspace_matching_rule); + } + } + SocketMessage::InitialNamedWorkspaceRule(identifier, ref id, ref workspace) => { + if let Some((monitor_idx, workspace_idx)) = + self.monitor_workspace_index_by_name(workspace) + { + let mut workspace_rules = WORKSPACE_MATCHING_RULES.lock(); + let workspace_matching_rule = WorkspaceMatchingRule { + monitor_index: monitor_idx, + workspace_index: workspace_idx, + matching_rule: MatchingRule::Simple(IdWithIdentifier { + kind: identifier, + id: id.to_string(), + matching_strategy: Some(MatchingStrategy::Legacy), + }), + initial_only: true, + }; + + if !workspace_rules.contains(&workspace_matching_rule) { + workspace_rules.push(workspace_matching_rule); + } + } + } + SocketMessage::WorkspaceRule(identifier, ref id, monitor_idx, workspace_idx) => { + let mut workspace_rules = WORKSPACE_MATCHING_RULES.lock(); + let workspace_matching_rule = WorkspaceMatchingRule { + monitor_index: monitor_idx, + workspace_index: workspace_idx, + matching_rule: MatchingRule::Simple(IdWithIdentifier { + kind: identifier, + id: id.to_string(), + matching_strategy: Some(MatchingStrategy::Legacy), + }), + initial_only: false, + }; + + if !workspace_rules.contains(&workspace_matching_rule) { + workspace_rules.push(workspace_matching_rule); + } + } + SocketMessage::NamedWorkspaceRule(identifier, ref id, ref workspace) => { + if let Some((monitor_idx, workspace_idx)) = + self.monitor_workspace_index_by_name(workspace) + { + let mut workspace_rules = WORKSPACE_MATCHING_RULES.lock(); + let workspace_matching_rule = WorkspaceMatchingRule { + monitor_index: monitor_idx, + workspace_index: workspace_idx, + matching_rule: MatchingRule::Simple(IdWithIdentifier { + kind: identifier, + id: id.to_string(), + matching_strategy: Some(MatchingStrategy::Legacy), + }), + initial_only: false, + }; + + if !workspace_rules.contains(&workspace_matching_rule) { + workspace_rules.push(workspace_matching_rule); + } + } + } + SocketMessage::ClearWorkspaceRules(monitor_idx, workspace_idx) => { + let mut workspace_rules = WORKSPACE_MATCHING_RULES.lock(); + + workspace_rules.retain(|r| { + r.monitor_index != monitor_idx && r.workspace_index != workspace_idx + }); + } + SocketMessage::ClearNamedWorkspaceRules(ref workspace) => { + if let Some((monitor_idx, workspace_idx)) = + self.monitor_workspace_index_by_name(workspace) + { + let mut workspace_rules = WORKSPACE_MATCHING_RULES.lock(); + workspace_rules.retain(|r| { + r.monitor_index != monitor_idx && r.workspace_index != workspace_idx + }); + } + } + SocketMessage::ClearAllWorkspaceRules => { + let mut workspace_rules = WORKSPACE_MATCHING_RULES.lock(); + workspace_rules.clear(); + } + SocketMessage::EnforceWorkspaceRules => { + { + let mut already_moved = self.already_moved_window_handles.lock(); + already_moved.clear(); + } + self.enforce_workspace_rules()?; + } + SocketMessage::IgnoreRule(identifier, ref id) => { + let mut ignore_identifiers = IGNORE_IDENTIFIERS.lock(); + + let mut should_push = true; + for i in &*ignore_identifiers { + if let MatchingRule::Simple(i) = i + && i.id.eq(id) + { + should_push = false; + } + } + + if should_push { + ignore_identifiers.push(MatchingRule::Simple(IdWithIdentifier { + kind: identifier, + id: id.clone(), + matching_strategy: Option::from(MatchingStrategy::Legacy), + })); + } + + let offset = self.work_area_offset; + + let mut window_ids_to_purge = vec![]; + for (i, monitor) in self.monitors().iter().enumerate() { + for container in monitor + .focused_workspace() + .ok_or_eyre("there is no workspace")? + .containers() + { + for window in container.windows() { + match identifier { + ApplicationIdentifier::Path => { + if window.path().unwrap_or_default().to_string_lossy() == *id { + window_ids_to_purge.push((i, window.id)); + } + } + ApplicationIdentifier::Exe => { + if window.exe().unwrap_or_default() == *id { + window_ids_to_purge.push((i, window.id)); + } + } + ApplicationIdentifier::Class => { + if window.role().unwrap_or_default() == *id { + window_ids_to_purge.push((i, window.id)); + } + + if window.subrole().unwrap_or_default() == *id { + window_ids_to_purge.push((i, window.id)); + } + } + ApplicationIdentifier::Title => { + if window.title().unwrap_or_default() == *id { + window_ids_to_purge.push((i, window.id)); + } + } + } + } + } + } + + for (monitor_idx, id) in window_ids_to_purge { + let monitor = self + .monitors_mut() + .get_mut(monitor_idx) + .ok_or_eyre("there is no monitor")?; + + monitor + .focused_workspace_mut() + .ok_or_eyre("there is no focused workspace")? + .remove_window(id)?; + + monitor.update_focused_workspace(offset)?; + } + } + SocketMessage::ManageRule(identifier, ref id) => { + let mut manage_identifiers = MANAGE_IDENTIFIERS.lock(); + + let mut should_push = true; + for m in &*manage_identifiers { + if let MatchingRule::Simple(m) = m + && m.id.eq(id) + { + should_push = false; + } + } + + if should_push { + manage_identifiers.push(MatchingRule::Simple(IdWithIdentifier { + kind: identifier, + id: id.clone(), + matching_strategy: Option::from(MatchingStrategy::Legacy), + })); + } + } } self.update_known_window_ids(); diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index b5796764..12b00e92 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -4,6 +4,7 @@ use chrono::Utc; use clap::Parser; use color_eyre::eyre; use fs_tail::TailedFile; +use komorebi_client::ApplicationIdentifier; use komorebi_client::Axis; use komorebi_client::CycleDirection; use komorebi_client::DefaultLayout; @@ -143,6 +144,89 @@ gen_named_target_subcommand_args! { ClearNamedWorkspaceLayoutRules } +macro_rules! gen_application_target_subcommand_args { + // SubCommand Pattern + ( $( $name:ident ),+ $(,)? ) => { + $( + #[derive(clap::Parser)] + pub struct $name { + #[clap(value_enum)] + identifier: ApplicationIdentifier, + /// Identifier as a string + id: String, + } + )+ + }; +} + +gen_application_target_subcommand_args! { + IgnoreRule, + ManageRule, + IdentifyTrayApplication, + IdentifyLayeredApplication, + IdentifyObjectNameChangeApplication, + IdentifyBorderOverflowApplication, + RemoveTitleBar, +} + +#[derive(Parser)] +struct InitialWorkspaceRule { + #[clap(value_enum)] + identifier: ApplicationIdentifier, + /// Identifier as a string + id: String, + /// Monitor index (zero-indexed) + monitor: usize, + /// Workspace index on the specified monitor (zero-indexed) + workspace: usize, +} + +#[derive(Parser)] +struct InitialNamedWorkspaceRule { + #[clap(value_enum)] + identifier: ApplicationIdentifier, + /// Identifier as a string + id: String, + /// Name of a workspace + workspace: String, +} + +#[derive(Parser)] +struct WorkspaceRule { + #[clap(value_enum)] + identifier: ApplicationIdentifier, + /// Identifier as a string + id: String, + /// Monitor index (zero-indexed) + monitor: usize, + /// Workspace index on the specified monitor (zero-indexed) + workspace: usize, +} + +#[derive(Parser)] +struct NamedWorkspaceRule { + #[clap(value_enum)] + identifier: ApplicationIdentifier, + /// Identifier as a string + id: String, + /// Name of a workspace + workspace: String, +} + +#[derive(Parser)] +struct ClearWorkspaceRules { + /// Monitor index (zero-indexed) + monitor: usize, + /// Workspace index on the specified monitor (zero-indexed) + workspace: usize, +} + +#[derive(Parser)] +struct ClearNamedWorkspaceRules { + /// Name of a workspace + workspace: String, +} + #[derive(Parser)] struct Resize { #[clap(value_enum)] @@ -587,35 +671,35 @@ enum SubCommand { SessionFloatRules, /// Clear all session float rules ClearSessionFloatRules, - // /// Add a rule to ignore the specified application - // #[clap(arg_required_else_help = true)] - // #[clap(alias = "float-rule")] - // IgnoreRule(IgnoreRule), - // /// Add a rule to always manage the specified application - // #[clap(arg_required_else_help = true)] - // ManageRule(ManageRule), - // /// Add a rule to associate an application with a workspace on first show - // #[clap(arg_required_else_help = true)] - // InitialWorkspaceRule(InitialWorkspaceRule), - // /// Add a rule to associate an application with a named workspace on first show - // #[clap(arg_required_else_help = true)] - // InitialNamedWorkspaceRule(InitialNamedWorkspaceRule), - // /// Add a rule to associate an application with a workspace - // #[clap(arg_required_else_help = true)] - // WorkspaceRule(WorkspaceRule), - // /// Add a rule to associate an application with a named workspace - // #[clap(arg_required_else_help = true)] - // NamedWorkspaceRule(NamedWorkspaceRule), - // /// Remove all application association rules for a workspace by monitor and workspace index - // #[clap(arg_required_else_help = true)] - // ClearWorkspaceRules(ClearWorkspaceRules), - // /// Remove all application association rules for a named workspace - // #[clap(arg_required_else_help = true)] - // ClearNamedWorkspaceRules(ClearNamedWorkspaceRules), - // /// Remove all application association rules for all workspaces - // ClearAllWorkspaceRules, - // /// Enforce all workspace rules, including initial workspace rules that have already been applied - // EnforceWorkspaceRules, + /// Add a rule to ignore the specified application + #[clap(arg_required_else_help = true)] + #[clap(alias = "float-rule")] + IgnoreRule(IgnoreRule), + /// Add a rule to always manage the specified application + #[clap(arg_required_else_help = true)] + ManageRule(ManageRule), + /// Add a rule to associate an application with a workspace on first show + #[clap(arg_required_else_help = true)] + InitialWorkspaceRule(InitialWorkspaceRule), + /// Add a rule to associate an application with a named workspace on first show + #[clap(arg_required_else_help = true)] + InitialNamedWorkspaceRule(InitialNamedWorkspaceRule), + /// Add a rule to associate an application with a workspace + #[clap(arg_required_else_help = true)] + WorkspaceRule(WorkspaceRule), + /// Add a rule to associate an application with a named workspace + #[clap(arg_required_else_help = true)] + NamedWorkspaceRule(NamedWorkspaceRule), + /// Remove all application association rules for a workspace by monitor and workspace index + #[clap(arg_required_else_help = true)] + ClearWorkspaceRules(ClearWorkspaceRules), + /// Remove all application association rules for a named workspace + #[clap(arg_required_else_help = true)] + ClearNamedWorkspaceRules(ClearNamedWorkspaceRules), + /// Remove all application association rules for all workspaces + ClearAllWorkspaceRules, + /// Enforce all workspace rules, including initial workspace rules that have already been applied + EnforceWorkspaceRules, // /// Identify an application that sends EVENT_OBJECT_NAMECHANGE on launch // #[clap(arg_required_else_help = true)] // IdentifyObjectNameChangeApplication(IdentifyObjectNameChangeApplication), @@ -1104,6 +1188,57 @@ fn main() -> eyre::Result<()> { SubCommand::ClearSessionFloatRules => { send_message(&SocketMessage::ClearSessionFloatRules)?; } + SubCommand::IgnoreRule(arg) => { + send_message(&SocketMessage::IgnoreRule(arg.identifier, arg.id))?; + } + SubCommand::ManageRule(arg) => { + send_message(&SocketMessage::ManageRule(arg.identifier, arg.id))?; + } + SubCommand::InitialWorkspaceRule(arg) => { + send_message(&SocketMessage::InitialWorkspaceRule( + arg.identifier, + arg.id, + arg.monitor, + arg.workspace, + ))?; + } + SubCommand::InitialNamedWorkspaceRule(arg) => { + send_message(&SocketMessage::InitialNamedWorkspaceRule( + arg.identifier, + arg.id, + arg.workspace, + ))?; + } + SubCommand::WorkspaceRule(arg) => { + send_message(&SocketMessage::WorkspaceRule( + arg.identifier, + arg.id, + arg.monitor, + arg.workspace, + ))?; + } + SubCommand::NamedWorkspaceRule(arg) => { + send_message(&SocketMessage::NamedWorkspaceRule( + arg.identifier, + arg.id, + arg.workspace, + ))?; + } + SubCommand::ClearWorkspaceRules(arg) => { + send_message(&SocketMessage::ClearWorkspaceRules( + arg.monitor, + arg.workspace, + ))?; + } + SubCommand::ClearNamedWorkspaceRules(arg) => { + send_message(&SocketMessage::ClearNamedWorkspaceRules(arg.workspace))?; + } + SubCommand::ClearAllWorkspaceRules => { + send_message(&SocketMessage::ClearAllWorkspaceRules)?; + } + SubCommand::EnforceWorkspaceRules => { + send_message(&SocketMessage::EnforceWorkspaceRules)?; + } } Ok(())