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.
This commit is contained in:
LGUG2Z committed 2025-09-26 12:43:27 -07:00
1 parent 4b0b2f4013
commit ae0ce4c95d
3 files changed
+56 -97

No files matched your search

-40
View File
@@ -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<Mutex<WindowManager>>) -> 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();
+36 -40
View File
@@ -116,17 +116,12 @@ pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> 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<Mutex<WindowManager>>) -> 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<Mutex<WindowManager>>) -> 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<Mutex<WindowManager>>) -> 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<Mutex<WindowManager>>) -> 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::<Vec<_>>();
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::<Vec<_>>();
// // 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<Mutex<WindowManager>>) -> 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)?;
}
}
}
}
+20 -17
View File
@@ -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("<NO TITLE FOUND>")),
rect.origin.x,
rect.origin.y,
);
tracing::debug!(
"hiding {} and setting restore point to {},{}",
self.title()
.unwrap_or_else(|| String::from("<NO TITLE FOUND>")),
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(())