From ae0ce4c95d648dc0cf888487b953900aa243d641 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 26 Sep 2025 12:17:04 -0700 Subject: [PATCH] fix(wm): address more monitor reconciliation bugs This commit fixes another deadlock and a forces the hiding of non-focused workspaces on all monitors after a DisplayConnectionChange event results in a different number of total monitors in the wm state. --- komorebi/src/macos_api.rs | 40 -------------- komorebi/src/monitor_reconciliator.rs | 76 +++++++++++++-------------- komorebi/src/window.rs | 37 +++++++------ 3 files changed, 56 insertions(+), 97 deletions(-) diff --git a/komorebi/src/macos_api.rs b/komorebi/src/macos_api.rs index 4e6066ad..9abf1247 100644 --- a/komorebi/src/macos_api.rs +++ b/komorebi/src/macos_api.rs @@ -33,12 +33,10 @@ use objc2_core_foundation::CGRect; use objc2_core_foundation::CGSize; use objc2_core_graphics::CGMainDisplayID; use objc2_foundation::NSNumber; -use parking_lot::Mutex; use std::collections::HashMap; use std::collections::hash_map::Entry; use std::ffi::c_void; use std::ptr::NonNull; -use std::sync::Arc; pub struct MacosApi; @@ -251,44 +249,6 @@ impl MacosApi { Ok(all_devices) } - pub fn reconile_monitors(wm: Arc>) -> eyre::Result<()> { - let screens = NSScreen::screens(MainThreadMarker::new().unwrap()); - - for display_id in CoreGraphicsApi::connected_display_ids()? { - let display_bounds = CoreGraphicsApi::display_bounds(display_id); - let mut wm = wm.lock(); - - for screen in &screens { - let menu_bar_height = screen.frame().size.height - - screen.visibleFrame().size.height - - screen.visibleFrame().origin.y; - - if screen.frame() == display_bounds { - let size = Rect::from(display_bounds); - let mut work_area_size = Rect::from(display_bounds); - work_area_size.top += menu_bar_height as i32; - work_area_size.bottom = screen.visibleFrame().size.height as i32; - - for monitor in wm.monitors_mut() { - if monitor.id == display_id { - monitor.size = size; - monitor.work_area_size = work_area_size; - } - - tracing::info!( - "updated monitor size and work area for monitor {display_id}" - ) - } - } - } - - let focus_follows_mouse = wm.mouse_follows_focus; - wm.update_focused_workspace(focus_follows_mouse, true)?; - } - - Ok(()) - } - #[tracing::instrument(skip_all)] pub fn load_workspace_information(wm: &mut WindowManager) -> Result<(), LibraryError> { let mut monitor_size_map = HashMap::new(); diff --git a/komorebi/src/monitor_reconciliator.rs b/komorebi/src/monitor_reconciliator.rs index 6ec5b0aa..49f666d2 100644 --- a/komorebi/src/monitor_reconciliator.rs +++ b/komorebi/src/monitor_reconciliator.rs @@ -116,17 +116,12 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result let initial_monitor_count = wm.monitors().len(); - // Get the currently attached display devices - // TODO: I think because of the way the threading model works on macOS we'll - // need to run this on the main thread to get access to info from NSScreen - // let attached_devices = attached_display_devices(display_provider)?; { let mut latest_monitor_information = LATEST_MONITOR_INFORMATION.write(); *latest_monitor_information = None; + UPDATE_LATEST_MONITOR_INFORMATION.store(true, Ordering::Relaxed); } - UPDATE_LATEST_MONITOR_INFORMATION.store(true, Ordering::Relaxed); - let mut attached_devices = vec![]; while attached_devices.is_empty() { if let Some(latest) = &*LATEST_MONITOR_INFORMATION.read() { @@ -141,23 +136,13 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result // Make sure that in our state any attached displays have the latest macOS API data for monitor in wm.monitors_mut() { for attached in &attached_devices { - // let serial_number_ids_match = if let (Some(attached_snid), Some(m_snid)) = - // (&attached.serial_number_id, &monitor.serial_number_id) - // { - // attached_snid.eq(m_snid) - // } else { - // false - // }; - let serial_number_ids_match = attached.serial_number_id == monitor.serial_number_id; if serial_number_ids_match { monitor.id = attached.id; monitor.device = attached.device.clone(); - // monitor.device_id = attached.device_id.clone(); monitor.serial_number_id = attached.serial_number_id.clone(); - // monitor.name = attached.name.clone(); monitor.size = attached.size; monitor.work_area_size = attached.work_area_size; } @@ -267,6 +252,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result rules_to_remove.push(i); } } + for i in rules_to_remove { workspace_rules.remove(i); } @@ -286,7 +272,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result } } - // Update known_hwnds + // Update known_window_ids wm.known_window_ids .retain(|i, _| !windows_to_remove.contains(i)); @@ -317,32 +303,32 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result let post_removal_monitor_count = wm.monitors().len(); - // // This is the list of device ids after we have removed detached displays. We can - // // keep this with just the device_ids without the serial numbers since this is used - // // only to check which one is the newly added monitor below if there is a new - // // monitor. Everything done after with said new monitor will again consider both - // // serial number and device ids. - // let post_removal_device_ids = wm - // .monitors() - // .iter() - // .map(|m| &m.device_id) - // .cloned() - // .collect::>(); - let post_removal_serial_ids = []; + // This is the list of device ids after we have removed detached displays. We can + // keep this with just the device_ids without the serial numbers since this is used + // only to check which one is the newly added monitor below if there is a new + // monitor. Everything done after with said new monitor will again consider both + // serial number and device ids. + let post_removal_serial_ids = wm + .monitors() + .iter() + .map(|m| &m.serial_number_id) + .cloned() + .collect::>(); - // // Check for and add any new monitors that may have been plugged in - // // Monitor and display index preferences get applied in this function - // MacosApi::load_monitor_information(&mut wm)?; - - // need to drop here because the main thread needs the lock to load the latest - // monitor info into the wm state - drop(wm); - LOAD_LATEST_MONITOR_INFORMATION.store(true, Ordering::Relaxed); - while LOAD_LATEST_MONITOR_INFORMATION.load(Ordering::Relaxed) { - std::thread::sleep(std::time::Duration::from_millis(100)); - tracing::trace!("waiting for latest monitor info to be loaded {}", line!()); + // Check for and add any new monitors that may have been plugged in + // Monitor and display index preferences get applied in this function + { + // need to drop here because the main thread needs the lock to load the latest + // monitor info into the wm state + drop(wm); + LOAD_LATEST_MONITOR_INFORMATION.store(true, Ordering::Relaxed); + while LOAD_LATEST_MONITOR_INFORMATION.load(Ordering::Relaxed) { + std::thread::sleep(std::time::Duration::from_millis(50)); + tracing::trace!("waiting for latest monitor info to be loaded {}", line!()); + } } + // regain the lock here once the monitor information has been updated let mut wm = arc.lock(); let post_addition_monitor_count = wm.monitors().len(); @@ -562,6 +548,16 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result // Border updates to fix DPI/resolution related jank // border_manager::send_notification(None); } + + let mouse_follows_focus = wm.mouse_follows_focus; + // when a monitor is added/removed some of our "hidden" windows + // get brought back onto the screen, so we wanna make sure unfocused + // workspaces get hidden again + if initial_monitor_count != final_count { + for monitor in wm.monitors_mut() { + monitor.load_focused_workspace(mouse_follows_focus)?; + } + } } } diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index 68869947..bbc48110 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -352,27 +352,30 @@ impl Window { #[tracing::instrument(skip_all)] pub fn hide(&mut self) -> Result<(), AccessibilityError> { + let rect = MacosApi::window_rect(&self.element)?; + let mut window_restore_positions = WINDOW_RESTORE_POSITIONS.lock(); - if let Entry::Vacant(entry) = window_restore_positions.entry(self.id) { - let rect = MacosApi::window_rect(&self.element)?; - if let Some(monitor_size) = CoreGraphicsApi::display_bounds_for_window_rect(rect) { - entry.insert(rect); - drop(window_restore_positions); + if let Entry::Vacant(entry) = window_restore_positions.entry(self.id) + && CoreGraphicsApi::display_bounds_for_window_rect(rect).is_none() + { + entry.insert(rect); + drop(window_restore_positions); + } - // I don't love this, but it's basically what Aerospace does in lieu of an actual "Hide" API - let hidden_rect = hidden_frame_bottom_left(monitor_size, rect.size); + if let Some(monitor_size) = CoreGraphicsApi::display_bounds_for_window_rect(rect) { + // I don't love this, but it's basically what Aerospace does in lieu of an actual "Hide" API + let hidden_rect = hidden_frame_bottom_left(monitor_size, rect.size); - tracing::debug!( - "hiding {} and setting restore point to {},{}", - self.title() - .unwrap_or_else(|| String::from("")), - rect.origin.x, - rect.origin.y, - ); + tracing::debug!( + "hiding {} and setting restore point to {},{}", + self.title() + .unwrap_or_else(|| String::from("")), + rect.origin.x, + rect.origin.y, + ); - self.set_point(hidden_rect.origin, true)?; - self.set_size(hidden_rect.size, true)?; - } + self.set_point(hidden_rect.origin, true)?; + self.set_size(hidden_rect.size, true)?; } Ok(())