forge
Explore
Log in

Don't you dare go Hollow

Support multiple open-on-output fallbacks for named workspaces (#4618)

Browse files at this commit

9a35d3774a7ff1acdce07fde05bdc94dfa5a0258UnverifiedArtem Timofeev and Ivan MolodetskikhParent: 7449fe8
* feat(config): support multiple open-on-output fallbacks

* Change open-on-output to be separate lines

---------
docs/wiki/Configuration:-Named-Workspaces.mdmodified+13 -0
@@ -46,6 +46,19 @@ Before, it could only use the connector name.
46 This means that named workspaces "stick" to their original output in more cases, reflecting their more permanent nature.46 This means that named workspaces "stick" to their original output in more cases, reflecting their more permanent nature.
47 Explicitly moving a named workspace to a different monitor will still update its original output.47 Explicitly moving a named workspace to a different monitor will still update its original output.
48 48
49+<sup>Since: next release</sup> You can now write multiple `open-on-output` lines.
50+If the first output is not connected, the workspace will open on the second output, and so on.
51+
52+```kdl
53+workspace "browser" {
54+ open-on-output "DP-9"
55+ open-on-output "DP-4"
56+}
57+```
58+
59+The output order is reevaluated whenever outputs connect or disconnect.
60+Connecting a higher-priority output will move the workspace there.
61+
49 ### Layout config overrides62 ### Layout config overrides
50 63
51 <sup>Since: 25.11</sup>64 <sup>Since: 25.11</sup>
niri-config/src/lib.rsmodified+6 -4
@@ -963,6 +963,7 @@ mod tests {
963 963
964 workspace "workspace-1" {964 workspace "workspace-1" {
965 open-on-output "eDP-1"965 open-on-output "eDP-1"
966+ open-on-output "DP-1"
966 }967 }
967 workspace "workspace-2"968 workspace "workspace-2"
968 workspace "workspace-3"969 workspace "workspace-3"
@@ -2312,23 +2313,24 @@ mod tests {
2312 name: WorkspaceName(2313 name: WorkspaceName(
2313 "workspace-1",2314 "workspace-1",
2314 ),2315 ),
2315- open_on_output: Some(2316+ open_on_output: [
2316 "eDP-1",2317 "eDP-1",
2317- ),2318+ "DP-1",
2319+ ],
2318 layout: None,2320 layout: None,
2319 },2321 },
2320 Workspace {2322 Workspace {
2321 name: WorkspaceName(2323 name: WorkspaceName(
2322 "workspace-2",2324 "workspace-2",
2323 ),2325 ),
2324- open_on_output: None,2326+ open_on_output: [],
2325 layout: None,2327 layout: None,
2326 },2328 },
2327 Workspace {2329 Workspace {
2328 name: WorkspaceName(2330 name: WorkspaceName(
2329 "workspace-3",2331 "workspace-3",
2330 ),2332 ),
2331- open_on_output: None,2333+ open_on_output: [],
2332 layout: None,2334 layout: None,
2333 },2335 },
2334 ],2336 ],
niri-config/src/workspace.rsmodified+2 -2
@@ -6,8 +6,8 @@ use crate::LayoutPart;
6 pub struct Workspace {6 pub struct Workspace {
7 #[knuffel(argument)]7 #[knuffel(argument)]
8 pub name: WorkspaceName,8 pub name: WorkspaceName,
9- #[knuffel(child, unwrap(argument))]9+ #[knuffel(children(name = "open-on-output"), unwrap(argument))]
10- pub open_on_output: Option<String>,10+ pub open_on_output: Vec<String>,
11 #[knuffel(child)]11 #[knuffel(child)]
12 pub layout: Option<WorkspaceLayoutPart>,12 pub layout: Option<WorkspaceLayoutPart>,
13 }13 }
src/layout/mod.rsmodified+78 -67
@@ -19,10 +19,10 @@
19 //! 2. Connecting an output must not change the layout for any workspaces that were never on that19 //! 2. Connecting an output must not change the layout for any workspaces that were never on that
20 //! output.20 //! output.
21 //!21 //!
22-//! Therefore, we implement the following logic: every workspace keeps track of which output it22+//! Therefore, we implement the following logic: every workspace keeps track of its ordered
23-//! originated on—its *original output*. When an output disconnects, its workspaces are appended to23+//! original output candidates. When an output disconnects, its workspaces move to the first
24-//! the (potentially new) primary output, but remember their original output. Then, if the original24+//! connected candidate or the (potentially new) primary output. When a higher-priority candidate
25-//! output connects again, all workspaces originally from there move back to that output.25+//! connects, matching workspaces move to it.
26 //!26 //!
27 //! In order to avoid surprising behavior, if the user creates or moves any new windows onto a27 //! In order to avoid surprising behavior, if the user creates or moves any new windows onto a
28 //! workspace, it forgets its original output, and its current output becomes its original output.28 //! workspace, it forgets its original output, and its current output becomes its original output.
@@ -764,21 +764,31 @@ impl<W: LayoutElement> Layout<W> {
764 primary_idx,764 primary_idx,
765 active_monitor_idx,765 active_monitor_idx,
766 } => {766 } => {
767- let primary = &mut monitors[primary_idx];767+ let new_output_idx = monitors.len();
768+ let mut workspaces = vec![];
769+ for source_idx in 0..monitors.len() {
770+ let mut stopped_ws_switch = false;
768 771
769- let mut stopped_primary_ws_switch = false;772+ for i in (0..monitors[source_idx].workspaces.len()).rev() {
773+ let preferred = monitors[source_idx].workspaces[i].find_preferred_output(
774+ monitors
775+ .iter()
776+ .map(|monitor| &monitor.output)
777+ .chain(std::iter::once(&output)),
778+ ) == Some(new_output_idx);
779+ if !preferred {
780+ continue;
781+ }
770 782
771- let mut workspaces = vec![];783+ let source = &mut monitors[source_idx];
772- for i in (0..primary.workspaces.len()).rev() {784+ let ws = source.workspaces.remove(i);
773- if primary.workspaces[i].original_output.matches(&output) {
774- let ws = primary.workspaces.remove(i);
775 785
776 // FIXME: this can be coded in a way that the workspace switch won't be786 // FIXME: this can be coded in a way that the workspace switch won't be
777 // affected if the removed workspace is invisible. But this is good enough787 // affected if the removed workspace is invisible. But this is good enough
778 // for now.788 // for now.
779- if primary.workspace_switch.is_some() {789+ if source.workspace_switch.is_some() {
780- primary.workspace_switch = None;790+ source.workspace_switch = None;
781- stopped_primary_ws_switch = true;791+ stopped_ws_switch = true;
782 }792 }
783 793
784 // The user could've closed a window while remaining on this workspace, on794 // The user could've closed a window while remaining on this workspace, on
@@ -788,7 +798,7 @@ impl<W: LayoutElement> Layout<W> {
788 workspaces.push(ws);798 workspaces.push(ws);
789 }799 }
790 800
791- if i <= primary.active_workspace_idx801+ if i <= source.active_workspace_idx
792 // Generally when moving the currently active workspace, we want to802 // Generally when moving the currently active workspace, we want to
793 // fall back to the workspace above, so as not to end up on the last803 // fall back to the workspace above, so as not to end up on the last
794 // empty workspace. However, with empty workspace above first, when804 // empty workspace. However, with empty workspace above first, when
@@ -799,27 +809,27 @@ impl<W: LayoutElement> Layout<W> {
799 // workspaces set up across multiple monitors. Without this check, the809 // workspaces set up across multiple monitors. Without this check, the
800 // first monitor to connect can end up with the first empty workspace810 // first monitor to connect can end up with the first empty workspace
801 // focused instead of the first named workspace.811 // focused instead of the first named workspace.
802- && !(primary.options.layout.empty_workspace_above_first812+ && !(source.options.layout.empty_workspace_above_first
803- && primary.active_workspace_idx == 1)813+ && source.active_workspace_idx == 1)
804 {814 {
805- primary.active_workspace_idx =815+ source.active_workspace_idx =
806- primary.active_workspace_idx.saturating_sub(1);816+ source.active_workspace_idx.saturating_sub(1);
807 }817 }
808 }818 }
809- }
810 819
811- // If we stopped a workspace switch, then we might need to clean up workspaces.820+ let source = &mut monitors[source_idx];
812- // Also if empty_workspace_above_first is set and there are only 2 workspaces left,
813- // both will be empty and one of them needs to be removed. clean_up_workspaces
814- // takes care of this.
815 821
816- if stopped_primary_ws_switch822+ // If we stopped a workspace switch, then we might need to clean up workspaces.
817- || (primary.options.layout.empty_workspace_above_first823+ // Also if empty_workspace_above_first is set and there are only 2 workspaces
818- && primary.workspaces.len() == 2)824+ // left, both will be empty and one of them needs to be removed.
819- {825+ // clean_up_workspaces takes care of this.
820- primary.clean_up_workspaces();826+ if stopped_ws_switch
827+ || (source.options.layout.empty_workspace_above_first
828+ && source.workspaces.len() == 2)
829+ {
830+ source.clean_up_workspaces();
831+ }
821 }832 }
822-
823 workspaces.reverse();833 workspaces.reverse();
824 834
825 let ws_id_to_activate = self.last_active_workspace_id.remove(&output.name());835 let ws_id_to_activate = self.last_active_workspace_id.remove(&output.name());
@@ -907,8 +917,18 @@ impl<W: LayoutElement> Layout<W> {
907 active_monitor_idx = active_monitor_idx.saturating_sub(1);917 active_monitor_idx = active_monitor_idx.saturating_sub(1);
908 }918 }
909 919
910- let primary = &mut monitors[primary_idx];920+ for target_idx in 0..monitors.len() {
911- primary.append_workspaces(workspaces);921+ let target_workspaces = workspaces
922+ .extract_if(.., |ws| {
923+ ws.find_preferred_output(
924+ monitors.iter().map(|monitor| &monitor.output),
925+ )
926+ .unwrap_or(primary_idx)
927+ == target_idx
928+ })
929+ .collect();
930+ monitors[target_idx].append_workspaces(target_workspaces);
931+ }
912 932
913 MonitorSet::Normal {933 MonitorSet::Normal {
914 monitors,934 monitors,
@@ -2538,34 +2558,22 @@ impl<W: LayoutElement> Layout<W> {
2538 2558
2539 monitor.verify_invariants();2559 monitor.verify_invariants();
2540 2560
2541- if idx == primary_idx {2561+ for workspace in &monitor.workspaces {
2542- for ws in &monitor.workspaces {2562+ let preferred =
2543- if ws.original_output.matches(&monitor.output) {2563+ workspace.find_preferred_output(monitors.iter().map(|monitor| &monitor.output));
2544- // This is the primary monitor's own workspace.2564+ let Some(preferred_idx) = preferred else {
2545- continue;2565+ assert_eq!(
2546- }2566+ idx, primary_idx,
2547-2567+ "workspace without a connected output candidate must be on the primary monitor"
2548- let own_monitor_exists = monitors
2549- .iter()
2550- .any(|m| ws.original_output.matches(&m.output));
2551- assert!(
2552- !own_monitor_exists,
2553- "primary monitor cannot have workspaces for which their own monitor exists"
2554 );2568 );
2555- }2569+ continue;
2556- } else {2570+ };
2557- assert!(2571+ assert_eq!(
2558- monitor2572+ idx, preferred_idx,
2559- .workspaces2573+ "workspace must be on its preferred connected output"
2560- .iter()
2561- .any(|workspace| workspace.original_output.matches(&monitor.output)),
2562- "secondary monitor must not have any non-own workspaces"
2563 );2574 );
2564 }2575 }
2565 2576
2566- // FIXME: verify that primary doesn't have any workspaces for which their own monitor
2567- // exists.
2568-
2569 for workspace in &monitor.workspaces {2577 for workspace in &monitor.workspaces {
2570 assert!(2578 assert!(
2571 seen_workspace_id.insert(workspace.id()),2579 seen_workspace_id.insert(workspace.id()),
@@ -2937,16 +2945,19 @@ impl<W: LayoutElement> Layout<W> {
2937 primary_idx,2945 primary_idx,
2938 active_monitor_idx,2946 active_monitor_idx,
2939 } => {2947 } => {
2940- let mon_idx = ws_config2948+ let mon_idx = if ws_config.open_on_output.is_empty() {
2941- .open_on_output2949+ *active_monitor_idx
2942- .as_deref()2950+ } else {
2943- .map(|name| {2951+ ws_config
2944- monitors2952+ .open_on_output
2945- .iter_mut()2953+ .iter()
2946- .position(|monitor| output_matches_name(&monitor.output, name))2954+ .find_map(|name| {
2947- .unwrap_or(*primary_idx)2955+ monitors
2948- })2956+ .iter()
2949- .unwrap_or(*active_monitor_idx);2957+ .position(|monitor| output_matches_name(&monitor.output, name))
2958+ })
2959+ .unwrap_or(*primary_idx)
2960+ };
2950 let mon = &mut monitors[mon_idx];2961 let mon = &mut monitors[mon_idx];
2951 2962
2952 let ws = Workspace::new_with_config(2963 let ws = Workspace::new_with_config(
@@ -3490,7 +3501,7 @@ impl<W: LayoutElement> Layout<W> {
3490 // Do not do anything if the output is already correct3501 // Do not do anything if the output is already correct
3491 if current_idx == target_idx {3502 if current_idx == target_idx {
3492 // Just update the original output since this is an explicit movement action.3503 // Just update the original output since this is an explicit movement action.
3493- current.workspaces[old_idx].original_output = OutputId::new(&current.output);3504+ current.workspaces[old_idx].original_outputs = vec![OutputId::new(&current.output)];
3494 3505
3495 return false;3506 return false;
3496 }3507 }
@@ -3501,7 +3512,7 @@ impl<W: LayoutElement> Layout<W> {
3501 current_idx == *active_monitor_idx && old_idx == current.active_workspace_idx;3512 current_idx == *active_monitor_idx && old_idx == current.active_workspace_idx;
3502 3513
3503 let mut ws = current.remove_workspace_by_idx(old_idx);3514 let mut ws = current.remove_workspace_by_idx(old_idx);
3504- ws.original_output = OutputId::new(new_output);3515+ ws.original_outputs = vec![OutputId::new(new_output)];
3505 3516
3506 let target = &mut monitors[target_idx];3517 let target = &mut monitors[target_idx];
3507 target.insert_workspace(ws, target.active_workspace_idx + 1, activate);3518 target.insert_workspace(ws, target.active_workspace_idx + 1, activate);
src/layout/monitor.rsmodified+3 -3
@@ -557,7 +557,7 @@ impl<W: LayoutElement> Monitor<W> {
557 557
558 // After adding a new window, workspace becomes this output's own.558 // After adding a new window, workspace becomes this output's own.
559 if workspace.name().is_none() {559 if workspace.name().is_none() {
560- workspace.original_output = OutputId::new(&self.output);560+ workspace.original_outputs = vec![OutputId::new(&self.output)];
561 }561 }
562 562
563 if workspace_idx == self.workspaces.len() - 1 {563 if workspace_idx == self.workspaces.len() - 1 {
@@ -602,7 +602,7 @@ impl<W: LayoutElement> Monitor<W> {
602 602
603 // After adding a new window, workspace becomes this output's own.603 // After adding a new window, workspace becomes this output's own.
604 if workspace.name().is_none() {604 if workspace.name().is_none() {
605- workspace.original_output = OutputId::new(&self.output);605+ workspace.original_outputs = vec![OutputId::new(&self.output)];
606 }606 }
607 607
608 if workspace_idx == self.workspaces.len() - 1 {608 if workspace_idx == self.workspaces.len() - 1 {
@@ -636,7 +636,7 @@ impl<W: LayoutElement> Monitor<W> {
636 636
637 // After adding a new window, workspace becomes this output's own.637 // After adding a new window, workspace becomes this output's own.
638 if workspace.name().is_none() {638 if workspace.name().is_none() {
639- workspace.original_output = OutputId::new(&self.output);639+ workspace.original_outputs = vec![OutputId::new(&self.output)];
640 }640 }
641 641
642 // Since we're adding window to an existing column, the workspace isn't empty, and642 // Since we're adding window to an existing column, the workspace isn't empty, and
src/layout/tests.rsmodified+130 -11
@@ -426,8 +426,8 @@ enum Op {
426 AddNamedWorkspace {426 AddNamedWorkspace {
427 #[proptest(strategy = "1..=5usize")]427 #[proptest(strategy = "1..=5usize")]
428 ws_name: usize,428 ws_name: usize,
429- #[proptest(strategy = "prop::option::of(1..=5usize)")]429+ #[proptest(strategy = "proptest::collection::vec(1..=5usize, 0..=3)")]
430- output_name: Option<usize>,430+ output_names: Vec<usize>,
431 #[proptest(strategy = "prop::option::of(arbitrary_layout_part().prop_map(Box::new))")]431 #[proptest(strategy = "prop::option::of(arbitrary_layout_part().prop_map(Box::new))")]
432 layout_config: Option<Box<niri_config::LayoutPart>>,432 layout_config: Option<Box<niri_config::LayoutPart>>,
433 },433 },
@@ -854,12 +854,15 @@ impl Op {
854 }854 }
855 Op::AddNamedWorkspace {855 Op::AddNamedWorkspace {
856 ws_name,856 ws_name,
857- output_name,857+ output_names,
858 layout_config,858 layout_config,
859 } => {859 } => {
860 layout.ensure_named_workspace(&WorkspaceConfig {860 layout.ensure_named_workspace(&WorkspaceConfig {
861 name: WorkspaceName(format!("ws{ws_name}")),861 name: WorkspaceName(format!("ws{ws_name}")),
862- open_on_output: output_name.map(|name| format!("output{name}")),862+ open_on_output: output_names
863+ .into_iter()
864+ .map(|name| format!("output{name}"))
865+ .collect(),
863 layout: layout_config.map(|x| niri_config::WorkspaceLayoutPart(*x)),866 layout: layout_config.map(|x| niri_config::WorkspaceLayoutPart(*x)),
864 });867 });
865 }868 }
@@ -1644,6 +1647,122 @@ fn check_ops(ops: impl IntoIterator<Item = Op>) -> Layout<TestWindow> {
1644 layout1647 layout
1645 }1648 }
1646 1649
1650+fn workspace_output_name(layout: &Layout<TestWindow>, ws_name: usize) -> &str {
1651+ layout
1652+ .workspaces()
1653+ .find(|(_, _, workspace)| workspace.name() == Some(&format!("ws{ws_name}")))
1654+ .and_then(|(monitor, _, _)| monitor)
1655+ .unwrap()
1656+ .output_name()
1657+}
1658+
1659+#[test]
1660+fn workspace_output_fallback_promotes_preferred_in_reverse_connection_order() {
1661+ // output3 is not a candidate, so it stays the primary monitor throughout; every assertion
1662+ // below can only hold because of the candidate list.
1663+ let mut layout = Layout::default();
1664+ check_ops_on_layout(
1665+ &mut layout,
1666+ [
1667+ Op::AddOutput(3),
1668+ Op::AddNamedWorkspace {
1669+ ws_name: 1,
1670+ output_names: vec![1, 2],
1671+ layout_config: None,
1672+ },
1673+ ],
1674+ );
1675+ assert_eq!(workspace_output_name(&layout, 1), "output3");
1676+
1677+ // The second candidate connects first; the workspace must leave the primary monitor.
1678+ check_ops_on_layout(&mut layout, [Op::AddOutput(2)]);
1679+ assert_eq!(workspace_output_name(&layout, 1), "output2");
1680+
1681+ // The first candidate connects last; it still wins.
1682+ check_ops_on_layout(&mut layout, [Op::AddOutput(1)]);
1683+ assert_eq!(workspace_output_name(&layout, 1), "output1");
1684+}
1685+
1686+#[test]
1687+fn workspace_output_preferred_removal_uses_fallback() {
1688+ let mut layout = Layout::default();
1689+ check_ops_on_layout(
1690+ &mut layout,
1691+ [
1692+ Op::AddOutput(3),
1693+ Op::AddNamedWorkspace {
1694+ ws_name: 1,
1695+ output_names: vec![1, 2],
1696+ layout_config: None,
1697+ },
1698+ Op::AddOutput(1),
1699+ Op::AddOutput(2),
1700+ ],
1701+ );
1702+ assert_eq!(workspace_output_name(&layout, 1), "output1");
1703+
1704+ // The preferred candidate disconnects: the next candidate wins over the primary monitor.
1705+ check_ops_on_layout(&mut layout, [Op::RemoveOutput(1)]);
1706+ assert_eq!(workspace_output_name(&layout, 1), "output2");
1707+
1708+ // No candidate left: fall back to the primary monitor.
1709+ check_ops_on_layout(&mut layout, [Op::RemoveOutput(2)]);
1710+ assert_eq!(workspace_output_name(&layout, 1), "output3");
1711+}
1712+
1713+#[test]
1714+fn workspace_output_without_match_stays_on_default_output() {
1715+ let mut layout = Layout::default();
1716+ check_ops_on_layout(
1717+ &mut layout,
1718+ [
1719+ Op::AddOutput(3),
1720+ Op::AddNamedWorkspace {
1721+ ws_name: 1,
1722+ output_names: vec![1, 2],
1723+ layout_config: None,
1724+ },
1725+ ],
1726+ );
1727+ assert_eq!(workspace_output_name(&layout, 1), "output3");
1728+
1729+ // An output that is not in the candidate list must not take the workspace.
1730+ check_ops_on_layout(&mut layout, [Op::AddOutput(4)]);
1731+ assert_eq!(workspace_output_name(&layout, 1), "output3");
1732+}
1733+
1734+#[test]
1735+fn explicit_workspace_move_overrides_output_candidates() {
1736+ let mut layout = Layout::default();
1737+ check_ops_on_layout(
1738+ &mut layout,
1739+ [
1740+ Op::AddOutput(3),
1741+ Op::AddNamedWorkspace {
1742+ ws_name: 1,
1743+ output_names: vec![1, 2],
1744+ layout_config: None,
1745+ },
1746+ Op::AddOutput(2),
1747+ ],
1748+ );
1749+ assert_eq!(workspace_output_name(&layout, 1), "output2");
1750+
1751+ // After an explicit move, the candidate list no longer applies, even when the preferred
1752+ // candidate connects afterwards.
1753+ check_ops_on_layout(
1754+ &mut layout,
1755+ [
1756+ Op::MoveWorkspaceToMonitor {
1757+ ws_name: Some(1),
1758+ output_id: 3,
1759+ },
1760+ Op::AddOutput(1),
1761+ ],
1762+ );
1763+ assert_eq!(workspace_output_name(&layout, 1), "output3");
1764+}
1765+
1647 #[track_caller]1766 #[track_caller]
1648 fn check_ops_with_options(1767 fn check_ops_with_options(
1649 options: Options,1768 options: Options,
@@ -1673,7 +1792,7 @@ fn operations_dont_panic() {
1673 Op::FocusOutput(2),1792 Op::FocusOutput(2),
1674 Op::AddNamedWorkspace {1793 Op::AddNamedWorkspace {
1675 ws_name: 1,1794 ws_name: 1,
1676- output_name: Some(1),1795+ output_names: vec![1],
1677 layout_config: None,1796 layout_config: None,
1678 },1797 },
1679 Op::UnnameWorkspace { ws_name: 1 },1798 Op::UnnameWorkspace { ws_name: 1 },
@@ -1824,7 +1943,7 @@ fn operations_from_starting_state_dont_panic() {
1824 Op::FocusOutput(2),1943 Op::FocusOutput(2),
1825 Op::AddNamedWorkspace {1944 Op::AddNamedWorkspace {
1826 ws_name: 1,1945 ws_name: 1,
1827- output_name: Some(1),1946+ output_names: vec![1],
1828 layout_config: None,1947 layout_config: None,
1829 },1948 },
1830 Op::UnnameWorkspace { ws_name: 1 },1949 Op::UnnameWorkspace { ws_name: 1 },
@@ -2363,12 +2482,12 @@ fn removing_all_outputs_preserves_empty_named_workspaces() {
2363 Op::AddOutput(1),2482 Op::AddOutput(1),
2364 Op::AddNamedWorkspace {2483 Op::AddNamedWorkspace {
2365 ws_name: 1,2484 ws_name: 1,
2366- output_name: None,2485+ output_names: vec![],
2367 layout_config: None,2486 layout_config: None,
2368 },2487 },
2369 Op::AddNamedWorkspace {2488 Op::AddNamedWorkspace {
2370 ws_name: 2,2489 ws_name: 2,
2371- output_name: None,2490+ output_names: vec![],
2372 layout_config: None,2491 layout_config: None,
2373 },2492 },
2374 Op::RemoveOutput(1),2493 Op::RemoveOutput(1),
@@ -2731,7 +2850,7 @@ fn named_workspace_to_output() {
2731 let ops = [2850 let ops = [
2732 Op::AddNamedWorkspace {2851 Op::AddNamedWorkspace {
2733 ws_name: 1,2852 ws_name: 1,
2734- output_name: None,2853+ output_names: vec![],
2735 layout_config: None,2854 layout_config: None,
2736 },2855 },
2737 Op::AddOutput(1),2856 Op::AddOutput(1),
@@ -2747,7 +2866,7 @@ fn named_workspace_to_output_ewaf() {
2747 let ops = [2866 let ops = [
2748 Op::AddNamedWorkspace {2867 Op::AddNamedWorkspace {
2749 ws_name: 1,2868 ws_name: 1,
2750- output_name: Some(2),2869+ output_names: vec![2],
2751 layout_config: None,2870 layout_config: None,
2752 },2871 },
2753 Op::AddOutput(1),2872 Op::AddOutput(1),
@@ -2981,7 +3100,7 @@ fn interactive_move_from_workspace_with_layout_config() {
2981 let ops = [3100 let ops = [
2982 Op::AddNamedWorkspace {3101 Op::AddNamedWorkspace {
2983 ws_name: 1,3102 ws_name: 1,
2984- output_name: Some(2),3103+ output_names: vec![2],
2985 layout_config: Some(Box::new(niri_config::LayoutPart {3104 layout_config: Some(Box::new(niri_config::LayoutPart {
2986 border: Some(niri_config::BorderRule {3105 border: Some(niri_config::BorderRule {
2987 on: true,3106 on: true,
src/layout/workspace.rsmodified+47 -19
@@ -54,11 +54,11 @@ pub struct Workspace<W: LayoutElement> {
54 /// Whether the floating layout is active instead of the scrolling layout.54 /// Whether the floating layout is active instead of the scrolling layout.
55 floating_is_active: FloatingActive,55 floating_is_active: FloatingActive,
56 56
57- /// The original output of this workspace.57+ /// The original outputs of this workspace, in preference order.
58 ///58 ///
59 /// Most of the time this will be the workspace's current output, however, after an output59 /// Most of the time this will be the workspace's current output, however, after an output
60- /// disconnection, it may remain pointing to the disconnected output.60+ /// disconnection, they may remain pointing to disconnected outputs.
61- pub(super) original_output: OutputId,61+ pub(super) original_outputs: Vec<OutputId>,
62 62
63 /// Current output of this workspace.63 /// Current output of this workspace.
64 output: Option<Output>,64 output: Option<Output>,
@@ -114,7 +114,7 @@ pub struct Workspace<W: LayoutElement> {
114 id: WorkspaceId,114 id: WorkspaceId,
115 }115 }
116 116
117-#[derive(Debug, Clone)]117+#[derive(Debug, Clone, PartialEq, Eq)]
118 pub struct OutputId(String);118 pub struct OutputId(String);
119 119
120 impl OutputId {120 impl OutputId {
@@ -217,11 +217,15 @@ impl<W: LayoutElement> Workspace<W> {
217 clock: Clock,217 clock: Clock,
218 base_options: Rc<Options>,218 base_options: Rc<Options>,
219 ) -> Self {219 ) -> Self {
220- let original_output = config220+ let mut original_outputs: Vec<OutputId> = config
221 .as_ref()221 .as_ref()
222- .and_then(|c| c.open_on_output.clone())222+ .map(|c| c.open_on_output.iter().cloned().map(OutputId).collect())
223- .map(OutputId)223+ .unwrap_or_default();
224- .unwrap_or(OutputId::new(&output));224+ if original_outputs.is_empty() {
225+ original_outputs.push(OutputId::new(&output));
226+ } else {
227+ Self::normalize_original_outputs(&mut original_outputs, &output);
228+ }
225 229
226 let layout_config = config.as_mut().and_then(|c| c.layout.take().map(|x| x.0));230 let layout_config = config.as_mut().and_then(|c| c.layout.take().map(|x| x.0));
227 231
@@ -258,7 +262,7 @@ impl<W: LayoutElement> Workspace<W> {
258 scrolling,262 scrolling,
259 floating,263 floating,
260 floating_is_active: FloatingActive::No,264 floating_is_active: FloatingActive::No,
261- original_output,265+ original_outputs,
262 scale,266 scale,
263 transform: output.current_transform(),267 transform: output.current_transform(),
264 view_size,268 view_size,
@@ -280,12 +284,10 @@ impl<W: LayoutElement> Workspace<W> {
280 clock: Clock,284 clock: Clock,
281 base_options: Rc<Options>,285 base_options: Rc<Options>,
282 ) -> Self {286 ) -> Self {
283- let original_output = OutputId(287+ let original_outputs = config
284- config288+ .as_ref()
285- .as_ref()289+ .map(|c| c.open_on_output.iter().cloned().map(OutputId).collect())
286- .and_then(|c| c.open_on_output.clone())290+ .unwrap_or_default();
287- .unwrap_or_default(),
288- );
289 291
290 let layout_config = config.as_mut().and_then(|c| c.layout.take().map(|x| x.0));292 let layout_config = config.as_mut().and_then(|c| c.layout.take().map(|x| x.0));
291 293
@@ -325,7 +327,7 @@ impl<W: LayoutElement> Workspace<W> {
325 output: None,327 output: None,
326 scale,328 scale,
327 transform: Transform::Normal,329 transform: Transform::Normal,
328- original_output,330+ original_outputs,
329 view_size,331 view_size,
330 working_area,332 working_area,
331 shadow: Shadow::new(shadow_config),333 shadow: Shadow::new(shadow_config),
@@ -474,6 +476,15 @@ impl<W: LayoutElement> Workspace<W> {
474 self.output.as_ref()476 self.output.as_ref()
475 }477 }
476 478
479+ pub(super) fn find_preferred_output<'a>(
480+ &self,
481+ outputs: impl Iterator<Item = &'a Output> + Clone,
482+ ) -> Option<usize> {
483+ self.original_outputs
484+ .iter()
485+ .find_map(|id| outputs.clone().position(|output| id.matches(output)))
486+ }
487+
477 pub fn active_window(&self) -> Option<&W> {488 pub fn active_window(&self) -> Option<&W> {
478 if self.floating_is_active.get() {489 if self.floating_is_active.get() {
479 self.floating.active_window()490 self.floating.active_window()
@@ -508,9 +519,11 @@ impl<W: LayoutElement> Workspace<W> {
508 self.output = output;519 self.output = output;
509 520
510 if let Some(output) = &self.output {521 if let Some(output) = &self.output {
511- // Normalize original output: possibly replace connector with make/model/serial.522+ if self.original_outputs.is_empty() {
512- if self.original_output.matches(output) {523+ self.original_outputs.push(OutputId::new(output));
513- self.original_output = OutputId::new(output);524+ } else {
525+ // Normalize original outputs: possibly replace connectors with make/model/serial.
526+ Self::normalize_original_outputs(&mut self.original_outputs, output);
514 }527 }
515 528
516 self.update_output_size();529 self.update_output_size();
@@ -521,6 +534,21 @@ impl<W: LayoutElement> Workspace<W> {
521 }534 }
522 }535 }
523 536
537+ fn normalize_original_outputs(outputs: &mut Vec<OutputId>, output: &Output) {
538+ for id in &mut *outputs {
539+ if id.matches(output) {
540+ *id = OutputId::new(output);
541+ }
542+ }
543+
544+ // Connector and make/model/serial candidates may resolve to the same output.
545+ for idx in (0..outputs.len()).rev() {
546+ if outputs[..idx].contains(&outputs[idx]) {
547+ outputs.remove(idx);
548+ }
549+ }
550+ }
551+
524 fn enter_output_for_window(&self, window: &W) {552 fn enter_output_for_window(&self, window: &W) {
525 if let Some(output) = &self.output {553 if let Some(output) = &self.output {
526 window.set_preferred_scale_transform(self.scale, self.transform);554 window.set_preferred_scale_transform(self.scale, self.transform);