From c06d9afa3bbc144cd1c296cbea064d0562b4ae1d Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Tue, 27 Aug 2024 08:57:50 -0700 Subject: [PATCH] fix(wm): grow monitors vec to accomodate idx prefs This commit fixes a bug in load_monitor_information which resulted in an infinite while loop due to a misunderstanding of how VecDeque::reserve works. --- komorebi/src/monitor.rs | 16 ++++++++++++++++ komorebi/src/windows_api.rs | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/komorebi/src/monitor.rs b/komorebi/src/monitor.rs index 7aea0283..e7dee59e 100644 --- a/komorebi/src/monitor.rs +++ b/komorebi/src/monitor.rs @@ -88,6 +88,22 @@ pub fn new( } impl Monitor { + pub fn placeholder() -> Self { + Self { + id: 0, + name: "PLACEHOLDER".to_string(), + device: "".to_string(), + device_id: "".to_string(), + size: Default::default(), + work_area_size: Default::default(), + work_area_offset: None, + window_based_work_area_offset: None, + window_based_work_area_offset_limit: 0, + workspaces: Default::default(), + last_focused_workspace: None, + workspace_names: Default::default(), + } + } pub fn load_focused_workspace(&mut self, mouse_follows_focus: bool) -> Result<()> { let focused_idx = self.focused_workspace_idx(); for (i, workspace) in self.workspaces_mut().iter_mut().enumerate() { diff --git a/komorebi/src/windows_api.rs b/komorebi/src/windows_api.rs index 2ebe6a2a..b724dcc2 100644 --- a/komorebi/src/windows_api.rs +++ b/komorebi/src/windows_api.rs @@ -288,7 +288,7 @@ impl WindowsApi { monitors.elements_mut().push_back(m); } else if let Some(preference) = index_preference { while *preference > monitors.elements().len() { - monitors.elements_mut().reserve(1); + monitors.elements_mut().push_back(Monitor::placeholder()); } monitors.elements_mut().insert(*preference, m); @@ -297,6 +297,10 @@ impl WindowsApi { } } + monitors + .elements_mut() + .retain(|m| m.name().ne("PLACEHOLDER")); + Ok(()) }