From 9a35d3774a7ff1acdce07fde05bdc94dfa5a0258 Mon Sep 17 00:00:00 2001 From: Artem Timofeev <39891735+atimofeev@users.noreply.github.com> Date: Tue, 22 Sep 2026 09:10:39 +0200 Subject: [PATCH] Support multiple open-on-output fallbacks for named workspaces (#4618) * feat(config): support multiple open-on-output fallbacks * Change open-on-output to be separate lines --------- Co-authored-by: Ivan Molodetskikh --- docs/wiki/Configuration:-Named-Workspaces.md | 13 ++ niri-config/src/lib.rs | 10 +- niri-config/src/workspace.rs | 4 +- src/layout/mod.rs | 145 ++++++++++--------- src/layout/monitor.rs | 6 +- src/layout/tests.rs | 141 ++++++++++++++++-- src/layout/workspace.rs | 66 ++++++--- 7 files changed, 279 insertions(+), 106 deletions(-) diff --git a/docs/wiki/Configuration:-Named-Workspaces.md b/docs/wiki/Configuration:-Named-Workspaces.md index c07367a7..712d25e1 100644 --- a/docs/wiki/Configuration:-Named-Workspaces.md +++ b/docs/wiki/Configuration:-Named-Workspaces.md @@ -46,6 +46,19 @@ Before, it could only use the connector name. This means that named workspaces "stick" to their original output in more cases, reflecting their more permanent nature. Explicitly moving a named workspace to a different monitor will still update its original output. +Since: next release You can now write multiple `open-on-output` lines. +If the first output is not connected, the workspace will open on the second output, and so on. + +```kdl +workspace "browser" { + open-on-output "DP-9" + open-on-output "DP-4" +} +``` + +The output order is reevaluated whenever outputs connect or disconnect. +Connecting a higher-priority output will move the workspace there. + ### Layout config overrides Since: 25.11 diff --git a/niri-config/src/lib.rs b/niri-config/src/lib.rs index 5dff4e5c..22e0d3db 100644 --- a/niri-config/src/lib.rs +++ b/niri-config/src/lib.rs @@ -963,6 +963,7 @@ mod tests { workspace "workspace-1" { open-on-output "eDP-1" + open-on-output "DP-1" } workspace "workspace-2" workspace "workspace-3" @@ -2312,23 +2313,24 @@ mod tests { name: WorkspaceName( "workspace-1", ), - open_on_output: Some( + open_on_output: [ "eDP-1", - ), + "DP-1", + ], layout: None, }, Workspace { name: WorkspaceName( "workspace-2", ), - open_on_output: None, + open_on_output: [], layout: None, }, Workspace { name: WorkspaceName( "workspace-3", ), - open_on_output: None, + open_on_output: [], layout: None, }, ], diff --git a/niri-config/src/workspace.rs b/niri-config/src/workspace.rs index d34dcf46..971a5464 100644 --- a/niri-config/src/workspace.rs +++ b/niri-config/src/workspace.rs @@ -6,8 +6,8 @@ use crate::LayoutPart; pub struct Workspace { #[knuffel(argument)] pub name: WorkspaceName, - #[knuffel(child, unwrap(argument))] - pub open_on_output: Option, + #[knuffel(children(name = "open-on-output"), unwrap(argument))] + pub open_on_output: Vec, #[knuffel(child)] pub layout: Option, } diff --git a/src/layout/mod.rs b/src/layout/mod.rs index c79a5036..97f917a9 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -19,10 +19,10 @@ //! 2. Connecting an output must not change the layout for any workspaces that were never on that //! output. //! -//! Therefore, we implement the following logic: every workspace keeps track of which output it -//! originated on—its *original output*. When an output disconnects, its workspaces are appended to -//! the (potentially new) primary output, but remember their original output. Then, if the original -//! output connects again, all workspaces originally from there move back to that output. +//! Therefore, we implement the following logic: every workspace keeps track of its ordered +//! original output candidates. When an output disconnects, its workspaces move to the first +//! connected candidate or the (potentially new) primary output. When a higher-priority candidate +//! connects, matching workspaces move to it. //! //! In order to avoid surprising behavior, if the user creates or moves any new windows onto a //! workspace, it forgets its original output, and its current output becomes its original output. @@ -764,21 +764,31 @@ impl Layout { primary_idx, active_monitor_idx, } => { - let primary = &mut monitors[primary_idx]; + let new_output_idx = monitors.len(); + let mut workspaces = vec![]; + for source_idx in 0..monitors.len() { + let mut stopped_ws_switch = false; - let mut stopped_primary_ws_switch = false; + for i in (0..monitors[source_idx].workspaces.len()).rev() { + let preferred = monitors[source_idx].workspaces[i].find_preferred_output( + monitors + .iter() + .map(|monitor| &monitor.output) + .chain(std::iter::once(&output)), + ) == Some(new_output_idx); + if !preferred { + continue; + } - let mut workspaces = vec![]; - for i in (0..primary.workspaces.len()).rev() { - if primary.workspaces[i].original_output.matches(&output) { - let ws = primary.workspaces.remove(i); + let source = &mut monitors[source_idx]; + let ws = source.workspaces.remove(i); // FIXME: this can be coded in a way that the workspace switch won't be // affected if the removed workspace is invisible. But this is good enough // for now. - if primary.workspace_switch.is_some() { - primary.workspace_switch = None; - stopped_primary_ws_switch = true; + if source.workspace_switch.is_some() { + source.workspace_switch = None; + stopped_ws_switch = true; } // The user could've closed a window while remaining on this workspace, on @@ -788,7 +798,7 @@ impl Layout { workspaces.push(ws); } - if i <= primary.active_workspace_idx + if i <= source.active_workspace_idx // Generally when moving the currently active workspace, we want to // fall back to the workspace above, so as not to end up on the last // empty workspace. However, with empty workspace above first, when @@ -799,27 +809,27 @@ impl Layout { // workspaces set up across multiple monitors. Without this check, the // first monitor to connect can end up with the first empty workspace // focused instead of the first named workspace. - && !(primary.options.layout.empty_workspace_above_first - && primary.active_workspace_idx == 1) + && !(source.options.layout.empty_workspace_above_first + && source.active_workspace_idx == 1) { - primary.active_workspace_idx = - primary.active_workspace_idx.saturating_sub(1); + source.active_workspace_idx = + source.active_workspace_idx.saturating_sub(1); } } - } - // If we stopped a workspace switch, then we might need to clean up workspaces. - // Also if empty_workspace_above_first is set and there are only 2 workspaces left, - // both will be empty and one of them needs to be removed. clean_up_workspaces - // takes care of this. + let source = &mut monitors[source_idx]; - if stopped_primary_ws_switch - || (primary.options.layout.empty_workspace_above_first - && primary.workspaces.len() == 2) - { - primary.clean_up_workspaces(); + // If we stopped a workspace switch, then we might need to clean up workspaces. + // Also if empty_workspace_above_first is set and there are only 2 workspaces + // left, both will be empty and one of them needs to be removed. + // clean_up_workspaces takes care of this. + if stopped_ws_switch + || (source.options.layout.empty_workspace_above_first + && source.workspaces.len() == 2) + { + source.clean_up_workspaces(); + } } - workspaces.reverse(); let ws_id_to_activate = self.last_active_workspace_id.remove(&output.name()); @@ -907,8 +917,18 @@ impl Layout { active_monitor_idx = active_monitor_idx.saturating_sub(1); } - let primary = &mut monitors[primary_idx]; - primary.append_workspaces(workspaces); + for target_idx in 0..monitors.len() { + let target_workspaces = workspaces + .extract_if(.., |ws| { + ws.find_preferred_output( + monitors.iter().map(|monitor| &monitor.output), + ) + .unwrap_or(primary_idx) + == target_idx + }) + .collect(); + monitors[target_idx].append_workspaces(target_workspaces); + } MonitorSet::Normal { monitors, @@ -2538,34 +2558,22 @@ impl Layout { monitor.verify_invariants(); - if idx == primary_idx { - for ws in &monitor.workspaces { - if ws.original_output.matches(&monitor.output) { - // This is the primary monitor's own workspace. - continue; - } - - let own_monitor_exists = monitors - .iter() - .any(|m| ws.original_output.matches(&m.output)); - assert!( - !own_monitor_exists, - "primary monitor cannot have workspaces for which their own monitor exists" + for workspace in &monitor.workspaces { + let preferred = + workspace.find_preferred_output(monitors.iter().map(|monitor| &monitor.output)); + let Some(preferred_idx) = preferred else { + assert_eq!( + idx, primary_idx, + "workspace without a connected output candidate must be on the primary monitor" ); - } - } else { - assert!( - monitor - .workspaces - .iter() - .any(|workspace| workspace.original_output.matches(&monitor.output)), - "secondary monitor must not have any non-own workspaces" + continue; + }; + assert_eq!( + idx, preferred_idx, + "workspace must be on its preferred connected output" ); } - // FIXME: verify that primary doesn't have any workspaces for which their own monitor - // exists. - for workspace in &monitor.workspaces { assert!( seen_workspace_id.insert(workspace.id()), @@ -2937,16 +2945,19 @@ impl Layout { primary_idx, active_monitor_idx, } => { - let mon_idx = ws_config - .open_on_output - .as_deref() - .map(|name| { - monitors - .iter_mut() - .position(|monitor| output_matches_name(&monitor.output, name)) - .unwrap_or(*primary_idx) - }) - .unwrap_or(*active_monitor_idx); + let mon_idx = if ws_config.open_on_output.is_empty() { + *active_monitor_idx + } else { + ws_config + .open_on_output + .iter() + .find_map(|name| { + monitors + .iter() + .position(|monitor| output_matches_name(&monitor.output, name)) + }) + .unwrap_or(*primary_idx) + }; let mon = &mut monitors[mon_idx]; let ws = Workspace::new_with_config( @@ -3490,7 +3501,7 @@ impl Layout { // Do not do anything if the output is already correct if current_idx == target_idx { // Just update the original output since this is an explicit movement action. - current.workspaces[old_idx].original_output = OutputId::new(¤t.output); + current.workspaces[old_idx].original_outputs = vec![OutputId::new(¤t.output)]; return false; } @@ -3501,7 +3512,7 @@ impl Layout { current_idx == *active_monitor_idx && old_idx == current.active_workspace_idx; let mut ws = current.remove_workspace_by_idx(old_idx); - ws.original_output = OutputId::new(new_output); + ws.original_outputs = vec![OutputId::new(new_output)]; let target = &mut monitors[target_idx]; target.insert_workspace(ws, target.active_workspace_idx + 1, activate); diff --git a/src/layout/monitor.rs b/src/layout/monitor.rs index f119e619..761d226b 100644 --- a/src/layout/monitor.rs +++ b/src/layout/monitor.rs @@ -557,7 +557,7 @@ impl Monitor { // After adding a new window, workspace becomes this output's own. if workspace.name().is_none() { - workspace.original_output = OutputId::new(&self.output); + workspace.original_outputs = vec![OutputId::new(&self.output)]; } if workspace_idx == self.workspaces.len() - 1 { @@ -602,7 +602,7 @@ impl Monitor { // After adding a new window, workspace becomes this output's own. if workspace.name().is_none() { - workspace.original_output = OutputId::new(&self.output); + workspace.original_outputs = vec![OutputId::new(&self.output)]; } if workspace_idx == self.workspaces.len() - 1 { @@ -636,7 +636,7 @@ impl Monitor { // After adding a new window, workspace becomes this output's own. if workspace.name().is_none() { - workspace.original_output = OutputId::new(&self.output); + workspace.original_outputs = vec![OutputId::new(&self.output)]; } // Since we're adding window to an existing column, the workspace isn't empty, and diff --git a/src/layout/tests.rs b/src/layout/tests.rs index bb760445..a2da4ce5 100644 --- a/src/layout/tests.rs +++ b/src/layout/tests.rs @@ -426,8 +426,8 @@ enum Op { AddNamedWorkspace { #[proptest(strategy = "1..=5usize")] ws_name: usize, - #[proptest(strategy = "prop::option::of(1..=5usize)")] - output_name: Option, + #[proptest(strategy = "proptest::collection::vec(1..=5usize, 0..=3)")] + output_names: Vec, #[proptest(strategy = "prop::option::of(arbitrary_layout_part().prop_map(Box::new))")] layout_config: Option>, }, @@ -854,12 +854,15 @@ impl Op { } Op::AddNamedWorkspace { ws_name, - output_name, + output_names, layout_config, } => { layout.ensure_named_workspace(&WorkspaceConfig { name: WorkspaceName(format!("ws{ws_name}")), - open_on_output: output_name.map(|name| format!("output{name}")), + open_on_output: output_names + .into_iter() + .map(|name| format!("output{name}")) + .collect(), layout: layout_config.map(|x| niri_config::WorkspaceLayoutPart(*x)), }); } @@ -1644,6 +1647,122 @@ fn check_ops(ops: impl IntoIterator) -> Layout { layout } +fn workspace_output_name(layout: &Layout, ws_name: usize) -> &str { + layout + .workspaces() + .find(|(_, _, workspace)| workspace.name() == Some(&format!("ws{ws_name}"))) + .and_then(|(monitor, _, _)| monitor) + .unwrap() + .output_name() +} + +#[test] +fn workspace_output_fallback_promotes_preferred_in_reverse_connection_order() { + // output3 is not a candidate, so it stays the primary monitor throughout; every assertion + // below can only hold because of the candidate list. + let mut layout = Layout::default(); + check_ops_on_layout( + &mut layout, + [ + Op::AddOutput(3), + Op::AddNamedWorkspace { + ws_name: 1, + output_names: vec![1, 2], + layout_config: None, + }, + ], + ); + assert_eq!(workspace_output_name(&layout, 1), "output3"); + + // The second candidate connects first; the workspace must leave the primary monitor. + check_ops_on_layout(&mut layout, [Op::AddOutput(2)]); + assert_eq!(workspace_output_name(&layout, 1), "output2"); + + // The first candidate connects last; it still wins. + check_ops_on_layout(&mut layout, [Op::AddOutput(1)]); + assert_eq!(workspace_output_name(&layout, 1), "output1"); +} + +#[test] +fn workspace_output_preferred_removal_uses_fallback() { + let mut layout = Layout::default(); + check_ops_on_layout( + &mut layout, + [ + Op::AddOutput(3), + Op::AddNamedWorkspace { + ws_name: 1, + output_names: vec![1, 2], + layout_config: None, + }, + Op::AddOutput(1), + Op::AddOutput(2), + ], + ); + assert_eq!(workspace_output_name(&layout, 1), "output1"); + + // The preferred candidate disconnects: the next candidate wins over the primary monitor. + check_ops_on_layout(&mut layout, [Op::RemoveOutput(1)]); + assert_eq!(workspace_output_name(&layout, 1), "output2"); + + // No candidate left: fall back to the primary monitor. + check_ops_on_layout(&mut layout, [Op::RemoveOutput(2)]); + assert_eq!(workspace_output_name(&layout, 1), "output3"); +} + +#[test] +fn workspace_output_without_match_stays_on_default_output() { + let mut layout = Layout::default(); + check_ops_on_layout( + &mut layout, + [ + Op::AddOutput(3), + Op::AddNamedWorkspace { + ws_name: 1, + output_names: vec![1, 2], + layout_config: None, + }, + ], + ); + assert_eq!(workspace_output_name(&layout, 1), "output3"); + + // An output that is not in the candidate list must not take the workspace. + check_ops_on_layout(&mut layout, [Op::AddOutput(4)]); + assert_eq!(workspace_output_name(&layout, 1), "output3"); +} + +#[test] +fn explicit_workspace_move_overrides_output_candidates() { + let mut layout = Layout::default(); + check_ops_on_layout( + &mut layout, + [ + Op::AddOutput(3), + Op::AddNamedWorkspace { + ws_name: 1, + output_names: vec![1, 2], + layout_config: None, + }, + Op::AddOutput(2), + ], + ); + assert_eq!(workspace_output_name(&layout, 1), "output2"); + + // After an explicit move, the candidate list no longer applies, even when the preferred + // candidate connects afterwards. + check_ops_on_layout( + &mut layout, + [ + Op::MoveWorkspaceToMonitor { + ws_name: Some(1), + output_id: 3, + }, + Op::AddOutput(1), + ], + ); + assert_eq!(workspace_output_name(&layout, 1), "output3"); +} + #[track_caller] fn check_ops_with_options( options: Options, @@ -1673,7 +1792,7 @@ fn operations_dont_panic() { Op::FocusOutput(2), Op::AddNamedWorkspace { ws_name: 1, - output_name: Some(1), + output_names: vec![1], layout_config: None, }, Op::UnnameWorkspace { ws_name: 1 }, @@ -1824,7 +1943,7 @@ fn operations_from_starting_state_dont_panic() { Op::FocusOutput(2), Op::AddNamedWorkspace { ws_name: 1, - output_name: Some(1), + output_names: vec![1], layout_config: None, }, Op::UnnameWorkspace { ws_name: 1 }, @@ -2363,12 +2482,12 @@ fn removing_all_outputs_preserves_empty_named_workspaces() { Op::AddOutput(1), Op::AddNamedWorkspace { ws_name: 1, - output_name: None, + output_names: vec![], layout_config: None, }, Op::AddNamedWorkspace { ws_name: 2, - output_name: None, + output_names: vec![], layout_config: None, }, Op::RemoveOutput(1), @@ -2731,7 +2850,7 @@ fn named_workspace_to_output() { let ops = [ Op::AddNamedWorkspace { ws_name: 1, - output_name: None, + output_names: vec![], layout_config: None, }, Op::AddOutput(1), @@ -2747,7 +2866,7 @@ fn named_workspace_to_output_ewaf() { let ops = [ Op::AddNamedWorkspace { ws_name: 1, - output_name: Some(2), + output_names: vec![2], layout_config: None, }, Op::AddOutput(1), @@ -2981,7 +3100,7 @@ fn interactive_move_from_workspace_with_layout_config() { let ops = [ Op::AddNamedWorkspace { ws_name: 1, - output_name: Some(2), + output_names: vec![2], layout_config: Some(Box::new(niri_config::LayoutPart { border: Some(niri_config::BorderRule { on: true, diff --git a/src/layout/workspace.rs b/src/layout/workspace.rs index 5e786ddf..8851a92f 100644 --- a/src/layout/workspace.rs +++ b/src/layout/workspace.rs @@ -54,11 +54,11 @@ pub struct Workspace { /// Whether the floating layout is active instead of the scrolling layout. floating_is_active: FloatingActive, - /// The original output of this workspace. + /// The original outputs of this workspace, in preference order. /// /// Most of the time this will be the workspace's current output, however, after an output - /// disconnection, it may remain pointing to the disconnected output. - pub(super) original_output: OutputId, + /// disconnection, they may remain pointing to disconnected outputs. + pub(super) original_outputs: Vec, /// Current output of this workspace. output: Option, @@ -114,7 +114,7 @@ pub struct Workspace { id: WorkspaceId, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct OutputId(String); impl OutputId { @@ -217,11 +217,15 @@ impl Workspace { clock: Clock, base_options: Rc, ) -> Self { - let original_output = config + let mut original_outputs: Vec = config .as_ref() - .and_then(|c| c.open_on_output.clone()) - .map(OutputId) - .unwrap_or(OutputId::new(&output)); + .map(|c| c.open_on_output.iter().cloned().map(OutputId).collect()) + .unwrap_or_default(); + if original_outputs.is_empty() { + original_outputs.push(OutputId::new(&output)); + } else { + Self::normalize_original_outputs(&mut original_outputs, &output); + } let layout_config = config.as_mut().and_then(|c| c.layout.take().map(|x| x.0)); @@ -258,7 +262,7 @@ impl Workspace { scrolling, floating, floating_is_active: FloatingActive::No, - original_output, + original_outputs, scale, transform: output.current_transform(), view_size, @@ -280,12 +284,10 @@ impl Workspace { clock: Clock, base_options: Rc, ) -> Self { - let original_output = OutputId( - config - .as_ref() - .and_then(|c| c.open_on_output.clone()) - .unwrap_or_default(), - ); + let original_outputs = config + .as_ref() + .map(|c| c.open_on_output.iter().cloned().map(OutputId).collect()) + .unwrap_or_default(); let layout_config = config.as_mut().and_then(|c| c.layout.take().map(|x| x.0)); @@ -325,7 +327,7 @@ impl Workspace { output: None, scale, transform: Transform::Normal, - original_output, + original_outputs, view_size, working_area, shadow: Shadow::new(shadow_config), @@ -474,6 +476,15 @@ impl Workspace { self.output.as_ref() } + pub(super) fn find_preferred_output<'a>( + &self, + outputs: impl Iterator + Clone, + ) -> Option { + self.original_outputs + .iter() + .find_map(|id| outputs.clone().position(|output| id.matches(output))) + } + pub fn active_window(&self) -> Option<&W> { if self.floating_is_active.get() { self.floating.active_window() @@ -508,9 +519,11 @@ impl Workspace { self.output = output; if let Some(output) = &self.output { - // Normalize original output: possibly replace connector with make/model/serial. - if self.original_output.matches(output) { - self.original_output = OutputId::new(output); + if self.original_outputs.is_empty() { + self.original_outputs.push(OutputId::new(output)); + } else { + // Normalize original outputs: possibly replace connectors with make/model/serial. + Self::normalize_original_outputs(&mut self.original_outputs, output); } self.update_output_size(); @@ -521,6 +534,21 @@ impl Workspace { } } + fn normalize_original_outputs(outputs: &mut Vec, output: &Output) { + for id in &mut *outputs { + if id.matches(output) { + *id = OutputId::new(output); + } + } + + // Connector and make/model/serial candidates may resolve to the same output. + for idx in (0..outputs.len()).rev() { + if outputs[..idx].contains(&outputs[idx]) { + outputs.remove(idx); + } + } + } + fn enter_output_for_window(&self, window: &W) { if let Some(output) = &self.output { window.set_preferred_scale_transform(self.scale, self.transform);