Files
craft-libs/crates/ui-contract/tests/it/collection/middle_scroll.rs
T
Dylan M. Taylor b138b533fe Merge remote-tracking branch 'upstream/next' into pr-2809-merge
# Conflicts:
#	crates/automation/Cargo.toml
#	crates/automation/src/access.rs
#	crates/automation/src/synthetic.rs
#	crates/automation/tests/it/access.rs
#	crates/automation/tests/it/synthetic.rs
#	crates/behavior/src/codec.rs
#	crates/behavior/src/edit_history.rs
#	crates/behavior/src/lib.rs
#	crates/behavior/src/wheel.rs
#	crates/color-ui/src/ui/paint_chip.rs
#	crates/color-ui/src/ui/pair.rs
#	crates/color-ui/src/ui/palette.rs
#	crates/color-ui/src/ui/palette/stories.rs
#	crates/dialogs/src/service.rs
#	crates/draw/src/ui/transform/transform_box.rs
#	crates/draw/src/ui/transform/transform_box/stories.rs
#	crates/draw/tests/it/transform_box.rs
#	crates/font-manager/src/glyphs.rs
#	crates/font-manager/src/picker.rs
#	crates/font-manager/src/picker/rank.rs
#	crates/font-manager/src/picker/rows.rs
#	crates/font-manager/src/picker/stories.rs
#	crates/font-manager/tests/it/picker.rs
#	crates/host/src/lib.rs
#	crates/menu/src/context.rs
#	crates/menu/src/mac.rs
#	crates/menu/src/model.rs
#	crates/pages/src/ui/view.rs
#	crates/platform/README.md
#	crates/platform/src/browser_files.rs
#	crates/print/src/ui/dialog.rs
#	crates/print/src/ui/mod.rs
#	crates/print/src/ui/panel.rs
#	crates/print/src/ui/stories.rs
#	crates/print/tests/it/dialog.rs
#	crates/text/src/ui/character.rs
#	crates/text/src/ui/character/rows.rs
#	crates/text/src/ui/editor.rs
#	crates/text/tests/it/editor.rs
#	crates/text/tests/it/main.rs
#	crates/ui-canvas/src/canvas/mod.rs
#	crates/ui-canvas/src/canvas/split/stories.rs
#	crates/ui-canvas/src/canvas/tool_host.rs
#	crates/ui-canvas/src/canvas_controls/outline/mod.rs
#	crates/ui-canvas/src/canvas_controls/task_bar.rs
#	crates/ui-canvas/src/canvas_controls/task_bar/stories.rs
#	crates/ui-canvas/tests/it/canvas/navigate.rs
#	crates/ui-canvas/tests/it/canvas/tool_keys.rs
#	crates/ui-canvas/tests/it/canvas_controls/task_bar.rs
#	crates/ui-contract/src/contract/collection/props.rs
#	crates/ui-contract/src/contract/cx/pointer.rs
#	crates/ui-contract/src/contract/items/access.rs
#	crates/ui-contract/src/contract/items/drag.rs
#	crates/ui-contract/src/contract/items/lead.rs
#	crates/ui-contract/src/contract/label.rs
#	crates/ui-contract/src/contract/mod.rs
#	crates/ui-contract/src/contract/run.rs
#	crates/ui-contract/src/contract/steps/report.rs
#	crates/ui-contract/src/contract/steps/update.rs
#	crates/ui-contract/src/primitives/mod.rs
#	crates/ui-contract/src/primitives/sensing.rs
#	crates/ui-contract/src/primitives/text_adapter.rs
#	crates/ui-contract/src/primitives/text_input.rs
#	crates/ui-contract/src/runtime/access/emit.rs
#	crates/ui-contract/src/runtime/access/text.rs
#	crates/ui-contract/src/runtime/commands/board.rs
#	crates/ui-contract/src/runtime/commands/mod.rs
#	crates/ui-contract/src/runtime/drag.rs
#	crates/ui-contract/src/runtime/egui/access.rs
#	crates/ui-contract/src/runtime/egui/access/tree.rs
#	crates/ui-contract/src/runtime/egui/input.rs
#	crates/ui-contract/src/runtime/env.rs
#	crates/ui-contract/src/runtime/focus/nav.rs
#	crates/ui-contract/src/runtime/frame.rs
#	crates/ui-contract/src/runtime/keys.rs
#	crates/ui-contract/src/runtime/keys/claims.rs
#	crates/ui-contract/src/runtime/keys/input.rs
#	crates/ui-contract/src/runtime/keys/route.rs
#	crates/ui-contract/src/runtime/pointer.rs
#	crates/ui-contract/src/runtime/windows.rs
#	crates/ui-contract/src/testing/fixture.rs
#	crates/ui-contract/src/testing/host_key_trials.rs
#	crates/ui-contract/tests/it/collection/rows.rs
#	crates/ui-contract/tests/it/drag/mod.rs
#	crates/ui-contract/tests/it/runtime/wheel.rs
#	crates/ui-macros/src/props.rs
#	crates/ui-panels/src/dialog/mod.rs
#	crates/ui-panels/src/dialog/prompts.rs
#	crates/ui-panels/src/dialog/recovery.rs
#	crates/ui-panels/src/dialog/trials.rs
#	crates/ui-panels/src/dialog/words.rs
#	crates/ui-panels/src/rails/drawer.rs
#	crates/ui-panels/src/rails/drawer/stories.rs
#	crates/ui-panels/src/rails/tool/trials.rs
#	crates/ui-panels/src/sections/footer.rs
#	crates/ui-panels/tests/it/rails/drawer.rs
#	crates/ui-patterns/src/patterns/dock/area.rs
#	crates/ui-patterns/src/patterns/form_dialogs/about.rs
#	crates/ui-patterns/src/patterns/form_dialogs/dialog.rs
#	crates/ui-patterns/src/patterns/form_dialogs/dialog/stories.rs
#	crates/ui-patterns/src/patterns/form_dialogs/host_requests.rs
#	crates/ui-patterns/src/patterns/form_dialogs/mod.rs
#	crates/ui-patterns/src/patterns/form_dialogs/param.rs
#	crates/ui-patterns/src/patterns/form_dialogs/properties.rs
#	crates/ui-patterns/src/patterns/form_dialogs/properties/stories.rs
#	crates/ui-patterns/src/patterns/form_dialogs/rows.rs
#	crates/ui-patterns/src/patterns/form_dialogs/rows/stories.rs
#	crates/ui-patterns/src/patterns/panels/history.rs
#	crates/ui-patterns/src/patterns/preferences/document/model.rs
#	crates/ui-patterns/src/patterns/preferences/model.rs
#	crates/ui-patterns/src/patterns/preferences/page.rs
#	crates/ui-patterns/src/patterns/preferences/rows.rs
#	crates/ui-patterns/src/patterns/preferences/window.rs
#	crates/ui-patterns/src/patterns/preferences/window/stories.rs
#	crates/ui-patterns/src/patterns/presets/browser.rs
#	crates/ui-patterns/tests/it/docking/group_view.rs
#	crates/ui-patterns/tests/it/form_dialogs/dialog.rs
#	crates/ui-patterns/tests/it/form_dialogs/params.rs
#	crates/ui-patterns/tests/it/preferences/model.rs
#	crates/ui-shell/src/shell/start.rs
#	crates/ui-shell/src/shell/start/stories.rs
#	crates/ui-widgets/src/buttons/icon_button.rs
#	crates/ui-widgets/src/buttons/mod.rs
#	crates/ui-widgets/src/buttons/stories.rs
#	crates/ui-widgets/src/choice/swatch.rs
#	crates/ui-widgets/src/choice/swatch_table.rs
#	crates/ui-widgets/src/display/text.rs
#	crates/ui-widgets/src/form/slider_field.rs
#	crates/ui-widgets/src/layout_components/mod.rs
#	crates/ui-widgets/src/list/mod.rs
#	crates/ui-widgets/src/list/row/paint.rs
#	crates/ui-widgets/src/list/row/show.rs
#	crates/ui-widgets/src/list/stories.rs
#	crates/ui-widgets/src/list/view.rs
#	crates/ui-widgets/src/list/view_stories.rs
#	crates/ui-widgets/src/menu/mod.rs
#	crates/ui-widgets/src/menu/stories.rs
#	crates/ui-widgets/src/pickers/select.rs
#	crates/ui-widgets/src/tabs/document.rs
#	crates/ui-widgets/src/text/number.rs
#	crates/ui-widgets/src/text/zoom_field.rs
#	crates/ui-widgets/src/tile_grid/grid.rs
#	crates/ui-widgets/src/tile_grid/grid/stories.rs
#	crates/ui-widgets/src/tile_grid/mod.rs
#	crates/ui-widgets/tests/it/label/tooltip.rs
#	crates/ui-widgets/tests/it/popover/popup.rs
#	crates/ui-widgets/tests/it/tabs.rs
#	crates/ui-widgets/tests/it/text/mod.rs
#	crates/ui-widgets/tests/it/text/number.rs
#	crates/ui-widgets/tests/it/tile_grid/grid.rs
#	crates/window/Cargo.toml
#	docs/components-index.md
#	docs/porting.md
2026-11-23 19:01:32 -05:00

