fix(monitor): lay out on the display that replaced the one that went away

Closing a laptop lid with an external display attached swaps one display
for another, so the monitor count is unchanged and the reconciliator
logged "reconciliation not required" while komorebi kept laying windows
out on the old display's work area. Compare the serial numbers as well as
the count and hand the unplugged monitor's workspaces to the display that
took its place.

The workspaces that are not showing kept the work area of the old display
too, so they were put away on a display that was gone and their windows
stayed in view. Refresh the work area of every workspace before putting
them away, and let a window that cannot be hidden not leave the ones
after it in view.

Resize notifications matched monitors by display id, which changes when a
display is reconnected, so they never updated anything and then re-laid
out the focused workspace once per device. Match by serial number, and
only re-lay out when a known monitor changed.
This commit is contained in:
LGUG2Z committed 2026-10-29 16:38:24 -07:00
1 parent 8f768fd4ca
commit 297c4ea0b7
4 files changed
+97 -10

No files matched your search

+3 -2
View File
@@ -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);
}
}
+14 -6
View File
@@ -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)?;
}
+14 -2
View File
@@ -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<Rect>) {
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<Rect>) {
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}");
}
}
+66
View File
@@ -144,6 +144,70 @@ pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> 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::<Vec<_>>();
let plugged = attached_devices
.iter()
.filter(|a| {
!wm.monitors()
.iter()
.any(|m| m.serial_number_id.eq(&a.serial_number_id))
})
.collect::<Vec<_>>();
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<Mutex<WindowManager>>) -> 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)?;
}
}