forge
Explore
Log in

Don't you dare go Hollow

Fix removing output not keeping empty focus on primary

Browse files at this commit

8a08e01a2d4e0fcfd746b23d1538ac512a8c7e8cIvan MolodetskikhParent: 1831d68
src/layout.rsmodified+37 -0
@@ -565,10 +565,20 @@ impl<W: LayoutElement> Layout<W> {
565565 ws.set_output(Some(primary.output.clone()));
566566 }
567567
568+ let empty_was_focused =
569+ primary.active_workspace_idx == primary.workspaces.len() - 1;
570+
571+ // Push the workspaces from the removed monitor in the end, right before the
572+ // last, empty, workspace.
568573 let empty = primary.workspaces.remove(primary.workspaces.len() - 1);
569574 primary.workspaces.extend(workspaces);
570575 primary.workspaces.push(empty);
571576
577+ // If the empty workspace was focused on the primary monitor, keep it focused.
578+ if empty_was_focused {
579+ primary.active_workspace_idx = primary.workspaces.len() - 1;
580+ }
581+
572582 MonitorSet::Normal {
573583 monitors,
574584 primary_idx,
@@ -3189,6 +3199,33 @@ mod tests {
31893199 check_ops(&ops);
31903200 }
31913201
3202+ #[test]
3203+ fn removing_output_must_keep_empty_focus_on_primary() {
3204+ let ops = [
3205+ Op::AddOutput(1),
3206+ Op::AddWindow {
3207+ id: 0,
3208+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3209+ activate: true,
3210+ },
3211+ Op::AddOutput(2),
3212+ Op::RemoveOutput(1),
3213+ ];
3214+
3215+ let mut layout = Layout::default();
3216+ for op in ops {
3217+ op.apply(&mut layout);
3218+ }
3219+
3220+ let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
3221+ unreachable!()
3222+ };
3223+
3224+ // The workspace from the removed output was inserted at position 0, so the active workspace
3225+ // must change to 1 to keep the focus on the empty workspace.
3226+ assert_eq!(monitors[0].active_workspace_idx, 1);
3227+ }
3228+
31923229 proptest! {
31933230 #![proptest_config(ProptestConfig {
31943231 cases: if std::env::var_os("RUN_SLOW_TESTS").is_none() {