diff --git a/crates/ui-contract/src/contract/collection/props.rs b/crates/ui-contract/src/contract/collection/props.rs index dd7dbd13b..ee6789156 100644 --- a/crates/ui-contract/src/contract/collection/props.rs +++ b/crates/ui-contract/src/contract/collection/props.rs @@ -159,6 +159,8 @@ pub struct CollectionProps { /// Typed letters move the lead to the next item whose text starts with them. Off: they stay shortcut candidates, /// so single-letter tool keys keep working while a list has focus. On for popup lists and pickers. pub type_ahead: bool, + /// The arrow keys wrap from the last item to the first and back (a command palette's list). + pub arrows_wrap: bool, /// The header row shows when a column has a header text. Off: the columns lay the rows out and no header row is built. pub header: bool, /// Typed digits reach the collection's own `update` as `Intent::Digit` (rating keys); other typed text stays @@ -252,6 +254,7 @@ impl CollectionProps { rows_stamp: None, space: SpaceAction::Activate, type_ahead: false, + arrows_wrap: false, header: true, digit_keys: false, menu_on_click: false, @@ -373,6 +376,13 @@ impl CollectionProps { self } + /// The arrow keys wrap at the ends (`CollectionProps::arrows_wrap`). + #[must_use] + pub const fn arrows_wrap(mut self, on: bool) -> Self { + self.arrows_wrap = on; + self + } + /// The rows are the set `stamp` names; the lead starts over when the stamp changes. #[must_use] pub const fn rows_stamp(mut self, stamp: u64) -> Self { diff --git a/crates/ui-contract/src/contract/items/keys.rs b/crates/ui-contract/src/contract/items/keys.rs index a2e9cd697..053fd66c9 100644 --- a/crates/ui-contract/src/contract/items/keys.rs +++ b/crates/ui-contract/src/contract/items/keys.rs @@ -33,7 +33,7 @@ impl ItemsHooks<'_, '_, C> { let start = lead .map(|at| (at, self.lead.column)) .map(|lead| if cells && mods.extend { self.cell_head(comp, layout).unwrap_or(lead) } else { lead }); - let mut state = NavState::new().page(page).column(start.map_or(self.lead.column, |(_, column)| column)); + let mut state = NavState::new().page(page).wrap(comp.items().arrows_wrap).column(start.map_or(self.lead.column, |(_, column)| column)); state.lead = start.map(|(at, _)| at); state.anchor = lead.map(|at| self.anchor_at(at)).filter(|_| mods.extend); let step = match nav::step(layout.nav(), &outline, &mut state, mv, mods) { diff --git a/crates/ui-contract/tests/it/collection/kit.rs b/crates/ui-contract/tests/it/collection/kit.rs index 03d72cefd..68b06dd41 100644 --- a/crates/ui-contract/tests/it/collection/kit.rs +++ b/crates/ui-contract/tests/it/collection/kit.rs @@ -191,6 +191,12 @@ impl<'a> List<'a> { self } + /// The arrows wrap at the ends. + pub fn wrap(mut self) -> Self { + self.items = self.items.arrows_wrap(true); + self + } + /// Typed letters move the lead. pub fn type_ahead(mut self) -> Self { self.items = self.items.type_ahead(true); diff --git a/crates/ui-contract/tests/it/collection/list.rs b/crates/ui-contract/tests/it/collection/list.rs index 14ce9c70f..ed8cf4268 100644 --- a/crates/ui-contract/tests/it/collection/list.rs +++ b/crates/ui-contract/tests/it/collection/list.rs @@ -190,6 +190,24 @@ fn enter_picks_the_lead_where_arrows_leave_the_selection() { assert!(activated, "Enter activates the lead"); } +/// In a list that wraps, Down on the last row goes to the first and Up on the first to the last. +#[trial] +fn arrows_wrap_at_the_ends_of_a_wrapping_list() { + let words = Words::flat(&labels()); + let seen = Seen::default(); + let mut frames = Frames::new(); + let mut show = |ui: &mut Ui| show_words(ui, List::new(SelectMode::Single).wrap(), &words, &seen); + frames.idle(2, &mut show); + focus_list(&mut frames, &seen, &mut show); + press(&mut frames, Key::ArrowDown, Modifiers::NONE, &mut show); + press(&mut frames, Key::End, Modifiers::NONE, &mut show); + let last = *seen.leads().last().expect("the lead moved"); + press(&mut frames, Key::ArrowDown, Modifiers::NONE, &mut show); + assert_eq!(seen.leads().last(), Some(&0), "Down on the last row wraps to the first"); + press(&mut frames, Key::ArrowUp, Modifiers::NONE, &mut show); + assert_eq!(seen.leads().last(), Some(&last), "Up on the first row wraps to the last"); +} + /// A list without type-ahead leaves typed letters to the app's tool keys: the lead stays where it is. #[trial] fn a_list_without_type_ahead_leaves_typed_letters_alone() { diff --git a/crates/ui-macros/src/props.rs b/crates/ui-macros/src/props.rs index cdfc10d55..a67adb9b0 100644 --- a/crates/ui-macros/src/props.rs +++ b/crates/ui-macros/src/props.rs @@ -170,6 +170,7 @@ const COLLECTION: &[Forwarded] = &[ ("lead_on_open", "::craft_ui::contract::LeadOnOpen", "Where the lead lands when rows first show."), ("lead_follows_hover", "bool", "The lead, and the selection with it, moves to the row under the pointer when the pointer moves."), ("arrows_select", "bool", "The arrow keys select the row they move the lead to; off, they move the lead only."), + ("arrows_wrap", "bool", "The arrow keys wrap from the last item to the first and back."), ("type_ahead", "bool", "Typed letters move the lead to the next item whose text starts with them; off, they stay shortcut candidates."), ( "rows_stamp", diff --git a/crates/ui-patterns/src/patterns/panels/palette.rs b/crates/ui-patterns/src/patterns/panels/palette.rs index 262dc4a2c..f08d4ed1e 100644 --- a/crates/ui-patterns/src/patterns/panels/palette.rs +++ b/crates/ui-patterns/src/patterns/panels/palette.rs @@ -66,7 +66,8 @@ impl crate::contract::Outcome for PaletteOutput { /// the label, the menu path under it and the command's chord at the end; a disabled command is listed and does /// not run. /// - Empty query: the commands last run, newest first (the `recents` binding), else the first entries. -/// - Keys: the field keeps the focus; Up, Down, Page Up, Page Down move the active row, Enter picks it, Escape clears +/// - Keys: the field keeps the focus; Up, Down, Page Up, Page Down move the active row (Up and Down wrap at the ends), +/// Enter picks it, Escape clears /// the query and, on an empty query, closes the box; a press outside closes it. A caption under the list names the keys. The number of results is announced politely. /// - Picking: the output's `picked` names the command and the box closes. With `recents` bound the command is also /// put first in the list; [`CommandPalette::remember`] is the rule. @@ -134,6 +135,7 @@ impl<'a> CommandPalette<'a> { let stamp = stamp_of(&state.query); let events = List::options(words.palette_list.clone()) .id(list_id) + .arrows_wrap(true) .empty(words.palette_empty.clone()) .selection(Bind::bound(&mut state.selection)) .rows_stamp(stamp)