From acfaad5f0602c52e1d23a7825da4e3cfc3721dd8 Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Sun, 13 Jul 2025 12:09:02 +0200 Subject: [PATCH] make rollover optional --- worf/src/lib/config.rs | 9 +++++++++ worf/src/lib/gui.rs | 24 +++++++++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/worf/src/lib/config.rs b/worf/src/lib/config.rs index 73de12e..9e2e9c5 100644 --- a/worf/src/lib/config.rs +++ b/worf/src/lib/config.rs @@ -637,6 +637,10 @@ pub struct Config { /// Auto select when only 1 possible choice is left #[clap(long = "auto-select-on-search")] auto_select_on_search: Option, + + /// Jump to the first/last entry when at the end/start and down/up is pressed + /// Defaults to true + rollover: Option, } impl Config { @@ -926,6 +930,11 @@ impl Config { pub fn auto_select_on_search(&self) -> bool { self.auto_select_on_search.unwrap_or(false) } + + #[must_use] + pub fn rollover(&self) -> bool { + self.rollover.unwrap_or(true) + } } fn default_false() -> bool { diff --git a/worf/src/lib/gui.rs b/worf/src/lib/gui.rs index 00fc8b0..7ee6af2 100644 --- a/worf/src/lib/gui.rs +++ b/worf/src/lib/gui.rs @@ -1083,10 +1083,10 @@ fn handle_key_press( } } gdk4::Key::Up => { - return move_selection(ui, true); + return move_selection(ui, meta, &Direction::Up); } gdk4::Key::Down => { - return move_selection(ui, false); + return move_selection(ui, meta, &Direction::Down); } _ => { if let Some(c) = keyboard_key.to_unicode() { @@ -1109,7 +1109,21 @@ fn handle_key_press( Propagation::Proceed } -fn move_selection(ui: &Rc>, up: bool) -> Propagation { +#[derive(PartialEq)] +enum Direction { + Up, + Down, +} + +fn move_selection( + ui: &Rc>, + meta: &Rc>, + direction: &Direction, +) -> Propagation { + if !meta.config.read().unwrap().rollover() { + return Propagation::Proceed; + } + let selected_children = ui.main_box.selected_children(); let Some(selected) = selected_children.first() else { return Propagation::Proceed; @@ -1131,7 +1145,7 @@ fn move_selection(ui: &Rc>, up: bool) - return Propagation::Proceed; }; - if up && first_child == *selected { + if *direction == Direction::Up && first_child == *selected { select_visible_child( &ui.menu_rows.read().unwrap(), &ui.main_box, @@ -1139,7 +1153,7 @@ fn move_selection(ui: &Rc>, up: bool) - &ChildPosition::Back, ); Propagation::Stop - } else if !up && last_child == *selected { + } else if *direction == Direction::Down && last_child == *selected { select_visible_child( &ui.menu_rows.read().unwrap(), &ui.main_box,