From 9fd8b0cdaad2b5320090f5a5892ba90fb8dab107 Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 24 Nov 2026 18:57:43 +0000 Subject: [PATCH] Add frame::will_rerun for host work once per shown frame Layout memory requests its discard pass from egui in its end hook, after the app's UI, so Context::will_discard() read by host code during a pass that a changed size runs again is still false. frame::will_rerun is true when egui will discard the pass or when this pass asked layout memory for the frame's discard pass and will get it. Hosts gate frame counts and settle counts on it. The discard request stays in the end hook, so lazy runs and memoized replays keep their records of the asking pass. --- crates/ui-contract/src/runtime/frame.rs | 8 +++++++ .../ui-contract/src/runtime/layout_memory.rs | 9 ++++++++ crates/ui-contract/tests/it/layout/memory.rs | 23 +++++++++++++++++++ docs/contract-api.md | 1 + docs/craft-framework.md | 2 ++ 5 files changed, 43 insertions(+) diff --git a/crates/ui-contract/src/runtime/frame.rs b/crates/ui-contract/src/runtime/frame.rs index 3531e0db63..98672ec6f1 100644 --- a/crates/ui-contract/src/runtime/frame.rs +++ b/crates/ui-contract/src/runtime/frame.rs @@ -582,6 +582,14 @@ pub fn discardable(ctx: &Context) -> bool { crate::runtime::egui::sizing::discardable(ctx) } +/// Whether egui will run the running pass again, so nothing of it paints: egui's `Context::will_discard`, or a discard +/// pass that layout memory asked for in this pass and requests from egui only at the pass's end. Host code that does +/// work once per shown frame (a frame count, a settle count) skips it while this is true; `will_discard` alone is still +/// false during the app's UI in a pass that a changed size runs again. +pub fn will_rerun(ctx: &Context) -> bool { + ctx.will_discard() || crate::runtime::layout_memory::discard_pending(ctx) +} + /// Discards the running pass, so nothing of it paints, and runs it again, when [`discardable`]: for a host that learns /// only after a part ran that the part showed what it must not. A pass after a discard carries no input (events, /// injected steps and press edges enter a frame's first pass only); held state persists. diff --git a/crates/ui-contract/src/runtime/layout_memory.rs b/crates/ui-contract/src/runtime/layout_memory.rs index 41cb3f9623..dbea6d3e98 100644 --- a/crates/ui-contract/src/runtime/layout_memory.rs +++ b/crates/ui-contract/src/runtime/layout_memory.rs @@ -244,9 +244,11 @@ pub(crate) struct Entries { replaying: Vec<(Arc, Id, usize)>, /// The drafts open, innermost last. drafts: Vec, + /// This pass asked for the frame's discard pass; the end hook requests it. ask: bool, /// How many times a discard pass was asked, so a record open around the asking run notes it (`with`). asks: u64, + /// The frame whose one discard pass the end hook requested. asked: Option, } @@ -1175,6 +1177,13 @@ pub(crate) fn ask_discard(ctx: &Context) { with(ctx, &moment, Entries::ask_discard); } +/// Whether this pass asked for the frame's discard pass and the end hook will request it from egui: no earlier pass of +/// the frame got it, and egui can still run this one again. +pub(crate) fn discard_pending(ctx: &Context) -> bool { + let moment = Moment::of(ctx); + with(ctx, &moment, |memory| memory.ask && memory.asked != Some(moment.frame)) && sizing::discardable(ctx) +} + /// Keeps the entries and kept transcripts of `ids` through this frame, for a replayed run that kept them /// (`replay::note_layout_keep`): the replay does not reach them, and an entry no pass touched in a frame is dropped. pub(crate) fn keep(ctx: &Context, ids: &[Id]) { diff --git a/crates/ui-contract/tests/it/layout/memory.rs b/crates/ui-contract/tests/it/layout/memory.rs index 16a7a5e041..e8bcb82888 100644 --- a/crates/ui-contract/tests/it/layout/memory.rs +++ b/crates/ui-contract/tests/it/layout/memory.rs @@ -4,6 +4,7 @@ use super::fill::{Held, scrolled_fixture}; use craft_behavior::ColumnWidth; use craft_ui::contract::{ContainerProps, Props as _, Show as _, ShowContainer as _, Sizing}; use craft_ui::layout::BodyLayout; +use craft_ui::runtime::frame; use craft_ui::testing::layout::{Block, body, measured_block}; use craft_ui::testing::{Frames, last_pass_measuring_runs, pointer, trial}; use craft_ui_test_kit::body::Slab; @@ -99,3 +100,25 @@ fn a_container_sharing_a_scrolled_view_runs_its_body_at_most_the_draft_more_than assert!(wheel <= plain_wheel + 1, "a wheel frame runs the body at most the draft more: {case}"); } } + +/// A block whose size asks for the frame's discard pass shows in `frame::will_rerun` for the rest of the pass, a host +/// after it included, while egui's `will_discard` stays false until the end hook requests the discard. +#[trial] +fn a_discard_asked_by_a_block_shows_in_will_rerun_in_the_same_pass() { + let mut frames = Frames::new(); + let seen = std::cell::RefCell::new(Vec::new()); + let mut run = |width: f32| { + seen.borrow_mut().clear(); + let passes = frames.frame(vec![], |ui| { + measured_block(ui, "readout", Vec2::new(width, 20.0)); + seen.borrow_mut().push((frame::will_rerun(ui.ctx()), ui.ctx().will_discard())); + }); + (passes, seen.borrow().clone()) + }; + assert_eq!(run(20.0), (2, vec![(true, false), (false, false)]), "the first appearance asks, and the host after it reads the rerun"); + for _ in 0..3 { + run(20.0); + } + assert_eq!(run(20.0), (1, vec![(false, false)]), "a settled block asks nothing"); + assert_eq!(run(300.0), (2, vec![(true, false), (false, false)]), "a change after a settled frame asks in its first pass"); +} diff --git a/docs/contract-api.md b/docs/contract-api.md index 30983c458b..2a5bcc261a 100644 --- a/docs/contract-api.md +++ b/docs/contract-api.md @@ -4081,6 +4081,7 @@ The frame plugin: the per-pass and per-frame steps, with the inputs the app sets - `craft_ui::runtime::frame::shell_root`: `pub fn shell_root(ctx: &Context, viewport: ViewportId) -> Option`. The root container of the shell the latest pass of `viewport` showed: where a key pressed with nothing focused - `craft_ui::runtime::frame::stepped_clock`: `pub fn stepped_clock(ctx: &Context, step: f64) -> f64`. The pass clock in seconds rounded down to a multiple of `step`, for content that changes with time (a rescan every - `craft_ui::runtime::frame::take_output`: `pub fn take_output(ctx: &Context) -> FrameOutput`. Everything accumulated since the last call, across passes and discarded passes. +- `craft_ui::runtime::frame::will_rerun`: `pub fn will_rerun(ctx: &Context) -> bool`. Whether egui will run the running pass again, so nothing of it paints: egui's `Context::will_discard`, or a discard ## `craft_ui::runtime::ime` diff --git a/docs/craft-framework.md b/docs/craft-framework.md index 235268024b..a717ace9ad 100644 --- a/docs/craft-framework.md +++ b/docs/craft-framework.md @@ -322,6 +322,8 @@ while let Some(Finished { key, result }) = queue.try_recv() { show(key, result) A pass shows the whole UI once. egui may repeat a pass within a frame (a discard pass); only the frame's last pass is painted. Within a pass, a container's contents run twice: a measuring run, then the real run. Both runs of a pass give the same result. +Host code that does work once per shown frame (a frame count, a screenshot's settle count) skips it while `runtime::frame::will_rerun` is true. egui's `Context::will_discard` is not enough: layout memory requests its discard pass from egui at the end of the pass, so during the app's UI `will_discard` is still false in a pass that a changed size runs again. + Content runs in every pass, and its sizes come from this pass. Layouts also read some state earlier passes left, for example a scroll area's offset and its last view and extent (step 4 and the scroll bars read them) and the sizes layout memory keeps (`runtime::layout_memory`; a changed size asks for a discard pass). The one exception to running in every pass is out-of-view content. **Layout memory.** A record of layout memory holds a measure or a text. A body whose inputs did not change takes its kept measurement instead of running a measuring run. A text that a measuring run found for content outside the body (a value readout beside a story, for example) is kept with the measurement: `layout_memory::emit_text` hands it forward, `layout_memory::text` reads it, and a body that takes its kept measurement gives the text its measuring run found. A real run that finds another text marks the kept transcript as changed.