diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index 53cb1784b..caaf54399 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -1,7 +1,7 @@ //! `Lazy`: a child of a scroll area that runs only while it is in view, and a child that runs only when something it //! shows changed (`runtime::lazy`). -use crate::layout::{flow_region, take_in_flow}; +use crate::layout::{flow_region, take_cell, take_in_flow}; use crate::runtime::egui::alloc; use crate::runtime::env::Env; use crate::runtime::focus; @@ -129,10 +129,7 @@ impl Lazy { if let Some(view) = view { // A run around this child decides it by the scroll view: it replays only while the view is the same. replay::note_input(&ctx, crate::runtime::inputs::Input::Scroll); - let at = |size: Vec2| { - let x = if rtl { available.max.x - size.x } else { available.min.x }; - Rect::from_min_size(Pos2::new(x, available.min.y), size) - }; + let at = |size: Vec2| Rect::from_min_size(at_start(available, rtl, size), size); let estimate = view.estimate(self.estimate); if self.reveal { crate::contract::queue_child_reveal(&ctx, view.owner, id); @@ -158,14 +155,20 @@ impl Lazy { pixels_per_point: ctx.pixels_per_point(), env: Env::of(ui).key, }; - if real && matches!(key, Key::Memo(_)) { + let memo = matches!(key, Key::Memo(_)); + if real && memo { 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.then_some(available), real.then_some(place), view, key) + && let Some((size, ran_at)) = 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(_)); + let took = if memo { + // A real replay is at the place its run was, so the kept rect is where the run's content went. + let rect = if real { ran_at } else { Rect::from_min_size(at_start(available, rtl, size), size) }; + place_view(ui, rect) + } else { + take_in_flow(ui, size).0 + }; let mut kept = None; if real { layout_memory::backstop(&ctx, id, size); @@ -197,7 +200,11 @@ impl Lazy { if real { layout_memory::backstop(&ctx, id, rect.size()); } - alloc::advance_past(ui, rect); + if memo { + place_view(ui, rect); + } else { + 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, sizing, lazy::Measured { size: rect.size(), key, room: available }, registered); @@ -222,6 +229,21 @@ impl Lazy { } } +/// Where content of `size` starts in `region`: at its start edge, the right one in a right-to-left layout. +fn at_start(region: Rect, rtl: bool, size: Vec2) -> Pos2 { + Pos2::new(if rtl { region.max.x - size.x } else { region.min.x }, region.min.y) +} + +/// Places a memoized view's extent `rect` in flow in `ui`, as its run and its replay both do, and returns `rect`: in the +/// `Grid` or spread `Row` body that encloses `ui` directly the view takes the next cell at its extent, as its content +/// starts a layout room of its own and takes none; elsewhere the flow moves past `rect`. +fn place_view(ui: &mut Ui, rect: Rect) -> Rect { + if take_cell(ui, rect.size()).is_none() { + alloc::advance_past(ui, rect); + } + rect +} + /// What a child came to in a pass. pub(super) enum Shown { /// It ran and returned this. diff --git a/crates/ui-contract/src/layout/body.rs b/crates/ui-contract/src/layout/body.rs index 20a004795..dbe2baa11 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -1037,16 +1037,24 @@ pub fn empty_cell(ui: &mut Ui) { /// `take_in_flow`, with `grid` the body of `ui`'s env that places its children itself. fn take_in(ui: &mut Ui, grid: Option, size: Vec2) -> (Rect, bool) { - let cell = grid.and_then(|body| pass_state::with::(ui.ctx(), |cursors| cursors.0.get_mut(&body).map(|cursor| cursor.cell(size)))); - match cell { - Some(cell) => { - alloc::advance_past(ui, cell); - (cell, true) - } + match cell_in(ui, grid, size) { + Some(cell) => (cell, true), None => (alloc::space(ui, size), false), } } +/// Takes the next cell of the `Grid` or spread `Row` body that encloses `ui` directly for a child of `size`, and returns +/// it; `None`, taking nothing, outside such a body. +pub(crate) fn take_cell(ui: &mut Ui, size: Vec2) -> Option { + cell_in(ui, Env::of(ui).grid, size) +} + +fn cell_in(ui: &mut Ui, grid: Option, size: Vec2) -> Option { + let cell = grid.and_then(|body| pass_state::with::(ui.ctx(), |cursors| cursors.0.get_mut(&body).map(|cursor| cursor.cell(size))))?; + alloc::advance_past(ui, cell); + Some(cell) +} + /// The region the next child shown in flow in `ui` measures and is placed in: that of the next cell of the `Grid` or /// spread `Row` body that encloses `ui` directly, else the rest of `ui`'s layout. pub(crate) fn flow_region(ui: &Ui) -> Rect { diff --git a/crates/ui-contract/src/layout/mod.rs b/crates/ui-contract/src/layout/mod.rs index 80fb38f06..067c1cb03 100644 --- a/crates/ui-contract/src/layout/mod.rs +++ b/crates/ui-contract/src/layout/mod.rs @@ -26,5 +26,5 @@ pub(crate) use body::run_body; pub(crate) use body::{ InFlow, close_first_line, counting_fills, fill_share, fill_size, flow_fill_room, flow_region, flow_room, grid_cell, in_grid_content_column, leaf_fill_room, members_line, open_first_lines, place_in_cell, report_held, report_least, report_shrink, row_alone_room, run_body_measured, - shares_y, shrink_cap, take_in_flow, view_room, + shares_y, shrink_cap, take_cell, take_in_flow, view_room, }; diff --git a/crates/ui-contract/src/runtime/lazy.rs b/crates/ui-contract/src/runtime/lazy.rs index 85435a5e6..8009e2ec4 100644 --- a/crates/ui-contract/src/runtime/lazy.rs +++ b/crates/ui-contract/src/runtime/lazy.rs @@ -366,8 +366,8 @@ 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 room a sizing run offers it, `None` outside a sizing pass). The first run +/// Whether the settling child `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), and the rect the kept real run took. 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 @@ -408,7 +408,7 @@ pub(crate) fn settled( place: Option, view: Option, key: Key, -) -> Option { +) -> Option<(Vec2, Rect)> { let input = ctx.input(|input| { input.events.iter().any(|event| !matches!(event, egui::Event::PointerMoved(_) | egui::Event::MouseMoved(_) | egui::Event::PointerGone)) }); @@ -473,7 +473,7 @@ pub(crate) fn settled( }); return None; } - Some(size) + Some((size, settled.rect)) } /// Whether something a memoized view's run read outside its props moved since: a framework input it read has another diff --git a/crates/ui-widgets/tests/it/body/memo_view.rs b/crates/ui-widgets/tests/it/body/memo_view.rs index a3ef356f3..76f72b3e4 100644 --- a/crates/ui-widgets/tests/it/body/memo_view.rs +++ b/crates/ui-widgets/tests/it/body/memo_view.rs @@ -691,3 +691,52 @@ fn a_view_offered_another_room_in_a_sizing_pass_is_measured_again() { 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"); } + +/// A block as tall as `height`. +#[derive(MemoProps)] +struct Tall { + height: u32, +} + +fn tall(props: &mut Tall, ui: &mut Ui) -> Rect { + craft_ui::testing::layout::Block::new(Vec2::new(100.0, props.height as f32)).show(ui).rect +} + +/// One frame of a two-column grid whose cells each hold a `Lazy` around a memoized block, the first 200 tall and the +/// second 40. Returns the passes and the rect each cell's `Lazy` took in the last real pass. +fn grid_frame(frames: &mut Frames) -> (usize, [Rect; 2]) { + const TWO: &[craft_behavior::ColumnWidth] = &[craft_behavior::ColumnWidth::fill(1.0); 2]; + let took = [Cell::new(Rect::NOTHING), Cell::new(Rect::NOTHING)]; + let passes = frames.frame(vec![], |ui| { + in_region(ui, Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(400.0, 400.0)), |ui| { + craft_ui::layout_components::Grid::new(TWO).show(ui, |ui, _| { + for (at, height) in [200, 40].into_iter().enumerate() { + craft_ui::contract::Lazy::new(at, 0.0).show(ui, |ui| { + let _ = MemoView::new(("cell", at), Tall { height }, tall).show(ui); + if !ui.is_sizing_pass() { + took[at].set(ui.min_rect()); + } + }); + } + }); + }); + }); + (passes, took.map(Cell::into_inner)) +} + +/// A view in a grid cell replays at the extent it ran at, not at its row's height: a replay places its kept extent as +/// its run placed its content, so the body around it finds the measure its kept transcript holds and asks for no +/// discard pass. +#[trial] +fn a_view_in_a_grid_cell_replays_at_the_extent_it_ran_at() { + let mut frames = Frames::new(); + let (_, ran) = grid_frame(&mut frames); + assert!(!ran[0].intersects(ran[1]), "the cells sit beside each other: {ran:?}"); + for _ in 0..WARM { + grid_frame(&mut frames); + } + let (passes, took) = grid_frame(&mut frames); + assert!(last_frame_memo_runs(&frames.ctx).iter().all(|(_, _, run)| *run == MemoRun::Replayed), "an idle frame replays both views"); + assert_eq!(passes, 1, "a replay asks for no discard pass"); + assert_eq!(took, ran, "each cell takes the extent its view ran at"); +}