From 6c72055339f113d73afdb9a7b85cfc0a86afbc5a Mon Sep 17 00:00:00 2001 From: aiongg Date: Sat, 21 Nov 2026 10:38:11 +0000 Subject: [PATCH] A memoized row unit's room follows the row, not its own content; a call's key is its own A memoized view the row does not narrow shows as in no room, in the row's flow region, and its content sizes from its content along x; only a row that narrows it cuts its room to the width it gives. The cap of a view the row did not narrow was its own counted width, so new content changed the view's room and the discard pass ran it again. The shrink record carries whether the row narrowed the unit. The replay decision of a pass covers the kept run; each call compares its own key. A row's counting sweep calls the view with another key (its content shows unbounded): that call no longer decides the pass against the real call after it, and a sizing call takes the extent the last sizing run measured for its own key. --- crates/ui-contract/src/contract/lazy.rs | 8 ++- crates/ui-contract/src/contract/memo_view.rs | 5 +- crates/ui-contract/src/layout/body.rs | 27 ++++++--- crates/ui-contract/src/runtime/env.rs | 8 ++- .../ui-contract/src/runtime/layout_memory.rs | 28 +++++---- crates/ui-contract/src/runtime/lazy.rs | 25 ++++---- .../ui-contract/tests/it/contract/memo_row.rs | 59 +++++++++++++++++++ crates/ui-contract/tests/it/contract/mod.rs | 1 + 8 files changed, 128 insertions(+), 33 deletions(-) create mode 100644 crates/ui-contract/tests/it/contract/memo_row.rs diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index 01d74889f..b087b47f6 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -78,11 +78,13 @@ impl Lazy { }; let rtl = ui.layout().prefer_right_to_left(); let memo = matches!(key, Key::Memo(_)); - // A memoized view is one unit of the shrinking row around it: it shows at most the width the row's measuring - // run gave it, the room its place then holds, so a new width runs it. + // A memoized view is one unit of the shrinking row around it. A row that narrows it gives it at most the width + // its measuring run settled on, the room its place then holds, so a new width runs it. A view the row does not + // narrow shows as in no room, in the row's flow region: its room does not follow its own content's width, so a + // new content runs it once and the discard pass its new size asks for replays that run. let shrink = Env::of(ui).shrink.filter(|_| memo); let cap = match shrink { - Some(Shrink::Apply(row)) => crate::layout::shrink_cap(&ctx, row, id), + Some(Shrink::Apply(row)) => crate::layout::shrink_cap(&ctx, row, id).filter(|cap| cap.narrowed), _ => None, }; let available = cap.map_or_else(|| flow_region(ui), |cap| at_most(flow_region(ui), rtl, cap.width)); diff --git a/crates/ui-contract/src/contract/memo_view.rs b/crates/ui-contract/src/contract/memo_view.rs index d503b1895..aaea525f7 100644 --- a/crates/ui-contract/src/contract/memo_view.rs +++ b/crates/ui-contract/src/contract/memo_view.rs @@ -53,8 +53,9 @@ use std::sync::Arc; /// cursor) does not reach in, so the room it is given reaches it only through its place. To the body /// around it the view is one unit, sized by its content's measure. In a shrinking `Row` it is a unit the row narrows /// as it narrows a body unit: from what its content takes in no room down to what it takes with every leaf at its -/// least. The width the row gives it is the room of its place, and the leaves of its content fit that room, as they -/// fit the room of a `Stack` the view is shown in. +/// least. When the row narrows it, the width the row gives it is the room of its place; otherwise it shows as in no +/// room, in the row's flow region. Its content sizes from its content along x, and its leaves fit the view's room, as +/// they fit the room of a `Stack` the view is shown in. /// /// What `show` returns holds layout facts only (a size, a rect, which part took the room). A replay returns the kept /// value again, moved with the record when the room moved (`Shift`), so an event returned through it would arrive once diff --git a/crates/ui-contract/src/layout/body.rs b/crates/ui-contract/src/layout/body.rs index 5a7d9148a..eef131a3a 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -541,11 +541,18 @@ pub(crate) struct ShrinkCap { pub width: f32, /// The unit dropped out of the row (`shrink_or_drop`): it shows nothing and takes no place. pub dropped: bool, + /// The row gave the unit less than it asks for in no room, or dropped it: what the unit shows depends on the row's + /// room. A unit the row did not narrow shows as in no room. + pub narrowed: bool, } /// What the shrink step of the row `row` gave its unit `leaf` in this pass's measuring run. pub(crate) fn shrink_cap(ctx: &egui::Context, row: Id, leaf: Id) -> Option { - layout_memory::shrink(ctx, shrink_key(row, leaf)).map(|(found, dropped)| ShrinkCap { width: found.took.x, dropped }) + layout_memory::shrink(ctx, shrink_key(row, leaf)).map(|(found, given)| ShrinkCap { + width: found.took.x, + dropped: given.dropped, + narrowed: given.narrowed, + }) } fn shrink_key(row: Id, leaf: Id) -> Id { @@ -649,9 +656,13 @@ fn shrink_or_drop( let shown_first: Vec = first.iter().zip(&kept).filter(|(_, kept)| **kept).map(|(first, _)| *first).collect(); let mut widths = shrink_in_turns(&shown, &pick(whole), &shown_first, fixed, room, order).into_iter(); kept.iter() - .map(|kept| match kept { - true => ShrinkCap { width: widths.next().unwrap_or_default(), dropped: false }, - false => ShrinkCap { width: 0.0, dropped: true }, + .zip(units) + .map(|(kept, unit)| match kept { + true => { + let width = widths.next().unwrap_or_default(); + ShrinkCap { width, dropped: false, narrowed: unit.ideal - width >= 0.5 } + } + false => ShrinkCap { width: 0.0, dropped: true, narrowed: true }, }) .collect() } @@ -732,21 +743,23 @@ fn run_shrinking(ui: &mut Ui, id: Id, mut body: impl FnMut(&mut Ui) -> R) -> let widths = if room.is_finite() { shrink_or_drop(&units, &whole, &first, (fixed + held, gap), room, order, drops) } else { - units.iter().map(|unit| ShrinkCap { width: unit.ideal, dropped: false }).collect() + units.iter().map(|unit| ShrinkCap { width: unit.ideal, dropped: false, narrowed: false }).collect() }; // A levelled unit took no place of its own content in the first sweep (it measured each level apart), so the // records its content's run leaves for the real run come from a sweep at the levels given. let mut again = !levelled.is_empty(); for (leaf, cap) in continuous.iter().zip(widths) { let measured = layout_memory::Measured::new(Vec2::new(room, 0.0), Vec2::new(cap.width, 0.0)); - layout_memory::emit_shrink(&ctx, shrink_key(id, leaf.leaf), measured, cap.dropped); + let given = layout_memory::ShrinkGiven { dropped: cap.dropped, narrowed: cap.narrowed }; + layout_memory::emit_shrink(&ctx, shrink_key(id, leaf.leaf), measured, given); // Half a point: less than a pixel shows at any scale the box runs. again |= (leaf.took - cap.width).abs() >= 0.5; } for (leaf, level) in levelled.iter().zip(levels) { // The level travels as the record's height (`level_of`). let measured = layout_memory::Measured::new(Vec2::new(room, 0.0), Vec2::new(width_at(leaf, level), level as f32)); - layout_memory::emit_shrink(&ctx, shrink_key(id, leaf.leaf), measured, false); + let given = layout_memory::ShrinkGiven { dropped: false, narrowed: level > 0 }; + layout_memory::emit_shrink(&ctx, shrink_key(id, leaf.leaf), measured, given); } again }; diff --git a/crates/ui-contract/src/runtime/env.rs b/crates/ui-contract/src/runtime/env.rs index 891f8abb6..bf7ac1ed0 100644 --- a/crates/ui-contract/src/runtime/env.rs +++ b/crates/ui-contract/src/runtime/env.rs @@ -410,13 +410,19 @@ impl Scoped { } /// Starts the layout room of a memoized view's content (`start_room`). Where a leaf shown here would fit its room - /// (`view_fits`), the leaves of the view's content fit the room the view is given (`Shrink::Fit`). + /// (`view_fits`), the leaves of the view's content fit the room the view is given (`Shrink::Fit`). A view that is a + /// unit of a shrinking `Row` sizes from its content along x, as the row counted it: a child that fills there takes + /// its measured width, not the row's flow region a view the row does not narrow is shown in. pub(crate) fn start_view_room(&mut self) { let fits = self.view_fits(); + let row_unit = matches!(self.shrink, Some(Shrink::Count(_) | Shrink::Least(_) | Shrink::Apply(_))); self.start_room(); if fits { self.shrink = Some(Shrink::Fit); } + if row_unit { + self.content.0 = true; + } } /// Whether a memoized view shown in flow here fits its content to its room, as a leaf here would: in a `Stack` body, diff --git a/crates/ui-contract/src/runtime/layout_memory.rs b/crates/ui-contract/src/runtime/layout_memory.rs index 3894fd46b..84ab4bb8c 100644 --- a/crates/ui-contract/src/runtime/layout_memory.rs +++ b/crates/ui-contract/src/runtime/layout_memory.rs @@ -97,8 +97,8 @@ pub(crate) type TakeBack = Box; #[derive(Clone, Debug, PartialEq)] enum Recorded { Measured(Measured), - /// The width a row's shrink step gave a unit, as a measure, and whether the unit dropped out of the row. - Shrink(Measured, bool), + /// The width a row's shrink step gave a unit, as a measure, and whether it narrowed the unit or dropped it. + Shrink(Measured, ShrinkGiven), Text(Option>), } @@ -499,10 +499,18 @@ pub(crate) fn emit_record(ctx: &Context, id: Id, measured: Measured) { emit(ctx, id, Recorded::Measured(measured)); } -/// Hands forward what a row's shrink step gave a unit under `id`: its width as `measured` and whether it `dropped` -/// out of the row. It serves as [`emit_record`]'s measure does. -pub(crate) fn emit_shrink(ctx: &Context, id: Id, measured: Measured, dropped: bool) { - emit(ctx, id, Recorded::Shrink(measured, dropped)); +/// Whether a row's shrink step narrowed a unit below what it asks for in no room, and whether it dropped the unit out +/// of the row. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct ShrinkGiven { + pub dropped: bool, + pub narrowed: bool, +} + +/// Hands forward what a row's shrink step gave a unit under `id`: its width as `measured` and what else it `given`. +/// It serves as [`emit_record`]'s measure does. +pub(crate) fn emit_shrink(ctx: &Context, id: Id, measured: Measured, given: ShrinkGiven) { + emit(ctx, id, Recorded::Shrink(measured, given)); } /// Hands forward a text a run found under `id` that content outside the body it runs in shows, as `emit_record` @@ -565,7 +573,7 @@ fn kept_differs(last: &Found, now: &Recorded, pass: u64, tolerance: f32) -> Opti }; let differs = match (was, now) { (Recorded::Measured(was), Recorded::Measured(now)) => measures(was, now, false), - (Recorded::Shrink(was, was_dropped), Recorded::Shrink(now, now_dropped)) => measures(was, now, was_dropped != now_dropped), + (Recorded::Shrink(was, was_given), Recorded::Shrink(now, now_given)) => measures(was, now, was_given != now_given), (was, now) => was != now, }; if *at != pass || !differs { @@ -922,12 +930,12 @@ pub(crate) fn measured(ctx: &Context, id: Id) -> Option { } /// What a row's shrink step of this pass gave the unit under `id` ([`emit_shrink`]): its width as a measure and -/// whether it dropped out of the row. -pub(crate) fn shrink(ctx: &Context, id: Id) -> Option<(Measured, bool)> { +/// whether it narrowed or dropped the unit. +pub(crate) fn shrink(ctx: &Context, id: Id) -> Option<(Measured, ShrinkGiven)> { let moment = Moment::of(ctx); let pass = ctx.cumulative_pass_nr(); with(ctx, &moment, |memory| match memory.measured.get(&id).filter(|found| found.pass == pass && !memory.probing()) { - Some(Found { recorded: Recorded::Shrink(measured, dropped), .. }) => Some((*measured, *dropped)), + Some(Found { recorded: Recorded::Shrink(measured, given), .. }) => Some((*measured, *given)), _ => None, }) } diff --git a/crates/ui-contract/src/runtime/lazy.rs b/crates/ui-contract/src/runtime/lazy.rs index 5aa98cc1d..5e4031df6 100644 --- a/crates/ui-contract/src/runtime/lazy.rs +++ b/crates/ui-contract/src/runtime/lazy.rs @@ -449,10 +449,12 @@ pub(crate) fn replay(ctx: &Context, id: Id) { /// Whether the memoized view `id` adds its last real run to this pass in place of running, the extent that run took in /// a run of this kind (`sizing`: the room a sizing run offers it, `None` outside a sizing pass), the rect the kept real /// run takes now and the shift its record moves by. The first run of a pass decides from what changed since that run; a -/// real run also needs a `place` the run can move to (`Place::shift_to`), which a measuring run cannot tell. A sizing -/// run of a pass that replays takes the extent the last sizing run measured of the content the kept run showed (its -/// key) in the room it offers now; when that sizing run showed other content or had another room, or there was none, it -/// runs once as a measuring run, which keeps no record and leaves the decision of the pass alone, so the real run still +/// real run also needs a `place` the run can move to (`Place::shift_to`), which a measuring run cannot tell. Each call +/// also needs the key of the kept run: any other call with another key (a row's counting sweep shows the content +/// unbounded, and the scope key holds that) runs, and leaves the decision of the pass alone for the calls after it. A +/// sizing run of a pass that replays takes the extent the last sizing run measured of the content it shows (its key) in +/// the room it offers now; when that sizing run showed other content or had another room, or there was none, it runs +/// once as a measuring run, which keeps no record and leaves the decision of the pass alone, so the real run still /// replays. /// /// It replays when its last run was in an earlier pass, of this frame or an earlier one, and since then: @@ -464,8 +466,8 @@ pub(crate) fn replay(ctx: &Context, id: Id) { /// - no intent of this pass is routed to a component in it (an AccessKit request on one of its nodes); /// - no value it read (a bound value, an inspection record, a command's state) changed this frame (`note_edit`), or, /// replaying a run of this frame, after that run started; -/// - its key is the one of its last run (its props key after that run), and its environment (theme, density, scale, -/// language, window active) the one it ran in; +/// - the call's key is the one of its last run (its props key after that run), and its environment (theme, density, +/// scale, language, window active) the one it ran in; /// - the run did nothing a replay cannot repeat (`runtime::replay`) and asked for no discard pass: what it added is /// what the pass was thrown away for; /// - the shown life of no component it showed ended (`memory::ended_since`): a replay would show memory state that is @@ -492,6 +494,10 @@ pub(crate) fn settled( // Usually `None`: only the values written this frame. let edited = changes.edited_in(frame); let entry = store.touch(owner, id, frame); + // The decision of the pass covers the kept run; the key is the call's own. A call that shows other content (a + // row's counting sweep, which shows it unbounded) leaves the decision alone for the calls after it: a sizing call + // takes the extent the last sizing run measured of its own content, and any other runs. + let keyed = entry.settled.as_ref().is_some_and(|settled| settled.key == key) || sizing.is_some(); let replays = match entry.replays { Some((decided_in, replays)) if decided_in == pass_nr => replays, _ => { @@ -500,7 +506,6 @@ pub(crate) fn settled( // A run of this frame read the values edited before it started. let edited = edited_since(edited.as_ref(), if settled.frame < frame { 0 } else { settled.edits }); (settled.frame < frame || settled.pass < pass_nr) - && settled.key == key && !settled.engaged && !facts.blocked && !facts.discards @@ -518,14 +523,14 @@ pub(crate) fn settled( Some(place) => settled.place.shift_to(settled.rect, place, settled.record.movable(), transformed), None => Some(Moved { shift: RecordShift::none(settled.place.clip), with_clip: true }), }; - let settled = entry.settled.clone().filter(|_| replays).and_then(|settled| Some((moved(&settled)?, settled))); - if replays && settled.is_none() { + let settled = entry.settled.clone().filter(|_| replays && keyed).and_then(|settled| Some((moved(&settled)?, settled))); + if replays && keyed && settled.is_none() { entry.replays = Some((pass_nr, false)); } settled.and_then(|(moved, settled)| { // An extent depends on the room's size, not on where the room is. let size = match sizing { - Some(room) => entry.sizes[0].filter(|measured| measured.key == settled.key && measured.room.size() == room.size())?.size, + Some(room) => entry.sizes[0].filter(|measured| measured.key == key && measured.room.size() == room.size())?.size, None => entry.sizes[1].filter(|measured| measured.key == settled.key).map_or(settled.rect.size(), |measured| measured.size), }; Some((size, moved, settled)) diff --git a/crates/ui-contract/tests/it/contract/memo_row.rs b/crates/ui-contract/tests/it/contract/memo_row.rs new file mode 100644 index 000000000..1c5019b3e --- /dev/null +++ b/crates/ui-contract/tests/it/contract/memo_row.rs @@ -0,0 +1,59 @@ +//! A memoized view that is one unit of a shrinking row: the row's counting sweep calls it with another key than its +//! real run, and that call leaves the replay decision of the pass to the calls after it and reuses its own measure. + +use craft_ui::contract::{MemoProps, MemoView, Props as _, Show as _, ShowContainer as _, Sizing}; +use craft_ui::layout::BodyLayout; +use craft_ui::testing::layout::in_region; +use craft_ui::testing::{Frames, MemoRun, last_frame_memo_runs, last_pass_memo_runs, trial}; +use craft_ui_test_kit::contract::{Panel, Probe, Seen}; +use egui::{Pos2, Rect, Ui, Vec2}; +use std::cell::{Cell, RefCell}; + +/// A view that shows one probe `width` wide. +#[derive(MemoProps)] +struct Widened { + width: f32, +} + +fn widened(props: &mut Widened, ui: &mut Ui) { + let seen = RefCell::new(Seen::default()); + Probe::new(&seen).size(Vec2::new(props.width, 20.0)).id_salt("content").show(ui); +} + +/// How the `Widened` views took part in `runs`. +fn of_view(runs: Vec<(&'static str, egui::Id, MemoRun)>) -> Vec { + runs.into_iter().filter(|(name, _, _)| name.contains("Widened")).map(|(_, _, run)| run).collect() +} + +/// A memoized view in a row that is not short, whose new content is wider: it runs once in the real run that meets the +/// new props and once as a measuring run in the row's counting sweep, whose call keys apart from the real run's +/// (the content shows unbounded there). The real run of the discard pass that sweep belongs to replays, and so does +/// every call of the next frame, a counting sweep's included. +#[trial] +fn a_memoized_row_unit_replays_after_the_rows_counting_call_with_another_key() { + let width = Cell::new(60.0); + let mut frames = Frames::new(); + let frame = |frames: &mut Frames| { + frames.frame(vec![], |ui| { + in_region(ui, Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(400.0, 40.0)), |ui| { + Panel::new().layout(BodyLayout::row()).sizing(Sizing::FillWidth).id_salt("row").show(ui, |ui, _| { + let _ = MemoView::new("view", Widened { width: width.get() }, widened).show(ui); + let seen = RefCell::new(Seen::default()); + Probe::new(&seen).id_salt("after").show(ui); + }); + }); + }); + }; + for _ in 0..30 { + frame(&mut frames); + } + assert_eq!(of_view(last_frame_memo_runs(&frames.ctx)), [MemoRun::Replayed], "an idle frame replays the view"); + width.set(90.0); + frame(&mut frames); + let calls = of_view(last_frame_memo_runs(&frames.ctx)); + assert_eq!(calls.iter().filter(|run| **run == MemoRun::Ran).count(), 2, "one real run and one measuring run: {calls:?}"); + assert_eq!(of_view(last_pass_memo_runs(&frames.ctx)), [MemoRun::Replayed], "the discard pass's real call replays: {calls:?}"); + frame(&mut frames); + let calls = of_view(last_frame_memo_runs(&frames.ctx)); + assert!(calls.iter().all(|run| *run == MemoRun::Replayed), "the next frame replays the view in every call: {calls:?}"); +} diff --git a/crates/ui-contract/tests/it/contract/mod.rs b/crates/ui-contract/tests/it/contract/mod.rs index 556140903..2e95af053 100644 --- a/crates/ui-contract/tests/it/contract/mod.rs +++ b/crates/ui-contract/tests/it/contract/mod.rs @@ -5,6 +5,7 @@ pub(crate) mod gesture; pub(crate) mod hit_order; pub(crate) mod level; pub(crate) mod memo_key; +pub(crate) mod memo_row; pub(crate) mod pages; pub(crate) mod pointer; pub(crate) mod style;