164 lines
7.0 KiB
Rust

//! Middle-button auto-scroll on a collection's rows: the latch the items step runs under `MiddleButton::AutoScroll`.
use super::kit::{Count, Dismissed, ROW_H, Row};
use super::table::Table;
use craft_ui::contract::{CollectionEvent, CollectionProps, MiddleButton, Props as _, SelectMode, ShowCollection as _};
use craft_ui::testing::layout::in_region;
use craft_ui::testing::{Frames, pointer, trial};
use egui::{Pos2, Rect, Ui, Vec2};
use std::cell::{Cell, RefCell};
const ORIGIN: Pos2 = Pos2::new(10.0, 10.0);
const VIEW: Vec2 = Vec2::new(300.0, 200.0);
const ROWS: usize = 200;
const SAME: f32 = 0.5;
/// How far below the latch the pointer pulls: past the 15 pt dead zone, still over the rows.
const PULL: f32 = 80.0;
/// A scrolling list of `ROWS` rows with the middle button `button`, what it reported, and a switch that reruns the
/// next pass.
struct Rig {
button: MiddleButton,
source: Count,
rect: Cell<Rect>,
/// The first row the last pass built, and its top.
first: Cell<Option<(usize, f32)>>,
events: RefCell<Vec<CollectionEvent<usize, Dismissed>>>,
discard: Cell<bool>,
}
impl Rig {
fn new(button: MiddleButton) -> Self {
Rig {
button,
source: Count(ROWS),
rect: Cell::new(Rect::NOTHING),
first: Cell::new(None),
events: RefCell::new(Vec::new()),
discard: Cell::new(false),
}
}
fn show(&self, ui: &mut Ui) {
self.first.set(None);
let items = CollectionProps::new().select(SelectMode::Single).middle_button(self.button);
in_region(ui, Rect::from_min_size(ORIGIN, VIEW), |ui| {
let out = Table::new(items).id_salt("rows").show_items(ui, &self.source, &mut |key, cx| {
if self.first.get().is_none_or(|(first, _)| *key < first) {
self.first.set(Some((*key, cx.rect.top())));
}
Row::new()
});
self.rect.set(out.response.rect);
self.events.borrow_mut().extend(out.events);
});
if self.discard.take() {
ui.ctx().request_discard("the trial reruns the pass");
}
}
/// The rows' scroll offset, read from where the first built row lies: the rows are `ROW_H` apart.
fn offset(&self) -> f32 {
let (key, top) = self.first.get().expect("the list built a row");
key as f32 * ROW_H + self.rect.get().top() - top
}
/// The reports of a row press: a pick, a lead move or an activation.
fn row_presses(&self) -> usize {
self.events
.borrow()
.iter()
.filter(|event| matches!(event, CollectionEvent::Selection { .. } | CollectionEvent::LeadMoved { .. } | CollectionEvent::Activate(_)))
.count()
}
}
/// A middle click at `at`: hover, press and release in three frames.
fn middle_click(frames: &mut Frames, at: Pos2, show: &mut impl FnMut(&mut Ui)) {
frames.frame(vec![pointer::moved(at)], &mut *show);
frames.frame(vec![pointer::middle(at, true)], &mut *show);
frames.frame(vec![pointer::middle(at, false)], &mut *show);
}
#[trial]
fn one_frame_middle_click_latches_at_the_press_and_the_stopping_click_does_not_pick() {
let rig = Rig::new(MiddleButton::AutoScroll);
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| rig.show(ui);
frames.idle(2, &mut show);
let at = rig.rect.get().center();
let below = at + Vec2::new(0.0, PULL);
frames.frame(vec![pointer::moved(at), pointer::middle(at, true), pointer::moved(below), pointer::middle(below, false)], &mut show);
let before = rig.offset();
frames.idle(10, &mut show);
assert!(rig.offset() > before + SAME, "the latch uses the press point");
let picks = rig.row_presses();
frames.frame(vec![pointer::press(below)], &mut show);
frames.frame(vec![pointer::release(below)], &mut show);
frames.idle(3, &mut show);
assert_eq!(rig.row_presses(), picks, "the click ending the latch does not select a row");
}
#[trial]
fn a_latch_does_not_resume_when_the_pointer_leaves_and_returns() {
let rig = Rig::new(MiddleButton::AutoScroll);
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| rig.show(ui);
frames.idle(2, &mut show);
let at = rig.rect.get().center();
middle_click(&mut frames, at, &mut show);
frames.frame(vec![egui::Event::PointerGone], &mut show);
frames.frame(vec![pointer::moved(at + Vec2::new(0.0, PULL))], &mut show);
let before = rig.offset();
frames.idle(5, &mut show);
assert!((rig.offset() - before).abs() < SAME, "the latch stays ended after the pointer returns");
}
/// Under `AutoScroll` a middle click on the rows latches: the pointer held below the latch scrolls the rows toward the
/// end, once per frame however many passes the frame runs, no row is picked, and a second middle click ends it. Under
/// the default `Pan` the same events scroll nothing and the press picks its row as any press does. Guards the items
/// step's wiring of the latch: the row press taking a latching press, or the offset not following the latch.
#[trial]
fn a_latched_middle_press_scrolls_the_rows_and_presses_nothing() {
let rig = Rig::new(MiddleButton::AutoScroll);
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| rig.show(ui);
frames.idle(2, &mut show);
let at = rig.rect.get().center();
let below = at + Vec2::new(0.0, PULL);
middle_click(&mut frames, at, &mut show);
let before = rig.offset();
frames.frame(vec![pointer::moved(below)], &mut show);
frames.idle(10, &mut show);
assert!(rig.offset() > before + SAME, "the latch scrolls the rows toward the end: {before} -> {}", rig.offset());
// A frame that runs a second pass steps once: the rerun pass carries no time of its own.
let start = rig.offset();
frames.idle(1, &mut show);
let step = rig.offset() - start;
let start = rig.offset();
rig.discard.set(true);
assert_eq!(frames.frame(Vec::new(), &mut show), 2, "the frame reran its pass");
let rerun = rig.offset() - start;
assert!(step > SAME, "one frame of the latch scrolls: {step}");
assert!(rerun > SAME && rerun < step * 1.5, "a frame of two passes scrolls one step: {rerun} against {step}");
middle_click(&mut frames, below, &mut show);
let ended = rig.offset();
frames.idle(5, &mut show);
assert!((rig.offset() - ended).abs() < SAME, "a second middle press ends the latch: {ended} -> {}", rig.offset());
assert_eq!(rig.row_presses(), 0, "no middle press picked, led to or activated a row");
let pan = Rig::new(MiddleButton::Pan);
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| pan.show(ui);
frames.idle(2, &mut show);
let at = pan.rect.get().center();
middle_click(&mut frames, at, &mut show);
let before = pan.offset();
frames.frame(vec![pointer::moved(at + Vec2::new(0.0, PULL))], &mut show);
frames.idle(10, &mut show);
assert!((pan.offset() - before).abs() < SAME, "under Pan a middle click latches nothing: {before} -> {}", pan.offset());
assert!(pan.row_presses() > 0, "under Pan the middle press picks its row");
}