Support multiple open-on-output fallbacks for named workspaces (#4618)
Browse files at this commit7 changed files +279 -106
- docs/wiki/Configuration:-Named-Workspaces.md +13 -0
- niri-config/src/lib.rs +6 -4
- niri-config/src/workspace.rs +2 -2
- src/layout/mod.rs +78 -67
- src/layout/monitor.rs +3 -3
- src/layout/tests.rs +130 -11
- src/layout/workspace.rs +47 -19
docs/wiki/Configuration:-Named-Workspaces.mdmodified+13 -0
| @@ -46,6 +46,19 @@ Before, it could only use the connector name. | ||
| 46 | 46 | This means that named workspaces "stick" to their original output in more cases, reflecting their more permanent nature. |
| 47 | 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 | 62 | ### Layout config overrides |
| 50 | 63 | |
| 51 | 64 | <sup>Since: 25.11</sup> |
niri-config/src/lib.rsmodified+6 -4
| @@ -963,6 +963,7 @@ mod tests { | ||
| 963 | 963 | |
| 964 | 964 | workspace "workspace-1" { |
| 965 | 965 | open-on-output "eDP-1" |
| 966 | + open-on-output "DP-1" | |
| 966 | 967 | } |
| 967 | 968 | workspace "workspace-2" |
| 968 | 969 | workspace "workspace-3" |
| @@ -2312,23 +2313,24 @@ mod tests { | ||
| 2312 | 2313 | name: WorkspaceName( |
| 2313 | 2314 | "workspace-1", |
| 2314 | 2315 | ), |
| 2315 | - open_on_output: Some( | |
| 2316 | + open_on_output: [ | |
| 2316 | 2317 | "eDP-1", |
| 2317 | - ), | |
| 2318 | + "DP-1", | |
| 2319 | + ], | |
| 2318 | 2320 | layout: None, |
| 2319 | 2321 | }, |
| 2320 | 2322 | Workspace { |
| 2321 | 2323 | name: WorkspaceName( |
| 2322 | 2324 | "workspace-2", |
| 2323 | 2325 | ), |
| 2324 | - open_on_output: None, | |
| 2326 | + open_on_output: [], | |
| 2325 | 2327 | layout: None, |
| 2326 | 2328 | }, |
| 2327 | 2329 | Workspace { |
| 2328 | 2330 | name: WorkspaceName( |
| 2329 | 2331 | "workspace-3", |
| 2330 | 2332 | ), |
| 2331 | - open_on_output: None, | |
| 2333 | + open_on_output: [], | |
| 2332 | 2334 | layout: None, |
| 2333 | 2335 | }, |
| 2334 | 2336 | ], |
niri-config/src/workspace.rsmodified+2 -2
| @@ -6,8 +6,8 @@ use crate::LayoutPart; | ||
| 6 | 6 | pub struct Workspace { |
| 7 | 7 | #[knuffel(argument)] |
| 8 | 8 | pub name: WorkspaceName, |
| 9 | - #[knuffel(child, unwrap(argument))] | |
| 10 | - pub open_on_output: Option<String>, | |
| 9 | + #[knuffel(children(name = "open-on-output"), unwrap(argument))] | |
| 10 | + pub open_on_output: Vec<String>, | |
| 11 | 11 | #[knuffel(child)] |
| 12 | 12 | pub layout: Option<WorkspaceLayoutPart>, |
| 13 | 13 | } |
src/layout/mod.rsmodified+78 -67
| @@ -19,10 +19,10 @@ | ||
| 19 | 19 | //! 2. Connecting an output must not change the layout for any workspaces that were never on that |
| 20 | 20 | //! output. |
| 21 | 21 | //! |
| 22 | -//! Therefore, we implement the following logic: every workspace keeps track of which output it | |
| 23 | -//! originated on—its *original output*. When an output disconnects, its workspaces are appended to | |
| 24 | -//! the (potentially new) primary output, but remember their original output. Then, if the original | |
| 25 | -//! output connects again, all workspaces originally from there move back to that output. | |
| 22 | +//! Therefore, we implement the following logic: every workspace keeps track of its ordered | |
| 23 | +//! original output candidates. When an output disconnects, its workspaces move to the first | |
| 24 | +//! connected candidate or the (potentially new) primary output. When a higher-priority candidate | |
| 25 | +//! connects, matching workspaces move to it. | |
| 26 | 26 | //! |
| 27 | 27 | //! In order to avoid surprising behavior, if the user creates or moves any new windows onto a |
| 28 | 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 | 764 | primary_idx, |
| 765 | 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![]; | |
| 772 | - for i in (0..primary.workspaces.len()).rev() { | |
| 773 | - if primary.workspaces[i].original_output.matches(&output) { | |
| 774 | - let ws = primary.workspaces.remove(i); | |
| 783 | + let source = &mut monitors[source_idx]; | |
| 784 | + let ws = source.workspaces.remove(i); | |
| 775 | 785 | |
| 776 | 786 | // FIXME: this can be coded in a way that the workspace switch won't be |
| 777 | 787 | // affected if the removed workspace is invisible. But this is good enough |
| 778 | 788 | // for now. |
| 779 | - if primary.workspace_switch.is_some() { | |
| 780 | - primary.workspace_switch = None; | |
| 781 | - stopped_primary_ws_switch = true; | |
| 789 | + if source.workspace_switch.is_some() { | |
| 790 | + source.workspace_switch = None; | |
| 791 | + stopped_ws_switch = true; | |
| 782 | 792 | } |
| 783 | 793 | |
| 784 | 794 | // The user could've closed a window while remaining on this workspace, on |
| @@ -788,7 +798,7 @@ impl<W: LayoutElement> Layout<W> { | ||
| 788 | 798 | workspaces.push(ws); |
| 789 | 799 | } |
| 790 | 800 | |
| 791 | - if i <= primary.active_workspace_idx | |
| 801 | + if i <= source.active_workspace_idx | |
| 792 | 802 | // Generally when moving the currently active workspace, we want to |
| 793 | 803 | // fall back to the workspace above, so as not to end up on the last |
| 794 | 804 | // empty workspace. However, with empty workspace above first, when |
| @@ -799,27 +809,27 @@ impl<W: LayoutElement> Layout<W> { | ||
| 799 | 809 | // workspaces set up across multiple monitors. Without this check, the |
| 800 | 810 | // first monitor to connect can end up with the first empty workspace |
| 801 | 811 | // focused instead of the first named workspace. |
| 802 | - && !(primary.options.layout.empty_workspace_above_first | |
| 803 | - && primary.active_workspace_idx == 1) | |
| 812 | + && !(source.options.layout.empty_workspace_above_first | |
| 813 | + && source.active_workspace_idx == 1) | |
| 804 | 814 | { |
| 805 | - primary.active_workspace_idx = | |
| 806 | - primary.active_workspace_idx.saturating_sub(1); | |
| 815 | + source.active_workspace_idx = | |
| 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. | |
| 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. | |
| 820 | + let source = &mut monitors[source_idx]; | |
| 815 | 821 | |
| 816 | - if stopped_primary_ws_switch | |
| 817 | - || (primary.options.layout.empty_workspace_above_first | |
| 818 | - && primary.workspaces.len() == 2) | |
| 819 | - { | |
| 820 | - primary.clean_up_workspaces(); | |
| 822 | + // If we stopped a workspace switch, then we might need to clean up workspaces. | |
| 823 | + // Also if empty_workspace_above_first is set and there are only 2 workspaces | |
| 824 | + // left, both will be empty and one of them needs to be removed. | |
| 825 | + // clean_up_workspaces takes care of this. | |
| 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 | 833 | workspaces.reverse(); |
| 824 | 834 | |
| 825 | 835 | let ws_id_to_activate = self.last_active_workspace_id.remove(&output.name()); |
| @@ -907,8 +917,18 @@ impl<W: LayoutElement> Layout<W> { | ||
| 907 | 917 | active_monitor_idx = active_monitor_idx.saturating_sub(1); |
| 908 | 918 | } |
| 909 | 919 | |
| 910 | - let primary = &mut monitors[primary_idx]; | |
| 911 | - primary.append_workspaces(workspaces); | |
| 920 | + for target_idx in 0..monitors.len() { | |
| 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 | 933 | MonitorSet::Normal { |
| 914 | 934 | monitors, |
| @@ -2538,34 +2558,22 @@ impl<W: LayoutElement> Layout<W> { | ||
| 2538 | 2558 | |
| 2539 | 2559 | monitor.verify_invariants(); |
| 2540 | 2560 | |
| 2541 | - if idx == primary_idx { | |
| 2542 | - for ws in &monitor.workspaces { | |
| 2543 | - if ws.original_output.matches(&monitor.output) { | |
| 2544 | - // This is the primary monitor's own workspace. | |
| 2545 | - continue; | |
| 2546 | - } | |
| 2547 | - | |
| 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" | |
| 2561 | + for workspace in &monitor.workspaces { | |
| 2562 | + let preferred = | |
| 2563 | + workspace.find_preferred_output(monitors.iter().map(|monitor| &monitor.output)); | |
| 2564 | + let Some(preferred_idx) = preferred else { | |
| 2565 | + assert_eq!( | |
| 2566 | + idx, primary_idx, | |
| 2567 | + "workspace without a connected output candidate must be on the primary monitor" | |
| 2554 | 2568 | ); |
| 2555 | - } | |
| 2556 | - } else { | |
| 2557 | - assert!( | |
| 2558 | - monitor | |
| 2559 | - .workspaces | |
| 2560 | - .iter() | |
| 2561 | - .any(|workspace| workspace.original_output.matches(&monitor.output)), | |
| 2562 | - "secondary monitor must not have any non-own workspaces" | |
| 2569 | + continue; | |
| 2570 | + }; | |
| 2571 | + assert_eq!( | |
| 2572 | + idx, preferred_idx, | |
| 2573 | + "workspace must be on its preferred connected output" | |
| 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 | 2577 | for workspace in &monitor.workspaces { |
| 2570 | 2578 | assert!( |
| 2571 | 2579 | seen_workspace_id.insert(workspace.id()), |
| @@ -2937,16 +2945,19 @@ impl<W: LayoutElement> Layout<W> { | ||
| 2937 | 2945 | primary_idx, |
| 2938 | 2946 | active_monitor_idx, |
| 2939 | 2947 | } => { |
| 2940 | - let mon_idx = ws_config | |
| 2941 | - .open_on_output | |
| 2942 | - .as_deref() | |
| 2943 | - .map(|name| { | |
| 2944 | - monitors | |
| 2945 | - .iter_mut() | |
| 2946 | - .position(|monitor| output_matches_name(&monitor.output, name)) | |
| 2947 | - .unwrap_or(*primary_idx) | |
| 2948 | - }) | |
| 2949 | - .unwrap_or(*active_monitor_idx); | |
| 2948 | + let mon_idx = if ws_config.open_on_output.is_empty() { | |
| 2949 | + *active_monitor_idx | |
| 2950 | + } else { | |
| 2951 | + ws_config | |
| 2952 | + .open_on_output | |
| 2953 | + .iter() | |
| 2954 | + .find_map(|name| { | |
| 2955 | + monitors | |
| 2956 | + .iter() | |
| 2957 | + .position(|monitor| output_matches_name(&monitor.output, name)) | |
| 2958 | + }) | |
| 2959 | + .unwrap_or(*primary_idx) | |
| 2960 | + }; | |
| 2950 | 2961 | let mon = &mut monitors[mon_idx]; |
| 2951 | 2962 | |
| 2952 | 2963 | let ws = Workspace::new_with_config( |
| @@ -3490,7 +3501,7 @@ impl<W: LayoutElement> Layout<W> { | ||
| 3490 | 3501 | // Do not do anything if the output is already correct |
| 3491 | 3502 | if current_idx == target_idx { |
| 3492 | 3503 | // Just update the original output since this is an explicit movement action. |
| 3493 | - current.workspaces[old_idx].original_output = OutputId::new(¤t.output); | |
| 3504 | + current.workspaces[old_idx].original_outputs = vec![OutputId::new(¤t.output)]; | |
| 3494 | 3505 | |
| 3495 | 3506 | return false; |
| 3496 | 3507 | } |
| @@ -3501,7 +3512,7 @@ impl<W: LayoutElement> Layout<W> { | ||
| 3501 | 3512 | current_idx == *active_monitor_idx && old_idx == current.active_workspace_idx; |
| 3502 | 3513 | |
| 3503 | 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 | 3517 | let target = &mut monitors[target_idx]; |
| 3507 | 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 | 558 | // After adding a new window, workspace becomes this output's own. |
| 559 | 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 | 563 | if workspace_idx == self.workspaces.len() - 1 { |
| @@ -602,7 +602,7 @@ impl<W: LayoutElement> Monitor<W> { | ||
| 602 | 602 | |
| 603 | 603 | // After adding a new window, workspace becomes this output's own. |
| 604 | 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 | 608 | if workspace_idx == self.workspaces.len() - 1 { |
| @@ -636,7 +636,7 @@ impl<W: LayoutElement> Monitor<W> { | ||
| 636 | 636 | |
| 637 | 637 | // After adding a new window, workspace becomes this output's own. |
| 638 | 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 | 642 | // 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 | 426 | AddNamedWorkspace { |
| 427 | 427 | #[proptest(strategy = "1..=5usize")] |
| 428 | 428 | ws_name: usize, |
| 429 | - #[proptest(strategy = "prop::option::of(1..=5usize)")] | |
| 430 | - output_name: Option<usize>, | |
| 429 | + #[proptest(strategy = "proptest::collection::vec(1..=5usize, 0..=3)")] | |
| 430 | + output_names: Vec<usize>, | |
| 431 | 431 | #[proptest(strategy = "prop::option::of(arbitrary_layout_part().prop_map(Box::new))")] |
| 432 | 432 | layout_config: Option<Box<niri_config::LayoutPart>>, |
| 433 | 433 | }, |
| @@ -854,12 +854,15 @@ impl Op { | ||
| 854 | 854 | } |
| 855 | 855 | Op::AddNamedWorkspace { |
| 856 | 856 | ws_name, |
| 857 | - output_name, | |
| 857 | + output_names, | |
| 858 | 858 | layout_config, |
| 859 | 859 | } => { |
| 860 | 860 | layout.ensure_named_workspace(&WorkspaceConfig { |
| 861 | 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 | 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 | 1647 | 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 | 1766 | #[track_caller] |
| 1648 | 1767 | fn check_ops_with_options( |
| 1649 | 1768 | options: Options, |
| @@ -1673,7 +1792,7 @@ fn operations_dont_panic() { | ||
| 1673 | 1792 | Op::FocusOutput(2), |
| 1674 | 1793 | Op::AddNamedWorkspace { |
| 1675 | 1794 | ws_name: 1, |
| 1676 | - output_name: Some(1), | |
| 1795 | + output_names: vec![1], | |
| 1677 | 1796 | layout_config: None, |
| 1678 | 1797 | }, |
| 1679 | 1798 | Op::UnnameWorkspace { ws_name: 1 }, |
| @@ -1824,7 +1943,7 @@ fn operations_from_starting_state_dont_panic() { | ||
| 1824 | 1943 | Op::FocusOutput(2), |
| 1825 | 1944 | Op::AddNamedWorkspace { |
| 1826 | 1945 | ws_name: 1, |
| 1827 | - output_name: Some(1), | |
| 1946 | + output_names: vec![1], | |
| 1828 | 1947 | layout_config: None, |
| 1829 | 1948 | }, |
| 1830 | 1949 | Op::UnnameWorkspace { ws_name: 1 }, |
| @@ -2363,12 +2482,12 @@ fn removing_all_outputs_preserves_empty_named_workspaces() { | ||
| 2363 | 2482 | Op::AddOutput(1), |
| 2364 | 2483 | Op::AddNamedWorkspace { |
| 2365 | 2484 | ws_name: 1, |
| 2366 | - output_name: None, | |
| 2485 | + output_names: vec![], | |
| 2367 | 2486 | layout_config: None, |
| 2368 | 2487 | }, |
| 2369 | 2488 | Op::AddNamedWorkspace { |
| 2370 | 2489 | ws_name: 2, |
| 2371 | - output_name: None, | |
| 2490 | + output_names: vec![], | |
| 2372 | 2491 | layout_config: None, |
| 2373 | 2492 | }, |
| 2374 | 2493 | Op::RemoveOutput(1), |
| @@ -2731,7 +2850,7 @@ fn named_workspace_to_output() { | ||
| 2731 | 2850 | let ops = [ |
| 2732 | 2851 | Op::AddNamedWorkspace { |
| 2733 | 2852 | ws_name: 1, |
| 2734 | - output_name: None, | |
| 2853 | + output_names: vec![], | |
| 2735 | 2854 | layout_config: None, |
| 2736 | 2855 | }, |
| 2737 | 2856 | Op::AddOutput(1), |
| @@ -2747,7 +2866,7 @@ fn named_workspace_to_output_ewaf() { | ||
| 2747 | 2866 | let ops = [ |
| 2748 | 2867 | Op::AddNamedWorkspace { |
| 2749 | 2868 | ws_name: 1, |
| 2750 | - output_name: Some(2), | |
| 2869 | + output_names: vec![2], | |
| 2751 | 2870 | layout_config: None, |
| 2752 | 2871 | }, |
| 2753 | 2872 | Op::AddOutput(1), |
| @@ -2981,7 +3100,7 @@ fn interactive_move_from_workspace_with_layout_config() { | ||
| 2981 | 3100 | let ops = [ |
| 2982 | 3101 | Op::AddNamedWorkspace { |
| 2983 | 3102 | ws_name: 1, |
| 2984 | - output_name: Some(2), | |
| 3103 | + output_names: vec![2], | |
| 2985 | 3104 | layout_config: Some(Box::new(niri_config::LayoutPart { |
| 2986 | 3105 | border: Some(niri_config::BorderRule { |
| 2987 | 3106 | on: true, |
src/layout/workspace.rsmodified+47 -19
| @@ -54,11 +54,11 @@ pub struct Workspace<W: LayoutElement> { | ||
| 54 | 54 | /// Whether the floating layout is active instead of the scrolling layout. |
| 55 | 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 | 59 | /// 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. | |
| 61 | - pub(super) original_output: OutputId, | |
| 60 | + /// disconnection, they may remain pointing to disconnected outputs. | |
| 61 | + pub(super) original_outputs: Vec<OutputId>, | |
| 62 | 62 | |
| 63 | 63 | /// Current output of this workspace. |
| 64 | 64 | output: Option<Output>, |
| @@ -114,7 +114,7 @@ pub struct Workspace<W: LayoutElement> { | ||
| 114 | 114 | id: WorkspaceId, |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | -#[derive(Debug, Clone)] | |
| 117 | +#[derive(Debug, Clone, PartialEq, Eq)] | |
| 118 | 118 | pub struct OutputId(String); |
| 119 | 119 | |
| 120 | 120 | impl OutputId { |
| @@ -217,11 +217,15 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 217 | 217 | clock: Clock, |
| 218 | 218 | base_options: Rc<Options>, |
| 219 | 219 | ) -> Self { |
| 220 | - let original_output = config | |
| 220 | + let mut original_outputs: Vec<OutputId> = config | |
| 221 | 221 | .as_ref() |
| 222 | - .and_then(|c| c.open_on_output.clone()) | |
| 223 | - .map(OutputId) | |
| 224 | - .unwrap_or(OutputId::new(&output)); | |
| 222 | + .map(|c| c.open_on_output.iter().cloned().map(OutputId).collect()) | |
| 223 | + .unwrap_or_default(); | |
| 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 | 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 | 262 | scrolling, |
| 259 | 263 | floating, |
| 260 | 264 | floating_is_active: FloatingActive::No, |
| 261 | - original_output, | |
| 265 | + original_outputs, | |
| 262 | 266 | scale, |
| 263 | 267 | transform: output.current_transform(), |
| 264 | 268 | view_size, |
| @@ -280,12 +284,10 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 280 | 284 | clock: Clock, |
| 281 | 285 | base_options: Rc<Options>, |
| 282 | 286 | ) -> Self { |
| 283 | - let original_output = OutputId( | |
| 284 | - config | |
| 285 | - .as_ref() | |
| 286 | - .and_then(|c| c.open_on_output.clone()) | |
| 287 | - .unwrap_or_default(), | |
| 288 | - ); | |
| 287 | + let original_outputs = config | |
| 288 | + .as_ref() | |
| 289 | + .map(|c| c.open_on_output.iter().cloned().map(OutputId).collect()) | |
| 290 | + .unwrap_or_default(); | |
| 289 | 291 | |
| 290 | 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 | 327 | output: None, |
| 326 | 328 | scale, |
| 327 | 329 | transform: Transform::Normal, |
| 328 | - original_output, | |
| 330 | + original_outputs, | |
| 329 | 331 | view_size, |
| 330 | 332 | working_area, |
| 331 | 333 | shadow: Shadow::new(shadow_config), |
| @@ -474,6 +476,15 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 474 | 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 | 488 | pub fn active_window(&self) -> Option<&W> { |
| 478 | 489 | if self.floating_is_active.get() { |
| 479 | 490 | self.floating.active_window() |
| @@ -508,9 +519,11 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 508 | 519 | self.output = output; |
| 509 | 520 | |
| 510 | 521 | if let Some(output) = &self.output { |
| 511 | - // Normalize original output: possibly replace connector with make/model/serial. | |
| 512 | - if self.original_output.matches(output) { | |
| 513 | - self.original_output = OutputId::new(output); | |
| 522 | + if self.original_outputs.is_empty() { | |
| 523 | + self.original_outputs.push(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 | 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 | 552 | fn enter_output_for_window(&self, window: &W) { |
| 525 | 553 | if let Some(output) = &self.output { |
| 526 | 554 | window.set_preferred_scale_transform(self.scale, self.transform); |