From 8af8aeffad8129ba0b8dcaf91b3bb1239a2e059b Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 20 Nov 2026 07:31:57 +0000 Subject: [PATCH] craft-ui: a record's journal holds what its real run registered, not its measuring runs A body inside a memoized view measures only in some passes, and what a measuring run hands forward lives in the body's kept transcript. The journal took those entries too, so a replay added a measuring sweep's registrations in a pass that measured nothing. Trial: a view whose body measured in its run replays as a run with the audit on. --- crates/ui-contract/src/runtime/replay.rs | 15 ++++++-- crates/ui-widgets/tests/it/body/memo_audit.rs | 38 ++++++++++++++++++- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/crates/ui-contract/src/runtime/replay.rs b/crates/ui-contract/src/runtime/replay.rs index 8ff45c698..7ca5e501e 100644 --- a/crates/ui-contract/src/runtime/replay.rs +++ b/crates/ui-contract/src/runtime/replay.rs @@ -2,7 +2,8 @@ //! again in place of running the view. A measuring run of a measure-only child (a page `Pages` holds out of view) opens //! a record too; only the values it read are kept, so a later run in the pass takes its size and notes them. A record //! holds the view's paint, egui widget rects and AccessKit nodes (egui's pass records), what it registered with the -//! focus service (`focus::Transcript`), and a journal of every other registration the services below take per pass: +//! focus service (`focus::Transcript`), and a journal of every other registration the services below take per pass in +//! the real run (`journaling`): //! each service that keeps one notes it here while a record is open, as the call that makes it again. Records nest: a //! view's record holds the records of the views inside it that replayed. //! @@ -350,12 +351,20 @@ where F: Fn(&Context, &A) + Copy + Send + Sync + 'static, { const { assert!(std::mem::size_of::() == 0, "`journal`'s `again` captures nothing: pass its state in `args`") }; - if recording(ctx) && !pass_state::with::(ctx, |recorder| recorder.replaying) { + if journaling(ctx) { let entry = Entry(Arc::new(Note { args: args(), again })); pass_state::with::(ctx, |recorder| recorder.journal.push(entry)); } } +/// Whether a registration the running code makes goes into the open records' journal: a record is open, no replay is +/// adding its journal, and no measuring run is in progress. A journal holds what the real run registered: a measuring +/// run inside it (a body measuring its content) runs only in a pass that measures, and what it hands forward lives in +/// that body's kept transcript, so a replay in a pass that measures nothing adds only what the real run added. +fn journaling(ctx: &Context) -> bool { + recording(ctx) && !crate::runtime::measure::active(ctx) && !pass_state::with::(ctx, |recorder| recorder.replaying) +} + /// `journal` for a registration that a second equal one adds nothing to: the innermost open record keeps it once, so /// its journal does not depend on how many times its run made it. pub(crate) fn journal_distinct(ctx: &Context, args: impl FnOnce() -> A, again: F) @@ -364,7 +373,7 @@ where F: Fn(&Context, &A) + Copy + Send + Sync + 'static, { const { assert!(std::mem::size_of::() == 0, "`journal_distinct`'s `again` captures nothing: pass its state in `args`") }; - if recording(ctx) && !pass_state::with::(ctx, |recorder| recorder.replaying) { + if journaling(ctx) { let entry = Entry(Arc::new(Note { args: args(), again })); pass_state::with::(ctx, |recorder| { let from = recorder.open.last().map_or(0, |open| open.from); diff --git a/crates/ui-widgets/tests/it/body/memo_audit.rs b/crates/ui-widgets/tests/it/body/memo_audit.rs index 49bcd1793..2052781fa 100644 --- a/crates/ui-widgets/tests/it/body/memo_audit.rs +++ b/crates/ui-widgets/tests/it/body/memo_audit.rs @@ -3,13 +3,14 @@ //! no count. use craft_ui::buttons::Button; -use craft_ui::contract::{MemoProps, MemoView, Show as _}; +use craft_ui::contract::{ContainerProps, Lazy, MemoProps, MemoView, Props as _, Scroll, Show as _, ShowContainer as _}; use craft_ui::testing::layout::in_region; use craft_ui::testing::pointer::moved; use craft_ui::testing::{ Frames, MemoRun, audit_replays, in_audit_run, last_frame_memo_runs, last_pass_memo_runs, last_pass_runs, last_pass_text_layouts, trial, }; use egui::{Color32, Pos2, Rect, Ui, Vec2}; +use craft_ui_test_kit::body::Slab; use std::cell::Cell; use std::panic::{AssertUnwindSafe, catch_unwind}; use std::sync::Arc; @@ -199,3 +200,38 @@ fn a_view_painting_a_callback_runs_every_pass() { assert_eq!(runs, [MemoRun::Ran], "frame {frame}"); } } + +/// A view holding a scrolling body of rows that run only in view. +#[derive(MemoProps)] +struct ScrolledRows { + rows: usize, +} + +fn scrolled_rows(props: &mut ScrolledRows, ui: &mut Ui) { + Slab::new(ContainerProps::new().scroll(Scroll::vertical())).id_salt("rows").show(ui, |ui, _| { + for at in 0..props.rows { + Lazy::new(at, 20.0).show(ui, |ui| { + Button::new(format!("Row {at}")).show(ui); + }); + } + }); +} + +/// A view whose run measured the scrolling body inside it replays in passes that measure nothing: its record holds what +/// its real run registered, not what the body's measuring run did, so the audit finds the replay equal to a run. +#[trial] +fn a_view_whose_body_measured_in_its_run_replays_as_a_run_in_a_pass_that_does_not_measure() { + let mut frames = Frames::new(); + audit_replays(&frames.ctx, true); + let rows = Cell::new(12); + let mut show = |ui: &mut Ui| { + in_region(ui, Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(160.0, 100.0)), |ui| { + MemoView::new("rows", ScrolledRows { rows: rows.get() }, scrolled_rows).show(ui); + }); + }; + frames.idle(SETTLE, &mut show); + rows.set(16); + 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:?}"); +}