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

This commit is contained in:
bflatastic
2026-11-23 15:38:27 +07:00
parent ecd1565856
commit 12d67b1cdf
2 changed files with 34 additions and 1 deletions
@@ -333,7 +333,13 @@ impl<C: Collection> ShapeHooks<C> 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 {
+28 -1
View File
@@ -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<usize> = (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:?}");
}