diff --git a/docs/wiki/Configuration:-Named-Workspaces.md b/docs/wiki/Configuration:-Named-Workspaces.md index 712d25e1..41b0933d 100644 --- a/docs/wiki/Configuration:-Named-Workspaces.md +++ b/docs/wiki/Configuration:-Named-Workspaces.md @@ -108,3 +108,15 @@ workspace "uncentered" { } } ``` + +### Hidden workspaces + +Since: next release Add a `hidden` flag to a named workspace to make it behave like an unnamed workspace. +A hidden named workspace exists only while it has windows, or while it's the active workspace of its monitor. +It keeps its name while it exists, and it's created on demand when something refers to it by name, such as `focus-workspace` or an `open-on-workspace` window rule. + +```kdl +workspace "discord" { + hidden +} +``` diff --git a/niri-config/src/lib.rs b/niri-config/src/lib.rs index 378da877..4db28554 100644 --- a/niri-config/src/lib.rs +++ b/niri-config/src/lib.rs @@ -2835,6 +2835,7 @@ mod tests { "eDP-1", "DP-1", ], + hidden: false, layout: None, }, Workspace { @@ -2842,6 +2843,7 @@ mod tests { "workspace-2", ), open_on_output: [], + hidden: false, layout: None, }, Workspace { @@ -2849,6 +2851,7 @@ mod tests { "workspace-3", ), open_on_output: [], + hidden: false, layout: None, }, ], diff --git a/niri-config/src/workspace.rs b/niri-config/src/workspace.rs index 971a5464..de835192 100644 --- a/niri-config/src/workspace.rs +++ b/niri-config/src/workspace.rs @@ -8,6 +8,9 @@ pub struct Workspace { pub name: WorkspaceName, #[knuffel(children(name = "open-on-output"), unwrap(argument))] pub open_on_output: Vec, + /// Exists only while it has windows; created on demand when referred to by name. + #[knuffel(child)] + pub hidden: bool, #[knuffel(child)] pub layout: Option, } diff --git a/src/handlers/compositor.rs b/src/handlers/compositor.rs index aba1509e..69ea65c6 100644 --- a/src/handlers/compositor.rs +++ b/src/handlers/compositor.rs @@ -114,7 +114,11 @@ impl CompositorHandler for State { let output = output.filter(|o| self.niri.layout.monitor_for_output(o).is_some()); - // Check that the workspace still exists. + // Check that the workspace still exists. A hidden workspace may have + // been cleaned up since the initial configure, so create it if needed. + if let Some(n) = workspace_name.as_deref() { + self.niri.ensure_hidden_workspace(n); + } let workspace_id = workspace_name .as_deref() .and_then(|n| self.niri.layout.find_workspace_by_name(n)) diff --git a/src/handlers/xdg_shell.rs b/src/handlers/xdg_shell.rs index 6477d65a..e7450d2c 100644 --- a/src/handlers/xdg_shell.rs +++ b/src/handlers/xdg_shell.rs @@ -1102,6 +1102,19 @@ impl State { return; }; + // A hidden workspace only exists while it has windows. This is the same as + // Niri::ensure_hidden_workspace, but only borrows disjoint fields, because `unmapped` + // (which borrows self.niri.unmapped_windows) is still in use below. + if let Some(name) = rules.open_on_workspace.as_deref() { + let ws_config = config + .workspaces + .iter() + .find(|ws| ws.hidden && ws.name.0.eq_ignore_ascii_case(name)); + if let Some(ws_config) = ws_config { + self.niri.layout.ensure_hidden_workspace(ws_config); + } + } + // Pick the target monitor. First, check if we had a workspace set in the window rules. let mon = rules .open_on_workspace diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 8575a1af..3a932a11 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -738,6 +738,7 @@ impl Layout { let workspaces = config .workspaces .iter() + .filter(|ws| !ws.hidden) .map(|ws| { Workspace::new_with_config_no_outputs(Some(ws.clone()), clock.clone(), opts.clone()) }) @@ -2947,6 +2948,15 @@ impl Layout { } pub fn ensure_named_workspace(&mut self, ws_config: &WorkspaceConfig) { + self.ensure_named_workspace_at(ws_config, false); + } + + /// Creates a hidden named workspace on demand, at the bottom of its monitor. + pub fn ensure_hidden_workspace(&mut self, ws_config: &WorkspaceConfig) { + self.ensure_named_workspace_at(ws_config, true); + } + + fn ensure_named_workspace_at(&mut self, ws_config: &WorkspaceConfig, at_bottom: bool) { if self.find_workspace_by_name(&ws_config.name.0).is_some() { return; } @@ -2981,12 +2991,35 @@ impl Layout { clock, options, ); - mon.insert_workspace(ws, 0, false); + + if at_bottom { + // Insert directly rather than via insert_workspace, which would clean up + // the new (empty, hidden) workspace right away. + mon.workspace_switch = None; + let mut ws = ws; + ws.update_config(mon.options.clone()); + let mut idx = mon.workspaces.len() - 1; + if idx == 0 && mon.options.layout.empty_workspace_above_first { + // Keep the empty workspace above the first one. + mon.add_workspace_top(); + idx += 1; + } + mon.workspaces.insert(idx, ws); + if idx <= mon.active_workspace_idx { + mon.active_workspace_idx += 1; + } + } else { + mon.insert_workspace(ws, 0, false); + } } MonitorSet::NoOutputs { workspaces } => { let ws = Workspace::new_with_config_no_outputs(Some(ws_config.clone()), clock, options); - workspaces.insert(0, ws); + if at_bottom { + workspaces.push(ws); + } else { + workspaces.insert(0, ws); + } } } } @@ -2997,6 +3030,16 @@ impl Layout { let Some(name) = ws.name() else { continue }; if let Some(config) = config.workspaces.iter().find(|w| &w.name.0 == name) { ws.update_layout_config(config.layout.clone().map(|x| x.0)); + ws.set_hidden(config.hidden); + } + } + + // Workspaces that just became hidden and are empty should go away. + if let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set { + for mon in monitors { + if mon.workspace_switch.is_none() { + mon.clean_up_workspaces(); + } } } diff --git a/src/layout/tests.rs b/src/layout/tests.rs index a2da4ce5..befbf735 100644 --- a/src/layout/tests.rs +++ b/src/layout/tests.rs @@ -863,6 +863,7 @@ impl Op { .into_iter() .map(|name| format!("output{name}")) .collect(), + hidden: false, layout: layout_config.map(|x| niri_config::WorkspaceLayoutPart(*x)), }); } diff --git a/src/layout/workspace.rs b/src/layout/workspace.rs index effa38b2..5568acff 100644 --- a/src/layout/workspace.rs +++ b/src/layout/workspace.rs @@ -107,6 +107,9 @@ pub struct Workspace { /// Optional name of this workspace. pub(super) name: Option, + /// Named workspace that is removed when empty, like an unnamed one. + pub(super) hidden: bool, + /// Layout config overrides for this workspace. layout_config: Option, @@ -217,6 +220,7 @@ impl Workspace { clock: Clock, base_options: Rc, ) -> Self { + let hidden = config.as_ref().is_some_and(|c| c.hidden); let mut original_outputs: Vec = config .as_ref() .map(|c| c.open_on_output.iter().cloned().map(OutputId).collect()) @@ -274,6 +278,7 @@ impl Workspace { base_options, options, name: config.map(|c| c.name.0), + hidden, layout_config, id: WorkspaceId::next(), } @@ -284,6 +289,7 @@ impl Workspace { clock: Clock, base_options: Rc, ) -> Self { + let hidden = config.as_ref().is_some_and(|c| c.hidden); let original_outputs = config .as_ref() .map(|c| c.open_on_output.iter().cloned().map(OutputId).collect()) @@ -336,6 +342,7 @@ impl Workspace { base_options, options, name: config.map(|c| c.name.0), + hidden, layout_config, id: WorkspaceId::next(), } @@ -355,10 +362,15 @@ impl Workspace { pub fn unname(&mut self) { self.name = None; + self.hidden = false; + } + + pub fn set_hidden(&mut self, hidden: bool) { + self.hidden = hidden; } pub fn has_windows_or_name(&self) -> bool { - self.has_windows() || self.name.is_some() + self.has_windows() || (self.name.is_some() && !self.hidden) } pub fn scale(&self) -> smithay::output::Scale { diff --git a/src/niri.rs b/src/niri.rs index e5d7ba9a..d299d986 100644 --- a/src/niri.rs +++ b/src/niri.rs @@ -1722,7 +1722,7 @@ impl State { } // Create new named workspaces. - for ws_config in &config.workspaces { + for ws_config in config.workspaces.iter().filter(|ws| !ws.hidden) { self.niri.layout.ensure_named_workspace(ws_config); } @@ -3888,10 +3888,29 @@ impl Niri { self.output_next_of(active) } + /// Creates the hidden named workspace called `name` if it's configured but doesn't exist. + pub fn ensure_hidden_workspace(&mut self, name: &str) { + let ws_config = { + let config = self.config.borrow(); + config + .workspaces + .iter() + .find(|ws| ws.hidden && ws.name.0.eq_ignore_ascii_case(name)) + .cloned() + }; + if let Some(ws_config) = ws_config { + self.layout.ensure_hidden_workspace(&ws_config); + } + } + pub fn find_output_and_workspace_index( - &self, + &mut self, workspace_reference: WorkspaceReference, ) -> Option<(Option, usize)> { + if let WorkspaceReference::Name(name) = &workspace_reference { + self.ensure_hidden_workspace(name); + } + let (target_workspace_index, target_workspace) = match workspace_reference { WorkspaceReference::Index(index) => { return Some((None, index.saturating_sub(1) as usize));