forge
Explore
Log in

Don't you dare go Hollow

Implement empty-workspace-above-first (#745)

Browse files at this commit

11f351dbeb937a7de1de2f15b08c81f3682e423bUnverifiedFluxTape and Ivan MolodetskikhParent: 815fa37
* Implement empty-workspace-above-first option

* add two failing tests

* fix interactive_move_onto_empty_output_ewaf and
interactive_move_onto_first_empty_workspace tests

* Add two failing ewaf option toggle tests

* Fix adding/removing first empty workspace on option toggle

* Don't remove first empty workspace if focused

* Stop workspace switch when enabling ewaf

* layout/monitor: Offset workspace switch on adding workspace above

* Fix some initial active workspace ids with ewaf

* wiki: Document empty-workspace-above-first

---------
niri-config/src/lib.rsmodified+4 -0
@@ -444,6 +444,8 @@ pub struct Layout {
444 pub center_focused_column: CenterFocusedColumn,444 pub center_focused_column: CenterFocusedColumn,
445 #[knuffel(child)]445 #[knuffel(child)]
446 pub always_center_single_column: bool,446 pub always_center_single_column: bool,
447+ #[knuffel(child)]
448+ pub empty_workspace_above_first: bool,
447 #[knuffel(child, unwrap(argument), default = Self::default().gaps)]449 #[knuffel(child, unwrap(argument), default = Self::default().gaps)]
448 pub gaps: FloatOrInt<0, 65535>,450 pub gaps: FloatOrInt<0, 65535>,
449 #[knuffel(child, default)]451 #[knuffel(child, default)]
@@ -460,6 +462,7 @@ impl Default for Layout {
460 default_column_width: Default::default(),462 default_column_width: Default::default(),
461 center_focused_column: Default::default(),463 center_focused_column: Default::default(),
462 always_center_single_column: false,464 always_center_single_column: false,
465+ empty_workspace_above_first: false,
463 gaps: FloatOrInt(16.),466 gaps: FloatOrInt(16.),
464 struts: Default::default(),467 struts: Default::default(),
465 preset_window_heights: Default::default(),468 preset_window_heights: Default::default(),
@@ -3310,6 +3313,7 @@ mod tests {
3310 },3313 },
3311 center_focused_column: CenterFocusedColumn::OnOverflow,3314 center_focused_column: CenterFocusedColumn::OnOverflow,
3312 always_center_single_column: false,3315 always_center_single_column: false,
3316+ empty_workspace_above_first: false,
3313 },3317 },
3314 spawn_at_startup: vec![SpawnAtStartup {3318 spawn_at_startup: vec![SpawnAtStartup {
3315 command: vec!["alacritty".to_owned(), "-e".to_owned(), "fish".to_owned()],3319 command: vec!["alacritty".to_owned(), "-e".to_owned(), "fish".to_owned()],
src/layout/mod.rsmodified+386 -14
@@ -254,6 +254,7 @@ pub struct Options {
254 pub insert_hint: niri_config::InsertHint,254 pub insert_hint: niri_config::InsertHint,
255 pub center_focused_column: CenterFocusedColumn,255 pub center_focused_column: CenterFocusedColumn,
256 pub always_center_single_column: bool,256 pub always_center_single_column: bool,
257+ pub empty_workspace_above_first: bool,
257 /// Column widths that `toggle_width()` switches between.258 /// Column widths that `toggle_width()` switches between.
258 pub preset_column_widths: Vec<ColumnWidth>,259 pub preset_column_widths: Vec<ColumnWidth>,
259 /// Initial width for new columns.260 /// Initial width for new columns.
@@ -276,6 +277,7 @@ impl Default for Options {
276 insert_hint: Default::default(),277 insert_hint: Default::default(),
277 center_focused_column: Default::default(),278 center_focused_column: Default::default(),
278 always_center_single_column: false,279 always_center_single_column: false,
280+ empty_workspace_above_first: false,
279 preset_column_widths: vec![281 preset_column_widths: vec![
280 ColumnWidth::Proportion(1. / 3.),282 ColumnWidth::Proportion(1. / 3.),
281 ColumnWidth::Proportion(0.5),283 ColumnWidth::Proportion(0.5),
@@ -417,6 +419,7 @@ impl Options {
417 insert_hint: layout.insert_hint,419 insert_hint: layout.insert_hint,
418 center_focused_column: layout.center_focused_column,420 center_focused_column: layout.center_focused_column,
419 always_center_single_column: layout.always_center_single_column,421 always_center_single_column: layout.always_center_single_column,
422+ empty_workspace_above_first: layout.empty_workspace_above_first,
420 preset_column_widths,423 preset_column_widths,
421 default_column_width,424 default_column_width,
422 animations: config.animations.clone(),425 animations: config.animations.clone(),
@@ -506,7 +509,7 @@ impl<W: LayoutElement> Layout<W> {
506 // The user could've closed a window while remaining on this workspace, on509 // The user could've closed a window while remaining on this workspace, on
507 // another monitor. However, we will add an empty workspace in the end510 // another monitor. However, we will add an empty workspace in the end
508 // instead.511 // instead.
509- if ws.has_windows() || ws.name.is_some() {512+ if ws.has_windows_or_name() {
510 if Some(ws.id()) == ws_id_to_activate {513 if Some(ws.id()) == ws_id_to_activate {
511 active_workspace_idx = Some(workspaces.len());514 active_workspace_idx = Some(workspaces.len());
512 }515 }
@@ -514,7 +517,20 @@ impl<W: LayoutElement> Layout<W> {
514 workspaces.push(ws);517 workspaces.push(ws);
515 }518 }
516 519
517- if i <= primary.active_workspace_idx {520+ if i <= primary.active_workspace_idx
521+ // Generally when moving the currently active workspace, we want to
522+ // fall back to the workspace above, so as not to end up on the last
523+ // empty workspace. However, with empty workspace above first, when
524+ // moving the workspace at index 1 (first non-empty), we want to stay
525+ // at index 1, so as once again not to end up on an empty workspace.
526+ //
527+ // This comes into play at compositor startup when having named
528+ // workspaces set up across multiple monitors. Without this check, the
529+ // first monitor to connect can end up with the first empty workspace
530+ // focused instead of the first named workspace.
531+ && !(self.options.empty_workspace_above_first
532+ && primary.active_workspace_idx == 1)
533+ {
518 primary.active_workspace_idx =534 primary.active_workspace_idx =
519 primary.active_workspace_idx.saturating_sub(1);535 primary.active_workspace_idx.saturating_sub(1);
520 }536 }
@@ -522,7 +538,14 @@ impl<W: LayoutElement> Layout<W> {
522 }538 }
523 539
524 // If we stopped a workspace switch, then we might need to clean up workspaces.540 // If we stopped a workspace switch, then we might need to clean up workspaces.
525- if stopped_primary_ws_switch {541+ // Also if empty_workspace_above_first is set and there are only 2 workspaces left,
542+ // both will be empty and one of them needs to be removed. clean_up_workspaces
543+ // takes care of this.
544+
545+ if stopped_primary_ws_switch
546+ || (primary.options.empty_workspace_above_first
547+ && primary.workspaces.len() == 2)
548+ {
526 primary.clean_up_workspaces();549 primary.clean_up_workspaces();
527 }550 }
528 551
@@ -531,6 +554,7 @@ impl<W: LayoutElement> Layout<W> {
531 if let Some(idx) = &mut active_workspace_idx {554 if let Some(idx) = &mut active_workspace_idx {
532 *idx = workspaces.len() - *idx - 1;555 *idx = workspaces.len() - *idx - 1;
533 }556 }
557+ let mut active_workspace_idx = active_workspace_idx.unwrap_or(0);
534 558
535 // Make sure there's always an empty workspace.559 // Make sure there's always an empty workspace.
536 workspaces.push(Workspace::new(560 workspaces.push(Workspace::new(
@@ -539,13 +563,21 @@ impl<W: LayoutElement> Layout<W> {
539 self.options.clone(),563 self.options.clone(),
540 ));564 ));
541 565
566+ if self.options.empty_workspace_above_first && workspaces.len() > 1 {
567+ workspaces.insert(
568+ 0,
569+ Workspace::new(output.clone(), self.clock.clone(), self.options.clone()),
570+ );
571+ active_workspace_idx += 1;
572+ }
573+
542 for ws in &mut workspaces {574 for ws in &mut workspaces {
543 ws.set_output(Some(output.clone()));575 ws.set_output(Some(output.clone()));
544 }576 }
545 577
546 let mut monitor =578 let mut monitor =
547 Monitor::new(output, workspaces, self.clock.clone(), self.options.clone());579 Monitor::new(output, workspaces, self.clock.clone(), self.options.clone());
548- monitor.active_workspace_idx = active_workspace_idx.unwrap_or(0);580+ monitor.active_workspace_idx = active_workspace_idx;
549 monitors.push(monitor);581 monitors.push(monitor);
550 582
551 MonitorSet::Normal {583 MonitorSet::Normal {
@@ -562,8 +594,16 @@ impl<W: LayoutElement> Layout<W> {
562 self.options.clone(),594 self.options.clone(),
563 ));595 ));
564 596
565- let ws_id_to_activate = self.last_active_workspace_id.remove(&output.name());
566 let mut active_workspace_idx = 0;597 let mut active_workspace_idx = 0;
598+ if self.options.empty_workspace_above_first && workspaces.len() > 1 {
599+ workspaces.insert(
600+ 0,
601+ Workspace::new(output.clone(), self.clock.clone(), self.options.clone()),
602+ );
603+ active_workspace_idx += 1;
604+ }
605+
606+ let ws_id_to_activate = self.last_active_workspace_id.remove(&output.name());
567 607
568 for (i, workspace) in workspaces.iter_mut().enumerate() {608 for (i, workspace) in workspaces.iter_mut().enumerate() {
569 workspace.set_output(Some(output.clone()));609 workspace.set_output(Some(output.clone()));
@@ -611,7 +651,7 @@ impl<W: LayoutElement> Layout<W> {
611 }651 }
612 652
613 // Get rid of empty workspaces.653 // Get rid of empty workspaces.
614- workspaces.retain(|ws| ws.has_windows() || ws.name.is_some());654+ workspaces.retain(|ws| ws.has_windows_or_name());
615 655
616 if monitors.is_empty() {656 if monitors.is_empty() {
617 // Removed the last monitor.657 // Removed the last monitor.
@@ -651,6 +691,14 @@ impl<W: LayoutElement> Layout<W> {
651 primary.workspaces.extend(workspaces);691 primary.workspaces.extend(workspaces);
652 primary.workspaces.push(empty);692 primary.workspaces.push(empty);
653 693
694+ // If empty_workspace_above_first is set and the first workspace is now no
695+ // longer empty, add a new empty workspace on top.
696+ if primary.options.empty_workspace_above_first
697+ && primary.workspaces[0].has_windows_or_name()
698+ {
699+ primary.add_workspace_top();
700+ }
701+
654 // If the empty workspace was focused on the primary monitor, keep it focused.702 // If the empty workspace was focused on the primary monitor, keep it focused.
655 if empty_was_focused {703 if empty_was_focused {
656 primary.active_workspace_idx = primary.workspaces.len() - 1;704 primary.active_workspace_idx = primary.workspaces.len() - 1;
@@ -958,8 +1006,7 @@ impl<W: LayoutElement> Layout<W> {
958 let removed = ws.remove_tile(window, transaction);1006 let removed = ws.remove_tile(window, transaction);
959 1007
960 // Clean up empty workspaces that are not active and not last.1008 // Clean up empty workspaces that are not active and not last.
961- if !ws.has_windows()1009+ if !ws.has_windows_or_name()
962- && ws.name.is_none()
963 && idx != mon.active_workspace_idx1010 && idx != mon.active_workspace_idx
964 && idx != mon.workspaces.len() - 11011 && idx != mon.workspaces.len() - 1
965 && mon.workspace_switch.is_none()1012 && mon.workspace_switch.is_none()
@@ -971,6 +1018,17 @@ impl<W: LayoutElement> Layout<W> {
971 }1018 }
972 }1019 }
973 1020
1021+ // Special case handling when empty_workspace_above_first is set and all
1022+ // workspaces are empty.
1023+ if mon.options.empty_workspace_above_first
1024+ && mon.workspaces.len() == 2
1025+ && mon.workspace_switch.is_none()
1026+ {
1027+ assert!(!mon.workspaces[0].has_windows_or_name());
1028+ assert!(!mon.workspaces[1].has_windows_or_name());
1029+ mon.workspaces.remove(1);
1030+ mon.active_workspace_idx = 0;
1031+ }
974 return Some(removed);1032 return Some(removed);
975 }1033 }
976 }1034 }
@@ -982,7 +1040,7 @@ impl<W: LayoutElement> Layout<W> {
982 let removed = ws.remove_tile(window, transaction);1040 let removed = ws.remove_tile(window, transaction);
983 1041
984 // Clean up empty workspaces.1042 // Clean up empty workspaces.
985- if !ws.has_windows() && ws.name.is_none() {1043+ if !ws.has_windows_or_name() {
986 workspaces.remove(idx);1044 workspaces.remove(idx);
987 }1045 }
988 1046
@@ -2060,7 +2118,7 @@ impl<W: LayoutElement> Layout<W> {
2060 MonitorSet::NoOutputs { workspaces } => {2118 MonitorSet::NoOutputs { workspaces } => {
2061 for workspace in workspaces {2119 for workspace in workspaces {
2062 assert!(2120 assert!(
2063- workspace.has_windows() || workspace.name.is_some(),2121+ workspace.has_windows_or_name(),
2064 "with no outputs there cannot be empty unnamed workspaces"2122 "with no outputs there cannot be empty unnamed workspaces"
2065 );2123 );
2066 2124
@@ -2153,19 +2211,52 @@ impl<W: LayoutElement> Layout<W> {
2153 monitor.workspaces.last().unwrap().columns.is_empty(),2211 monitor.workspaces.last().unwrap().columns.is_empty(),
2154 "monitor must have an empty workspace in the end"2212 "monitor must have an empty workspace in the end"
2155 );2213 );
2214+ if monitor.options.empty_workspace_above_first {
2215+ assert!(
2216+ monitor.workspaces.first().unwrap().columns.is_empty(),
2217+ "first workspace must be empty when empty_workspace_above_first is set"
2218+ )
2219+ }
2156 2220
2157 assert!(2221 assert!(
2158 monitor.workspaces.last().unwrap().name.is_none(),2222 monitor.workspaces.last().unwrap().name.is_none(),
2159 "monitor must have an unnamed workspace in the end"2223 "monitor must have an unnamed workspace in the end"
2160 );2224 );
2225+ if monitor.options.empty_workspace_above_first {
2226+ assert!(
2227+ monitor.workspaces.first().unwrap().name.is_none(),
2228+ "first workspace must be unnamed when empty_workspace_above_first is set"
2229+ )
2230+ }
2231+
2232+ if monitor.options.empty_workspace_above_first {
2233+ assert!(
2234+ monitor.workspaces.len() != 2,
2235+ "if empty_workspace_above_first is set there must be just 1 or 3+ workspaces"
2236+ )
2237+ }
2161 2238
2162 // If there's no workspace switch in progress, there can't be any non-last non-active2239 // If there's no workspace switch in progress, there can't be any non-last non-active
2163- // empty workspaces.2240+ // empty workspaces. If empty_workspace_above_first is set then the first workspace
2241+ // will be empty too.
2242+ let pre_skip = if monitor.options.empty_workspace_above_first {
2243+ 1
2244+ } else {
2245+ 0
2246+ };
2164 if monitor.workspace_switch.is_none() {2247 if monitor.workspace_switch.is_none() {
2165- for (idx, ws) in monitor.workspaces.iter().enumerate().rev().skip(1) {2248+ for (idx, ws) in monitor
2249+ .workspaces
2250+ .iter()
2251+ .enumerate()
2252+ .skip(pre_skip)
2253+ .rev()
2254+ // skip last
2255+ .skip(1)
2256+ {
2166 if idx != monitor.active_workspace_idx {2257 if idx != monitor.active_workspace_idx {
2167 assert!(2258 assert!(
2168- !ws.columns.is_empty() || ws.name.is_some(),2259+ ws.has_windows_or_name(),
2169 "non-active workspace can't be empty and unnamed except the last one"2260 "non-active workspace can't be empty and unnamed except the last one"
2170 );2261 );
2171 }2262 }
@@ -2392,14 +2483,22 @@ impl<W: LayoutElement> Layout<W> {
2392 .unwrap_or(*active_monitor_idx);2483 .unwrap_or(*active_monitor_idx);
2393 let mon = &mut monitors[mon_idx];2484 let mon = &mut monitors[mon_idx];
2394 2485
2486+ let mut insert_idx = 0;
2487+ if mon.options.empty_workspace_above_first {
2488+ // need to insert new empty workspace on top
2489+ mon.add_workspace_top();
2490+ insert_idx += 1;
2491+ }
2492+
2395 let ws = Workspace::new_with_config(2493 let ws = Workspace::new_with_config(
2396 mon.output.clone(),2494 mon.output.clone(),
2397 Some(ws_config.clone()),2495 Some(ws_config.clone()),
2398 clock,2496 clock,
2399 options,2497 options,
2400 );2498 );
2401- mon.workspaces.insert(0, ws);2499+ mon.workspaces.insert(insert_idx, ws);
2402 mon.active_workspace_idx += 1;2500 mon.active_workspace_idx += 1;
2501+
2403 mon.workspace_switch = None;2502 mon.workspace_switch = None;
2404 mon.clean_up_workspaces();2503 mon.clean_up_workspaces();
2405 }2504 }
@@ -2674,6 +2773,10 @@ impl<W: LayoutElement> Layout<W> {
2674 // Insert a new empty workspace.2773 // Insert a new empty workspace.
2675 current.add_workspace_bottom();2774 current.add_workspace_bottom();
2676 }2775 }
2776+ if current.options.empty_workspace_above_first && current.active_workspace_idx == 0 {
2777+ current.add_workspace_top();
2778+ }
2779+
2677 let mut ws = current.workspaces.remove(current.active_workspace_idx);2780 let mut ws = current.workspaces.remove(current.active_workspace_idx);
2678 current.active_workspace_idx = current.active_workspace_idx.saturating_sub(1);2781 current.active_workspace_idx = current.active_workspace_idx.saturating_sub(1);
2679 current.workspace_switch = None;2782 current.workspace_switch = None;
@@ -2690,6 +2793,10 @@ impl<W: LayoutElement> Layout<W> {
2690 2793
2691 target.previous_workspace_id = Some(target.workspaces[target.active_workspace_idx].id());2794 target.previous_workspace_id = Some(target.workspaces[target.active_workspace_idx].id());
2692 2795
2796+ if target.options.empty_workspace_above_first && target.workspaces.len() == 1 {
2797+ // Insert a new empty workspace on top to prepare for insertion of new workspce.
2798+ target.add_workspace_top();
2799+ }
2693 // Insert the workspace after the currently active one. Unless the currently active one is2800 // Insert the workspace after the currently active one. Unless the currently active one is
2694 // the last empty workspace, then insert before.2801 // the last empty workspace, then insert before.
2695 let target_ws_idx = min(target.active_workspace_idx + 1, target.workspaces.len() - 1);2802 let target_ws_idx = min(target.active_workspace_idx + 1, target.workspaces.len() - 1);
@@ -3203,6 +3310,8 @@ impl<W: LayoutElement> Layout<W> {
3203 }3310 }
3204 }3311 }
3205 3312
3313+ // needed because empty_workspace_above_first could have modified the idx
3314+ let ws_idx = mon.active_workspace_idx();
3206 let ws = &mut mon.workspaces[ws_idx];3315 let ws = &mut mon.workspaces[ws_idx];
3207 let (tile, tile_render_loc) = ws3316 let (tile, tile_render_loc) = ws
3208 .tiles_with_render_positions_mut(false)3317 .tiles_with_render_positions_mut(false)
@@ -5382,6 +5491,51 @@ mod tests {
5382 );5491 );
5383 }5492 }
5384 5493
5494+ #[test]
5495+ // empty_workspace_above_first = true
5496+ fn open_right_of_on_different_workspace_ewaf() {
5497+ let ops = [
5498+ Op::AddOutput(1),
5499+ Op::AddWindow {
5500+ id: 1,
5501+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
5502+ min_max_size: (Size::from((0, 0)), Size::from((i32::MAX, i32::MAX))),
5503+ },
5504+ Op::FocusWorkspaceDown,
5505+ Op::AddWindow {
5506+ id: 2,
5507+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
5508+ min_max_size: (Size::from((0, 0)), Size::from((i32::MAX, i32::MAX))),
5509+ },
5510+ Op::AddWindowRightOf {
5511+ id: 3,
5512+ right_of_id: 1,
5513+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
5514+ min_max_size: (Size::from((0, 0)), Size::from((i32::MAX, i32::MAX))),
5515+ },
5516+ ];
5517+
5518+ let options = Options {
5519+ empty_workspace_above_first: true,
5520+ ..Default::default()
5521+ };
5522+ let layout = check_ops_with_options(options, &ops);
5523+
5524+ let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
5525+ unreachable!()
5526+ };
5527+
5528+ let mon = monitors.into_iter().next().unwrap();
5529+ assert_eq!(
5530+ mon.active_workspace_idx, 2,
5531+ "the second workspace must remain active"
5532+ );
5533+ assert_eq!(
5534+ mon.workspaces[1].active_column_idx, 1,
5535+ "the new window must become active"
5536+ );
5537+ }
5538+
5385 #[test]5539 #[test]
5386 fn unfullscreen_view_offset_not_reset_on_removal() {5540 fn unfullscreen_view_offset_not_reset_on_removal() {
5387 let ops = [5541 let ops = [
@@ -5830,6 +5984,40 @@ mod tests {
5830 check_ops(&ops);5984 check_ops(&ops);
5831 }5985 }
5832 5986
5987+ #[test]
5988+ fn interactive_move_onto_empty_output_ewaf() {
5989+ let ops = [
5990+ Op::AddOutput(1),
5991+ Op::AddWindow {
5992+ id: 0,
5993+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
5994+ min_max_size: Default::default(),
5995+ },
5996+ Op::InteractiveMoveBegin {
5997+ window: 0,
5998+ output_idx: 1,
5999+ px: 0.,
6000+ py: 0.,
6001+ },
6002+ Op::AddOutput(2),
6003+ Op::InteractiveMoveUpdate {
6004+ window: 0,
6005+ dx: 1000.,
6006+ dy: 0.,
6007+ output_idx: 2,
6008+ px: 0.,
6009+ py: 0.,
6010+ },
6011+ Op::InteractiveMoveEnd { window: 0 },
6012+ ];
6013+
6014+ let options = Options {
6015+ empty_workspace_above_first: true,
6016+ ..Default::default()
6017+ };
6018+ check_ops_with_options(options, &ops);
6019+ }
6020+
5833 #[test]6021 #[test]
5834 fn interactive_move_onto_last_workspace() {6022 fn interactive_move_onto_last_workspace() {
5835 let ops = [6023 let ops = [
@@ -5861,6 +6049,40 @@ mod tests {
5861 check_ops(&ops);6049 check_ops(&ops);
5862 }6050 }
5863 6051
6052+ #[test]
6053+ fn interactive_move_onto_first_empty_workspace() {
6054+ let ops = [
6055+ Op::AddOutput(1),
6056+ Op::AddWindow {
6057+ id: 1,
6058+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
6059+ min_max_size: Default::default(),
6060+ },
6061+ Op::InteractiveMoveBegin {
6062+ window: 1,
6063+ output_idx: 1,
6064+ px: 0.,
6065+ py: 0.,
6066+ },
6067+ Op::InteractiveMoveUpdate {
6068+ window: 1,
6069+ dx: 1000.,
6070+ dy: 0.,
6071+ output_idx: 1,
6072+ px: 0.,
6073+ py: 0.,
6074+ },
6075+ Op::FocusWorkspaceUp,
6076+ Op::AdvanceAnimations { msec_delta: 1000 },
6077+ Op::InteractiveMoveEnd { window: 1 },
6078+ ];
6079+ let options = Options {
6080+ empty_workspace_above_first: true,
6081+ ..Default::default()
6082+ };
6083+ check_ops_with_options(options, &ops);
6084+ }
6085+
5864 #[test]6086 #[test]
5865 fn output_active_workspace_is_preserved() {6087 fn output_active_workspace_is_preserved() {
5866 let ops = [6088 let ops = [
@@ -5918,6 +6140,154 @@ mod tests {
5918 assert_eq!(monitors[1].active_workspace_idx, 1);6140 assert_eq!(monitors[1].active_workspace_idx, 1);
5919 }6141 }
5920 6142
6143+ #[test]
6144+ fn named_workspace_to_output() {
6145+ let ops = [
6146+ Op::AddNamedWorkspace {
6147+ ws_name: 1,
6148+ output_name: None,
6149+ },
6150+ Op::AddOutput(1),
6151+ Op::MoveWorkspaceToOutput(1),
6152+ Op::FocusWorkspaceUp,
6153+ ];
6154+ check_ops(&ops);
6155+ }
6156+
6157+ #[test]
6158+ // empty_workspace_above_first = true
6159+ fn named_workspace_to_output_ewaf() {
6160+ let ops = [
6161+ Op::AddNamedWorkspace {
6162+ ws_name: 1,
6163+ output_name: Some(2),
6164+ },
6165+ Op::AddOutput(1),
6166+ Op::AddOutput(2),
6167+ ];
6168+ let options = Options {
6169+ empty_workspace_above_first: true,
6170+ ..Default::default()
6171+ };
6172+ check_ops_with_options(options, &ops);
6173+ }
6174+
6175+ #[test]
6176+ fn move_window_to_empty_workspace_above_first() {
6177+ let ops = [
6178+ Op::AddOutput(1),
6179+ Op::AddWindow {
6180+ id: 1,
6181+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
6182+ min_max_size: Default::default(),
6183+ },
6184+ Op::MoveWorkspaceUp,
6185+ Op::MoveWorkspaceDown,
6186+ Op::FocusWorkspaceUp,
6187+ Op::MoveWorkspaceDown,
6188+ ];
6189+ let options = Options {
6190+ empty_workspace_above_first: true,
6191+ ..Default::default()
6192+ };
6193+ check_ops_with_options(options, &ops);
6194+ }
6195+
6196+ #[test]
6197+ fn move_window_to_different_output() {
6198+ let ops = [
6199+ Op::AddWindow {
6200+ id: 1,
6201+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
6202+ min_max_size: Default::default(),
6203+ },
6204+ Op::AddOutput(1),
6205+ Op::AddOutput(2),
6206+ Op::MoveWorkspaceToOutput(2),
6207+ ];
6208+ let options = Options {
6209+ empty_workspace_above_first: true,
6210+ ..Default::default()
6211+ };
6212+ check_ops_with_options(options, &ops);
6213+ }
6214+
6215+ #[test]
6216+ fn close_window_empty_ws_above_first() {
6217+ let ops = [
6218+ Op::AddWindow {
6219+ id: 1,
6220+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
6221+ min_max_size: Default::default(),
6222+ },
6223+ Op::AddOutput(1),
6224+ Op::CloseWindow(1),
6225+ ];
6226+ let options = Options {
6227+ empty_workspace_above_first: true,
6228+ ..Default::default()
6229+ };
6230+ check_ops_with_options(options, &ops);
6231+ }
6232+
6233+ #[test]
6234+ fn add_and_remove_output() {
6235+ let ops = [
6236+ Op::AddOutput(2),
6237+ Op::AddOutput(1),
6238+ Op::AddWindow {
6239+ id: 1,
6240+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
6241+ min_max_size: Default::default(),
6242+ },
6243+ Op::RemoveOutput(2),
6244+ ];
6245+ let options = Options {
6246+ empty_workspace_above_first: true,
6247+ ..Default::default()
6248+ };
6249+ check_ops_with_options(options, &ops);
6250+ }
6251+
6252+ #[test]
6253+ fn switch_ewaf_on() {
6254+ let ops = [
6255+ Op::AddOutput(1),
6256+ Op::AddWindow {
6257+ id: 1,
6258+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
6259+ min_max_size: Default::default(),
6260+ },
6261+ ];
6262+
6263+ let mut layout = check_ops(&ops);
6264+ layout.update_options(Options {
6265+ empty_workspace_above_first: true,
6266+ ..Default::default()
6267+ });
6268+ layout.verify_invariants();
6269+ }
6270+
6271+ #[test]
6272+ fn switch_ewaf_off() {
6273+ let ops = [
6274+ Op::AddOutput(1),
6275+ Op::AddWindow {
6276+ id: 1,
6277+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
6278+ min_max_size: Default::default(),
6279+ },
6280+ ];
6281+
6282+ let options = Options {
6283+ empty_workspace_above_first: true,
6284+ ..Default::default()
6285+ };
6286+ let mut layout = check_ops_with_options(options, &ops);
6287+ layout.update_options(Options::default());
6288+ layout.verify_invariants();
6289+ }
6290+
5921 fn arbitrary_spacing() -> impl Strategy<Value = f64> {6291 fn arbitrary_spacing() -> impl Strategy<Value = f64> {
5922 // Give equal weight to:6292 // Give equal weight to:
5923 // - 0: the element is disabled6293 // - 0: the element is disabled
@@ -5992,12 +6362,14 @@ mod tests {
5992 border in arbitrary_border(),6362 border in arbitrary_border(),
5993 center_focused_column in arbitrary_center_focused_column(),6363 center_focused_column in arbitrary_center_focused_column(),
5994 always_center_single_column in any::<bool>(),6364 always_center_single_column in any::<bool>(),
6365+ empty_workspace_above_first in any::<bool>(),
5995 ) -> Options {6366 ) -> Options {
5996 Options {6367 Options {
5997 gaps,6368 gaps,
5998 struts,6369 struts,
5999 center_focused_column,6370 center_focused_column,
6000 always_center_single_column,6371 always_center_single_column,
6372+ empty_workspace_above_first,
6001 focus_ring,6373 focus_ring,
6002 border,6374 border,
6003 ..Default::default()6375 ..Default::default()
src/layout/monitor.rsmodified+84 -9
@@ -86,6 +86,20 @@ impl WorkspaceSwitch {
86 }86 }
87 }87 }
88 88
89+ pub fn offset(&mut self, delta: isize) {
90+ match self {
91+ WorkspaceSwitch::Animation(anim) => anim.offset(delta as f64),
92+ WorkspaceSwitch::Gesture(gesture) => {
93+ if delta >= 0 {
94+ gesture.center_idx += delta as usize;
95+ } else {
96+ gesture.center_idx -= (-delta) as usize;
97+ }
98+ gesture.current_idx += delta as f64;
99+ }
100+ }
101+ }
102+
89 /// Returns `true` if the workspace switch is [`Animation`].103 /// Returns `true` if the workspace switch is [`Animation`].
90 ///104 ///
91 /// [`Animation`]: WorkspaceSwitch::Animation105 /// [`Animation`]: WorkspaceSwitch::Animation
@@ -158,6 +172,20 @@ impl<W: LayoutElement> Monitor<W> {
158 self.windows().any(|win| win.id() == window)172 self.windows().any(|win| win.id() == window)
159 }173 }
160 174
175+ pub fn add_workspace_top(&mut self) {
176+ let ws = Workspace::new(
177+ self.output.clone(),
178+ self.clock.clone(),
179+ self.options.clone(),
180+ );
181+ self.workspaces.insert(0, ws);
182+ self.active_workspace_idx += 1;
183+
184+ if let Some(switch) = &mut self.workspace_switch {
185+ switch.offset(1);
186+ }
187+ }
188+
161 pub fn add_workspace_bottom(&mut self) {189 pub fn add_workspace_bottom(&mut self) {
162 let ws = Workspace::new(190 let ws = Workspace::new(
163 self.output.clone(),191 self.output.clone(),
@@ -194,7 +222,7 @@ impl<W: LayoutElement> Monitor<W> {
194 222
195 pub fn add_window(223 pub fn add_window(
196 &mut self,224 &mut self,
197- workspace_idx: usize,225+ mut workspace_idx: usize,
198 window: W,226 window: W,
199 activate: bool,227 activate: bool,
200 width: ColumnWidth,228 width: ColumnWidth,
@@ -208,10 +236,14 @@ impl<W: LayoutElement> Monitor<W> {
208 workspace.original_output = OutputId::new(&self.output);236 workspace.original_output = OutputId::new(&self.output);
209 237
210 if workspace_idx == self.workspaces.len() - 1 {238 if workspace_idx == self.workspaces.len() - 1 {
211- // Insert a new empty workspace.
212 self.add_workspace_bottom();239 self.add_workspace_bottom();
213 }240 }
214 241
242+ if self.options.empty_workspace_above_first && workspace_idx == 0 {
243+ self.add_workspace_top();
244+ workspace_idx += 1;
245+ }
246+
215 if activate {247 if activate {
216 self.activate_workspace(workspace_idx);248 self.activate_workspace(workspace_idx);
217 }249 }
@@ -240,7 +272,7 @@ impl<W: LayoutElement> Monitor<W> {
240 // cannot be the last one, so we never need to insert a new empty workspace.272 // cannot be the last one, so we never need to insert a new empty workspace.
241 }273 }
242 274
243- pub fn add_column(&mut self, workspace_idx: usize, column: Column<W>, activate: bool) {275+ pub fn add_column(&mut self, mut workspace_idx: usize, column: Column<W>, activate: bool) {
244 let workspace = &mut self.workspaces[workspace_idx];276 let workspace = &mut self.workspaces[workspace_idx];
245 277
246 workspace.add_column(None, column, activate, None);278 workspace.add_column(None, column, activate, None);
@@ -249,9 +281,12 @@ impl<W: LayoutElement> Monitor<W> {
249 workspace.original_output = OutputId::new(&self.output);281 workspace.original_output = OutputId::new(&self.output);
250 282
251 if workspace_idx == self.workspaces.len() - 1 {283 if workspace_idx == self.workspaces.len() - 1 {
252- // Insert a new empty workspace.
253 self.add_workspace_bottom();284 self.add_workspace_bottom();
254 }285 }
286+ if self.options.empty_workspace_above_first && workspace_idx == 0 {
287+ self.add_workspace_top();
288+ workspace_idx += 1;
289+ }
255 290
256 if activate {291 if activate {
257 self.activate_workspace(workspace_idx);292 self.activate_workspace(workspace_idx);
@@ -260,7 +295,7 @@ impl<W: LayoutElement> Monitor<W> {
260 295
261 pub fn add_tile(296 pub fn add_tile(
262 &mut self,297 &mut self,
263- workspace_idx: usize,298+ mut workspace_idx: usize,
264 column_idx: Option<usize>,299 column_idx: Option<usize>,
265 tile: Tile<W>,300 tile: Tile<W>,
266 activate: bool,301 activate: bool,
@@ -279,6 +314,11 @@ impl<W: LayoutElement> Monitor<W> {
279 self.add_workspace_bottom();314 self.add_workspace_bottom();
280 }315 }
281 316
317+ if self.options.empty_workspace_above_first && workspace_idx == 0 {
318+ self.add_workspace_top();
319+ workspace_idx += 1;
320+ }
321+
282 if activate {322 if activate {
283 self.activate_workspace(workspace_idx);323 self.activate_workspace(workspace_idx);
284 }324 }
@@ -310,18 +350,32 @@ impl<W: LayoutElement> Monitor<W> {
310 pub fn clean_up_workspaces(&mut self) {350 pub fn clean_up_workspaces(&mut self) {
311 assert!(self.workspace_switch.is_none());351 assert!(self.workspace_switch.is_none());
312 352
313- for idx in (0..self.workspaces.len() - 1).rev() {353+ let range_start = if self.options.empty_workspace_above_first {
354+ 1
355+ } else {
356+ 0
357+ };
358+ for idx in (range_start..self.workspaces.len() - 1).rev() {
314 if self.active_workspace_idx == idx {359 if self.active_workspace_idx == idx {
315 continue;360 continue;
316 }361 }
317 362
318- if !self.workspaces[idx].has_windows() && self.workspaces[idx].name.is_none() {363+ if !self.workspaces[idx].has_windows_or_name() {
319 self.workspaces.remove(idx);364 self.workspaces.remove(idx);
320 if self.active_workspace_idx > idx {365 if self.active_workspace_idx > idx {
321 self.active_workspace_idx -= 1;366 self.active_workspace_idx -= 1;
322 }367 }
323 }368 }
324 }369 }
370+
371+ // Special case handling when empty_workspace_above_first is set and all workspaces
372+ // are empty.
373+ if self.options.empty_workspace_above_first && self.workspaces.len() == 2 {
374+ assert!(!self.workspaces[0].has_windows_or_name());
375+ assert!(!self.workspaces[1].has_windows_or_name());
376+ self.workspaces.remove(1);
377+ self.active_workspace_idx = 0;
378+ }
325 }379 }
326 380
327 pub fn unname_workspace(&mut self, workspace_name: &str) -> bool {381 pub fn unname_workspace(&mut self, workspace_name: &str) -> bool {
@@ -792,6 +846,17 @@ impl<W: LayoutElement> Monitor<W> {
792 }846 }
793 847
794 pub fn update_config(&mut self, options: Rc<Options>) {848 pub fn update_config(&mut self, options: Rc<Options>) {
849+ if self.options.empty_workspace_above_first != options.empty_workspace_above_first
850+ && self.workspaces.len() > 1
851+ {
852+ if options.empty_workspace_above_first {
853+ self.add_workspace_top();
854+ } else if self.workspace_switch.is_none() && self.active_workspace_idx != 0 {
855+ self.workspaces.remove(0);
856+ self.active_workspace_idx = self.active_workspace_idx.saturating_sub(1);
857+ }
858+ }
859+
795 for ws in &mut self.workspaces {860 for ws in &mut self.workspaces {
796 ws.update_config(options.clone());861 ws.update_config(options.clone());
797 }862 }
@@ -823,7 +888,7 @@ impl<W: LayoutElement> Monitor<W> {
823 }888 }
824 889
825 pub fn move_workspace_down(&mut self) {890 pub fn move_workspace_down(&mut self) {
826- let new_idx = min(self.active_workspace_idx + 1, self.workspaces.len() - 1);891+ let mut new_idx = min(self.active_workspace_idx + 1, self.workspaces.len() - 1);
827 if new_idx == self.active_workspace_idx {892 if new_idx == self.active_workspace_idx {
828 return;893 return;
829 }894 }
@@ -835,6 +900,11 @@ impl<W: LayoutElement> Monitor<W> {
835 self.add_workspace_bottom();900 self.add_workspace_bottom();
836 }901 }
837 902
903+ if self.options.empty_workspace_above_first && self.active_workspace_idx == 0 {
904+ self.add_workspace_top();
905+ new_idx += 1;
906+ }
907+
838 let previous_workspace_id = self.previous_workspace_id;908 let previous_workspace_id = self.previous_workspace_id;
839 self.activate_workspace(new_idx);909 self.activate_workspace(new_idx);
840 self.workspace_switch = None;910 self.workspace_switch = None;
@@ -844,7 +914,7 @@ impl<W: LayoutElement> Monitor<W> {
844 }914 }
845 915
846 pub fn move_workspace_up(&mut self) {916 pub fn move_workspace_up(&mut self) {
847- let new_idx = self.active_workspace_idx.saturating_sub(1);917+ let mut new_idx = self.active_workspace_idx.saturating_sub(1);
848 if new_idx == self.active_workspace_idx {918 if new_idx == self.active_workspace_idx {
849 return;919 return;
850 }920 }
@@ -856,6 +926,11 @@ impl<W: LayoutElement> Monitor<W> {
856 self.add_workspace_bottom();926 self.add_workspace_bottom();
857 }927 }
858 928
929+ if self.options.empty_workspace_above_first && new_idx == 0 {
930+ self.add_workspace_top();
931+ new_idx += 1;
932+ }
933+
859 let previous_workspace_id = self.previous_workspace_id;934 let previous_workspace_id = self.previous_workspace_id;
860 self.activate_workspace(new_idx);935 self.activate_workspace(new_idx);
861 self.workspace_switch = None;936 self.workspace_switch = None;
src/layout/workspace.rsmodified+4 -0
@@ -567,6 +567,10 @@ impl<W: LayoutElement> Workspace<W> {
567 self.name = None;567 self.name = None;
568 }568 }
569 569
570+ pub fn has_windows_or_name(&self) -> bool {
571+ self.has_windows() || self.name.is_some()
572+ }
573+
570 pub fn scale(&self) -> smithay::output::Scale {574 pub fn scale(&self) -> smithay::output::Scale {
571 self.scale575 self.scale
572 }576 }
wiki/Configuration:-Layout.mdmodified+13 -0
@@ -9,6 +9,7 @@ layout {
9 gaps 169 gaps 16
10 center-focused-column "never"10 center-focused-column "never"
11 always-center-single-column11 always-center-single-column
12+ empty-workspace-above-first
12 13
13 preset-column-widths {14 preset-column-widths {
14 proportion 0.3333315 proportion 0.33333
@@ -100,6 +101,18 @@ layout {
100 }101 }
101 ```102 ```
102 103
104+### `empty-workspace-above-first`
105+
106+<sup>Since: 0.1.11</sup>
107+
108+If set, niri will always add an empty workspace at the very start, in addition to the empty workspace at the very end.
109+
110+```kdl
111+layout {
112+ empty-workspace-above-first
113+}
114+```
115+
103 ### `preset-column-widths`116 ### `preset-column-widths`
104 117
105 Set the widths that the `switch-preset-column-width` action (Mod+R) toggles between.118 Set the widths that the `switch-preset-column-width` action (Mod+R) toggles between.