craft-ui: the replay audit compares a moved replay with the kept run moved where the replay adds it

A view whose room moved (a scroll) replays its record shifted. Its audit run compares with the kept record as that
replay adds it: egui's records through PassRecords::shifted, journal entries with their arguments shifted, and the
kept rect moved by the shift's offset. journal's again is Copy (every capture-free closure is).
This commit is contained in:
aiongg
2026-11-20 00:11:15 +00:00
parent bf98f15211
commit a818e5dc45
5 changed files with 54 additions and 40 deletions
+15 -17
View File
@@ -120,9 +120,9 @@ impl Lazy {
}
let settled = if memo { lazy::settled(&ctx, owner, id, sizing.then_some(available), real.then_some(place), view, key) } else { None };
// The replay audit runs a view that would replay for real in place of the replay, and compares the two.
let audited = settled.is_some() && audits(&ctx, real);
let audited = settled.as_ref().filter(|_| audits(&ctx, real)).map(|(_, _, shift)| *shift);
if let Some((size, ran_at, shift)) = settled
&& !audited
&& audited.is_none()
{
// A real replay is where the run's content went, moved with the room.
let rect = if real { ran_at } else { Rect::from_min_size(at_start(available, rtl, size), size) };
@@ -151,7 +151,7 @@ impl Lazy {
let edits = if run.recording { lazy::edits(&ctx) } else { 0 };
let seen = if run.recording { crate::runtime::inputs::seen(&ctx) } else { Default::default() };
if run.recording {
open_record(&ctx, audited);
open_record(&ctx, audited.is_some());
}
let mut child = alloc::child(ui, UiBuilder::new().max_rect(available).layout(layout).scope_id(id));
// Its extent in the body around it comes from its content's measure, so a change inside reaches that body.
@@ -160,8 +160,8 @@ impl Lazy {
// The child `Ui` writes its widget rect when it drops: the record holds it as written.
drop(child);
let record = run.recording.then(|| replay::close(&ctx, place.layer));
if audited {
return place_audit_run(ui, Audited { name, id, view, rect, record: record.as_ref(), memo }, out, &run);
if let Some(shift) = audited {
return place_audit_run(ui, Audited { name, id, view, rect, record: record.as_ref(), shift }, out, &run);
}
#[cfg(feature = "testing")]
if let Some(record) = &record
@@ -201,28 +201,25 @@ impl Lazy {
}
}
/// Places the audit run of a child that would have replayed, as a replay places it, and compares the run with the
/// kept run (`runtime::audit`). It keeps the kept run: every later pass compares with what a replay adds.
/// Places the audit run of a view that would have replayed, as a replay places it, and compares the run with the kept
/// run replayed at the place `shift` names (`runtime::audit`). It keeps the kept run: every later pass compares with
/// what a replay adds.
fn place_audit_run<R>(ui: &mut Ui, audited: Audited<'_>, out: R, run: &Run) -> Shown<R> {
let Audited { name, id, view, rect, record, memo } = audited;
let Audited { name, id, view, rect, record, shift } = audited;
let ctx = ui.ctx().clone();
layout_memory::backstop(&ctx, id, rect.size());
if memo {
place_view(ui, rect);
} else {
alloc::advance_past(ui, rect);
}
place_view(ui, rect);
if let Some(view) = view {
lazy::place(&ctx, &view, id, rect);
}
#[cfg(feature = "testing")]
{
if let Some(record) = record {
lazy::audit(&ctx, name, id, crate::runtime::audit::Shown { record, rect, output: run.output.as_ref() }, run.same_output);
lazy::audit(&ctx, name, id, crate::runtime::audit::Shown { record, rect, output: run.output.as_ref() }, run.same_output, &shift);
}
count_memo_run(&ctx, name, id, MemoRun::Replayed, true);
}
let _ = (name, record, run);
let _ = (name, record, run, shift);
Shown::Ran(out)
}
@@ -233,7 +230,8 @@ struct Audited<'a> {
view: Option<lazy::View>,
rect: Rect,
record: Option<&'a replay::Record>,
memo: bool,
/// Where the replay would have added the kept run.
shift: egui::RecordShift,
}
/// Whether the replay audit runs a child that would replay in a pass whose runs are `real` (test builds only).
@@ -291,5 +289,5 @@ pub(super) struct Run {
pub(super) output: Option<lazy::Output>,
/// Whether two outputs of a memoized view are equal, for the replay audit.
#[cfg(feature = "testing")]
pub(super) same_output: Option<fn(&lazy::Output, &lazy::Output) -> bool>,
pub(super) same_output: Option<crate::runtime::audit::SameOutput>,
}
+14 -11
View File
@@ -11,11 +11,12 @@
use crate::runtime::egui::memory as store;
use crate::runtime::lazy::Output;
use crate::runtime::replay::{self, Record};
use egui::{Context, Id, Rect};
use crate::runtime::shift::Shift;
use egui::{Context, Id, RecordShift, Rect};
use std::collections::HashSet;
/// Whether two outputs of a memoized view are equal: both of its output type, and `==`.
pub(crate) type SameOutput = fn(&Output, &Output) -> bool;
pub(crate) type SameOutput = fn(&Output, &Output, &RecordShift) -> bool;
/// The audit's switch for one context and the views it named.
#[derive(Default)]
@@ -47,9 +48,9 @@ pub(crate) fn runs(ctx: &Context) -> bool {
on(ctx) && !replay::in_audit_run(ctx)
}
/// Whether two outputs are equal as `R`.
pub(crate) fn same<R: PartialEq + 'static>(a: &Output, b: &Output) -> bool {
a.downcast_ref::<R>() == b.downcast_ref::<R>()
/// Whether the kept output `kept`, as a replay at the place `shift` names returns it, equals `run`'s, as `R`.
pub(crate) fn same<R: PartialEq + Shift + 'static>(kept: &Output, run: &Output, shift: &RecordShift) -> bool {
kept.downcast_ref::<R>().map(|kept| kept.shifted(shift)).as_ref() == run.downcast_ref::<R>()
}
/// What a view's kept run, or its audit run, added and returned.
@@ -59,15 +60,17 @@ pub(crate) struct Shown<'a> {
pub(crate) output: Option<&'a Output>,
}
/// Compares the audit run `run` of the view `name` (`id`) with its kept run `kept`, and panics on a difference.
pub(crate) fn compare(name: &'static str, id: Id, kept: Shown<'_>, run: Shown<'_>, same: Option<SameOutput>) {
/// Compares the audit run `run` of the view `name` (`id`) with its kept run `kept` replayed at the place `shift` names,
/// and panics on a difference.
pub(crate) fn compare(name: &'static str, id: Id, (kept, shift): (Shown<'_>, &RecordShift), run: Shown<'_>, same: Option<SameOutput>) {
let mut found = Vec::new();
if kept.rect != run.rect {
found.push(format!("rect: left {:?} / right {:?}", kept.rect, run.rect));
let kept_rect = shift.rect(kept.rect);
if kept_rect != run.rect {
found.push(format!("rect: left {:?} / right {:?}", kept_rect, run.rect));
}
found.extend(kept.record.differences(run.record));
found.extend(kept.record.differences(run.record, shift));
let output = match (kept.output, run.output, same) {
(Some(kept), Some(run), Some(same)) => same(kept, run),
(Some(kept), Some(run), Some(same)) => same(kept, run, shift),
(kept, run, _) => kept.is_none() == run.is_none(),
};
if !output {
+4 -3
View File
@@ -516,8 +516,8 @@ pub(crate) fn replay_settled(ctx: &Context, id: Id, shift: &RecordShift) -> Opti
settled.output.clone()
}
/// Compares the audit run of the settling child `name` (`id`), which added `record`, took `rect` and returned `output`,
/// with its kept run (`runtime::audit`).
/// Compares the audit run of the memoized view `name` (`id`), which added `record`, took `rect` and returned `output`,
/// with its kept run replayed at the place `shift` names (`runtime::audit`).
#[cfg(feature = "testing")]
pub(crate) fn audit(
ctx: &Context,
@@ -525,10 +525,11 @@ pub(crate) fn audit(
id: Id,
run: crate::runtime::audit::Shown<'_>,
same: Option<crate::runtime::audit::SameOutput>,
shift: &RecordShift,
) {
let Some(kept) = with(ctx, |store, _, _| store.entries.get(&id).and_then(|entry| entry.settled.clone())) else { return };
let kept_run = crate::runtime::audit::Shown { record: &kept.record, rect: kept.rect, output: kept.output.as_ref() };
crate::runtime::audit::compare(name, id, kept_run, run, same);
crate::runtime::audit::compare(name, id, (kept_run, shift), run, same);
}
/// What the last real run of the memoized view `id` returned, for a measuring run that takes its kept extent.
+20 -8
View File
@@ -32,6 +32,9 @@ pub(crate) struct Entry(Arc<dyn Noted>);
trait Noted: Send + Sync {
fn call(&self, ctx: &Context, shift: &RecordShift);
/// The entry a replay at the place `shift` names calls (`call`).
#[cfg(feature = "testing")]
fn shifted(&self, shift: &RecordShift) -> Entry;
fn as_any(&self) -> &dyn Any;
fn same(&self, other: &dyn Noted) -> bool;
fn describe(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
@@ -45,8 +48,13 @@ struct Note<A, F> {
impl<A, F> Noted for Note<A, F>
where
A: PartialEq + fmt::Debug + Shift + Send + Sync + 'static,
F: Fn(&Context, &A) + Send + Sync + 'static,
F: Fn(&Context, &A) + Copy + Send + Sync + 'static,
{
#[cfg(feature = "testing")]
fn shifted(&self, shift: &RecordShift) -> Entry {
Entry(Arc::new(Note { args: self.args.shifted(shift), again: self.again }))
}
fn call(&self, ctx: &Context, shift: &RecordShift) {
if shift.is_none() {
(self.again)(ctx, &self.args);
@@ -233,22 +241,26 @@ impl Record {
self.egui.paints_callback()
}
/// How a replay of `self` differs from the real run `run`: one line per kind that differs (paint, widget rects
/// and AccessKit nodes; focus registrations; the journal), each with its first difference.
pub(crate) fn differences(&self, run: &Record) -> Vec<String> {
/// How a replay of `self` at the place `shift` names differs from the real run `run` there: one line per kind that
/// differs (paint, widget rects and AccessKit nodes; focus registrations; the journal), each with its first
/// difference.
pub(crate) fn differences(&self, run: &Record, shift: &RecordShift) -> Vec<String> {
let mut found = Vec::new();
match self.egui.first_difference(&run.egui) {
match self.egui.shifted(shift).first_difference(&run.egui) {
// The display names a widget by its rect only; the difference may be in its sense, state or interact rect.
Some(egui::PassRecordsDiff::Widget { index, left, right }) => found.push(format!("widget rect {index}: left {left:?} / right {right:?}")),
// The display names a node by its bounds only; the difference may be in any other property.
Some(egui::PassRecordsDiff::Node { index, left, right }) => found.push(format!("AccessKit node {index}: left {left:?} / right {right:?}")),
Some(egui::PassRecordsDiff::Node { index, left, right }) => {
found.push(format!("AccessKit node {index}: left {left:?} / right {right:?}"))
}
Some(diff) => found.push(format!("paint, widgets or AccessKit: {diff}")),
None => {}
}
if let Some(diff) = self.focus.first_difference(&run.focus) {
found.push(format!("focus: {diff}"));
}
let (left, right) = (&self.journal, &run.journal);
let shifted: Vec<Entry> = self.journal.iter().map(|entry| entry.0.shifted(shift)).collect();
let (left, right) = (&shifted, &run.journal);
if let Some(index) =
left.iter().zip(right).position(|(a, b)| a != b).or_else(|| (left.len() != right.len()).then(|| left.len().min(right.len())))
{
@@ -327,7 +339,7 @@ fn with_open(ctx: &Context, f: impl FnOnce(&mut Facts)) {
pub fn journal<A, F>(ctx: &Context, args: impl FnOnce() -> A, again: F)
where
A: PartialEq + fmt::Debug + Shift + Send + Sync + 'static,
F: Fn(&Context, &A) + Send + Sync + 'static,
F: Fn(&Context, &A) + Copy + Send + Sync + 'static,
{
const { assert!(std::mem::size_of::<F>() == 0, "`journal`'s `again` captures nothing: pass its state in `args`") };
if recording(ctx) && !pass_state::with::<Recorder, _>(ctx, |recorder| recorder.replaying) {
+1 -1
View File
@@ -521,7 +521,7 @@ impl<P: Clone + PartialEq + Debug + Send + Sync + 'static> DockTargets<P> {
fn journal<A, F>(ctx: &Context, args: impl FnOnce() -> A, again: F)
where
A: PartialEq + Debug + crate::runtime::Shift + Send + Sync + 'static,
F: Fn(&Context, &A) + Send + Sync + 'static,
F: Fn(&Context, &A) + Copy + Send + Sync + 'static,
{
crate::runtime::journal(ctx, args, again);
}