Files
craft-libs/crates/ui-contract/tests/it/collection/rows.rs
T
aiongg e872978edc Overview first show runs only the cards in view
A Lazy child no pass showed yet takes the estimate its app gives
(`Lazy::new(key, estimate)`): that long along the axes its scroll area
scrolls, nothing across them, as a collection places rows it never built
by its item layout's estimate. The never-shown "always run" branch of
`lazy::decide` is gone. Gallery cards estimate their height from their
story rows.

A scroll position the framework holds is also named by content: the
first child at or after the view's start (`lazy::Anchor`, kept with the
offset in `ScrollState` and a collection's `Scrolled`). When extents
before it change, the offset follows the child and the pass runs again,
so the content in view does not move. Bound offsets keep no anchor.

Entries of the out-of-view store record their scroll area. A frame drops
only the entries an area that ran did not reach, so a hidden area (a tab
switched away) keeps its children's extents.

Trials: first show, shown again after a hide, content in view stays when
an estimate above it resolves, collection rows stay when a row above them
changes height; the gallery trial asserts each level first runs the cards
in view.
2026-11-12 14:32:38 +00:00

441 lines
17 KiB
Rust

//! The row trials: rows with focusable children, cell selection in a sheet, rows of measured heights and a lead that
//! leaves the source.
use super::kit::{Count, Dismissed, List, ROW_H, RowOut};
use craft_behavior::{
CellAt, CellRange, CellSelection, ColumnKey, ItemSource, PartKey, Selection, SelectionModel as _, Sense, SheetLayout, SpanLayout, keyed,
};
use craft_ui::contract::{
Bind, BindMut, Collection, CollectionEvent, CollectionProps, CollectionResponse, Common, Component, ContainerProps, Cx, Focus as _, MeasureCx,
PartsCx, Props as _, Role, SelectMode, ShowCollection as _,
};
use craft_ui::runtime::focus::{FocusTarget, request};
use craft_ui::style::StyleCx;
use craft_ui::testing::layout::in_region;
use craft_ui::testing::{Frames, input, pointer, trial};
use craft_ui_test_kit::contract::PlainStyle;
use egui::{Id, Key, Modifiers, Pos2, Rect, Ui, Vec2};
use std::cell::{Cell, RefCell};
/// Places that differ by less are the same place: the rounding of the layout.
const SAME: f32 = 0.5;
const ORIGIN: Pos2 = Pos2::new(10.0, 10.0);
const VIEW: Vec2 = Vec2::new(300.0, 200.0);
const CELL_W: f32 = 50.0;
const TALL: f32 = 3.0;
const CHILD_A: PartKey = PartKey::new("a");
const CHILD_B: PartKey = PartKey::new("b");
/// What the trials' rows and collections saw.
#[derive(Default)]
struct Log {
/// The row and part that held focus in the last pass that had one.
focus: RefCell<Vec<(usize, &'static str)>>,
rects: RefCell<Vec<(usize, Rect)>>,
events: RefCell<Vec<CollectionEvent<usize, Dismissed>>>,
id: Cell<Option<Id>>,
}
impl Log {
fn new() -> &'static Log {
Box::leak(Box::default())
}
fn rect(&self, key: usize) -> Rect {
self.rects.borrow().iter().rev().find(|(row, _)| *row == key).map(|(_, rect)| *rect).expect("the row ran")
}
fn leads(&self) -> Vec<usize> {
self.events.borrow().iter().filter_map(|event| if let CollectionEvent::LeadMoved { key, .. } = event { Some(*key) } else { None }).collect()
}
fn take(&self, out: CollectionResponse<usize, Dismissed>) {
self.id.set(Some(out.response.id));
self.events.borrow_mut().extend(out.events);
}
fn id(&self) -> Id {
self.id.get().expect("the list ran")
}
}
/// A row that records its rect and focus, with two focusable parts when it has children and a tall measure for
/// the key 1 of an auto-height list.
#[derive(craft_ui::contract::Component)]
struct ProbeRow {
#[common]
common: Common,
key: usize,
children: bool,
tall: bool,
log: &'static Log,
}
impl ProbeRow {
fn new(key: usize, log: &'static Log) -> Self {
ProbeRow { common: Common::new(), key, children: false, tall: false, log }
}
fn children(mut self) -> Self {
self.children = true;
self
}
fn tall_when(mut self, tall: bool) -> Self {
self.tall = tall;
self
}
}
impl Component for ProbeRow {
const ROLE: Role = Role::LIST_ITEM;
type Style = PlainStyle;
type Output = RowOut;
fn style(_: &StyleCx<'_>, _: ProbeRowStyleKey) -> PlainStyle {
PlainStyle::default()
}
fn measure(&mut self, _: &MeasureCx<'_, Self>) -> Vec2 {
Vec2::new(10.0, if self.tall { ROW_H * TALL } else { ROW_H })
}
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
if self.children {
let half = Vec2::new(rect.width() / 2.0, rect.height());
cx.part(CHILD_A, Rect::from_min_size(rect.min, half)).sense(Sense::CLICK).focusable();
cx.part(CHILD_B, Rect::from_min_size(rect.min + Vec2::new(half.x, 0.0), half)).sense(Sense::CLICK).focusable();
}
}
fn update(&mut self, cx: &mut Cx<'_, Self>) {
self.log.rects.borrow_mut().push((self.key, cx.rect));
if cx.is_focused() {
self.log.focus.borrow_mut().push((self.key, cx.active_part().map_or("row", |part| part.key.name())));
}
}
fn finish(self, cx: Cx<'_, Self>) -> RowOut {
RowOut { response: cx.response.clone(), event: None }
}
}
/// A list of `ProbeRow`s over `SpanLayout`, rows of the control height or of the heights they measure.
#[derive(craft_ui::contract::Component)]
#[component(collection)]
struct Probe<'a> {
#[common]
common: Common,
#[container]
props: ContainerProps,
items: CollectionProps,
selection: Option<Bind<'a, CellSelection<usize>>>,
auto: bool,
}
impl<'a> Probe<'a> {
fn new(select: SelectMode) -> Self {
Probe {
common: Common::new(),
props: ContainerProps::new().sizing(craft_ui::contract::Sizing::Fill),
items: CollectionProps::new().select(select),
selection: None,
auto: false,
}
}
fn auto(mut self) -> Self {
self.auto = true;
self
}
}
impl Component for Probe<'_> {
const ROLE: Role = Role::LIST;
type Style = PlainStyle;
type Output = CollectionResponse<usize, Dismissed>;
fn style(_: &StyleCx<'_>, _: ProbeStyleKey) -> PlainStyle {
PlainStyle::default()
}
fn finish(self, mut cx: Cx<'_, Self>) -> Self::Output {
cx.collection_response()
}
}
impl Collection for Probe<'_> {
type Key = usize;
type ItemLayout = SpanLayout;
type Selection = CellSelection<usize>;
type Item<'r> = ProbeRow;
type ItemEvent = Dismissed;
fn items(&self) -> &CollectionProps {
&self.items
}
fn selection(&mut self) -> Option<BindMut<'_, CellSelection<usize>>> {
self.selection.as_mut().map(Bind::reborrow)
}
fn item_layout(&self, source: &dyn ItemSource<usize>, width: f32, _: &StyleCx<'_>) -> SpanLayout {
if self.auto { SpanLayout::sized(source, width, ROW_H) } else { SpanLayout::new(source, width, ROW_H) }
}
}
/// A sheet of `CELL_W` columns over `SheetLayout`, with the first column frozen.
#[derive(craft_ui::contract::Component)]
#[component(collection)]
struct Sheet<'a> {
#[common]
common: Common,
#[container]
props: ContainerProps,
items: CollectionProps,
selection: Option<Bind<'a, CellSelection<usize>>>,
}
const COLUMNS: [&str; 6] = ["a", "b", "c", "d", "e", "f"];
impl<'a> Sheet<'a> {
fn new() -> Self {
Sheet {
common: Common::new(),
props: ContainerProps::new().sizing(craft_ui::contract::Sizing::Fill),
items: CollectionProps::new().select(SelectMode::Cells),
selection: None,
}
}
fn selection(mut self, selection: Bind<'a, CellSelection<usize>>) -> Self {
self.selection = Some(selection);
self
}
}
impl Component for Sheet<'_> {
const ROLE: Role = Role::LIST;
type Style = PlainStyle;
type Output = CollectionResponse<usize, Dismissed>;
fn style(_: &StyleCx<'_>, _: SheetStyleKey) -> PlainStyle {
PlainStyle::default()
}
fn finish(self, mut cx: Cx<'_, Self>) -> Self::Output {
cx.collection_response()
}
}
impl Collection for Sheet<'_> {
type Key = usize;
type ItemLayout = SheetLayout;
type Selection = CellSelection<usize>;
type Item<'r> = ProbeRow;
type ItemEvent = Dismissed;
fn items(&self) -> &CollectionProps {
&self.items
}
fn selection(&mut self) -> Option<BindMut<'_, CellSelection<usize>>> {
self.selection.as_mut().map(Bind::reborrow)
}
fn item_layout(&self, source: &dyn ItemSource<usize>, _: f32, _: &StyleCx<'_>) -> SheetLayout {
let columns = COLUMNS.iter().map(|key| (ColumnKey::new(key), CELL_W)).collect();
SheetLayout::new(source, ROW_H, columns).frozen(1)
}
}
fn press(frames: &mut Frames, key: Key, mods: Modifiers, show: &mut impl FnMut(&mut Ui)) {
frames.frame(vec![input::key_press(key, mods)], &mut *show);
frames.frame(vec![craft_ui::testing::focus::key_release(key, mods)], &mut *show);
}
/// A key press and release with the focus record cleared first, so it holds only what the key's frames saw.
fn tap(frames: &mut Frames, log: &Log, key: Key, show: &mut impl FnMut(&mut Ui)) {
log.focus.borrow_mut().clear();
press(frames, key, Modifiers::NONE, show);
}
fn focus_list(frames: &mut Frames, log: &Log, show: &mut impl FnMut(&mut Ui)) {
request(&frames.ctx, FocusTarget::Id(log.id()));
frames.idle(2, &mut *show);
}
/// 17.5: Forward enters a row's first child and moves on through them, Back returns to the row from the first,
/// Up, Down and Escape leave the children for the row, and the collection stays the one egui focus.
#[trial]
fn forward_enters_a_rows_children_and_arrows_and_escape_leave_them() {
let (log, source) = (Log::new(), Count(4));
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| {
in_region(ui, Rect::from_min_size(ORIGIN, VIEW), |ui| {
let list = Probe::new(SelectMode::Single).id_salt("list");
log.take(list.show_items(ui, &source, &mut |key, _| ProbeRow::new(*key, log).children()));
});
};
frames.idle(3, &mut show);
focus_list(&mut frames, log, &mut show);
let last_focus = || log.focus.borrow().last().copied();
tap(&mut frames, log, Key::ArrowDown, &mut show);
assert_eq!(log.leads().last(), Some(&0));
assert_eq!(last_focus(), None, "the lead row alone holds focus until Forward");
tap(&mut frames, log, Key::ArrowRight, &mut show);
assert_eq!(last_focus(), Some((0, "a")), "Forward enters the row's first child");
assert_eq!(craft_ui::testing::focus::egui_focused(&frames.ctx), Some(log.id()), "the children are members of the collection's arena");
tap(&mut frames, log, Key::ArrowRight, &mut show);
assert_eq!(last_focus(), Some((0, "b")));
tap(&mut frames, log, Key::ArrowLeft, &mut show);
assert_eq!(last_focus(), Some((0, "a")));
tap(&mut frames, log, Key::ArrowLeft, &mut show);
assert_eq!(last_focus(), None, "Back from the first child returns to the row");
assert_eq!(log.leads().last(), Some(&0));
tap(&mut frames, log, Key::ArrowRight, &mut show);
tap(&mut frames, log, Key::ArrowDown, &mut show);
assert_eq!((last_focus(), log.leads().last().copied()), (None, Some(1)), "Down leaves the children and moves the lead");
tap(&mut frames, log, Key::ArrowRight, &mut show);
assert_eq!(last_focus(), Some((1, "a")));
tap(&mut frames, log, Key::Escape, &mut show);
assert_eq!((last_focus(), log.leads().last().copied()), (None, Some(1)), "Escape returns to the row and keeps the lead");
}
/// 17.7: the arrows move the lead cell, Shift extends the range from the anchor cell, and the selection reports
/// the rows and the columns, with the active cell at the anchor. The lead stays with the active cell while the range
/// extends, and a plain arrow then steps from the active cell (Excel), here against the sheet's first column.
#[trial]
fn shift_arrows_extend_a_cell_selection_in_a_sheet() {
let (log, source) = (Log::new(), Count(5));
let selection = RefCell::new(CellSelection::<usize>::default());
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| {
let mut held = selection.borrow().clone();
in_region(ui, Rect::from_min_size(ORIGIN, VIEW), |ui| {
log.take(Sheet::new().selection(Bind::bound(&mut held)).id_salt("sheet").show_items(ui, &source, &mut |key, _| ProbeRow::new(*key, log)));
});
*selection.borrow_mut() = held;
};
frames.idle(3, &mut show);
focus_list(&mut frames, log, &mut show);
press(&mut frames, Key::ArrowDown, Modifiers::NONE, &mut show);
press(&mut frames, Key::ArrowRight, Modifiers::SHIFT, &mut show);
press(&mut frames, Key::ArrowRight, Modifiers::SHIFT, &mut show);
press(&mut frames, Key::ArrowDown, Modifiers::SHIFT, &mut show);
let held = selection.borrow().clone();
assert_eq!(held.ranges(), [CellRange::cells(CellAt::new(0, 0), CellAt::new(1, 2))], "the rows and columns between the anchor cell and the head");
assert_eq!((held.lead(), held.lead_column()), (Some(&0), Some(0)), "the active cell stays at the anchor");
press(&mut frames, Key::ArrowLeft, Modifiers::NONE, &mut show);
let held = selection.borrow().clone();
assert_eq!(
(held.ranges(), held.lead(), held.lead_column()),
([CellRange::cells(CellAt::new(0, 0), CellAt::new(0, 0))].as_slice(), Some(&0), Some(0)),
"a plain arrow after an extend steps from the active cell, and the lead stays with it"
);
}
/// 17.3, I1: rows of different measured heights place one under the other and a press finds the row under it.
#[trial]
fn rows_of_different_measured_heights_place_and_hit_correctly() {
let (log, source) = (Log::new(), Count(6));
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| {
in_region(ui, Rect::from_min_size(ORIGIN, VIEW), |ui| {
let list = Probe::new(SelectMode::Single).auto().id_salt("list");
log.take(list.show_items(ui, &source, &mut |key, _| ProbeRow::new(*key, log).tall_when(*key == 1)));
});
};
frames.idle(4, &mut show);
assert_eq!(log.rect(1).height(), ROW_H * TALL, "an auto row takes the height it measures");
assert_eq!((log.rect(0).height(), log.rect(2).min.y), (ROW_H, log.rect(1).max.y), "the rows after it move down");
let inside = |key: usize, fraction: f32| log.rect(key).min + Vec2::new(5.0, log.rect(key).height() * fraction);
pointer::click(&mut frames, inside(1, 0.9), &mut show);
assert_eq!(log.leads().last(), Some(&1), "the lower part of the tall row is the tall row");
pointer::click(&mut frames, inside(2, 0.5), &mut show);
assert_eq!(log.leads().last(), Some(&2));
}
/// 17.3 step 9: when the lead's key leaves the source, the lead moves to the key now in its place and a selection
/// that held the old lead moves with it.
#[trial]
fn a_selected_lead_that_leaves_the_source_moves_the_selection_to_the_new_lead() {
let rows = RefCell::new(vec![(10_usize, "a"), (11, "b"), (12, "c"), (13, "d")]);
let selection = RefCell::new(Selection::<usize>::default());
let seen = Cell::new(None);
let events = RefCell::new(Vec::new());
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| {
let mut held = selection.borrow().clone();
let rows = rows.borrow();
let source = keyed(&rows, |row| row.0);
in_region(ui, Rect::from_min_size(ORIGIN, VIEW), |ui| {
let out = List::new(SelectMode::Single)
.selection(Bind::bound(&mut held))
.id_salt("list")
.show_items(ui, &source, &mut |_, _| super::kit::Row::new());
seen.set(Some(out.response.id));
events.borrow_mut().extend(out.events);
});
*selection.borrow_mut() = held;
};
frames.idle(3, &mut show);
pointer::click(&mut frames, ORIGIN + Vec2::new(5.0, ROW_H * 2.5), &mut show);
assert_eq!(selection.borrow().items, vec![12]);
rows.borrow_mut().remove(2);
frames.idle(2, &mut show);
assert_eq!(selection.borrow().items, vec![13], "the selection follows the lead to the key now in its place");
let leads: Vec<usize> =
events.borrow().iter().filter_map(|event| if let CollectionEvent::LeadMoved { key, .. } = event { Some(*key) } else { None }).collect();
assert_eq!(leads.last(), Some(&13));
}
/// 17.3, I1: the lead row above the view grows (an auto row it measures taller): the rows in view stay where they
/// were, in the frame it grows, and the frame after runs no extra pass. Guards the collection's scroll anchor: its
/// position follows the first row at or after the view's start.
#[trial]
fn rows_in_view_stay_when_a_row_above_them_changes_height() {
let (log, source) = (Log::new(), Count(60));
let tall = Cell::new(None);
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| {
in_region(ui, Rect::from_min_size(ORIGIN, VIEW), |ui| {
let list = Probe::new(SelectMode::Single).auto().id_salt("list");
log.take(list.show_items(ui, &source, &mut |key, _| ProbeRow::new(*key, log).tall_when(tall.get() == Some(*key))));
});
};
frames.idle(3, &mut show);
pointer::click(&mut frames, ORIGIN + Vec2::new(5.0, ROW_H * 2.5), &mut show);
assert_eq!(log.leads().last(), Some(&2), "the press made row 2 the lead");
let over = ORIGIN + VIEW / 2.0;
for _ in 0..4 {
frames.frame(vec![pointer::moved(over)], &mut show);
frames.frame(vec![pointer::wheel(-VIEW.y)], &mut show);
}
frames.idle(2, &mut show);
let view = Rect::from_min_size(ORIGIN, VIEW);
let in_view =
|rects: &[(usize, Rect)]| -> Vec<(usize, Rect)> { rects.iter().copied().filter(|(key, rect)| *key != 2 && rect.intersects(view)).collect() };
log.rects.borrow_mut().clear();
let idle = frames.frame(vec![], &mut show);
let before = in_view(&log.rects.borrow());
assert!(log.rect(2).max.y < view.min.y, "the lead row is above the view");
assert!(!before.is_empty(), "rows show in the view");
tall.set(Some(2));
log.rects.borrow_mut().clear();
let grown = frames.frame(vec![], &mut show);
assert!(log.rect(2).height() > ROW_H, "the lead row grew");
let after = in_view(&log.rects.borrow());
for (key, rect) in &before {
let now =
after.iter().rev().find(|(shown, _)| shown == key).map(|(_, rect)| *rect).unwrap_or_else(|| panic!("row {key} in view was built again"));
assert!((now.min - rect.min).length() <= SAME, "row {key} in view stayed: {now:?}, then {rect:?}");
}
let next = frames.frame(vec![], &mut show);
assert!(grown > idle, "the frame the row grew in ran the pass again at the anchored offset");
assert_eq!(next, idle, "an idle frame after it runs no extra pass");
}