From 6bb97dbcb20c580be0119302c19ea1c124d823a8 Mon Sep 17 00:00:00 2001 From: skylartaylor Date: Wed, 25 Nov 2026 08:14:54 +0100 Subject: [PATCH] Runtime: what code outside the framework measures in measure::run joins no body's kept transcript An app that sizes a row itself measures the component with Show::measure_in inside runtime::measure::run and then shows it with show_at. In the measuring run of the body around it, that measure joined the body's kept transcript as one of its components; in the real run, measure::run made it a measuring call with no transcript open, so it recorded nothing and checked nothing. The body then found a kept component its real run never asked, read as changed, and ran a discard pass every frame (LightCraft's Edit panel slider rows). runtime::measure::run now runs its closure inside a layout-memory boundary: what it measures is its own in both runs. The framework's own measuring runs use the new crate-internal measure::measuring, unchanged. Trial: SliderField rows sized through measure::run and shown with show_at in a Stack settle to one pass. --- .../ui-contract/src/contract/items/measure.rs | 2 +- crates/ui-contract/src/contract/pages.rs | 2 +- crates/ui-contract/src/contract/steps/id.rs | 2 +- crates/ui-contract/src/runtime/measure.rs | 13 +++++++- crates/ui-contract/src/testing/layout.rs | 2 +- .../ui-widgets/tests/it/layout/app_measure.rs | 31 +++++++++++++++++++ crates/ui-widgets/tests/it/layout/mod.rs | 1 + 7 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 crates/ui-widgets/tests/it/layout/app_measure.rs diff --git a/crates/ui-contract/src/contract/items/measure.rs b/crates/ui-contract/src/contract/items/measure.rs index 2a546e892..26986ead8 100644 --- a/crates/ui-contract/src/contract/items/measure.rs +++ b/crates/ui-contract/src/contract/items/measure.rs @@ -87,7 +87,7 @@ impl<'s, 'r, C: Collection> ItemsHooks<'s, 'r, C> { // A kept measure serves only the outermost measuring run, which this opens when none is open. let inputs = self.items_inputs(comp, ui, room); if let Some(inputs) = &inputs - && crate::runtime::measure::run(&ctx, || layout_memory::kept_serves(&ctx, key, inputs)) + && crate::runtime::measure::measuring(&ctx, || layout_memory::kept_serves(&ctx, key, inputs)) && let Some(found) = found(&ctx) { return found.took; diff --git a/crates/ui-contract/src/contract/pages.rs b/crates/ui-contract/src/contract/pages.rs index 02d0fb37f..5ab8b28c6 100644 --- a/crates/ui-contract/src/contract/pages.rs +++ b/crates/ui-contract/src/contract/pages.rs @@ -56,7 +56,7 @@ impl<'a, K: Hash + PartialEq> Pages<'a, K> { }); let size = kept.unwrap_or_else(|| { replay::open_in_pass(&ctx); - let size = measure::run(&ctx, || { + let size = measure::measuring(&ctx, || { // What the hidden page measures joins no transcript around it: the run that shows the pages does // not show it, so nothing there checks it. layout_memory::boundary(&ctx, true, || { diff --git a/crates/ui-contract/src/contract/steps/id.rs b/crates/ui-contract/src/contract/steps/id.rs index b1f65f7e5..cc55ad1f8 100644 --- a/crates/ui-contract/src/contract/steps/id.rs +++ b/crates/ui-contract/src/contract/steps/id.rs @@ -18,7 +18,7 @@ pub(crate) struct IdPass { /// Runs `f` as a measuring run that leaves the `Auto` ordinals as it found them, so the run that follows in the same /// pass gives every component the id the measuring run gave it. pub(in crate::contract) fn measuring(ctx: &Context, f: impl FnOnce() -> R) -> R { - keeping_ids(ctx, || (crate::runtime::measure::run(ctx, f), true)) + keeping_ids(ctx, || (crate::runtime::measure::measuring(ctx, f), true)) } /// Runs the measuring run `run` as a draft (`layout_memory::draft`) whose `Auto` ordinals go with it: taken back, it leaves diff --git a/crates/ui-contract/src/runtime/measure.rs b/crates/ui-contract/src/runtime/measure.rs index d64d0beef..ff9a453e7 100644 --- a/crates/ui-contract/src/runtime/measure.rs +++ b/crates/ui-contract/src/runtime/measure.rs @@ -111,8 +111,19 @@ struct LeastRuns { runs: Vec<(Id, u32)>, } -/// Runs `f` as a measuring run. Nested calls are fine. +/// Runs `f` as a measuring run, for code outside the framework that measures before it shows (an app that sizes a rect +/// by `Show::measure_in` and then shows the component with `show_at`). Nested calls are fine. +/// +/// What `f` measures is its own: it joins no kept transcript of the body around it, in that body's measuring run or in +/// its real run alike. Otherwise the body's measuring run would keep it as one of the body's components while the +/// real run, measuring outside every transcript, never asks it, and the body would read as changed every frame. pub fn run(ctx: &Context, f: impl FnOnce() -> R) -> R { + measuring(ctx, || super::layout_memory::boundary(ctx, true, f)) +} + +/// Runs `f` as a measuring run whose measures join the transcript of the body whose measuring run is open around it: +/// the framework's own measuring runs. Nested calls are fine. +pub(crate) fn measuring(ctx: &Context, f: impl FnOnce() -> R) -> R { /// Restores the depth when the run ends, by a panic too: a context used after a caught panic is not measuring. struct Open<'a>(&'a Context, u32); impl Drop for Open<'_> { diff --git a/crates/ui-contract/src/testing/layout.rs b/crates/ui-contract/src/testing/layout.rs index ef65b574a..3eeac1bfb 100644 --- a/crates/ui-contract/src/testing/layout.rs +++ b/crates/ui-contract/src/testing/layout.rs @@ -19,7 +19,7 @@ pub fn body(ui: &mut Ui, key: &'static str, layout: &BodyLayout, sizes: &[Vec2]) let place = |ui: &mut Ui| sizes.iter().map(|size| place_in_flow(ui, *size)).collect::>(); let builder = egui::UiBuilder::new().max_rect(ui.available_rect_before_wrap()).sizing_pass().invisible(); let env = crate::runtime::env::Env::of(ui); - crate::runtime::measure::run(&ui.ctx().clone(), || ui.scope_builder(builder, |ui| run_body(ui, &env, id, layout, Vec2::ZERO, place))); + crate::runtime::measure::measuring(&ui.ctx().clone(), || ui.scope_builder(builder, |ui| run_body(ui, &env, id, layout, Vec2::ZERO, place))); run_body(ui, &env, id, layout, Vec2::ZERO, place) } diff --git a/crates/ui-widgets/tests/it/layout/app_measure.rs b/crates/ui-widgets/tests/it/layout/app_measure.rs new file mode 100644 index 000000000..53fd98f6f --- /dev/null +++ b/crates/ui-widgets/tests/it/layout/app_measure.rs @@ -0,0 +1,31 @@ +//! An app that sizes a row itself, measuring the component inside `runtime::measure::run` and then showing it with +//! `show_at` (LightCraft's slider rows), leaves the body around it settled: a settled frame runs one pass. + +use craft_behavior::Proposal; +use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _}; +use craft_ui::form::SliderField; +use craft_ui::layout_components::Stack; +use craft_ui::runtime::measure; +use craft_ui::testing::{Frames, trial}; +use egui::{Ui, vec2}; + +fn rows(ui: &mut Ui) { + let _ = Stack::new().id_salt("rows").show(ui, |ui, _| { + for (row, label) in ["Exposure", "Contrast", "Highlights"].into_iter().enumerate() { + let mut field = SliderField::new(label, Bind::given(0.5), -5.0..=5.0).step(0.01).id_salt(row); + let width = ui.available_width().max(1.0); + let ctx = ui.ctx().clone(); + let size = measure::run(&ctx, || field.measure_in(ui, Proposal::Width(width))); + let (_, room) = ui.allocate_space(vec2(width, size.y)); + field.show_at(ui, room); + } + }); +} + +#[trial] +fn rows_an_app_measures_itself_and_shows_at_a_rect_settle_to_one_pass() { + let mut frames = Frames::new(); + frames.idle(4, rows); + assert_eq!(frames.frame(vec![], rows), 1, "a settled frame runs one pass"); + assert_eq!(frames.frame(vec![], rows), 1, "and the next one too"); +} diff --git a/crates/ui-widgets/tests/it/layout/mod.rs b/crates/ui-widgets/tests/it/layout/mod.rs index 580f62713..024eebd6b 100644 --- a/crates/ui-widgets/tests/it/layout/mod.rs +++ b/crates/ui-widgets/tests/it/layout/mod.rs @@ -1,3 +1,4 @@ +pub(crate) mod app_measure; pub(crate) mod cross_fill; pub(crate) mod fill_column; pub(crate) mod fill_height;