From 8e6490f61856cabc61ae4582b0bfe9289ba55281 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 19 Nov 2026 14:21:08 +0000 Subject: [PATCH] craft-ui: a sizing replay measures again in a room its last sizing run was not offered Each kind's extent keeps the room the run was offered beside the key of its content. A sizing call takes the kept sizing extent only when both match; a new room runs it once as a measuring run, as new content does, and the real call still decides by the settled rules. A container offered another room so follows the view's new extent. --- crates/ui-contract/src/contract/lazy.rs | 4 +- crates/ui-contract/src/runtime/lazy.rs | 60 +++++++++++++------- crates/ui-widgets/tests/it/body/memo_view.rs | 52 +++++++++++++++++ 3 files changed, 94 insertions(+), 22 deletions(-) diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index 61e60b754..53cb1784b 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -162,7 +162,7 @@ impl Lazy { crate::runtime::memory::claim_id(&ctx, id, "two memoized views share an id in one pass"); } if self.settles - && let Some(size) = lazy::settled(&ctx, owner, id, sizing, real.then_some(place), view, key) + && let Some(size) = lazy::settled(&ctx, owner, id, sizing.then_some(available), real.then_some(place), view, key) { let (took, _) = take_in_flow(ui, size); let memo = matches!(key, Key::Memo(_)); @@ -200,7 +200,7 @@ impl Lazy { alloc::advance_past(ui, rect); if view.is_some() || self.settles { let registered = real.then(|| record.as_ref().map_or_else(|| focus::transcript(&ctx, mark), |record| record.focus.clone())); - lazy::shown(&ctx, owner, id, rect.size(), sizing, key, registered); + lazy::shown(&ctx, owner, id, sizing, lazy::Measured { size: rect.size(), key, room: available }, registered); } if let Some(view) = view && real diff --git a/crates/ui-contract/src/runtime/lazy.rs b/crates/ui-contract/src/runtime/lazy.rs index 22c14125c..85435a5e6 100644 --- a/crates/ui-contract/src/runtime/lazy.rs +++ b/crates/ui-contract/src/runtime/lazy.rs @@ -128,11 +128,11 @@ pub(crate) enum Decision { struct Entry { /// The scroll area the child is kept under. owner: Option, - /// The extent of the last measuring run and of the last real run that showed the child, with the key of the - /// content it showed. A run that skips the child takes the extent of its own kind: a content-sized scroll area is - /// offered another room in its measuring run than in its real run, so each kind places the child as it last laid - /// it out. Out of view the child keeps it at any room; it is measured again when it meets the reach. - sizes: [Option<(Vec2, Key)>; 2], + /// What the last measuring run and the last real run that showed the child measured. A run that skips the child + /// takes the extent of its own kind: a content-sized scroll area is offered another room in its measuring run than + /// in its real run, so each kind places the child as it last laid it out. Out of view the child keeps it at any + /// room; it is measured again when it meets the reach. + sizes: [Option; 2], /// What the child registered with the focus service in the last real run that showed it. Out of view the /// child registers it again in its place, so Tab and F6 reach its stops; it runs while one of them holds focus /// or a focus request asks to reveal one. @@ -149,6 +149,15 @@ struct Entry { touched: u64, } +/// What a run that showed a child measured: the extent it took, the key of the content it showed and the room it was +/// offered (`Rect::NOTHING` for a collection row, whose extent its collection reports). +#[derive(Clone, Copy, Debug)] +pub(crate) struct Measured { + pub(crate) size: Vec2, + pub(crate) key: Key, + pub(crate) room: Rect, +} + /// Where a settling child ran: it is settled only while it runs at the same place, in the same environment. #[derive(Clone, Copy, Debug, PartialEq)] pub(crate) struct Place { @@ -322,7 +331,7 @@ pub(crate) fn decide(ctx: &Context, id: Id, view: &View, at: impl FnOnce(Vec2) - let entry = store.touch(Some(view.owner), id, frame); let kind = usize::from(!sizing); // A child the other kind of run showed last takes that run's extent. - let size = entry.sizes[kind].or(entry.sizes[1 - kind]).map_or(estimate, |(size, _)| size); + let size = entry.sizes[kind].or(entry.sizes[1 - kind]).map_or(estimate, |measured| measured.size); let skip = match entry.decided { Some((decided_in, skip)) if decided_in == pass_nr => skip, _ => { @@ -336,12 +345,12 @@ pub(crate) fn decide(ctx: &Context, id: Id, view: &View, at: impl FnOnce(Vec2) - }) } -/// Records what a run that showed the child `id` keyed `key` found: its extent in a run of this kind and, from a real -/// run, what it registered with the focus service. -pub(crate) fn shown(ctx: &Context, owner: Option, id: Id, size: Vec2, sizing: bool, key: Key, registered: Option) { +/// Records what a run that showed the child `id` found: what it measured in a run of this kind and, from a real run, +/// what it registered with the focus service. +pub(crate) fn shown(ctx: &Context, owner: Option, id: Id, sizing: bool, measured: Measured, registered: Option) { with(ctx, |store, _, frame| { let entry = store.touch(owner, id, frame); - entry.sizes[usize::from(!sizing)] = Some((size, key)); + entry.sizes[usize::from(!sizing)] = Some(measured); if let Some(registered) = registered { entry.focus = Arc::new(registered); } @@ -358,11 +367,12 @@ pub(crate) fn replay(ctx: &Context, id: Id) { } /// Whether the settling child `id` adds its last real run to this pass in place of running, and the extent that run -/// took in a run of this kind (`sizing`). The first run of a pass decides from what changed since that run; a real run -/// also needs the child at the `place` it ran at, 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); when that -/// sizing run showed other content, 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. +/// took in a run of this kind (`sizing`: the room a sizing run offers it, `None` outside a sizing pass). The first run +/// of a pass decides from what changed since that run; a real run also needs the child at the `place` it ran at, 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 replays. /// /// It replays when its last run was in an earlier frame, or, for a memoized view, in an earlier pass of this frame, and /// since then: @@ -390,7 +400,15 @@ pub(crate) fn replay(ctx: &Context, id: Id) { /// /// A memoized view's props hold every input it reads, so the two rules that infer a change of app data (input /// arrived, a repaint was asked) do not apply to it. -pub(crate) fn settled(ctx: &Context, owner: Option, id: Id, sizing: bool, place: Option, view: Option, key: Key) -> Option { +pub(crate) fn settled( + ctx: &Context, + owner: Option, + id: Id, + sizing: Option, + place: Option, + view: Option, + key: Key, +) -> Option { let input = ctx.input(|input| { input.events.iter().any(|event| !matches!(event, egui::Event::PointerMoved(_) | egui::Event::MouseMoved(_) | egui::Event::PointerGone)) }); @@ -433,9 +451,11 @@ pub(crate) fn settled(ctx: &Context, owner: Option, id: Id, sizing: bool, pl if replays && settled.is_none() { entry.replays = Some((pass_nr, false)); } - let measured = |kind: usize, key: Key| entry.sizes[kind].filter(|&(_, of)| of == key).map(|(size, _)| size); settled.and_then(|settled| { - let size = if sizing { measured(0, settled.key)? } else { measured(1, settled.key).unwrap_or(settled.rect.size()) }; + let size = match sizing { + Some(room) => entry.sizes[0].filter(|measured| measured.key == settled.key && measured.room == room)?.size, + None => entry.sizes[1].filter(|measured| measured.key == settled.key).map_or(settled.rect.size(), |measured| measured.size), + }; Some((size, settled)) }) }); @@ -581,7 +601,7 @@ pub(crate) fn unsettle(ctx: &Context) { /// Records the extent of a row of the collection `owner` that was built this pass. The collection places the rows it /// does not build by it (its item layout answers for rows it never built). pub(crate) fn remember(ctx: &Context, owner: Id, id: Id, size: Vec2) { - shown(ctx, Some(owner), id, size, false, Key::None, None); + shown(ctx, Some(owner), id, false, Measured { size, key: Key::None, room: Rect::NOTHING }, None); } /// Runs `f` with a reader of the extents the rows of the collection `owner` had in the last pass that built them, for @@ -594,7 +614,7 @@ pub(crate) fn reading(ctx: &Context, owner: Id, f: impl FnOnce(&dyn Fn(Id) -> let mut store = store.borrow_mut(); let entry = store.entries.get_mut(&id)?; entry.touched = frame; - entry.sizes[1].or(entry.sizes[0]).map(|(size, _)| size) + entry.sizes[1].or(entry.sizes[0]).map(|measured| measured.size) }) }) } diff --git a/crates/ui-widgets/tests/it/body/memo_view.rs b/crates/ui-widgets/tests/it/body/memo_view.rs index b3297236b..a3ef356f3 100644 --- a/crates/ui-widgets/tests/it/body/memo_view.rs +++ b/crates/ui-widgets/tests/it/body/memo_view.rs @@ -639,3 +639,55 @@ fn a_changed_view_its_container_measures_first_runs_in_the_sizing_pass_and_the_r stack_frame_in(&mut frames, wider, "Label", "A much longer heading"); assert_eq!(title_runs(), 0, "then the view replays in every run"); } + +/// A paragraph that wraps at its room's width. +#[derive(MemoProps)] +struct Paragraph { + text: String, +} + +fn paragraph(props: &mut Paragraph, ui: &mut Ui) -> Vec2 { + Text::new(props.text.clone()).show(ui).rect.size() +} + +/// One frame of a stack `width` wide, sized from its content's height, holding the memoized `paragraph`. Returns the +/// sizing passes the stack's content ran in, the stack's rect and what the view returned (the paragraph's size) in the +/// last real run. +fn paragraph_frame(frames: &mut Frames, width: f32) -> (usize, Rect, Option) { + let sizing = Cell::new(0); + let mut out = (Rect::NOTHING, None); + frames.frame(vec![], |ui| { + in_region(ui, Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(width, 400.0)), |ui| { + let shown = Stack::new().fill_width().show(ui, |ui, _| { + if ui.is_sizing_pass() { + sizing.set(sizing.get() + 1); + } + let text = "A paragraph long enough to wrap onto more lines in a narrow room".to_owned(); + MemoView::new("paragraph", Paragraph { text }, paragraph).show(ui) + }); + out = (shown.output.rect, shown.inner.flatten()); + }); + }); + (sizing.get(), out.0, out.1) +} + +/// A view whose props are unchanged, offered another room in its container's sizing pass, is measured there once: its +/// last sizing extent was measured in the old room. The container's size follows the new extent (here the paragraph +/// wraps onto more lines), and the real run runs it too, for the new room is also its own. +#[trial] +fn a_view_offered_another_room_in_a_sizing_pass_is_measured_again() { + let mut frames = Frames::new(); + for _ in 0..SETTLE { + paragraph_frame(&mut frames, 600.0); + } + let (_, wide, wide_text) = paragraph_frame(&mut frames, 600.0); + let (sizing, stack, text) = paragraph_frame(&mut frames, 150.0); + assert!(sizing > 0, "the stack measured its content in a sizing pass"); + let text = text.expect("the view returned the paragraph's size"); + assert!(text.y > wide_text.map_or(0.0, |size| size.y), "the paragraph wraps onto more lines in the narrow room"); + let ran = last_frame_memo_runs(&frames.ctx).into_iter().filter(|(_, _, run)| *run == MemoRun::Ran).count(); + assert_eq!(ran, 2, "one measuring run in the sizing pass and one real run, both in the new room"); + assert!(stack.height() + 0.5 >= text.y, "the stack follows the new extent: {stack:?} for a paragraph {text:?} (wide: {wide:?})"); + paragraph_frame(&mut frames, 150.0); + assert!(last_frame_memo_runs(&frames.ctx).iter().all(|(_, _, run)| *run == MemoRun::Replayed), "then the view replays in every run"); +}