Add hidden flag for named workspaces
Browse files at this commit53500dc01bca879019a64b00d874fc9c207b25c0Parent: 9d8f4f89 changed files +116 -6
- docs/wiki/Configuration:-Named-Workspaces.md +12 -0
- niri-config/src/lib.rs +3 -0
- niri-config/src/workspace.rs +3 -0
- src/handlers/compositor.rs +5 -1
- src/handlers/xdg_shell.rs +13 -0
- src/layout/mod.rs +45 -2
- src/layout/tests.rs +1 -0
- src/layout/workspace.rs +13 -1
- src/niri.rs +21 -2
docs/wiki/Configuration:-Named-Workspaces.mdmodified+12 -0
| @@ -108,3 +108,15 @@ workspace "uncentered" { | |||
| 108 | } | 108 | } |
| 109 | } | 109 | } |
| 110 | ``` | 110 | ``` |
| 111 | + | ||
| 112 | +### Hidden workspaces | ||
| 113 | + | ||
| 114 | +<sup>Since: next release</sup> Add a `hidden` flag to a named workspace to make it behave like an unnamed workspace. | ||
| 115 | +A hidden named workspace exists only while it has windows, or while it's the active workspace of its monitor. | ||
| 116 | +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. | ||
| 117 | + | ||
| 118 | +```kdl | ||
| 119 | +workspace "discord" { | ||
| 120 | + hidden | ||
| 121 | +} | ||
| 122 | +``` | ||
niri-config/src/lib.rsmodified+3 -0
| @@ -2835,6 +2835,7 @@ mod tests { | |||
| 2835 | "eDP-1", | 2835 | "eDP-1", |
| 2836 | "DP-1", | 2836 | "DP-1", |
| 2837 | ], | 2837 | ], |
| 2838 | + hidden: false, | ||
| 2838 | layout: None, | 2839 | layout: None, |
| 2839 | }, | 2840 | }, |
| 2840 | Workspace { | 2841 | Workspace { |
| @@ -2842,6 +2843,7 @@ mod tests { | |||
| 2842 | "workspace-2", | 2843 | "workspace-2", |
| 2843 | ), | 2844 | ), |
| 2844 | open_on_output: [], | 2845 | open_on_output: [], |
| 2846 | + hidden: false, | ||
| 2845 | layout: None, | 2847 | layout: None, |
| 2846 | }, | 2848 | }, |
| 2847 | Workspace { | 2849 | Workspace { |
| @@ -2849,6 +2851,7 @@ mod tests { | |||
| 2849 | "workspace-3", | 2851 | "workspace-3", |
| 2850 | ), | 2852 | ), |
| 2851 | open_on_output: [], | 2853 | open_on_output: [], |
| 2854 | + hidden: false, | ||
| 2852 | layout: None, | 2855 | layout: None, |
| 2853 | }, | 2856 | }, |
| 2854 | ], | 2857 | ], |
niri-config/src/workspace.rsmodified+3 -0
| @@ -8,6 +8,9 @@ pub struct Workspace { | |||
| 8 | pub name: WorkspaceName, | 8 | pub name: WorkspaceName, |
| 9 | #[knuffel(children(name = "open-on-output"), unwrap(argument))] | 9 | #[knuffel(children(name = "open-on-output"), unwrap(argument))] |
| 10 | pub open_on_output: Vec<String>, | 10 | pub open_on_output: Vec<String>, |
| 11 | + /// Exists only while it has windows; created on demand when referred to by name. | ||
| 12 | + #[knuffel(child)] | ||
| 13 | + pub hidden: bool, | ||
| 11 | #[knuffel(child)] | 14 | #[knuffel(child)] |
| 12 | pub layout: Option<WorkspaceLayoutPart>, | 15 | pub layout: Option<WorkspaceLayoutPart>, |
| 13 | } | 16 | } |
src/handlers/compositor.rsmodified+5 -1
| @@ -114,7 +114,11 @@ impl CompositorHandler for State { | |||
| 114 | let output = | 114 | let output = |
| 115 | output.filter(|o| self.niri.layout.monitor_for_output(o).is_some()); | 115 | output.filter(|o| self.niri.layout.monitor_for_output(o).is_some()); |
| 116 | 116 | ||
| 117 | - // Check that the workspace still exists. | 117 | + // Check that the workspace still exists. A hidden workspace may have |
| 118 | + // been cleaned up since the initial configure, so create it if needed. | ||
| 119 | + if let Some(n) = workspace_name.as_deref() { | ||
| 120 | + self.niri.ensure_hidden_workspace(n); | ||
| 121 | + } | ||
| 118 | let workspace_id = workspace_name | 122 | let workspace_id = workspace_name |
| 119 | .as_deref() | 123 | .as_deref() |
| 120 | .and_then(|n| self.niri.layout.find_workspace_by_name(n)) | 124 | .and_then(|n| self.niri.layout.find_workspace_by_name(n)) |
src/handlers/xdg_shell.rsmodified+13 -0
| @@ -1102,6 +1102,19 @@ impl State { | |||
| 1102 | return; | 1102 | return; |
| 1103 | }; | 1103 | }; |
| 1104 | 1104 | ||
| 1105 | + // A hidden workspace only exists while it has windows. This is the same as | ||
| 1106 | + // Niri::ensure_hidden_workspace, but only borrows disjoint fields, because `unmapped` | ||
| 1107 | + // (which borrows self.niri.unmapped_windows) is still in use below. | ||
| 1108 | + if let Some(name) = rules.open_on_workspace.as_deref() { | ||
| 1109 | + let ws_config = config | ||
| 1110 | + .workspaces | ||
| 1111 | + .iter() | ||
| 1112 | + .find(|ws| ws.hidden && ws.name.0.eq_ignore_ascii_case(name)); | ||
| 1113 | + if let Some(ws_config) = ws_config { | ||
| 1114 | + self.niri.layout.ensure_hidden_workspace(ws_config); | ||
| 1115 | + } | ||
| 1116 | + } | ||
| 1117 | + | ||
| 1105 | // Pick the target monitor. First, check if we had a workspace set in the window rules. | 1118 | // Pick the target monitor. First, check if we had a workspace set in the window rules. |
| 1106 | let mon = rules | 1119 | let mon = rules |
| 1107 | .open_on_workspace | 1120 | .open_on_workspace |
src/layout/mod.rsmodified+45 -2
| @@ -738,6 +738,7 @@ impl<W: LayoutElement> Layout<W> { | |||
| 738 | let workspaces = config | 738 | let workspaces = config |
| 739 | .workspaces | 739 | .workspaces |
| 740 | .iter() | 740 | .iter() |
| 741 | + .filter(|ws| !ws.hidden) | ||
| 741 | .map(|ws| { | 742 | .map(|ws| { |
| 742 | Workspace::new_with_config_no_outputs(Some(ws.clone()), clock.clone(), opts.clone()) | 743 | Workspace::new_with_config_no_outputs(Some(ws.clone()), clock.clone(), opts.clone()) |
| 743 | }) | 744 | }) |
| @@ -2947,6 +2948,15 @@ impl<W: LayoutElement> Layout<W> { | |||
| 2947 | } | 2948 | } |
| 2948 | 2949 | ||
| 2949 | pub fn ensure_named_workspace(&mut self, ws_config: &WorkspaceConfig) { | 2950 | pub fn ensure_named_workspace(&mut self, ws_config: &WorkspaceConfig) { |
| 2951 | + self.ensure_named_workspace_at(ws_config, false); | ||
| 2952 | + } | ||
| 2953 | + | ||
| 2954 | + /// Creates a hidden named workspace on demand, at the bottom of its monitor. | ||
| 2955 | + pub fn ensure_hidden_workspace(&mut self, ws_config: &WorkspaceConfig) { | ||
| 2956 | + self.ensure_named_workspace_at(ws_config, true); | ||
| 2957 | + } | ||
| 2958 | + | ||
| 2959 | + fn ensure_named_workspace_at(&mut self, ws_config: &WorkspaceConfig, at_bottom: bool) { | ||
| 2950 | if self.find_workspace_by_name(&ws_config.name.0).is_some() { | 2960 | if self.find_workspace_by_name(&ws_config.name.0).is_some() { |
| 2951 | return; | 2961 | return; |
| 2952 | } | 2962 | } |
| @@ -2981,12 +2991,35 @@ impl<W: LayoutElement> Layout<W> { | |||
| 2981 | clock, | 2991 | clock, |
| 2982 | options, | 2992 | options, |
| 2983 | ); | 2993 | ); |
| 2984 | - mon.insert_workspace(ws, 0, false); | 2994 | + |
| 2995 | + if at_bottom { | ||
| 2996 | + // Insert directly rather than via insert_workspace, which would clean up | ||
| 2997 | + // the new (empty, hidden) workspace right away. | ||
| 2998 | + mon.workspace_switch = None; | ||
| 2999 | + let mut ws = ws; | ||
| 3000 | + ws.update_config(mon.options.clone()); | ||
| 3001 | + let mut idx = mon.workspaces.len() - 1; | ||
| 3002 | + if idx == 0 && mon.options.layout.empty_workspace_above_first { | ||
| 3003 | + // Keep the empty workspace above the first one. | ||
| 3004 | + mon.add_workspace_top(); | ||
| 3005 | + idx += 1; | ||
| 3006 | + } | ||
| 3007 | + mon.workspaces.insert(idx, ws); | ||
| 3008 | + if idx <= mon.active_workspace_idx { | ||
| 3009 | + mon.active_workspace_idx += 1; | ||
| 3010 | + } | ||
| 3011 | + } else { | ||
| 3012 | + mon.insert_workspace(ws, 0, false); | ||
| 3013 | + } | ||
| 2985 | } | 3014 | } |
| 2986 | MonitorSet::NoOutputs { workspaces } => { | 3015 | MonitorSet::NoOutputs { workspaces } => { |
| 2987 | let ws = | 3016 | let ws = |
| 2988 | Workspace::new_with_config_no_outputs(Some(ws_config.clone()), clock, options); | 3017 | Workspace::new_with_config_no_outputs(Some(ws_config.clone()), clock, options); |
| 2989 | - workspaces.insert(0, ws); | 3018 | + if at_bottom { |
| 3019 | + workspaces.push(ws); | ||
| 3020 | + } else { | ||
| 3021 | + workspaces.insert(0, ws); | ||
| 3022 | + } | ||
| 2990 | } | 3023 | } |
| 2991 | } | 3024 | } |
| 2992 | } | 3025 | } |
| @@ -2997,6 +3030,16 @@ impl<W: LayoutElement> Layout<W> { | |||
| 2997 | let Some(name) = ws.name() else { continue }; | 3030 | let Some(name) = ws.name() else { continue }; |
| 2998 | if let Some(config) = config.workspaces.iter().find(|w| &w.name.0 == name) { | 3031 | if let Some(config) = config.workspaces.iter().find(|w| &w.name.0 == name) { |
| 2999 | ws.update_layout_config(config.layout.clone().map(|x| x.0)); | 3032 | ws.update_layout_config(config.layout.clone().map(|x| x.0)); |
| 3033 | + ws.set_hidden(config.hidden); | ||
| 3034 | + } | ||
| 3035 | + } | ||
| 3036 | + | ||
| 3037 | + // Workspaces that just became hidden and are empty should go away. | ||
| 3038 | + if let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set { | ||
| 3039 | + for mon in monitors { | ||
| 3040 | + if mon.workspace_switch.is_none() { | ||
| 3041 | + mon.clean_up_workspaces(); | ||
| 3042 | + } | ||
| 3000 | } | 3043 | } |
| 3001 | } | 3044 | } |
| 3002 | 3045 | ||
src/layout/tests.rsmodified+1 -0
| @@ -863,6 +863,7 @@ impl Op { | |||
| 863 | .into_iter() | 863 | .into_iter() |
| 864 | .map(|name| format!("output{name}")) | 864 | .map(|name| format!("output{name}")) |
| 865 | .collect(), | 865 | .collect(), |
| 866 | + hidden: false, | ||
| 866 | layout: layout_config.map(|x| niri_config::WorkspaceLayoutPart(*x)), | 867 | layout: layout_config.map(|x| niri_config::WorkspaceLayoutPart(*x)), |
| 867 | }); | 868 | }); |
| 868 | } | 869 | } |
src/layout/workspace.rsmodified+13 -1
| @@ -107,6 +107,9 @@ pub struct Workspace<W: LayoutElement> { | |||
| 107 | /// Optional name of this workspace. | 107 | /// Optional name of this workspace. |
| 108 | pub(super) name: Option<String>, | 108 | pub(super) name: Option<String>, |
| 109 | 109 | ||
| 110 | + /// Named workspace that is removed when empty, like an unnamed one. | ||
| 111 | + pub(super) hidden: bool, | ||
| 112 | + | ||
| 110 | /// Layout config overrides for this workspace. | 113 | /// Layout config overrides for this workspace. |
| 111 | layout_config: Option<niri_config::LayoutPart>, | 114 | layout_config: Option<niri_config::LayoutPart>, |
| 112 | 115 | ||
| @@ -217,6 +220,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 217 | clock: Clock, | 220 | clock: Clock, |
| 218 | base_options: Rc<Options>, | 221 | base_options: Rc<Options>, |
| 219 | ) -> Self { | 222 | ) -> Self { |
| 223 | + let hidden = config.as_ref().is_some_and(|c| c.hidden); | ||
| 220 | let mut original_outputs: Vec<OutputId> = config | 224 | let mut original_outputs: Vec<OutputId> = config |
| 221 | .as_ref() | 225 | .as_ref() |
| 222 | .map(|c| c.open_on_output.iter().cloned().map(OutputId).collect()) | 226 | .map(|c| c.open_on_output.iter().cloned().map(OutputId).collect()) |
| @@ -274,6 +278,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 274 | base_options, | 278 | base_options, |
| 275 | options, | 279 | options, |
| 276 | name: config.map(|c| c.name.0), | 280 | name: config.map(|c| c.name.0), |
| 281 | + hidden, | ||
| 277 | layout_config, | 282 | layout_config, |
| 278 | id: WorkspaceId::next(), | 283 | id: WorkspaceId::next(), |
| 279 | } | 284 | } |
| @@ -284,6 +289,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 284 | clock: Clock, | 289 | clock: Clock, |
| 285 | base_options: Rc<Options>, | 290 | base_options: Rc<Options>, |
| 286 | ) -> Self { | 291 | ) -> Self { |
| 292 | + let hidden = config.as_ref().is_some_and(|c| c.hidden); | ||
| 287 | let original_outputs = config | 293 | let original_outputs = config |
| 288 | .as_ref() | 294 | .as_ref() |
| 289 | .map(|c| c.open_on_output.iter().cloned().map(OutputId).collect()) | 295 | .map(|c| c.open_on_output.iter().cloned().map(OutputId).collect()) |
| @@ -336,6 +342,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 336 | base_options, | 342 | base_options, |
| 337 | options, | 343 | options, |
| 338 | name: config.map(|c| c.name.0), | 344 | name: config.map(|c| c.name.0), |
| 345 | + hidden, | ||
| 339 | layout_config, | 346 | layout_config, |
| 340 | id: WorkspaceId::next(), | 347 | id: WorkspaceId::next(), |
| 341 | } | 348 | } |
| @@ -355,10 +362,15 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 355 | 362 | ||
| 356 | pub fn unname(&mut self) { | 363 | pub fn unname(&mut self) { |
| 357 | self.name = None; | 364 | self.name = None; |
| 365 | + self.hidden = false; | ||
| 366 | + } | ||
| 367 | + | ||
| 368 | + pub fn set_hidden(&mut self, hidden: bool) { | ||
| 369 | + self.hidden = hidden; | ||
| 358 | } | 370 | } |
| 359 | 371 | ||
| 360 | pub fn has_windows_or_name(&self) -> bool { | 372 | pub fn has_windows_or_name(&self) -> bool { |
| 361 | - self.has_windows() || self.name.is_some() | 373 | + self.has_windows() || (self.name.is_some() && !self.hidden) |
| 362 | } | 374 | } |
| 363 | 375 | ||
| 364 | pub fn scale(&self) -> smithay::output::Scale { | 376 | pub fn scale(&self) -> smithay::output::Scale { |
src/niri.rsmodified+21 -2
| @@ -1722,7 +1722,7 @@ impl State { | |||
| 1722 | } | 1722 | } |
| 1723 | 1723 | ||
| 1724 | // Create new named workspaces. | 1724 | // Create new named workspaces. |
| 1725 | - for ws_config in &config.workspaces { | 1725 | + for ws_config in config.workspaces.iter().filter(|ws| !ws.hidden) { |
| 1726 | self.niri.layout.ensure_named_workspace(ws_config); | 1726 | self.niri.layout.ensure_named_workspace(ws_config); |
| 1727 | } | 1727 | } |
| 1728 | 1728 | ||
| @@ -3888,10 +3888,29 @@ impl Niri { | |||
| 3888 | self.output_next_of(active) | 3888 | self.output_next_of(active) |
| 3889 | } | 3889 | } |
| 3890 | 3890 | ||
| 3891 | + /// Creates the hidden named workspace called `name` if it's configured but doesn't exist. | ||
| 3892 | + pub fn ensure_hidden_workspace(&mut self, name: &str) { | ||
| 3893 | + let ws_config = { | ||
| 3894 | + let config = self.config.borrow(); | ||
| 3895 | + config | ||
| 3896 | + .workspaces | ||
| 3897 | + .iter() | ||
| 3898 | + .find(|ws| ws.hidden && ws.name.0.eq_ignore_ascii_case(name)) | ||
| 3899 | + .cloned() | ||
| 3900 | + }; | ||
| 3901 | + if let Some(ws_config) = ws_config { | ||
| 3902 | + self.layout.ensure_hidden_workspace(&ws_config); | ||
| 3903 | + } | ||
| 3904 | + } | ||
| 3905 | + | ||
| 3891 | pub fn find_output_and_workspace_index( | 3906 | pub fn find_output_and_workspace_index( |
| 3892 | - &self, | 3907 | + &mut self, |
| 3893 | workspace_reference: WorkspaceReference, | 3908 | workspace_reference: WorkspaceReference, |
| 3894 | ) -> Option<(Option<Output>, usize)> { | 3909 | ) -> Option<(Option<Output>, usize)> { |
| 3910 | + if let WorkspaceReference::Name(name) = &workspace_reference { | ||
| 3911 | + self.ensure_hidden_workspace(name); | ||
| 3912 | + } | ||
| 3913 | + | ||
| 3895 | let (target_workspace_index, target_workspace) = match workspace_reference { | 3914 | let (target_workspace_index, target_workspace) = match workspace_reference { |
| 3896 | WorkspaceReference::Index(index) => { | 3915 | WorkspaceReference::Index(index) => { |
| 3897 | return Some((None, index.saturating_sub(1) as usize)); | 3916 | return Some((None, index.saturating_sub(1) as usize)); |