Allow grid group headers to opt into keyboard selection

This commit is contained in:
Dylan M. Taylor committed 2026-11-20 17:18:26 -05:00
1 parent d7d2ba44e3
commit eecaef6de0
6 files changed
+61 -10

No files matched your search

+8 -3
View File
@@ -59,13 +59,18 @@ pub trait DisplayOrder<K> {
}
/// 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 {
@@ -41,7 +41,11 @@ impl<C: Collection> 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))
}
@@ -23,7 +23,9 @@ impl<'s, 'r, C: Collection> ItemsHooks<'s, 'r, C> {
}
pub(super) fn range(&self, from: usize, to: usize) -> Vec<C::Key> {
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);
+6 -1
View File
@@ -11,6 +11,7 @@ use std::hash::{Hash, Hasher};
struct Header<K> {
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<K>, groups: &[TileGroup<K>], tail: &[TailRow<K>], 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<K: Clone + PartialEq> DisplayOrder<K> 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())
}
+12 -2
View File
@@ -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<K> {
pub depth: usize,
/// The glyph before the header's title (a folder).
pub icon: Option<IconId>,
/// Whether the header represents a selectable item.
pub selectable: bool,
}
impl<K> TileGroup<K> {
/// 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<String>) -> 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.
+26 -1
View File
@@ -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()));