From 12d67b1cdf824bf2e7a8fdb2a0aac226e3797cad Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 15:38:27 +0700 Subject: [PATCH] ui-contract: a reveal that moves a collection's rows after it declared its parts asks for another pass, so a memoized view around it runs rather than replay a stale scroll bar --- .../ui-contract/src/contract/items/hooks.rs | 6 ++++ crates/ui-widgets/tests/it/body/memo_audit.rs | 29 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index b12f89701..2b455318e 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -333,7 +333,13 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { let base_env = Self::rows_env(comp, env, id, cx.core.resolved.size, own).derive(|scoped| scoped.extend_values(values)); let known = self.known(comp, false); let known_at_read = self.known(comp, true); + let declared = self.offset; let revealed = self.place_reveal(comp, ui, (id, cx.core.env, &base_env), (&mut layout, &known), rect); + // The parts (the scroll bar's thumb among them) stand at the offset before the reveal moved it: the next pass + // declares them where the rows now are, and a memoized view around the rows runs it instead of replaying this. + if self.offset != declared { + crate::runtime::egui::repaint::request(&ctx); + } let offset = ScrollPos { x: self.offset_x, y: self.offset }; Arc::make_mut(&mut layout).set_view(rect.size(), offset); if self.scrolls { diff --git a/crates/ui-widgets/tests/it/body/memo_audit.rs b/crates/ui-widgets/tests/it/body/memo_audit.rs index c8f1dad69..4891a49e0 100644 --- a/crates/ui-widgets/tests/it/body/memo_audit.rs +++ b/crates/ui-widgets/tests/it/body/memo_audit.rs @@ -3,7 +3,8 @@ //! no count. use craft_ui::buttons::Button; -use craft_ui::contract::{ContainerProps, Lazy, MemoProps, MemoView, Props as _, Scroll, Show as _, ShowContainer as _}; +use craft_ui::contract::{ContainerProps, Lazy, MemoProps, MemoView, Props as _, Scroll, Show as _, ShowCollection as _, ShowContainer as _}; +use craft_ui::list::{List, ListRow}; use craft_ui::runtime::lock_keys; use craft_ui::style::SurfaceKind; use craft_ui::testing::layout::{allocate, in_region}; @@ -434,3 +435,29 @@ fn a_view_replayed_moved_inside_a_run_is_kept_at_its_new_place() { assert!(runs.iter().any(|(name, _, run)| name.contains("Card") && *run == MemoRun::Replayed), "the card replayed in it: {runs:?}"); frames.idle(SETTLE, &mut show); } + +/// A view holding a capped list that reveals its last row on every run. +#[derive(MemoProps)] +struct RevealedRows {} + +fn revealed_rows(_: &mut RevealedRows, ui: &mut Ui) { + let keys: Vec = (0..40).collect(); + let source = craft_behavior::keyed(&keys, |key: &usize| *key); + List::new("Rows").max_rows(8).reveal(39).show_items(ui, &source, &mut |key, _| ListRow::new(format!("Row {key}"))); +} + +/// A reveal moves the rows after the list declared its scroll bar at the old offset: the view runs again rather than +/// replay a run whose thumb stands where the rows no longer are, so its replays match a real run. +#[trial] +fn a_list_revealing_a_row_in_a_memoized_view_replays_as_it_runs() { + let mut frames = Frames::new(); + audit_replays(&frames.ctx, true); + let mut show = |ui: &mut Ui| { + in_region(ui, Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(300.0, 400.0)), |ui| { + MemoView::new("revealed", RevealedRows {}, revealed_rows).show(ui); + }); + }; + frames.idle(SETTLE, &mut show); + let ran: Vec<_> = last_frame_memo_runs(&frames.ctx).into_iter().filter(|(_, _, run)| *run == MemoRun::Ran).collect(); + assert!(ran.is_empty(), "the view settles: {ran:?}"); +}