Fix removing output not keeping empty focus on primary
Browse files at this commit8a08e01a2d4e0fcfd746b23d1538ac512a8c7e8cParent: 1831d681 changed file +37 -0
- src/layout.rs +37 -0
src/layout.rsmodified+37 -0
| @@ -565,10 +565,20 @@ impl<W: LayoutElement> Layout<W> { | |||
| 565 | ws.set_output(Some(primary.output.clone())); | 565 | ws.set_output(Some(primary.output.clone())); |
| 566 | } | 566 | } |
| 567 | 567 | ||
| 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. | ||
| 568 | let empty = primary.workspaces.remove(primary.workspaces.len() - 1); | 573 | let empty = primary.workspaces.remove(primary.workspaces.len() - 1); |
| 569 | primary.workspaces.extend(workspaces); | 574 | primary.workspaces.extend(workspaces); |
| 570 | primary.workspaces.push(empty); | 575 | primary.workspaces.push(empty); |
| 571 | 576 | ||
| 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 | + | ||
| 572 | MonitorSet::Normal { | 582 | MonitorSet::Normal { |
| 573 | monitors, | 583 | monitors, |
| 574 | primary_idx, | 584 | primary_idx, |
| @@ -3189,6 +3199,33 @@ mod tests { | |||
| 3189 | check_ops(&ops); | 3199 | check_ops(&ops); |
| 3190 | } | 3200 | } |
| 3191 | 3201 | ||
| 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 | + | ||
| 3192 | proptest! { | 3229 | proptest! { |
| 3193 | #![proptest_config(ProptestConfig { | 3230 | #![proptest_config(ProptestConfig { |
| 3194 | cases: if std::env::var_os("RUN_SLOW_TESTS").is_none() { | 3231 | cases: if std::env::var_os("RUN_SLOW_TESTS").is_none() { |