mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 12:32:10 +01:00
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.
This commit is contained in:
1 parent
16001e7210
commit
6bb97dbcb2
7 files changed
+48
-5
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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, || {
|
||||
|
||||
@@ -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<R>(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
|
||||
|
||||
@@ -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<R>(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<R>(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<'_> {
|
||||
|
||||
@@ -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::<Vec<_>>();
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub(crate) mod app_measure;
|
||||
pub(crate) mod cross_fill;
|
||||
pub(crate) mod fill_column;
|
||||
pub(crate) mod fill_height;
|
||||
|
||||
Reference in new issue
Block a user