diff --git a/crates/behavior/src/item_source.rs b/crates/behavior/src/item_source.rs index 9908e16f6..25e280ba6 100644 --- a/crates/behavior/src/item_source.rs +++ b/crates/behavior/src/item_source.rs @@ -59,13 +59,18 @@ pub trait DisplayOrder { } /// Whether section `at` is a stop of navigation: it holds the lead, takes Enter, the context-menu key and a - /// press, and opens or closes with Forward and Back when it has children. A move stops on it, and it is never - /// selected, extended over, toggled, marqueed, dragged, renamed or reached by type-ahead. Only meaningful for a - /// section. + /// press, and opens or closes with Forward and Back when it has children. A move stops on it. By default it is + /// not selected or extended over; see `selectable_section`. It is not toggled, marqueed, dragged, renamed or + /// reached by type-ahead. Only meaningful for a section. fn section_stop(&self, _at: usize) -> bool { false } + /// Whether a section stop also represents an item that keyboard selection can pick or extend over. + fn selectable_section(&self, _at: usize) -> bool { + false + } + /// How many section stops (`section_stop`) come before entry `at`. They are not members of the set of items, so /// the positions an item reports to assistive technology do not count them. fn section_stops_before(&self, _at: usize) -> usize { diff --git a/crates/ui-contract/src/contract/items/keys.rs b/crates/ui-contract/src/contract/items/keys.rs index dd82030db..855e506ee 100644 --- a/crates/ui-contract/src/contract/items/keys.rs +++ b/crates/ui-contract/src/contract/items/keys.rs @@ -41,7 +41,11 @@ impl ItemsHooks<'_, '_, C> { let outline = Outline::new(&*self.source).columns(layout.column_count()).layout(layout); let step = match nav::step(layout.nav(), &outline, &mut state, mv, mods) { // The ends of the outline, and the first move into it, land on an item, not on a section stop. - NavStep::Moved(to) if self.source.section_stop(to) && (lead.is_none() || matches!(mv, NavMove::Start | NavMove::End)) => { + NavStep::Moved(to) + if self.source.section_stop(to) + && !self.source.selectable_section(to) + && (lead.is_none() || matches!(mv, NavMove::Start | NavMove::End)) => + { let forward = !matches!(mv, NavMove::End | NavMove::Back | NavMove::Up | NavMove::PageUp); NavStep::Moved(self.off_section(layout, to, forward)) } diff --git a/crates/ui-contract/src/contract/items/lead.rs b/crates/ui-contract/src/contract/items/lead.rs index 71318e24e..cc2161c14 100644 --- a/crates/ui-contract/src/contract/items/lead.rs +++ b/crates/ui-contract/src/contract/items/lead.rs @@ -23,7 +23,9 @@ impl<'s, 'r, C: Collection> ItemsHooks<'s, 'r, C> { } pub(super) fn range(&self, from: usize, to: usize) -> Vec { - let keys = (from.min(to)..=from.max(to)).filter(|&at| self.source.enabled(at) && !self.source.is_section(at)).map(|at| self.source.key(at)); + let keys = (from.min(to)..=from.max(to)) + .filter(|&at| self.source.enabled(at) && (!self.source.is_section(at) || self.source.selectable_section(at))) + .map(|at| self.source.key(at)); if from <= to { keys.collect() } else { keys.rev().collect() } } @@ -81,7 +83,7 @@ impl<'s, 'r, C: Collection> ItemsHooks<'s, 'r, C> { /// A press or key that selects row `at` the way the modifiers say. pub(super) fn pick(&mut self, comp: &C, at: usize, extend: bool, toggle: bool) { - if self.source.section_stop(at) { + if self.source.section_stop(at) && !self.source.selectable_section(at) { return; } let key = self.source.key(at); diff --git a/crates/ui-widgets/src/tile_grid/headers.rs b/crates/ui-widgets/src/tile_grid/headers.rs index 6f0ad957a..aae4f74ac 100644 --- a/crates/ui-widgets/src/tile_grid/headers.rs +++ b/crates/ui-widgets/src/tile_grid/headers.rs @@ -11,6 +11,7 @@ use std::hash::{Hash, Hasher}; struct Header { key: K, open: bool, + selectable: bool, } /// The tiles of `inner` with the header of every group just before the group's first tile, then the tail rows. Header @@ -28,7 +29,7 @@ pub(super) struct Outline<'s, K> { impl<'s, K: Clone> Outline<'s, K> { pub(super) fn new(inner: &'s dyn ItemSource, groups: &[TileGroup], tail: &[TailRow], collapsible: bool) -> Self { let spans = group_spans(groups, inner.len()); - let headers = groups.iter().map(|group| Header { key: group.key.clone(), open: group.open }).collect(); + let headers = groups.iter().map(|group| Header { key: group.key.clone(), open: group.open, selectable: group.selectable }).collect(); let tail = tail.iter().map(|row| row.key.clone()).collect(); Outline { inner, headers, starts: spans.iter().map(|span| span.start).collect(), tail, collapsible } } @@ -97,6 +98,10 @@ impl DisplayOrder for Outline<'_, K> { self.is_section(at) } + fn selectable_section(&self, at: usize) -> bool { + matches!(self.slot(at), Slot::Header(group) if self.headers[group].selectable) + } + fn section_stops_before(&self, at: usize) -> usize { at.checked_sub(1).map_or(0, |before| headers_through(&self.starts, before)) + at.saturating_sub(self.tail_from()) } diff --git a/crates/ui-widgets/src/tile_grid/layout.rs b/crates/ui-widgets/src/tile_grid/layout.rs index bd599b963..c0a202ef5 100644 --- a/crates/ui-widgets/src/tile_grid/layout.rs +++ b/crates/ui-widgets/src/tile_grid/layout.rs @@ -13,7 +13,8 @@ use std::sync::Arc; /// given. /// /// The header is a row of the grid's outline, as a tree's group row is: it holds the keyboard lead, opens and closes -/// with Left and Right, reports Enter and the context-menu key, and is never selected. Its `key` names that row, so +/// with Left and Right and reports Enter and the context-menu key. It is selected by keyboard only when opted in with +/// `selectable`. Its `key` names that row, so /// it differs from the key of every tile. Positions in the grid's events (`CollectionEvent::Reorder`, /// `Dropped`, the `accepts` test) count the headers as rows: the header of a group sits just before its first /// tile. @@ -36,12 +37,21 @@ pub struct TileGroup { pub depth: usize, /// The glyph before the header's title (a folder). pub icon: Option, + /// Whether the header represents a selectable item. + pub selectable: bool, } impl TileGroup { /// An open group of `count` tiles from tile position `start`, titled `title`, whose header row is keyed `key`. pub fn new(key: K, start: usize, count: usize, title: impl Into) -> Self { - TileGroup { key, start, count, title: title.into(), detail: String::new(), open: true, depth: 0, icon: None } + TileGroup { key, start, count, title: title.into(), detail: String::new(), open: true, depth: 0, icon: None, selectable: false } + } + + /// Lets the header be selected when it represents an item as well as a group. + #[must_use] + pub fn selectable(mut self) -> Self { + self.selectable = true; + self } /// The detail at the header's trailing end. diff --git a/crates/ui-widgets/tests/it/tile_grid/keys.rs b/crates/ui-widgets/tests/it/tile_grid/keys.rs index 41474acee..5b86ad9db 100644 --- a/crates/ui-widgets/tests/it/tile_grid/keys.rs +++ b/crates/ui-widgets/tests/it/tile_grid/keys.rs @@ -1,6 +1,6 @@ //! The tile grid's own keys and gestures: Bridge's rating and zoom keys, and the slow click that renames. -use craft_behavior::{EditEvent, keyed}; +use craft_behavior::{EditEvent, Selection, keyed}; use craft_ui::contract::{Bind, CollectionEvent, ShowCollection as _}; use craft_ui::testing::{Fixture, pointer, trial}; use craft_ui::tile_grid::{GridEvent, GridZoom, RatingKeys, Tile, TileGrid, TileGroup, TileZoom}; @@ -10,6 +10,31 @@ use std::rc::Rc; const NAMES: [&str; 4] = ["Tile 1", "Tile 2", "Tile 3", "Tile 4"]; +#[trial] +fn keyboard_selects_a_selectable_group_header() { + let selected = Rc::new(RefCell::new(Selection::single(1_usize))); + let held = selected.clone(); + let mut fixture = Fixture::new(move |ui| { + let keys = [1_usize, 2]; + let source = keyed(&keys, |key: &usize| *key); + let groups = [TileGroup::new(0, 0, 2, "Folder").selectable()]; + let mut selection = held.borrow_mut(); + let _ = + TileGrid::new("Library") + .groups(&groups) + .collapsible() + .selection(Bind::bound(&mut *selection)) + .show_items(ui, &source, &mut |_, _| Tile::new("Tile")); + }); + fixture.room(Vec2::new(600.0, 400.0)).settle(); + fixture.key(Key::Tab, Modifiers::NONE).idle(2); + fixture.key(Key::Home, Modifiers::NONE).idle(2); + fixture.key(Key::ArrowUp, Modifiers::NONE).idle(2); + assert_eq!(selected.borrow().items, [0], "the header is selected when the lead reaches it"); + fixture.key(Key::ArrowDown, Modifiers::SHIFT).idle(2); + assert_eq!(selected.borrow().items, [0, 1], "a range includes the selectable header"); +} + #[trial] fn keyboard_lead_on_a_group_header_is_reported() { let seen = Rc::new(RefCell::new(Vec::new()));