forge
Explore
Log in

Don't you dare go Hollow

Workspace back and forth (#253)

Browse files at this commit

23ac3d73232f307186212293713d6801d37cff2aUnverifiedFluxTape and Ivan MolodetskikhParent: c3327d3
* implement workspace back and forth

* Make our own ID counter instead of SerialCounter, use a newtype

* Rename FocusWorkspaceBackAndForth to FocusWorkspacePrevious

* Add focus-workspace-previous to tests

* Don't special case in switch_workspace_previous

* Minor clean up

* Add switch_workspace_auto_back_and_forth to tests

* Skip animation on switch_workspace_previous

* Preserve previous_workspace_id on workspace movement

* Make Workspace::id private with a getter

Reduce the chance it gets overwritten.

* Add test for workspace ID uniqueness

* Update previous workspace ID upon moving workspace across monitors

---------
niri-config/src/lib.rsmodified+6 -0
@@ -73,6 +73,8 @@ pub struct Input {
73 pub warp_mouse_to_focus: bool,73 pub warp_mouse_to_focus: bool,
74 #[knuffel(child)]74 #[knuffel(child)]
75 pub focus_follows_mouse: bool,75 pub focus_follows_mouse: bool,
76+ #[knuffel(child)]
77+ pub workspace_auto_back_and_forth: bool,
76 }78 }
77 79
78 #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)]80 #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)]
@@ -766,6 +768,7 @@ pub enum Action {
766 FocusWorkspaceDown,768 FocusWorkspaceDown,
767 FocusWorkspaceUp,769 FocusWorkspaceUp,
768 FocusWorkspace(#[knuffel(argument)] u8),770 FocusWorkspace(#[knuffel(argument)] u8),
771+ FocusWorkspacePrevious,
769 MoveWindowToWorkspaceDown,772 MoveWindowToWorkspaceDown,
770 MoveWindowToWorkspaceUp,773 MoveWindowToWorkspaceUp,
771 MoveWindowToWorkspace(#[knuffel(argument)] u8),774 MoveWindowToWorkspace(#[knuffel(argument)] u8),
@@ -835,6 +838,7 @@ impl From<niri_ipc::Action> for Action {
835 niri_ipc::Action::FocusWorkspaceDown => Self::FocusWorkspaceDown,838 niri_ipc::Action::FocusWorkspaceDown => Self::FocusWorkspaceDown,
836 niri_ipc::Action::FocusWorkspaceUp => Self::FocusWorkspaceUp,839 niri_ipc::Action::FocusWorkspaceUp => Self::FocusWorkspaceUp,
837 niri_ipc::Action::FocusWorkspace { index } => Self::FocusWorkspace(index),840 niri_ipc::Action::FocusWorkspace { index } => Self::FocusWorkspace(index),
841+ niri_ipc::Action::FocusWorkspacePrevious => Self::FocusWorkspacePrevious,
838 niri_ipc::Action::MoveWindowToWorkspaceDown => Self::MoveWindowToWorkspaceDown,842 niri_ipc::Action::MoveWindowToWorkspaceDown => Self::MoveWindowToWorkspaceDown,
839 niri_ipc::Action::MoveWindowToWorkspaceUp => Self::MoveWindowToWorkspaceUp,843 niri_ipc::Action::MoveWindowToWorkspaceUp => Self::MoveWindowToWorkspaceUp,
840 niri_ipc::Action::MoveWindowToWorkspace { index } => Self::MoveWindowToWorkspace(index),844 niri_ipc::Action::MoveWindowToWorkspace { index } => Self::MoveWindowToWorkspace(index),
@@ -1599,6 +1603,7 @@ mod tests {
1599 1603
1600 warp-mouse-to-focus1604 warp-mouse-to-focus
1601 focus-follows-mouse1605 focus-follows-mouse
1606+ workspace-auto-back-and-forth
1602 }1607 }
1603 1608
1604 output "eDP-1" {1609 output "eDP-1" {
@@ -1740,6 +1745,7 @@ mod tests {
1740 disable_power_key_handling: true,1745 disable_power_key_handling: true,
1741 warp_mouse_to_focus: true,1746 warp_mouse_to_focus: true,
1742 focus_follows_mouse: true,1747 focus_follows_mouse: true,
1748+ workspace_auto_back_and_forth: true,
1743 },1749 },
1744 outputs: vec![Output {1750 outputs: vec![Output {
1745 off: false,1751 off: false,
niri-ipc/src/lib.rsmodified+2 -0
@@ -123,6 +123,8 @@ pub enum Action {
123 #[cfg_attr(feature = "clap", arg())]123 #[cfg_attr(feature = "clap", arg())]
124 index: u8,124 index: u8,
125 },125 },
126+ /// Focus the previous workspace.
127+ FocusWorkspacePrevious,
126 /// Move the focused window to the workspace below.128 /// Move the focused window to the workspace below.
127 MoveWindowToWorkspaceDown,129 MoveWindowToWorkspaceDown,
128 /// Move the focused window to the workspace above.130 /// Move the focused window to the workspace above.
resources/default-config.kdlmodified+8 -0
@@ -73,6 +73,11 @@ input {
73 73
74 // Focus windows and outputs automatically when moving the mouse into them.74 // Focus windows and outputs automatically when moving the mouse into them.
75 // focus-follows-mouse75 // focus-follows-mouse
76+
77+ // Uncomment this to enable workspace auto-back-and-forth.
78+ // If enabled, switching to the same workspace by index twice will switch back to the
79+ // previous workspace.
80+ // workspace-auto-back-and-forth
76 }81 }
77 82
78 // You can configure outputs by their name, which you can find83 // You can configure outputs by their name, which you can find
@@ -518,6 +523,9 @@ binds {
518 // Alternatively, there are commands to move just a single window:523 // Alternatively, there are commands to move just a single window:
519 // Mod+Ctrl+1 { move-window-to-workspace 1; }524 // Mod+Ctrl+1 { move-window-to-workspace 1; }
520 525
526+ // Switches focus between the current and the previous workspace.
527+ // Mod+Tab { focus-workspace-previous; }
528+
521 Mod+Comma { consume-window-into-column; }529 Mod+Comma { consume-window-into-column; }
522 Mod+Period { expel-window-from-column; }530 Mod+Period { expel-window-from-column; }
523 531
src/input.rsmodified+13 -1
@@ -569,11 +569,23 @@ impl State {
569 }569 }
570 Action::FocusWorkspace(idx) => {570 Action::FocusWorkspace(idx) => {
571 let idx = idx.saturating_sub(1) as usize;571 let idx = idx.saturating_sub(1) as usize;
572- self.niri.layout.switch_workspace(idx);572+
573+ let config = &self.niri.config;
574+ if config.borrow().input.workspace_auto_back_and_forth {
575+ self.niri.layout.switch_workspace_auto_back_and_forth(idx);
576+ } else {
577+ self.niri.layout.switch_workspace(idx);
578+ }
579+
573 self.maybe_warp_cursor_to_focus();580 self.maybe_warp_cursor_to_focus();
574 // FIXME: granular581 // FIXME: granular
575 self.niri.queue_redraw_all();582 self.niri.queue_redraw_all();
576 }583 }
584+ Action::FocusWorkspacePrevious => {
585+ self.niri.layout.switch_workspace_previous();
586+ // FIXME: granular
587+ self.niri.queue_redraw_all();
588+ }
577 Action::MoveWorkspaceDown => {589 Action::MoveWorkspaceDown => {
578 self.niri.layout.move_workspace_down();590 self.niri.layout.move_workspace_down();
579 // FIXME: granular591 // FIXME: granular
src/layout/mod.rsmodified+36 -0
@@ -1165,6 +1165,20 @@ impl<W: LayoutElement> Layout<W> {
1165 monitor.switch_workspace(idx);1165 monitor.switch_workspace(idx);
1166 }1166 }
1167 1167
1168+ pub fn switch_workspace_auto_back_and_forth(&mut self, idx: usize) {
1169+ let Some(monitor) = self.active_monitor() else {
1170+ return;
1171+ };
1172+ monitor.switch_workspace_auto_back_and_forth(idx);
1173+ }
1174+
1175+ pub fn switch_workspace_previous(&mut self) {
1176+ let Some(monitor) = self.active_monitor() else {
1177+ return;
1178+ };
1179+ monitor.switch_workspace_previous();
1180+ }
1181+
1168 pub fn consume_into_column(&mut self) {1182 pub fn consume_into_column(&mut self) {
1169 let Some(monitor) = self.active_monitor() else {1183 let Some(monitor) = self.active_monitor() else {
1170 return;1184 return;
@@ -1221,8 +1235,12 @@ impl<W: LayoutElement> Layout<W> {
1221 1235
1222 #[cfg(test)]1236 #[cfg(test)]
1223 fn verify_invariants(&self) {1237 fn verify_invariants(&self) {
1238+ use std::collections::HashSet;
1239+
1224 use crate::layout::monitor::WorkspaceSwitch;1240 use crate::layout::monitor::WorkspaceSwitch;
1225 1241
1242+ let mut seen_workspace_id = HashSet::new();
1243+
1226 let (monitors, &primary_idx, &active_monitor_idx) = match &self.monitor_set {1244 let (monitors, &primary_idx, &active_monitor_idx) = match &self.monitor_set {
1227 MonitorSet::Normal {1245 MonitorSet::Normal {
1228 monitors,1246 monitors,
@@ -1241,6 +1259,11 @@ impl<W: LayoutElement> Layout<W> {
1241 "workspace options must be synchronized with layout"1259 "workspace options must be synchronized with layout"
1242 );1260 );
1243 1261
1262+ assert!(
1263+ seen_workspace_id.insert(workspace.id()),
1264+ "workspace id must be unique"
1265+ );
1266+
1244 workspace.verify_invariants();1267 workspace.verify_invariants();
1245 }1268 }
1246 1269
@@ -1325,6 +1348,11 @@ impl<W: LayoutElement> Layout<W> {
1325 "workspace options must be synchronized with layout"1348 "workspace options must be synchronized with layout"
1326 );1349 );
1327 1350
1351+ assert!(
1352+ seen_workspace_id.insert(workspace.id()),
1353+ "workspace id must be unique"
1354+ );
1355+
1328 workspace.verify_invariants();1356 workspace.verify_invariants();
1329 }1357 }
1330 }1358 }
@@ -1529,6 +1557,8 @@ impl<W: LayoutElement> Layout<W> {
1529 .unwrap();1557 .unwrap();
1530 let target = &mut monitors[target_idx];1558 let target = &mut monitors[target_idx];
1531 1559
1560+ target.previous_workspace_id = Some(target.workspaces[target.active_workspace_idx].id());
1561+
1532 // Insert the workspace after the currently active one. Unless the currently active one is1562 // Insert the workspace after the currently active one. Unless the currently active one is
1533 // the last empty workspace, then insert before.1563 // the last empty workspace, then insert before.
1534 let target_ws_idx = min(target.active_workspace_idx + 1, target.workspaces.len() - 1);1564 let target_ws_idx = min(target.active_workspace_idx + 1, target.workspaces.len() - 1);
@@ -2017,6 +2047,8 @@ mod tests {
2017 FocusWorkspaceDown,2047 FocusWorkspaceDown,
2018 FocusWorkspaceUp,2048 FocusWorkspaceUp,
2019 FocusWorkspace(#[proptest(strategy = "0..=4usize")] usize),2049 FocusWorkspace(#[proptest(strategy = "0..=4usize")] usize),
2050+ FocusWorkspaceAutoBackAndForth(#[proptest(strategy = "0..=4usize")] usize),
2051+ FocusWorkspacePrevious,
2020 MoveWindowToWorkspaceDown,2052 MoveWindowToWorkspaceDown,
2021 MoveWindowToWorkspaceUp,2053 MoveWindowToWorkspaceUp,
2022 MoveWindowToWorkspace(#[proptest(strategy = "0..=4usize")] usize),2054 MoveWindowToWorkspace(#[proptest(strategy = "0..=4usize")] usize),
@@ -2219,6 +2251,10 @@ mod tests {
2219 Op::FocusWorkspaceDown => layout.switch_workspace_down(),2251 Op::FocusWorkspaceDown => layout.switch_workspace_down(),
2220 Op::FocusWorkspaceUp => layout.switch_workspace_up(),2252 Op::FocusWorkspaceUp => layout.switch_workspace_up(),
2221 Op::FocusWorkspace(idx) => layout.switch_workspace(idx),2253 Op::FocusWorkspace(idx) => layout.switch_workspace(idx),
2254+ Op::FocusWorkspaceAutoBackAndForth(idx) => {
2255+ layout.switch_workspace_auto_back_and_forth(idx)
2256+ }
2257+ Op::FocusWorkspacePrevious => layout.switch_workspace_previous(),
2222 Op::MoveWindowToWorkspaceDown => layout.move_to_workspace_down(),2258 Op::MoveWindowToWorkspaceDown => layout.move_to_workspace_down(),
2223 Op::MoveWindowToWorkspaceUp => layout.move_to_workspace_up(),2259 Op::MoveWindowToWorkspaceUp => layout.move_to_workspace_up(),
2224 Op::MoveWindowToWorkspace(idx) => layout.move_to_workspace(idx),2260 Op::MoveWindowToWorkspace(idx) => layout.move_to_workspace(idx),
src/layout/monitor.rsmodified+36 -1
@@ -10,7 +10,8 @@ use smithay::output::Output;
10 use smithay::utils::{Logical, Point, Rectangle, Scale};10 use smithay::utils::{Logical, Point, Rectangle, Scale};
11 11
12 use super::workspace::{12 use super::workspace::{
13- compute_working_area, Column, ColumnWidth, OutputId, Workspace, WorkspaceRenderElement,13+ compute_working_area, Column, ColumnWidth, OutputId, Workspace, WorkspaceId,
14+ WorkspaceRenderElement,
14 };15 };
15 use super::{LayoutElement, Options};16 use super::{LayoutElement, Options};
16 use crate::animation::Animation;17 use crate::animation::Animation;
@@ -35,6 +36,8 @@ pub struct Monitor<W: LayoutElement> {
35 pub workspaces: Vec<Workspace<W>>,36 pub workspaces: Vec<Workspace<W>>,
36 /// Index of the currently active workspace.37 /// Index of the currently active workspace.
37 pub active_workspace_idx: usize,38 pub active_workspace_idx: usize,
39+ /// ID of the previously active workspace.
40+ pub previous_workspace_id: Option<WorkspaceId>,
38 /// In-progress switch between workspaces.41 /// In-progress switch between workspaces.
39 pub workspace_switch: Option<WorkspaceSwitch>,42 pub workspace_switch: Option<WorkspaceSwitch>,
40 /// Configurable properties of the layout.43 /// Configurable properties of the layout.
@@ -89,6 +92,7 @@ impl<W: LayoutElement> Monitor<W> {
89 output,92 output,
90 workspaces,93 workspaces,
91 active_workspace_idx: 0,94 active_workspace_idx: 0,
95+ previous_workspace_id: None,
92 workspace_switch: None,96 workspace_switch: None,
93 options,97 options,
94 }98 }
@@ -114,6 +118,8 @@ impl<W: LayoutElement> Monitor<W> {
114 .map(|s| s.current_idx())118 .map(|s| s.current_idx())
115 .unwrap_or(self.active_workspace_idx as f64);119 .unwrap_or(self.active_workspace_idx as f64);
116 120
121+ self.previous_workspace_id = Some(self.workspaces[self.active_workspace_idx].id());
122+
117 self.active_workspace_idx = idx;123 self.active_workspace_idx = idx;
118 124
119 self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(125 self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
@@ -461,6 +467,11 @@ impl<W: LayoutElement> Monitor<W> {
461 ));467 ));
462 }468 }
463 469
470+ fn previous_workspace_idx(&self) -> Option<usize> {
471+ let id = self.previous_workspace_id?;
472+ self.workspaces.iter().position(|w| w.id() == id)
473+ }
474+
464 pub fn switch_workspace(&mut self, idx: usize) {475 pub fn switch_workspace(&mut self, idx: usize) {
465 self.activate_workspace(min(idx, self.workspaces.len() - 1));476 self.activate_workspace(min(idx, self.workspaces.len() - 1));
466 // Don't animate this action.477 // Don't animate this action.
@@ -469,6 +480,24 @@ impl<W: LayoutElement> Monitor<W> {
469 self.clean_up_workspaces();480 self.clean_up_workspaces();
470 }481 }
471 482
483+ pub fn switch_workspace_auto_back_and_forth(&mut self, idx: usize) {
484+ let idx = min(idx, self.workspaces.len() - 1);
485+
486+ if idx == self.active_workspace_idx {
487+ if let Some(prev_idx) = self.previous_workspace_idx() {
488+ self.switch_workspace(prev_idx);
489+ }
490+ } else {
491+ self.switch_workspace(idx);
492+ }
493+ }
494+
495+ pub fn switch_workspace_previous(&mut self) {
496+ if let Some(idx) = self.previous_workspace_idx() {
497+ self.switch_workspace(idx);
498+ }
499+ }
500+
472 pub fn consume_into_column(&mut self) {501 pub fn consume_into_column(&mut self) {
473 self.active_workspace().consume_into_column();502 self.active_workspace().consume_into_column();
474 }503 }
@@ -564,8 +593,10 @@ impl<W: LayoutElement> Monitor<W> {
564 self.workspaces.push(ws);593 self.workspaces.push(ws);
565 }594 }
566 595
596+ let previous_workspace_id = self.previous_workspace_id;
567 self.activate_workspace(new_idx);597 self.activate_workspace(new_idx);
568 self.workspace_switch = None;598 self.workspace_switch = None;
599+ self.previous_workspace_id = previous_workspace_id;
569 600
570 self.clean_up_workspaces();601 self.clean_up_workspaces();
571 }602 }
@@ -584,8 +615,10 @@ impl<W: LayoutElement> Monitor<W> {
584 self.workspaces.push(ws);615 self.workspaces.push(ws);
585 }616 }
586 617
618+ let previous_workspace_id = self.previous_workspace_id;
587 self.activate_workspace(new_idx);619 self.activate_workspace(new_idx);
588 self.workspace_switch = None;620 self.workspace_switch = None;
621+ self.previous_workspace_id = previous_workspace_id;
589 622
590 self.clean_up_workspaces();623 self.clean_up_workspaces();
591 }624 }
@@ -833,6 +866,8 @@ impl<W: LayoutElement> Monitor<W> {
833 gesture.center_idx as f64 + current_pos,866 gesture.center_idx as f64 + current_pos,
834 );867 );
835 868
869+ self.previous_workspace_id = Some(self.workspaces[self.active_workspace_idx].id());
870+
836 self.active_workspace_idx = new_idx;871 self.active_workspace_idx = new_idx;
837 self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(872 self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
838 gesture.current_idx,873 gesture.current_idx,
src/layout/workspace.rsmodified+21 -0
@@ -18,6 +18,7 @@ use crate::animation::Animation;
18 use crate::niri_render_elements;18 use crate::niri_render_elements;
19 use crate::render_helpers::renderer::NiriRenderer;19 use crate::render_helpers::renderer::NiriRenderer;
20 use crate::swipe_tracker::SwipeTracker;20 use crate::swipe_tracker::SwipeTracker;
21+use crate::utils::id::IdCounter;
21 use crate::utils::output_size;22 use crate::utils::output_size;
22 23
23 /// Amount of touchpad movement to scroll the view for the width of one working area.24 /// Amount of touchpad movement to scroll the view for the width of one working area.
@@ -76,11 +77,25 @@ pub struct Workspace<W: LayoutElement> {
76 77
77 /// Configurable properties of the layout.78 /// Configurable properties of the layout.
78 pub options: Rc<Options>,79 pub options: Rc<Options>,
80+
81+ /// Unique ID of this workspace.
82+ id: WorkspaceId,
79 }83 }
80 84
81 #[derive(Debug, Clone, PartialEq, Eq)]85 #[derive(Debug, Clone, PartialEq, Eq)]
82 pub struct OutputId(String);86 pub struct OutputId(String);
83 87
88+static WORKSPACE_ID_COUNTER: IdCounter = IdCounter::new();
89+
90+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
91+pub struct WorkspaceId(u32);
92+
93+impl WorkspaceId {
94+ fn next() -> WorkspaceId {
95+ WorkspaceId(WORKSPACE_ID_COUNTER.next())
96+ }
97+}
98+
84 niri_render_elements! {99 niri_render_elements! {
85 WorkspaceRenderElement<R> => {100 WorkspaceRenderElement<R> => {
86 Tile = TileRenderElement<R>,101 Tile = TileRenderElement<R>,
@@ -225,6 +240,7 @@ impl<W: LayoutElement> Workspace<W> {
225 view_offset_adj: None,240 view_offset_adj: None,
226 activate_prev_column_on_removal: None,241 activate_prev_column_on_removal: None,
227 options,242 options,
243+ id: WorkspaceId::next(),
228 }244 }
229 }245 }
230 246
@@ -240,9 +256,14 @@ impl<W: LayoutElement> Workspace<W> {
240 view_offset_adj: None,256 view_offset_adj: None,
241 activate_prev_column_on_removal: None,257 activate_prev_column_on_removal: None,
242 options,258 options,
259+ id: WorkspaceId::next(),
243 }260 }
244 }261 }
245 262
263+ pub fn id(&self) -> WorkspaceId {
264+ self.id
265+ }
266+
246 pub fn advance_animations(&mut self, current_time: Duration, is_active: bool) {267 pub fn advance_animations(&mut self, current_time: Duration, is_active: bool) {
247 if let Some(ViewOffsetAdjustment::Animation(anim)) = &mut self.view_offset_adj {268 if let Some(ViewOffsetAdjustment::Animation(anim)) = &mut self.view_offset_adj {
248 anim.set_current_time(current_time);269 anim.set_current_time(current_time);
src/utils/id.rsadded+27 -0
@@ -0,0 +1,27 @@
1+use std::sync::atomic::{AtomicU32, Ordering};
2+
3+/// Counter that returns unique IDs.
4+///
5+/// Under the hood it uses a `u32` that will eventually wrap around. When incrementing it once a
6+/// second, it will wrap around after about 136 years.
7+pub struct IdCounter {
8+ value: AtomicU32,
9+}
10+
11+impl IdCounter {
12+ pub const fn new() -> Self {
13+ Self {
14+ value: AtomicU32::new(0),
15+ }
16+ }
17+
18+ pub fn next(&self) -> u32 {
19+ self.value.fetch_add(1, Ordering::SeqCst)
20+ }
21+}
22+
23+impl Default for IdCounter {
24+ fn default() -> Self {
25+ Self::new()
26+ }
27+}
src/utils/mod.rsmodified+1 -0
@@ -14,6 +14,7 @@ use smithay::output::Output;
14 use smithay::reexports::rustix::time::{clock_gettime, ClockId};14 use smithay::reexports::rustix::time::{clock_gettime, ClockId};
15 use smithay::utils::{Logical, Point, Rectangle, Size};15 use smithay::utils::{Logical, Point, Rectangle, Size};
16 16
17+pub mod id;
17 pub mod spawning;18 pub mod spawning;
18 pub mod watcher;19 pub mod watcher;
19 20