diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index bb9ec4da3..04b5bbaba 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -77,6 +77,7 @@ impl Lazy { None => sub(ui.scope_id(), ("lazy", self.key)), }; let available = flow_region(ui); + let available = if matches!(key, Key::Memo(_)) { on_row_line(ui, id, available) } else { available }; let rtl = ui.layout().prefer_right_to_left(); let sizing = ui.is_sizing_pass(); // The child is not part of its body's transcript: in a real run it counts there by its extent, whether it @@ -275,6 +276,24 @@ fn place_view(ui: &mut Ui, rect: Rect) -> Rect { rect } +/// The room of the memoized view `id` in the flow region `room` of `ui`. Its content starts a layout room of its own +/// (`Env::start_room`), so it would sit at the top of the unwrapped `Row` around it; as a child placed in flow +/// (`place_in_cell`), a view of one line sits with its centre on the row's line instead: its room moves down by the +/// whole pixels that put the centre of the extent it took (in this pass's measuring run, else its last run) there. A +/// view taller than that reaches, and a view no run showed yet, stays at the row's top. The room keeps its size, so a +/// kept run replays moved with it. +fn on_row_line(ui: &Ui, id: egui::Id, room: Rect) -> Rect { + let Some(centre) = Env::of(ui).row_line.and_then(|line| line.centre) else { + return room; + }; + let Some(size) = lazy::last_extent(ui.ctx(), id) else { + return room; + }; + let pixels_per_point = ui.ctx().pixels_per_point(); + let above = ((centre - size.y / 2.0) * pixels_per_point).round() / pixels_per_point; + if above.is_finite() && above > 0.0 { room.translate(Vec2::new(0.0, above)) } else { room } +} + /// What a child came to in a pass. pub(super) enum Shown { /// It ran and returned this. diff --git a/crates/ui-contract/src/runtime/lazy.rs b/crates/ui-contract/src/runtime/lazy.rs index 19b1b46e3..42bbcbb10 100644 --- a/crates/ui-contract/src/runtime/lazy.rs +++ b/crates/ui-contract/src/runtime/lazy.rs @@ -433,6 +433,12 @@ pub(crate) fn shown(ctx: &Context, owner: Option, id: Id, sizing: bool, meas }); } +/// The extent the child `id` took in the last measuring or real run that showed it (this pass's measuring run once it +/// ran), at whatever room it had; `None` before any run showed it. +pub(crate) fn last_extent(ctx: &Context, id: Id) -> Option { + with(ctx, |store, _, _| store.entries.get(&id).and_then(|entry| entry.sizes[1].map(|measured| measured.size))) +} + /// Registers what the skipped child `id` registered with the focus service in the last real run that showed it, in /// the real run that skips it. pub(crate) fn replay(ctx: &Context, id: Id) { diff --git a/crates/ui-contract/tests/it/layout/body.rs b/crates/ui-contract/tests/it/layout/body.rs index c5844b134..c5a9edcd7 100644 --- a/crates/ui-contract/tests/it/layout/body.rs +++ b/crates/ui-contract/tests/it/layout/body.rs @@ -3,7 +3,8 @@ use craft_behavior::{ColumnWidth, Proposal, Sense}; use craft_ui::contract::{ - BodyCx, Common, Component, Container, ContainerProps, Cx, EndRegion, MeasureCx, Props as _, Role, Show as _, ShowContainer as _, Sizing, + BodyCx, Common, Component, Container, ContainerProps, Cx, EndRegion, MeasureCx, MemoProps, MemoView, Props as _, Role, Show as _, + ShowContainer as _, Sizing, }; use craft_ui::layout::{BodyLayout, RowAlign}; use craft_ui::runtime::env::{Direction, with_env}; @@ -322,6 +323,39 @@ fn the_end_region_shares_the_members_line() { assert!((end.center().y - member.center().y).abs() < SLACK, "the end region's probe {end:?} is off the members' line {member:?}"); } +/// A memoized view of one short block. +#[derive(MemoProps)] +struct Short {} + +fn short(_: &mut Short, ui: &mut Ui) -> Rect { + Block::new(Vec2::new(40.0, 10.0)).show(ui).rect +} + +/// A memoized view in a row sits on the row's line, as the one-line child beside it does, also while it replays. Its +/// content starts a layout room of its own, which left it at the row's top (a menu bar beside an app icon, +/// storytold/photocraft#16113). +#[trial] +fn a_memoized_view_sits_on_the_rows_line() { + let seen = Rc::new(Cell::new((Rect::NOTHING, Rect::NOTHING))); + let shown = Rc::clone(&seen); + let mut fixture = Fixture::new(move |ui| { + Panel::new().layout(BodyLayout::row()).id_salt("row").show(ui, |ui, _| { + let tall = Block::new(Vec2::new(20.0, 30.0)).show(ui).rect; + let view = MemoView::new("short", Short {}, short).show(ui); + if !is_measuring(ui.ctx()) { + shown.set((tall, view.unwrap_or(Rect::NOTHING))); + } + }); + }); + fixture.room(Vec2::splat(400.0)).settle(); + for frame in 0..3 { + let (tall, view) = seen.get(); + assert!(view.height() > 0.0, "frame {frame}: the view showed: {view:?}"); + assert!((view.center().y - tall.center().y).abs() < SLACK, "frame {frame}: the view {view:?} is off the line of {tall:?}"); + fixture.settle(); + } +} + /// A Row in right-to-left holding a vertically scrolling container (content-sized, then filling its height) beside a /// probe: the container, the probe inside it and the probe beside it are finite, and the container lies in the row. #[trial]