GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
refactor: improve iteration of native monitors on display setting changes
Lars Berger72a14038236c
diff --git a/packages/wm/src/common/events/handle_display_settings_changed.rs b/packages/wm/src/common/events/handle_display_settings_changed.rsindex cc31d4a9..f5dd90de 100644--- a/packages/wm/src/common/events/handle_display_settings_changed.rs+++ b/packages/wm/src/common/events/handle_display_settings_changed.rs@@ -16,44 +16,68 @@ pub fn handle_display_settings_changed(.iter().filter_map(|m| m.hardware_id().ok()).flatten()+ .cloned().collect::<Vec<_>>();let monitors = state.monitors();+ let mut new_native_monitors = Vec::new();for native_monitor in native_monitors {- // Hardware ID is not guaranteed to be unique, so we check- // for that as a fallback.- let found_monitor = monitors.iter().find(|monitor| {- monitor.native().handle == native_monitor.handle- || monitor.native().device_path().is_ok_and(|path| {- path.is_some_and(|path| {- Some(Some(path)) == native_monitor.device_path().ok()- })- })- // TODO: Check that there aren't multiple monitors with the same- // hardware ID.- || monitor.native().hardware_id().is_ok_and(|id| {- id.is_some_and(|id| {- Some(Some(id)) == native_monitor.hardware_id().ok()+ // The `monitor_from_native` function uses the monitor's handle to+ // find the corresponding monitor. Since the monitor handle can change+ // after a display setting change, we also look for a matching device+ // path or hardware ID. The hardware ID is not guaranteed to be unique,+ // so we match against that last.+ let found_monitor =+ state.monitor_from_native(&native_monitor).or_else(|| {+ monitors+ .iter()+ .find_map(|monitor| {+ if monitor.native().device_path().ok()??+ == native_monitor.device_path().ok()??+ {+ return Some(monitor);+ }++ let hardware_id =+ monitor.native().hardware_id().ok()??.clone();++ // Check that there aren't multiple monitors with the same+ // hardware ID.+ let is_unique =+ hardware_ids.iter().filter(|&id| *id == hardware_id).count()+ == 1;++ match is_unique+ && hardware_id == *native_monitor.hardware_id().ok()??+ {+ true => Some(monitor),+ false => None,+ }})- })- // == native_monitor.hardware_id().ok() && !hardware_ids.contains(monitor.native().hardware_id().unwrap()))- });-- if let Some(found_monitor) = found_monitor {- info!(- "Monitor: {:?} {} {:?} {:?}",- native_monitor.handle,- native_monitor.device_name()?,- native_monitor.device_path()?,- native_monitor.hardware_id()?- );-- // found_monitor.set_native(native_monitor);-- state.emit_event(WmEvent::MonitorUpdated {- updated_monitor: found_monitor.clone(),- })+ .cloned()+ });++ match found_monitor {+ Some(found_monitor) => {+ info!(+ "Monitor: {:?} {} {:?} {:?}",+ native_monitor.handle,+ native_monitor.device_name()?,+ native_monitor.device_path()?,+ native_monitor.hardware_id()?+ );++ found_monitor.set_native(native_monitor);++ // TODO: Check that a property on the monitor actually changed.+ state.emit_event(WmEvent::MonitorUpdated {+ updated_monitor: found_monitor.clone(),+ });+ }+ None => {+ new_native_monitors.push(native_monitor);+ }}}info!("-------");diff --git a/packages/wm/src/monitors/monitor.rs b/packages/wm/src/monitors/monitor.rsindex e886be9b..381defe0 100644--- a/packages/wm/src/monitors/monitor.rs+++ b/packages/wm/src/monitors/monitor.rs@@ -47,6 +47,10 @@ impl Monitor {self.0.borrow().native.clone()}+ pub fn set_native(&self, native: NativeMonitor) {+ self.0.borrow_mut().native = native;+ }+pub fn name(&self) -> anyhow::Result<String> {self.native().device_name().cloned()}