diff --git a/komorebi/src/container.rs b/komorebi/src/container.rs index 1cf9c48a..81262027 100644 --- a/komorebi/src/container.rs +++ b/komorebi/src/container.rs @@ -165,8 +165,9 @@ impl Container { should_hide = true } - if should_hide { - window.hide(hiding_position)?; + // a window that has gone away must not leave the ones after it in view + if should_hide && let Err(error) = window.hide(hiding_position) { + tracing::warn!("failed to hide window {}: {error}", window.id); } } diff --git a/komorebi/src/macos_api.rs b/komorebi/src/macos_api.rs index f09ffb06..400bff6b 100644 --- a/komorebi/src/macos_api.rs +++ b/komorebi/src/macos_api.rs @@ -181,19 +181,27 @@ impl MacosApi { pub fn update_monitor_work_areas(wm: &mut WindowManager) -> eyre::Result<()> { let all_devices = Self::latest_monitor_information()?; + let mut updated = false; + for device in all_devices { + // Display ids change when a display is reconnected, the serial number does not for monitor in wm.monitors_mut() { - if monitor.id == device.id { + if monitor.serial_number_id == device.serial_number_id { + monitor.id = device.id; monitor.size = device.size; monitor.work_area_size = device.work_area_size; + updated = true; + + tracing::info!( + "updated monitor size and work area for monitor {}", + monitor.serial_number_id + ); } - - tracing::info!( - "updated monitor size and work area for monitor {}", - monitor.serial_number_id - ); } + } + // A display that komorebi does not know yet is the business of the reconciliator + if updated { let focus_follows_mouse = wm.mouse_follows_focus; wm.update_focused_workspace(focus_follows_mouse, true)?; } diff --git a/komorebi/src/monitor.rs b/komorebi/src/monitor.rs index 4d5f078f..6f794fdf 100644 --- a/komorebi/src/monitor.rs +++ b/komorebi/src/monitor.rs @@ -174,6 +174,17 @@ impl Monitor { } /// Updates the `globals` field of workspace with index `workspace_idx` + /// Give every workspace the work area of this monitor, not only the focused one + /// + /// A workspace that is put away is hidden on the display that its work area is on, so after + /// the display of a monitor changes, the workspaces that are not showing would be hidden on + /// the display that is gone, and their windows would be left in view. + pub fn update_all_workspace_globals(&mut self, offset: Option) { + for idx in 0..self.workspaces().len() { + self.update_workspace_globals(idx, offset); + } + } + pub fn update_workspace_globals(&mut self, workspace_idx: usize, offset: Option) { let container_padding = self .container_padding @@ -237,8 +248,9 @@ impl Monitor { for (i, workspace) in self.workspaces_mut().iter_mut().enumerate() { if i == focused_idx { restore = Some(workspace); - } else { - workspace.hide(None)?; + } else if let Err(error) = workspace.hide(None) { + // the windows that can be put away should be, whatever happened to the others + tracing::warn!("failed to hide workspace {i}: {error}"); } } diff --git a/komorebi/src/monitor_reconciliator.rs b/komorebi/src/monitor_reconciliator.rs index 559346a5..2d943c5e 100644 --- a/komorebi/src/monitor_reconciliator.rs +++ b/komorebi/src/monitor_reconciliator.rs @@ -144,6 +144,70 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result } if initial_monitor_count == attached_devices.len() { + // The count can match while the displays do not: closing a laptop lid with + // an external display connected swaps one display for another. Hand the + // unplugged monitor's workspaces to the display that took its place. + let unplugged = wm + .monitors() + .iter() + .enumerate() + .filter(|(_, m)| { + !attached_devices + .iter() + .any(|a| a.serial_number_id.eq(&m.serial_number_id)) + }) + .map(|(idx, _)| idx) + .collect::>(); + + let plugged = attached_devices + .iter() + .filter(|a| { + !wm.monitors() + .iter() + .any(|m| m.serial_number_id.eq(&a.serial_number_id)) + }) + .collect::>(); + + if !unplugged.is_empty() && unplugged.len() == plugged.len() { + tracing::info!( + "{} display(s) swapped for others, moving their monitors over", + unplugged.len() + ); + + for (idx, attached) in unplugged.iter().zip(plugged) { + if let Some(monitor) = wm.monitors_mut().get_mut(*idx) { + monitor.id = attached.id; + monitor.device = attached.device.clone(); + monitor.serial_number_id = attached.serial_number_id.clone(); + monitor.size = attached.size; + monitor.work_area_size = attached.work_area_size; + } + } + + let offset = wm.work_area_offset; + for monitor in wm.monitors_mut() { + monitor.update_all_workspace_globals(offset); + } + + if let Err(error) = wm.retile_all(true) { + tracing::error!("failed to retile after swapping displays: {error}"); + } + + let mouse_follows_focus = wm.mouse_follows_focus; + for monitor in wm.monitors_mut() { + if let Err(error) = monitor.load_focused_workspace(mouse_follows_focus) + { + tracing::error!( + "failed to load the focused workspace after swapping displays: {error}" + ); + } + } + + drop(wm); + border_manager::send_notification(None, None, false); + continue 'receiver; + } + tracing::debug!("monitor counts match, reconciliation not required"); drop(wm); continue 'receiver; @@ -551,7 +615,9 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result // get brought back onto the screen, so we wanna make sure unfocused // workspaces get hidden again if initial_monitor_count != final_count { + let offset = wm.work_area_offset; for monitor in wm.monitors_mut() { + monitor.update_all_workspace_globals(offset); monitor.load_focused_workspace(mouse_follows_focus)?; } }