Skip unavailable rows in collection type-ahead

This commit is contained in:
dotCooCoo committed 2026-11-26 08:15:17 -08:00
1 parent ea14416aaf
commit e4fd39a535
3 files changed
+49 -4

No files matched your search

@@ -283,7 +283,7 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
}
}
/// Typed text moves the lead to the next row whose label starts with it.
/// Typed text moves the lead to the next enabled, shown item whose label starts with it.
pub(super) fn type_ahead(&mut self, comp: &C, cx: &mut Cx<'_, C>, layout: &C::ItemLayout, lead_at: Option<usize>) {
let (ctx, id, len) = (cx.core.ctx.clone(), cx.core.id, self.source.len());
let folder = cx.core.env.folder();
@@ -296,7 +296,14 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
}
cx.core.memory.session::<Typing>(sub(id, "typing")).0 = typing;
let source = &*self.source;
if let Some(to) = TypeAhead::pick(&prefix, lead_at, len, |at| source.text(at).unwrap_or_default(), |text| folder.key(text)) {
let label = |at| {
if source.enabled(at) && !source.is_section(at) && layout.shown(at) {
source.text(at).unwrap_or_default()
} else {
Default::default()
}
};
if let Some(to) = TypeAhead::pick(&prefix, lead_at, len, label, |text| folder.key(text)) {
self.move_lead(to, LeadSource::Keys);
self.pick_by(comp, to, false, false, PickBy::Keys);
self.reveal_with(layout, to);
+15 -1
View File
@@ -1,7 +1,7 @@
//! The collection's keys: Space actions, ragged-grid moves, hidden items, digits against type-ahead, the header
//! flag, the lead's source and the first sort order.
use super::kit::{Count, Dismissed, ROW_H, Row};
use super::kit::{Count, Dismissed, ROW_H, Row, Words};
use craft_behavior::{ItemLayout, ItemSource, NavShape, ScrollPos, Selection};
use craft_ui::contract::{
Collection, CollectionEvent, CollectionProps, CollectionResponse, Common, Component, ContainerProps, Cx, Focus as _, LeadSource, Props as _,
@@ -208,6 +208,20 @@ fn end_skips_the_items_of_a_closed_group() {
assert!(rows.iter().all(|(key, rect)| *key < 5 && rect.is_finite()), "only shown items are built: {rows:?}");
}
#[trial]
fn type_ahead_wraps_past_a_hidden_item_and_a_matching_section_heading() {
let (seen, layout) = (Seen::default(), Ragged { rows: vec![3, 1], hidden_from: 3 });
let mut source = Words::flat(&["Banner", "Beta", "Current", "Blocked"]);
source.sections.push(0);
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| show_probe(ui, CollectionProps::new().select(SelectMode::Single).type_ahead(true), &layout, &source, &seen);
frames.idle(3, &mut show);
focus(&mut frames, &seen, &mut show);
press(&mut frames, Key::End, &mut show);
frames.frame(vec![input::text("b")], &mut show);
assert_eq!(seen.lead_keys(), vec![2, 1], "type-ahead wraps past the hidden row and the heading to the shown match");
}
#[trial]
fn unused_moves_reach_the_regions_collection_in_either_declaration_order() {
use craft_ui::contract::{Show as _, ShowContainer as _};
@@ -1,10 +1,11 @@
//! A Select steps over its stepped entries only. A value on an entry that is not stepped has no place among them: it
//! offers both steps, and a step forward lands on the first stop and a step back on the last.
use craft_ui::buttons::Button;
use craft_ui::contract::{Bind, Entry, Role, Show as _};
use craft_ui::list::ThumbAspect;
use craft_ui::pickers::Select;
use craft_ui::testing::access::Declared;
use craft_ui::testing::access::{Declared, focused_key};
use craft_ui::testing::{Fixture, Story, StoryData, StoryOutput, trial};
use egui::{Key, Modifiers, Rect, Ui};
use std::cell::RefCell;
@@ -39,6 +40,29 @@ fn a_select_steps_over_its_stepped_entries_and_from_an_unstepped_one_to_an_end()
assert_eq!(step(4, Key::ArrowUp), (3, (true, true)), "from an unstepped entry, back lands on the last stop");
}
#[trial]
fn tab_in_a_select_keeps_the_enabled_match_reached_by_type_ahead() {
let story = Story::new("select_skips_disabled_option", |ui, data| {
let entries = [Entry::new(1u8, "Current"), Entry::new(2u8, "Blocked").enabled(false), Entry::new(3u8, "Better")];
let out = Select::new("Pick", data.bind("value", 1u8), &entries).show(ui);
Button::new("Next").show(ui);
out.into()
});
let mut fixture = Fixture::story(&story);
fixture.settle();
fixture.key(Key::Tab, Modifiers::NONE).settle();
fixture.key(Key::ArrowDown, Modifiers::ALT).settle();
assert!(!fixture.by_role(Role::LIST_BOX).is_empty(), "Alt+Down opens the list before typing");
fixture.type_text("b").settle();
let preview = *fixture.data().value("value", 1u8);
fixture.key(Key::Tab, Modifiers::NONE).settle();
assert!(fixture.by_role(Role::LIST_BOX).is_empty(), "Tab closes the list");
let next = fixture.by_label("Next").expect("the next control");
assert_eq!(focused_key(fixture.tree()), next.node, "Tab moves focus to the next control");
assert_eq!(*fixture.data().value("value", 1u8), 3, "Tab keeps the enabled match");
assert_eq!(preview, 3, "typing already previewed that enabled match");
}
#[trial]
fn the_closed_face_shows_an_entrys_closed_label_and_the_accessible_value_keeps_its_label() {
let mut fixture = Fixture::new(|ui| {