forge
Explore
Log in

Don't you dare go Hollow

layout: Preserve active workspace for removed outputs

Browse files at this commit

88f4c1d610b7370774e8cd6d0370fd7bec7023f9Ivan MolodetskikhParent: ddcb5c5
src/layout/mod.rsmodified+105 -3
@@ -30,6 +30,7 @@
30 //! making the primary output their original output.30 //! making the primary output their original output.
31 31
32 use std::cmp::min;32 use std::cmp::min;
33+use std::collections::HashMap;
33 use std::mem;34 use std::mem;
34 use std::rc::Rc;35 use std::rc::Rc;
35 use std::time::Duration;36 use std::time::Duration;
@@ -205,6 +206,14 @@ pub struct Layout<W: LayoutElement> {
205 /// This normally indicates that the layout has keyboard focus, but not always. E.g. when the206 /// This normally indicates that the layout has keyboard focus, but not always. E.g. when the
206 /// screenshot UI is open, it keeps the layout drawing as active.207 /// screenshot UI is open, it keeps the layout drawing as active.
207 is_active: bool,208 is_active: bool,
209+ /// Map from monitor name to id of its last active workspace.
210+ ///
211+ /// This data is stored upon monitor removal and is used to restore the active workspace when
212+ /// the monitor is reconnected.
213+ ///
214+ /// The workspace id does not necessarily point to a valid workspace. If it doesn't, then it is
215+ /// simply ignored.
216+ last_active_workspace_id: HashMap<String, WorkspaceId>,
208 /// Ongoing interactive move.217 /// Ongoing interactive move.
209 interactive_move: Option<InteractiveMoveState<W>>,218 interactive_move: Option<InteractiveMoveState<W>>,
210 /// Configurable properties of the layout.219 /// Configurable properties of the layout.
@@ -432,6 +441,7 @@ impl<W: LayoutElement> Layout<W> {
432 Self {441 Self {
433 monitor_set: MonitorSet::NoOutputs { workspaces: vec![] },442 monitor_set: MonitorSet::NoOutputs { workspaces: vec![] },
434 is_active: true,443 is_active: true,
444+ last_active_workspace_id: HashMap::new(),
435 interactive_move: None,445 interactive_move: None,
436 options: Rc::new(options),446 options: Rc::new(options),
437 }447 }
@@ -449,6 +459,7 @@ impl<W: LayoutElement> Layout<W> {
449 Self {459 Self {
450 monitor_set: MonitorSet::NoOutputs { workspaces },460 monitor_set: MonitorSet::NoOutputs { workspaces },
451 is_active: true,461 is_active: true,
462+ last_active_workspace_id: HashMap::new(),
452 interactive_move: None,463 interactive_move: None,
453 options: opts,464 options: opts,
454 }465 }
@@ -463,6 +474,9 @@ impl<W: LayoutElement> Layout<W> {
463 } => {474 } => {
464 let primary = &mut monitors[primary_idx];475 let primary = &mut monitors[primary_idx];
465 476
477+ let ws_id_to_activate = self.last_active_workspace_id.remove(&output.name());
478+ let mut active_workspace_idx = None;
479+
466 let mut stopped_primary_ws_switch = false;480 let mut stopped_primary_ws_switch = false;
467 481
468 let mut workspaces = vec![];482 let mut workspaces = vec![];
@@ -482,6 +496,10 @@ impl<W: LayoutElement> Layout<W> {
482 // another monitor. However, we will add an empty workspace in the end496 // another monitor. However, we will add an empty workspace in the end
483 // instead.497 // instead.
484 if ws.has_windows() || ws.name.is_some() {498 if ws.has_windows() || ws.name.is_some() {
499+ if Some(ws.id()) == ws_id_to_activate {
500+ active_workspace_idx = Some(workspaces.len());
501+ }
502+
485 workspaces.push(ws);503 workspaces.push(ws);
486 }504 }
487 505
@@ -499,6 +517,10 @@ impl<W: LayoutElement> Layout<W> {
499 517
500 workspaces.reverse();518 workspaces.reverse();
501 519
520+ if let Some(idx) = &mut active_workspace_idx {
521+ *idx = workspaces.len() - *idx - 1;
522+ }
523+
502 // Make sure there's always an empty workspace.524 // Make sure there's always an empty workspace.
503 workspaces.push(Workspace::new(output.clone(), self.options.clone()));525 workspaces.push(Workspace::new(output.clone(), self.options.clone()));
504 526
@@ -506,7 +528,10 @@ impl<W: LayoutElement> Layout<W> {
506 ws.set_output(Some(output.clone()));528 ws.set_output(Some(output.clone()));
507 }529 }
508 530
509- monitors.push(Monitor::new(output, workspaces, self.options.clone()));531+ let mut monitor = Monitor::new(output, workspaces, self.options.clone());
532+ monitor.active_workspace_idx = active_workspace_idx.unwrap_or(0);
533+ monitors.push(monitor);
534+
510 MonitorSet::Normal {535 MonitorSet::Normal {
511 monitors,536 monitors,
512 primary_idx,537 primary_idx,
@@ -517,11 +542,19 @@ impl<W: LayoutElement> Layout<W> {
517 // We know there are no empty workspaces there, so add one.542 // We know there are no empty workspaces there, so add one.
518 workspaces.push(Workspace::new(output.clone(), self.options.clone()));543 workspaces.push(Workspace::new(output.clone(), self.options.clone()));
519 544
520- for workspace in &mut workspaces {545+ let ws_id_to_activate = self.last_active_workspace_id.remove(&output.name());
546+ let mut active_workspace_idx = 0;
547+
548+ for (i, workspace) in workspaces.iter_mut().enumerate() {
521 workspace.set_output(Some(output.clone()));549 workspace.set_output(Some(output.clone()));
550+
551+ if Some(workspace.id()) == ws_id_to_activate {
552+ active_workspace_idx = i;
553+ }
522 }554 }
523 555
524- let monitor = Monitor::new(output, workspaces, self.options.clone());556+ let mut monitor = Monitor::new(output, workspaces, self.options.clone());
557+ monitor.active_workspace_idx = active_workspace_idx;
525 558
526 MonitorSet::Normal {559 MonitorSet::Normal {
527 monitors: vec![monitor],560 monitors: vec![monitor],
@@ -544,6 +577,12 @@ impl<W: LayoutElement> Layout<W> {
544 .position(|mon| &mon.output == output)577 .position(|mon| &mon.output == output)
545 .expect("trying to remove non-existing output");578 .expect("trying to remove non-existing output");
546 let monitor = monitors.remove(idx);579 let monitor = monitors.remove(idx);
580+
581+ self.last_active_workspace_id.insert(
582+ monitor.output_name().clone(),
583+ monitor.workspaces[monitor.active_workspace_idx].id(),
584+ );
585+
547 let mut workspaces = monitor.workspaces;586 let mut workspaces = monitor.workspaces;
548 587
549 for ws in &mut workspaces {588 for ws in &mut workspaces {
@@ -5684,6 +5723,69 @@ mod tests {
5684 check_ops(&ops);5723 check_ops(&ops);
5685 }5724 }
5686 5725
5726+ #[test]
5727+ fn output_active_workspace_is_preserved() {
5728+ let ops = [
5729+ Op::AddOutput(1),
5730+ Op::AddWindow {
5731+ id: 1,
5732+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
5733+ min_max_size: Default::default(),
5734+ },
5735+ Op::FocusWorkspaceDown,
5736+ Op::AddWindow {
5737+ id: 2,
5738+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
5739+ min_max_size: Default::default(),
5740+ },
5741+ Op::RemoveOutput(1),
5742+ Op::AddOutput(1),
5743+ ];
5744+
5745+ let mut layout = Layout::default();
5746+ for op in ops {
5747+ op.apply(&mut layout);
5748+ }
5749+
5750+ let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
5751+ unreachable!()
5752+ };
5753+
5754+ assert_eq!(monitors[0].active_workspace_idx, 1);
5755+ }
5756+
5757+ #[test]
5758+ fn output_active_workspace_is_preserved_with_other_outputs() {
5759+ let ops = [
5760+ Op::AddOutput(1),
5761+ Op::AddOutput(2),
5762+ Op::AddWindow {
5763+ id: 1,
5764+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
5765+ min_max_size: Default::default(),
5766+ },
5767+ Op::FocusWorkspaceDown,
5768+ Op::AddWindow {
5769+ id: 2,
5770+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
5771+ min_max_size: Default::default(),
5772+ },
5773+ Op::RemoveOutput(1),
5774+ Op::AddOutput(1),
5775+ ];
5776+
5777+ let mut layout = Layout::default();
5778+ for op in ops {
5779+ op.apply(&mut layout);
5780+ }
5781+
5782+ let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
5783+ unreachable!()
5784+ };
5785+
5786+ assert_eq!(monitors[1].active_workspace_idx, 1);
5787+ }
5788+
5687 fn arbitrary_spacing() -> impl Strategy<Value = f64> {5789 fn arbitrary_spacing() -> impl Strategy<Value = f64> {
5688 // Give equal weight to:5790 // Give equal weight to:
5689 // - 0: the element is disabled5791 // - 0: the element is disabled