From c66db5a0bd264949c8c98ee87c56eafbca3a34e9 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Thu, 25 Sep 2025 14:23:41 -0700 Subject: [PATCH] feat(cli): implement display index preference cmd This commit implements the komorebic display-index-preference command for updating display index preferences, however the monitor reconciliator module has not been reimplemented completely yet so these values will not have any effect. --- komorebi/src/core/mod.rs | 2 +- komorebi/src/lib.rs | 3 ++ komorebi/src/process_command.rs | 6 ++- komorebi/src/static_config.rs | 68 ++++++++++++++++----------------- komorebic/src/main.rs | 20 ++++++++-- 5 files changed, 60 insertions(+), 39 deletions(-) diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index bf80cf9c..60451749 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -279,7 +279,7 @@ pub enum SocketMessage { ToggleWorkspaceFloatOverride, // Monitor and Workspace Commands // MonitorIndexPreference(usize, i32, i32, i32, i32), - // DisplayIndexPreference(usize, String), + DisplayIndexPreference(usize, String), EnsureWorkspaces(usize, usize), EnsureNamedWorkspaces(usize, Vec), NewWorkspace, diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index 90a89b70..e7c157ba 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -29,6 +29,7 @@ use objc2_core_foundation::CGPoint; use objc2_core_foundation::CGRect; use objc2_core_foundation::CGSize; use parking_lot::Mutex; +use parking_lot::RwLock; use regex::Regex; use serde::Deserialize; use serde::Serialize; @@ -94,6 +95,8 @@ lazy_static! { static ref FLOATING_WINDOW_TOGGLE_ASPECT_RATIO: Arc> = Arc::new(Mutex::new( AspectRatio::Predefined(PredefinedAspectRatio::Widescreen) )); + static ref DISPLAY_INDEX_PREFERENCES: Arc>> = + Arc::new(RwLock::new(HashMap::new())); 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)); diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index f48ef02f..e43bfdcc 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -1,4 +1,5 @@ use crate::DATA_DIR; +use crate::DISPLAY_INDEX_PREFERENCES; use crate::FLOATING_APPLICATIONS; use crate::IGNORE_IDENTIFIERS; use crate::MANAGE_IDENTIFIERS; @@ -1624,8 +1625,11 @@ impl WindowManager { } } } + SocketMessage::DisplayIndexPreference(index_preference, ref display) => { + let mut display_index_preferences = DISPLAY_INDEX_PREFERENCES.write(); + display_index_preferences.insert(index_preference, display.clone()); + } } - notify_subscribers( Notification { event: NotificationEvent::Socket(message.clone()), diff --git a/komorebi/src/static_config.rs b/komorebi/src/static_config.rs index 58759718..b0030726 100644 --- a/komorebi/src/static_config.rs +++ b/komorebi/src/static_config.rs @@ -2,6 +2,7 @@ use crate::CoreFoundationRunLoop; use crate::DATA_DIR; use crate::DEFAULT_CONTAINER_PADDING; use crate::DEFAULT_WORKSPACE_PADDING; +use crate::DISPLAY_INDEX_PREFERENCES; use crate::FLOATING_APPLICATIONS; use crate::FLOATING_WINDOW_TOGGLE_ASPECT_RATIO; use crate::IGNORE_IDENTIFIERS; @@ -480,9 +481,9 @@ pub struct StaticConfig { // /// Set monitor index preferences // #[serde(skip_serializing_if = "Option::is_none")] // pub monitor_index_preferences: Option>, - // /// Set display index preferences - // #[serde(skip_serializing_if = "Option::is_none")] - // pub display_index_preferences: Option>, + /// Set display index preferences + #[serde(skip_serializing_if = "Option::is_none")] + pub display_index_preferences: Option>, // /// Stackbar configuration options // #[serde(skip_serializing_if = "Option::is_none")] // pub stackbar: Option, @@ -772,7 +773,7 @@ impl From<&WindowManager> for StaticConfig { object_name_change_applications: None, object_name_change_title_ignore_list: None, // monitor_index_preferences: Option::from(MONITOR_INDEX_PREFERENCES.lock().clone()), - // display_index_preferences: Option::from(DISPLAY_INDEX_PREFERENCES.read().clone()), + display_index_preferences: Option::from(DISPLAY_INDEX_PREFERENCES.read().clone()), // stackbar: None, // animation: None, // theme: None, @@ -800,10 +801,10 @@ impl StaticConfig { // preferences.clone_from(monitor_index_preferences); // } // - // if let Some(display_index_preferences) = &self.display_index_preferences { - // let mut preferences = DISPLAY_INDEX_PREFERENCES.write(); - // preferences.clone_from(display_index_preferences); - // } + if let Some(display_index_preferences) = &self.display_index_preferences { + let mut preferences = DISPLAY_INDEX_PREFERENCES.write(); + preferences.clone_from(display_index_preferences); + } // if let Some(behaviour) = self.window_hiding_behaviour { // let mut window_hiding_behaviour = HIDING_BEHAVIOUR.lock(); @@ -1197,9 +1198,8 @@ impl StaticConfig { let mut value = Self::read(path)?; let mut wm = wm.lock(); - // let configs_with_preference: Vec<_> = - // DISPLAY_INDEX_PREFERENCES.read().keys().copied().collect(); - let configs_with_preference = []; + let configs_with_preference: Vec<_> = + DISPLAY_INDEX_PREFERENCES.read().keys().copied().collect(); let mut configs_used = Vec::new(); let mut workspace_matching_rules = WORKSPACE_MATCHING_RULES.lock(); @@ -1210,16 +1210,17 @@ impl StaticConfig { let offset = wm.work_area_offset; for (i, monitor) in wm.monitors_mut().iter_mut().enumerate() { - let preferred_config_idx = None; - // let preferred_config_idx = { - // let display_index_preferences = DISPLAY_INDEX_PREFERENCES.read(); - // let c_idx = display_index_preferences.iter().find_map(|(c_idx, id)| { - // (monitor.serial_number_id.as_ref().is_some_and(|sn| sn == id) - // || monitor.device_id.eq(id)) - // .then_some(*c_idx) - // }); - // c_idx - // }; + let preferred_config_idx = { + let display_index_preferences = DISPLAY_INDEX_PREFERENCES.read(); + + display_index_preferences.iter().find_map(|(c_idx, id)| { + if monitor.serial_number_id.eq(id) { + Some(*c_idx) + } else { + None + } + }) + }; let idx = preferred_config_idx.or({ // Monitor without preferred config idx. @@ -1374,9 +1375,8 @@ impl StaticConfig { value.apply_globals()?; - // let configs_with_preference: Vec<_> = - // DISPLAY_INDEX_PREFERENCES.read().keys().copied().collect(); - let configs_with_preference = []; + let configs_with_preference: Vec<_> = + DISPLAY_INDEX_PREFERENCES.read().keys().copied().collect(); let mut configs_used = Vec::new(); let mut workspace_matching_rules = WORKSPACE_MATCHING_RULES.lock(); @@ -1385,17 +1385,17 @@ impl StaticConfig { let offset = wm.work_area_offset; for (i, monitor) in wm.monitors_mut().iter_mut().enumerate() { - // let preferred_config_idx = { - // let display_index_preferences = DISPLAY_INDEX_PREFERENCES.read(); - // let c_idx = display_index_preferences.iter().find_map(|(c_idx, id)| { - // (monitor.serial_number_id.as_ref().is_some_and(|sn| sn == id) - // || monitor.device_id.eq(id)) - // .then_some(*c_idx) - // }); - // c_idx - // }; + let preferred_config_idx = { + let display_index_preferences = DISPLAY_INDEX_PREFERENCES.read(); - let preferred_config_idx = None; + display_index_preferences.iter().find_map(|(c_idx, id)| { + if monitor.serial_number_id.eq(id) { + Some(*c_idx) + } else { + None + } + }) + }; let idx = preferred_config_idx.or({ // Monitor without preferred config idx. diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index aa4d3c81..51d96779 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -525,6 +525,14 @@ pub struct NamedWorkspaceLayoutRule { layout: DefaultLayout, } +#[derive(Parser)] +struct DisplayIndexPreference { + /// Preferred monitor index (zero-indexed) + index_preference: usize, + /// Display name as identified in komorebic state + display: String, +} + #[derive(Parser)] struct EnsureWorkspaces { /// Monitor index (zero-indexed) @@ -851,9 +859,9 @@ enum SubCommand { // /// Set the monitor index preference for a monitor identified using its size // #[clap(arg_required_else_help = true)] // MonitorIndexPreference(MonitorIndexPreference), - // /// Set the display index preference for a monitor identified using its display name - // #[clap(arg_required_else_help = true)] - // DisplayIndexPreference(DisplayIndexPreference), + /// Set the display index preference for a monitor identified using its display name + #[clap(arg_required_else_help = true)] + DisplayIndexPreference(DisplayIndexPreference), /// Create at least this many workspaces for the specified monitor #[clap(arg_required_else_help = true)] EnsureWorkspaces(EnsureWorkspaces), @@ -1802,6 +1810,12 @@ fn main() -> eyre::Result<()> { SubCommand::LoadResize(arg) => { send_message(&SocketMessage::Load(arg.path))?; } + SubCommand::DisplayIndexPreference(arg) => { + send_message(&SocketMessage::DisplayIndexPreference( + arg.index_preference, + arg.display, + ))?; + } } Ok(())