From ea07db8d0da26edad2d147b8a985ea78c84c973d Mon Sep 17 00:00:00 2001 From: riccardo Date: Sat, 14 Oct 2023 18:35:28 +0200 Subject: [PATCH 1/2] ability to cycle tags, tests --- src/pure/stack_set.rs | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/pure/stack_set.rs b/src/pure/stack_set.rs index 23e134bd..cc34bd00 100644 --- a/src/pure/stack_set.rs +++ b/src/pure/stack_set.rs @@ -564,6 +564,38 @@ where self.screens.focus_up(); } + /// Move focus to the next [Workspace] tag ordered by their id (creation order) + pub fn next_tag(&mut self) { + let tags = self.ordered_tags(); + if tags.is_empty() { + return; + } + let i = tags + .iter() + .position(|a| a == self.current_tag()) + .unwrap_or_default(); + + let new_i = (i + 1) % tags.len(); + let new_tag = &tags[new_i]; + self.focus_tag(new_tag); + } + + /// Move focus to previous [Workspace] tag ordered by their id (creation order) + pub fn previous_tag(&mut self) { + let tags = self.ordered_tags(); + if tags.is_empty() { + return; + } + let i = tags + .iter() + .position(|a| a == self.current_tag()) + .unwrap_or_default(); + + let new_i = if i == 0 { tags.len() - 1 } else { i - 1 }; + let new_tag = &tags[new_i]; + self.focus_tag(new_tag); + } + /// Drag the focused workspace onto the next [Screen], holding focus pub fn drag_workspace_forward(&mut self) { if self.screens.len() == 1 { @@ -1286,6 +1318,25 @@ pub mod tests { assert!(matches!(res, Err(Error::NoScreens))); } + + #[test_case(true, 1, 1; "forward")] + #[test_case(false, 1, 3; "backward")] + #[test_case(true, 4, 0; "forward loop to start")] + #[test] + fn tag_cycle_focuses_correct_tag(forward: bool, count: usize, expected_index: usize) { + let mut s = test_stack_set(4, 2); + + for _ in 0..count { + if forward { + s.next_tag() + } else { + s.previous_tag() + } + } + let tags = s.ordered_tags(); + + assert_eq!(s.current_tag(), tags[expected_index]); + } } #[cfg(test)] From 791fce0f9b8e1aab7035786e3fc2176613ba7f36 Mon Sep 17 00:00:00 2001 From: riccardo Date: Sat, 21 Oct 2023 10:03:36 +0200 Subject: [PATCH 2/2] tags unwrap_or_default to expect --- src/pure/stack_set.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pure/stack_set.rs b/src/pure/stack_set.rs index cc34bd00..1892edaf 100644 --- a/src/pure/stack_set.rs +++ b/src/pure/stack_set.rs @@ -573,7 +573,7 @@ where let i = tags .iter() .position(|a| a == self.current_tag()) - .unwrap_or_default(); + .expect("current tag is a known tag"); let new_i = (i + 1) % tags.len(); let new_tag = &tags[new_i]; @@ -589,7 +589,7 @@ where let i = tags .iter() .position(|a| a == self.current_tag()) - .unwrap_or_default(); + .expect("current tag is a known tag"); let new_i = if i == 0 { tags.len() - 1 } else { i - 1 }; let new_tag = &tags[new_i];