From f300c5954a69606cae4421376f15e769bc5e57ef Mon Sep 17 00:00:00 2001 From: aiongg Date: Sat, 21 Nov 2026 04:08:59 +0000 Subject: [PATCH] Memoized views: the header trial asserts every call replayed; a measuring replay's value holds the real run's layout The trial checks the frame's memo log: three replays (the sizing pass and the real run of each of the frame's two passes), the last pass's real run among them. MemoView::show and the runner say that a measuring call that replays returns the kept real run's value, laid out in that run's room and flow, so a caller that measures takes the extent the call reports. --- crates/ui-contract/src/contract/lazy.rs | 2 ++ crates/ui-contract/src/contract/memo_view.rs | 4 +++- crates/ui-widgets/tests/it/body/memo_view.rs | 21 +++++++++++++++----- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index 74d9fc6095..bb622ea9fc 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -150,6 +150,8 @@ impl Lazy { } kept } else { + // The kept real run's output, laid out in that run's room and flow: a caller that measures sizes from + // the extent this call takes, never from rects in it. lazy::kept_output(&ctx, id) }; report_unit(&ctx, shrink, id, rect.size()); diff --git a/crates/ui-contract/src/contract/memo_view.rs b/crates/ui-contract/src/contract/memo_view.rs index 57088ece26..de103c619b 100644 --- a/crates/ui-contract/src/contract/memo_view.rs +++ b/crates/ui-contract/src/contract/memo_view.rs @@ -110,7 +110,9 @@ impl MemoVie } /// Runs the view in a child `Ui` placed in flow in `ui`, or adds its last run again. Returns what the run returned, - /// the kept value when it replayed; `None` only when it is out of view in a scroll area. + /// the kept value when it replayed; `None` only when it is out of view in a scroll area. A measuring run that + /// replays returns the kept real run's value, laid out in that run's room and flow: code that measures sizes from + /// the extent the view takes in `ui`, never from rects in the value. pub fn show(self, ui: &mut Ui) -> Option { let MemoView { lazy, mut props, show } = self; let ctx = ui.ctx().clone(); diff --git a/crates/ui-widgets/tests/it/body/memo_view.rs b/crates/ui-widgets/tests/it/body/memo_view.rs index 0e128ff9b5..cceab42135 100644 --- a/crates/ui-widgets/tests/it/body/memo_view.rs +++ b/crates/ui-widgets/tests/it/body/memo_view.rs @@ -741,10 +741,11 @@ impl Container for Headed { } } -/// One frame of `Headed` with the body text `label`; returns how many sizing passes its body ran in. -fn headed_frame(frames: &mut Frames, label: &str) -> usize { +/// One frame of `Headed` with the body text `label`; returns the frame's passes and how many sizing passes its body ran +/// in. +fn headed_frame(frames: &mut Frames, label: &str) -> (usize, usize) { let sizing = Cell::new(0); - frames.frame(vec![], |ui| { + let passes = frames.frame(vec![], |ui| { in_region(ui, STACK_ROOM, |ui| { let headed = Headed { common: Common::new(), props: ContainerProps::new().header(HeaderRegion::new()) }; headed.show(ui, |ui, _| { @@ -755,7 +756,7 @@ fn headed_frame(frames: &mut Frames, label: &str) -> usize { }); }); }); - sizing.get() + (passes, sizing.get()) } /// A view in the header of a content-sized container replays when the container measures its regions again: its @@ -771,9 +772,19 @@ fn a_view_in_a_measured_header_replays_in_the_sizing_pass_and_the_real_run() { headed_frame(&mut frames, "Short"); assert_eq!(title_runs(), 0, "settled: an idle frame replays the header's view"); // A second line, no wider than the first: the container's width, and with it the header's room, stays. - let sizing = headed_frame(&mut frames, "Short\nShort"); + let (passes, sizing) = headed_frame(&mut frames, "Short\nShort"); assert!(sizing > 0, "the taller label made the container measure its regions again"); + assert_eq!(passes, 2, "the taller container asks for one discard pass"); assert_eq!(title_runs(), 0, "the unchanged view replayed in every call: {:?}", last_frame_memo_runs(&frames.ctx)); + let heading = |runs: Vec<(&'static str, Id, MemoRun)>| -> Vec { + runs.into_iter().filter(|(name, _, _)| name.ends_with("::Title")).map(|(_, _, run)| run).collect() + }; + assert_eq!(heading(last_pass_memo_runs(&frames.ctx)), [MemoRun::Replayed], "the discard pass's real run replayed the view"); + assert_eq!( + heading(last_frame_memo_runs(&frames.ctx)), + [MemoRun::Replayed; 3], + "the view was called and replayed in the sizing pass and in the real run of each of the two passes" + ); } /// A paragraph that wraps at its room's width.