diff --git a/komorebi/src/live_resize.rs b/komorebi/src/live_resize.rs index 5cbc6e87..7b3377e6 100644 --- a/komorebi/src/live_resize.rs +++ b/komorebi/src/live_resize.rs @@ -27,6 +27,7 @@ use crate::core::AnimationStyle; use crate::core::OperationDirection; use crate::core::Rect; use crate::core::Sizing; +use crate::monitor::MonitorExt; use crate::window::Window; use crate::window_manager::WindowManager; use crate::windows_backend::backend; diff --git a/komorebi/src/monitor.rs b/komorebi/src/monitor.rs index 6c70adb1..774de709 100644 --- a/komorebi/src/monitor.rs +++ b/komorebi/src/monitor.rs @@ -1,6 +1,5 @@ use crate::workspace::WorkspaceExt; use std::collections::HashMap; -use std::collections::VecDeque; use std::sync::atomic::Ordering; use color_eyre::eyre; @@ -19,16 +18,17 @@ use komorebi_core::ContainerWindow; use crate::DEFAULT_CONTAINER_PADDING; use crate::DEFAULT_WORKSPACE_PADDING; use crate::DefaultLayout; -use crate::FloatingLayerBehaviour; use crate::Layout; use crate::OperationDirection; -use crate::Wallpaper; use crate::WindowsApi; use crate::container::Container; use crate::ring::Ring; +use crate::workspace::WindowsPlatform; use crate::workspace::Workspace; use crate::workspace::WorkspaceGlobals; use crate::workspace::WorkspaceLayer; +use serde::Deserializer; +use serde::Serializer; /// `Monitor::id` is a `MonitorId` in Rust and the same plain JSON number as the `isize` handle it /// was before: `komorebic state` and the saved state files do not change. @@ -46,33 +46,57 @@ mod hmonitor_json { Ok(MonitorId::from_hmonitor(isize::deserialize(deserializer)?)) } } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] + +/// The fields only a Windows monitor has, written flat into the monitor's JSON +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -pub struct Monitor { - #[serde(with = "hmonitor_json")] - #[cfg_attr(feature = "schemars", schemars(with = "isize"))] - pub id: MonitorId, +pub struct WindowsMonitorExtra { + /// The name of the display (`DISPLAY1`) pub name: String, - pub device: String, + /// The device id of the display pub device_id: String, - pub serial_number_id: Option, - pub size: Rect, - pub work_area_size: Rect, - pub work_area_offset: Option, - pub window_based_work_area_offset: Option, - pub window_based_work_area_offset_limit: isize, - pub workspaces: Ring, - #[serde(skip_serializing_if = "Option::is_none")] - pub last_focused_workspace: Option, + /// The latest known name of each workspace, by index. A flattened map is read through a buffer + /// that keeps its keys as strings, so they are parsed here. + #[serde(deserialize_with = "workspace_names_from_json")] pub workspace_names: HashMap, - pub container_padding: Option, - pub workspace_padding: Option, - pub wallpaper: Option, - pub floating_layer_behaviour: Option, } -impl_ring_elements!(Monitor, Workspace); +fn workspace_names_from_json<'de, D: Deserializer<'de>>( + deserializer: D, +) -> Result, D::Error> { + HashMap::::deserialize(deserializer)? + .into_iter() + .map(|(key, name)| { + key.parse() + .map(|key| (key, name)) + .map_err(serde::de::Error::custom) + }) + .collect() +} +/// The monitor over the Windows [`Window`], which is `komorebi_core::Monitor` with this platform's +/// methods added by [`MonitorExt`] +pub type Monitor = komorebi_core::Monitor; + +impl komorebi_core::MonitorPlatform for WindowsPlatform { + type Id = MonitorId; + type SerialNumber = Option; + type Extra = WindowsMonitorExtra; + #[cfg(feature = "schemars")] + type IdSchema = isize; + + fn serialize_id(id: &MonitorId, serializer: S) -> Result { + hmonitor_json::serialize(id, serializer) + } + + fn deserialize_id<'de, D: Deserializer<'de>>(deserializer: D) -> Result { + hmonitor_json::deserialize(deserializer) + } + + fn known_workspace_name(monitor: &Monitor, idx: usize) -> Option { + monitor.extra.workspace_names.get(&idx).cloned() + } +} #[derive(Serialize)] pub struct MonitorInformation { pub id: isize, @@ -87,9 +111,9 @@ impl From<&Monitor> for MonitorInformation { fn from(monitor: &Monitor) -> Self { Self { id: monitor.id.to_hmonitor(), - name: monitor.name.clone(), + name: monitor.extra.name.clone(), device: monitor.device.clone(), - device_id: monitor.device_id.clone(), + device_id: monitor.extra.device_id.clone(), serial_number_id: monitor.serial_number_id.clone(), size: monitor.size, } @@ -110,9 +134,7 @@ pub fn new( Monitor { id: MonitorId::from_hmonitor(id), - name, device, - device_id, serial_number_id, size, work_area_size, @@ -121,7 +143,11 @@ pub fn new( window_based_work_area_offset_limit: 1, workspaces, last_focused_workspace: None, - workspace_names: HashMap::default(), + extra: WindowsMonitorExtra { + name, + device_id, + workspace_names: HashMap::default(), + }, container_padding: None, workspace_padding: None, wallpaper: None, @@ -129,8 +155,50 @@ pub fn new( } } -impl Monitor { - pub fn new( +pub trait MonitorExt { + fn new( + id: isize, + size: Rect, + work_area_size: Rect, + name: String, + device: String, + device_id: String, + serial_number_id: Option, + ) -> Self; + + fn placeholder() -> Self; + + fn load_focused_workspace(&mut self, mouse_follows_focus: bool) -> eyre::Result<()>; + + /// Updates the `globals` field of all workspaces + fn update_workspaces_globals(&mut self, offset: Option); + + /// Updates the `globals` field of workspace with index `workspace_idx` + fn update_workspace_globals(&mut self, workspace_idx: usize, offset: Option); + + /// Adds a container to this `Monitor` using the move direction to calculate if the container + /// should be added in front of all containers, in the back or in place of the focused + /// container, moving the rest along. The move direction should be from the origin monitor + /// towards the target monitor or from the origin workspace towards the target workspace. + fn add_container_with_direction( + &mut self, + container: Container, + workspace_idx: Option, + direction: OperationDirection, + ) -> eyre::Result<()>; + + fn move_container_to_workspace( + &mut self, + target_workspace_idx: usize, + follow: bool, + direction: Option, + ) -> eyre::Result<()>; + + fn update_focused_workspace(&mut self, offset: Option) -> eyre::Result<()>; +} + +impl MonitorExt for Monitor { + fn new( id: isize, size: Rect, work_area_size: Rect, @@ -150,12 +218,10 @@ impl Monitor { ) } - pub fn placeholder() -> Self { + fn placeholder() -> Self { Self { id: MonitorId(0), - name: "PLACEHOLDER".to_string(), device: "".to_string(), - device_id: "".to_string(), serial_number_id: None, size: Default::default(), work_area_size: Default::default(), @@ -164,7 +230,11 @@ impl Monitor { window_based_work_area_offset_limit: 0, workspaces: Default::default(), last_focused_workspace: None, - workspace_names: Default::default(), + extra: WindowsMonitorExtra { + name: "PLACEHOLDER".to_string(), + device_id: "".to_string(), + workspace_names: Default::default(), + }, container_padding: None, workspace_padding: None, wallpaper: None, @@ -172,23 +242,7 @@ impl Monitor { } } - pub fn focused_workspace_name(&self) -> Option { - self.focused_workspace() - .map(|w| w.name.clone()) - .unwrap_or(None) - } - - pub fn focused_workspace_layout(&self) -> Option { - self.focused_workspace().and_then(|workspace| { - if workspace.tile { - Some(workspace.layout.clone()) - } else { - None - } - }) - } - - pub fn load_focused_workspace(&mut self, mouse_follows_focus: bool) -> eyre::Result<()> { + fn load_focused_workspace(&mut self, mouse_follows_focus: bool) -> eyre::Result<()> { let focused_idx = self.focused_workspace_idx(); let hmonitor = self.id.to_hmonitor(); let monitor_wp = self.wallpaper.clone(); @@ -203,8 +257,7 @@ impl Monitor { Ok(()) } - /// Updates the `globals` field of all workspaces - pub fn update_workspaces_globals(&mut self, offset: Option) { + fn update_workspaces_globals(&mut self, offset: Option) { let container_padding = self .container_padding .or(Some(DEFAULT_CONTAINER_PADDING.load(Ordering::SeqCst))); @@ -242,8 +295,7 @@ impl Monitor { } } - /// Updates the `globals` field of workspace with index `workspace_idx` - pub fn update_workspace_globals(&mut self, workspace_idx: usize, offset: Option) { + fn update_workspace_globals(&mut self, workspace_idx: usize, offset: Option) { let container_padding = self .container_padding .or(Some(DEFAULT_CONTAINER_PADDING.load(Ordering::SeqCst))); @@ -281,30 +333,7 @@ impl Monitor { } } - pub fn add_container( - &mut self, - container: Container, - workspace_idx: Option, - ) -> eyre::Result<()> { - let workspace = if let Some(idx) = workspace_idx { - self.workspaces_mut() - .get_mut(idx) - .ok_or_eyre(format!("there is no workspace at index {idx}"))? - } else { - self.focused_workspace_mut() - .ok_or_eyre("there is no workspace")? - }; - - workspace.add_container_to_back(container); - - Ok(()) - } - - /// Adds a container to this `Monitor` using the move direction to calculate if the container - /// should be added in front of all containers, in the back or in place of the focused - /// container, moving the rest along. The move direction should be from the origin monitor - /// towards the target monitor or from the origin workspace towards the target workspace. - pub fn add_container_with_direction( + fn add_container_with_direction( &mut self, container: Container, workspace_idx: Option, @@ -376,33 +405,8 @@ impl Monitor { Ok(()) } - pub fn remove_workspace_by_idx(&mut self, idx: usize) -> Option { - if idx < self.workspaces().len() { - return self.workspaces_mut().remove(idx); - } - - if idx == 0 { - self.workspaces_mut().push_back(Workspace::default()); - } else { - self.focus_workspace(idx.saturating_sub(1)).ok()?; - }; - - None - } - - pub fn ensure_workspace_count(&mut self, ensure_count: usize) { - if self.workspaces().len() < ensure_count { - self.workspaces_mut() - .resize(ensure_count, Workspace::default()); - } - } - - pub fn remove_workspaces(&mut self) -> VecDeque { - std::mem::take(self.workspaces_mut()) - } - #[tracing::instrument(skip(self))] - pub fn move_container_to_workspace( + fn move_container_to_workspace( &mut self, target_workspace_idx: usize, follow: bool, @@ -485,39 +489,7 @@ impl Monitor { Ok(()) } - #[tracing::instrument(skip(self))] - pub fn focus_workspace(&mut self, idx: usize) -> eyre::Result<()> { - tracing::info!("focusing workspace"); - - { - let workspaces = self.workspaces_mut(); - - if workspaces.get(idx).is_none() { - workspaces.resize(idx + 1, Workspace::default()); - } - self.last_focused_workspace = Some(self.workspaces.focused_idx()); - self.workspaces.focus(idx); - } - - // Always set the latest known name when creating the workspace for the first time - { - let name = { self.workspace_names.get(&idx).cloned() }; - if name.is_some() { - self.workspaces_mut() - .get_mut(idx) - .ok_or_eyre("there is no workspace")? - .name = name; - } - } - - Ok(()) - } - - pub fn new_workspace_idx(&self) -> usize { - self.workspaces().len() - } - - pub fn update_focused_workspace(&mut self, offset: Option) -> eyre::Result<()> { + fn update_focused_workspace(&mut self, offset: Option) -> eyre::Result<()> { let offset = if self.work_area_offset.is_some() { self.work_area_offset } else { diff --git a/komorebi/src/monitor_reconciliator/mod.rs b/komorebi/src/monitor_reconciliator/mod.rs index 58249c3b..64c5aa85 100644 --- a/komorebi/src/monitor_reconciliator/mod.rs +++ b/komorebi/src/monitor_reconciliator/mod.rs @@ -12,6 +12,8 @@ use crate::config_generation::WorkspaceMatchingRule; use crate::core::Rect; use crate::monitor; use crate::monitor::Monitor; +use crate::monitor::MonitorExt; +use crate::monitor::WindowsMonitorExtra; use crate::monitor_reconciliator::hidden::Hidden; use crate::notify_subscribers; use crate::state::State; @@ -106,8 +108,8 @@ pub fn display_change_in_progress(grace_period: std::time::Duration) -> bool { pub fn insert_in_monitor_cache(serial_or_device_id: &str, monitor: Monitor) { let dip = DISPLAY_INDEX_PREFERENCES.read(); let mut dip_ids = dip.values(); - let preferred_id = if dip_ids.any(|id| id.eq(&monitor.device_id)) { - monitor.device_id.clone() + let preferred_id = if dip_ids.any(|id| id.eq(&monitor.extra.device_id)) { + monitor.extra.device_id.clone() } else if dip_ids.any(|id| Some(id) == monitor.serial_number_id.as_ref()) { monitor.serial_number_id.clone().unwrap_or_default() } else { @@ -307,13 +309,13 @@ where } if should_update { - tracing::info!("updated work area for {}", monitor.device_id); + tracing::info!("updated work area for {}", monitor.extra.device_id); monitor.update_focused_workspace(offset)?; border_manager::send_notification(None); } else { tracing::debug!( "work areas match, reconciliation not required for {}", - monitor.device_id + monitor.extra.device_id ); } } @@ -352,7 +354,7 @@ where if should_update { tracing::info!( "updated monitor resolution/scaling for {}", - monitor.device_id + monitor.extra.device_id ); monitor.update_focused_workspace(offset)?; @@ -360,7 +362,7 @@ where } else { tracing::debug!( "resolutions match, reconciliation not required for {}", - monitor.device_id + monitor.extra.device_id ); } } @@ -387,9 +389,9 @@ where if same_display(attached, monitor) { monitor.id = attached.id; monitor.device = attached.device.clone(); - monitor.device_id = attached.device_id.clone(); + monitor.extra.device_id = attached.extra.device_id.clone(); monitor.serial_number_id = attached.serial_number_id.clone(); - monitor.name = attached.name.clone(); + monitor.extra.name = attached.extra.name.clone(); monitor.size = attached.size; monitor.work_area_size = attached.work_area_size; } @@ -458,9 +460,9 @@ where if same_display(attached, monitor) { monitor.id = attached.id; monitor.device = attached.device.clone(); - monitor.device_id = attached.device_id.clone(); + monitor.extra.device_id = attached.extra.device_id.clone(); monitor.serial_number_id = attached.serial_number_id.clone(); - monitor.name = attached.name.clone(); + monitor.extra.name = attached.extra.name.clone(); monitor.size = attached.size; monitor.work_area_size = attached.work_area_size; } @@ -545,9 +547,9 @@ where if same_display(attached, monitor) { monitor.id = attached.id; monitor.device = attached.device.clone(); - monitor.device_id = attached.device_id.clone(); + monitor.extra.device_id = attached.extra.device_id.clone(); monitor.serial_number_id = attached.serial_number_id.clone(); - monitor.name = attached.name.clone(); + monitor.extra.name = attached.extra.name.clone(); monitor.size = attached.size; monitor.work_area_size = attached.work_area_size; } @@ -576,7 +578,7 @@ where let id = m .serial_number_id .as_ref() - .map_or(m.device_id.clone(), |sn| sn.clone()); + .map_or(m.extra.device_id.clone(), |sn| sn.clone()); let focused_workspace_idx = m.focused_workspace_idx(); @@ -653,8 +655,8 @@ where // the user set as preference as the id. let dip = DISPLAY_INDEX_PREFERENCES.read(); let mut dip_ids = dip.values(); - let preferred_id = if dip_ids.any(|id| id.eq(&m.device_id)) { - m.device_id.clone() + let preferred_id = if dip_ids.any(|id| id.eq(&m.extra.device_id)) { + m.extra.device_id.clone() } else if dip_ids.any(|id| Some(id) == m.serial_number_id.as_ref()) { m.serial_number_id.clone().unwrap_or_default() } else { @@ -703,7 +705,7 @@ where let post_removal_device_ids = wm .monitors() .iter() - .map(|m| &m.device_id) + .map(|m| &m.extra.device_id) .cloned() .collect::>(); @@ -726,7 +728,7 @@ where // Look in the updated state for new monitors for (i, m) in wm.monitors_mut().iter_mut().enumerate() { - let device_id = &m.device_id; + let device_id = &m.extra.device_id; // We identify a new monitor when we encounter a new device id if !post_removal_device_ids.contains(device_id) { let mut cache_hit = false; @@ -750,9 +752,7 @@ where *m = Monitor { // Data that should be the one just read from `win32-display-data` id: m.id, - name: m.name.clone(), device: m.device.clone(), - device_id: m.device_id.clone(), serial_number_id: m.serial_number_id.clone(), size: m.size, work_area_size: m.work_area_size, @@ -765,7 +765,11 @@ where .window_based_work_area_offset_limit, workspaces: cached.workspaces.clone(), last_focused_workspace: cached.last_focused_workspace, - workspace_names: cached.workspace_names.clone(), + extra: WindowsMonitorExtra { + name: m.extra.name.clone(), + device_id: m.extra.device_id.clone(), + workspace_names: cached.extra.workspace_names.clone(), + }, container_padding: cached.container_padding, workspace_padding: cached.workspace_padding, wallpaper: cached.wallpaper.clone(), @@ -976,8 +980,8 @@ fn same_display(attached: &Monitor, monitor: &Monitor) -> bool { } same_device( - (&attached.device_id, &attached.name), - (&monitor.device_id, &monitor.name), + (&attached.extra.device_id, &attached.extra.name), + (&monitor.extra.device_id, &monitor.extra.name), ) } @@ -1278,14 +1282,14 @@ mod tests { assert_eq!(monitors[0].id.to_hmonitor(), 1); // device name - assert_eq!(monitors[0].name, String::from("DISPLAY1")); + assert_eq!(monitors[0].extra.name, String::from("DISPLAY1")); // Device assert_eq!(monitors[0].device, String::from("ABC123")); // Device ID assert_eq!( - monitors[0].device_id, + monitors[0].extra.device_id, String::from("ABC123-4&123456&0&UID0") ); diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 555173ec..d091ad92 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -1,3 +1,4 @@ +use crate::monitor::MonitorExt; use crate::scrolling::WorkspaceScrolling; use crate::workspace::WorkspaceExt; use color_eyre::eyre; @@ -978,7 +979,7 @@ impl WindowManager { for monitor in self.monitors() { if let Some(ws) = monitor.focused_workspace() { monitor_visible_windows.insert( - monitor.device_id.clone(), + monitor.extra.device_id.clone(), ws.visible_window_details().clone(), ); } diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index df59a5f5..121b1742 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -1,3 +1,4 @@ +use crate::monitor::MonitorExt; use crate::workspace::WorkspaceExt; use std::process::Command; use std::sync::Arc; diff --git a/komorebi/src/state.rs b/komorebi/src/state.rs index c6102625..78270b0a 100644 --- a/komorebi/src/state.rs +++ b/komorebi/src/state.rs @@ -31,6 +31,7 @@ use crate::border_manager::STYLE; use crate::config_generation::MatchingRule; use crate::config_generation::WorkspaceMatchingRule; use crate::monitor::Monitor; +use crate::monitor::WindowsMonitorExtra; use crate::ring::Ring; use crate::stackbar_manager::STACKBAR_FOCUSED_TEXT_COLOUR; use crate::stackbar_manager::STACKBAR_LABEL; @@ -234,9 +235,7 @@ impl From<&WindowManager> for State { .iter() .map(|monitor| Monitor { id: monitor.id, - name: monitor.name.clone(), device: monitor.device.clone(), - device_id: monitor.device_id.clone(), serial_number_id: monitor.serial_number_id.clone(), size: monitor.size, work_area_size: monitor.work_area_size, @@ -294,7 +293,11 @@ impl From<&WindowManager> for State { ws }, last_focused_workspace: monitor.last_focused_workspace, - workspace_names: monitor.workspace_names.clone(), + extra: WindowsMonitorExtra { + name: monitor.extra.name.clone(), + device_id: monitor.extra.device_id.clone(), + workspace_names: monitor.extra.workspace_names.clone(), + }, container_padding: monitor.container_padding, workspace_padding: monitor.workspace_padding, wallpaper: monitor.wallpaper.clone(), diff --git a/komorebi/src/static_config.rs b/komorebi/src/static_config.rs index 18506012..c508cc41 100644 --- a/komorebi/src/static_config.rs +++ b/komorebi/src/static_config.rs @@ -62,6 +62,7 @@ use crate::core::config_generation::MatchingStrategy; use crate::current_virtual_desktop; use crate::monitor; use crate::monitor::Monitor; +use crate::monitor::MonitorExt; use crate::monitor_reconciliator; use crate::ring::Ring; use crate::stackbar_manager::STACKBAR_FOCUSED_TEXT_COLOUR; @@ -218,8 +219,8 @@ pub fn workspace_config(value: &Workspace) -> WorkspaceConfig { } } -impl From<&Monitor> for MonitorConfig { - fn from(value: &Monitor) -> Self { +pub fn monitor_config(value: &Monitor) -> MonitorConfig { + { let mut workspaces = vec![]; for w in value.workspaces() { workspaces.push(workspace_config(w)); @@ -244,7 +245,7 @@ impl From<&Monitor> for MonitorConfig { } }); - Self { + MonitorConfig { workspaces, work_area_offset: value.work_area_offset, window_based_work_area_offset: value.window_based_work_area_offset, @@ -265,7 +266,7 @@ impl From<&WindowManager> for StaticConfig { fn from(value: &WindowManager) -> Self { let mut monitors = vec![]; for m in value.monitors() { - monitors.push(MonitorConfig::from(m)); + monitors.push(monitor_config(m)); } let border_colours = if border_manager::FOCUSED.load(Ordering::SeqCst) == 0 { @@ -858,7 +859,7 @@ impl StaticConfigExt for StaticConfig { 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)) + || monitor.extra.device_id.eq(id)) .then_some(*c_idx) }) }; @@ -909,7 +910,7 @@ impl StaticConfigExt for StaticConfig { let id = monitor .serial_number_id .as_ref() - .map_or(&monitor.device_id, |sn| sn); + .map_or(&monitor.extra.device_id, |sn| sn); monitor_reconciliator::insert_in_monitor_cache(id, monitor.clone()); } @@ -1023,7 +1024,7 @@ impl StaticConfigExt for StaticConfig { 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)) + || monitor.extra.device_id.eq(id)) .then_some(*c_idx) }) }; @@ -1077,7 +1078,7 @@ impl StaticConfigExt for StaticConfig { let id = monitor .serial_number_id .as_ref() - .map_or(&monitor.device_id, |sn| sn); + .map_or(&monitor.extra.device_id, |sn| sn); monitor_reconciliator::insert_in_monitor_cache(id, monitor.clone()); } diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index c8b19863..7affaebd 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -1,3 +1,4 @@ +use crate::monitor::MonitorExt; use crate::scrolling::WorkspaceScrolling; use crate::workspace::WorkspaceExt; use std::collections::HashMap; @@ -3708,7 +3709,7 @@ impl WindowManager { .ok_or_eyre("there is no monitor")?; workspace.name = Option::from(name.clone()); - monitor.workspace_names.insert(workspace_idx, name); + monitor.extra.workspace_names.insert(workspace_idx, name); Ok(()) } @@ -3777,7 +3778,7 @@ impl WindowManager { // info taken from win32_display_data and update our hmonitor while we're at it if let Ok(latest) = WindowsApi::monitor(hmonitor) { for (i, monitor) in self.monitors_mut().iter_mut().enumerate() { - if monitor.device_id == latest.device_id { + if monitor.extra.device_id == latest.extra.device_id { monitor.id = latest.id; return Option::from(i); } @@ -3800,7 +3801,7 @@ impl WindowManager { // info taken from win32_display_data and update our hmonitor while we're at it if let Ok(latest) = WindowsApi::monitor(hmonitor) { for (i, monitor) in self.monitors_mut().iter_mut().enumerate() { - if monitor.device_id == latest.device_id { + if monitor.extra.device_id == latest.extra.device_id { monitor.id = latest.id; return Option::from(i); } diff --git a/komorebi/src/windows_api.rs b/komorebi/src/windows_api.rs index 6c698b23..625245d9 100644 --- a/komorebi/src/windows_api.rs +++ b/komorebi/src/windows_api.rs @@ -1,3 +1,4 @@ +use crate::monitor::MonitorExt; use crate::workspace::WorkspaceExt; use color_eyre::eyre; use color_eyre::eyre::Error; @@ -335,7 +336,10 @@ impl WindowsApi { let name = name.split('\\').collect::>()[0].to_string(); for monitor in monitors.elements() { - if same_device((&device_id, &name), (&monitor.device_id, &monitor.name)) { + if same_device( + (&device_id, &name), + (&monitor.extra.device_id, &monitor.extra.name), + ) { continue 'read; } } @@ -367,7 +371,9 @@ impl WindowsApi { let display_index_preferences = DISPLAY_INDEX_PREFERENCES.read(); for (index, id) in &*display_index_preferences { - if m.serial_number_id.as_ref().is_some_and(|sn| sn == id) || id.eq(&m.device_id) { + if m.serial_number_id.as_ref().is_some_and(|sn| sn == id) + || id.eq(&m.extra.device_id) + { index_preference = Option::from(index); } } @@ -380,7 +386,7 @@ impl WindowsApi { let current_name = monitors .elements_mut() .get(*preference) - .map_or("", |m| &m.name); + .map_or("", |m| &m.extra.name); if current_name == "PLACEHOLDER" { let _ = monitors.elements_mut().remove(*preference); monitors.elements_mut().insert(*preference, m); @@ -392,14 +398,16 @@ impl WindowsApi { } } - monitors.elements_mut().retain(|m| m.name.ne("PLACEHOLDER")); + monitors + .elements_mut() + .retain(|m| m.extra.name.ne("PLACEHOLDER")); // Rebuild monitor index map *monitor_usr_idx_map = HashMap::new(); let mut added_monitor_idxs = Vec::new(); for (index, id) in &*DISPLAY_INDEX_PREFERENCES.read() { if let Some(m_idx) = monitors.elements().iter().position(|m| { - m.serial_number_id.as_ref().is_some_and(|sn| sn == id) || m.device_id.eq(id) + m.serial_number_id.as_ref().is_some_and(|sn| sn == id) || m.extra.device_id.eq(id) }) { monitor_usr_idx_map.insert(*index, m_idx); added_monitor_idxs.push(m_idx); @@ -437,7 +445,7 @@ impl WindowsApi { pub fn load_workspace_information(monitors: &mut Ring) -> eyre::Result<()> { for monitor in monitors.elements_mut() { - let monitor_name = monitor.name.clone(); + let monitor_name = monitor.extra.name.clone(); if let Some(workspace) = monitor.workspaces_mut().front_mut() { // EnumWindows will enumerate through windows on all monitors Self::enum_windows( @@ -486,6 +494,7 @@ impl WindowsApi { Ok(Self::monitor( unsafe { MonitorFromWindow(HWND(as_ptr!(hwnd)), MONITOR_DEFAULTTONEAREST) }.0 as isize, )? + .extra .name .to_string()) } diff --git a/komorebi/src/windows_backend.rs b/komorebi/src/windows_backend.rs index febc1a04..978b2865 100644 --- a/komorebi/src/windows_backend.rs +++ b/komorebi/src/windows_backend.rs @@ -113,7 +113,7 @@ impl Backend for WindowsBackend { id: monitor.id, size: monitor.size, work_area: monitor.work_area_size, - name: Some(monitor.name), + name: Some(monitor.extra.name), serial_number_id: monitor.serial_number_id, }) })