From fb0a7756ff06e30ed648fa8184072bd29b42d4c9 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 15:00:56 -0800 Subject: [PATCH] ui-contract: a memoized view's content keeps the flow start and gap of the Stack around it A MemoView started a layout room of its own, so its content lost the flow start and the Stack gap of the body around it. A section shown in a MemoView under SectionRhythm::Spaced then took the full section gap above its rule instead of the gap less the Stack's, and the first section under a heading title drew a rule it does not draw outside the view: 17 pt lower at the top and 4 pt lower for each later section in the new test. The view's direct children now go on with the flow the view stands in: the first starts that flow when the view does, after a rule when the view is, and each keeps the gap a Stack leaves before the view. Where the view stands in that flow is part of its place, so a view that moves from a body's start to a later cell runs again instead of replaying. --- .../ui-contract/src/contract/body/inherit.rs | 1 + .../ui-contract/src/contract/body/regions.rs | 2 +- crates/ui-contract/src/contract/lazy.rs | 10 +- crates/ui-contract/src/contract/memo_view.rs | 11 ++- crates/ui-contract/src/layout/body.rs | 25 ++++- crates/ui-contract/src/layout/mod.rs | 6 +- crates/ui-contract/src/runtime/env.rs | 58 ++++++++++-- crates/ui-panels/tests/it/sections/rhythm.rs | 93 +++++++++++++++++++ 8 files changed, 187 insertions(+), 19 deletions(-) diff --git a/crates/ui-contract/src/contract/body/inherit.rs b/crates/ui-contract/src/contract/body/inherit.rs index 3a232d1d90..63ce185b7a 100644 --- a/crates/ui-contract/src/contract/body/inherit.rs +++ b/crates/ui-contract/src/contract/body/inherit.rs @@ -124,6 +124,7 @@ impl BodyHooks<'_> { scoped.after_rule = props.header.is_some_and(|header| (header.rule || (header.heads && heads)) && !header.inline) || (props.header.is_none() && props.surface.is_none() && props.mount == Mount::Inline && self.after_rule); scoped.flow_body = None; + scoped.view_flow = None; scoped.column_row = self.in_column && matches!(props.layout, crate::layout::BodyLayout::Row { wrap: false, align: crate::layout::RowAlign::Start }); // Children side by side share the layer's width, so none of them spans it: a collection in a row or a grid takes diff --git a/crates/ui-contract/src/contract/body/regions.rs b/crates/ui-contract/src/contract/body/regions.rs index d3b73ff6a0..20efb82c40 100644 --- a/crates/ui-contract/src/contract/body/regions.rs +++ b/crates/ui-contract/src/contract/body/regions.rs @@ -101,7 +101,7 @@ impl RuleBands { let above = on(start != Some(true) && props.header.is_some_and(|header| header.rule_above)); let spaced = above > 0.0 && env.get::() == SectionRhythm::Spaced; let gaps = &tokens.tokens().space.gap; - let stack_gap = env.stack_gap.filter(|(body, _)| env.flow_body == Some(*body)).map_or(0.0, |(_, gap)| gap); + let stack_gap = env.flow_gap().unwrap_or(0.0); RuleBands { lead: if spaced && start.is_none() { (gaps.section - stack_gap).max(0.0) } else { 0.0 }, above, diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index e52c88d6d2..2644823b53 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -3,7 +3,7 @@ use crate::layout::{flow_region, take_cell, take_in_flow}; use crate::runtime::egui::alloc; -use crate::runtime::env::{Env, Shrink}; +use crate::runtime::env::{Env, Flow, Shrink}; use crate::runtime::focus; use crate::runtime::layout_memory; use crate::runtime::lazy::{self, Call, Decision, Key, Place}; @@ -149,7 +149,7 @@ impl Lazy { access: crate::runtime::egui::access::builds_nodes(&ctx), opacity: ui.opacity(), env: env.key, - flow: env.flow(), + flow: flow_around(&ctx, &env, memo), }; if real && memo { crate::runtime::memory::claim_id(&ctx, id, "two memoized views share an id in one pass"); @@ -314,6 +314,12 @@ fn open_record(ctx: &egui::Context, audited: bool) { replay::open(ctx); } +/// The flow facts of the body around a child shown under `env` (`Place::flow`). A memoized view's also hold where it +/// stands in that body's flow, which its content goes on with (`layout::open_view_flow`). +fn flow_around(ctx: &egui::Context, env: &Env, memo: bool) -> Flow { + if memo { env.flow().in_flow(crate::layout::flow_start(ctx, env), env.flow_gap()) } else { env.flow() } +} + /// 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) diff --git a/crates/ui-contract/src/contract/memo_view.rs b/crates/ui-contract/src/contract/memo_view.rs index 886bb5c688..737a9d4620 100644 --- a/crates/ui-contract/src/contract/memo_view.rs +++ b/crates/ui-contract/src/contract/memo_view.rs @@ -50,7 +50,8 @@ use std::sync::Arc; /// theme, density, direction, language, window active) and the flow of the body around it (whether that body is a /// content-sized or a given column, the axes it sizes from its content, the axis it shares among the children that /// fill it, a least measure, the axes a scroll leaves unbounded, a beside label's cell, whether its leaves fit the -/// room), compared for every view and for each kind of run on its own; +/// room, whether it starts that body's flow and the gap a `Stack` body leaves before it), compared for every view and +/// for each kind of run on its own; /// - what the scope around it hands down beyond those (enabled, read-only, size, ink, the command chain, motion, every /// app value, and the rest of `Scoped::scope_key`), in the key; /// - the framework inputs it reads outside its props (`runtime::inputs`: the modifiers, the pointer, the keys held, the @@ -67,7 +68,9 @@ use std::sync::Arc; /// no version: the containers inside measure as outside any view. /// /// Its content is a layout room of its own, as a pane's is: the flow of the body around it (a column's sweep, a grid's -/// cursor) does not reach in, so the room it is given reaches it only through its place. To the body +/// cursor) does not reach in, so the room it is given reaches it only through its place. Its direct children go on with +/// the flow the view stands in: the first one starts that flow when the view does, and each one keeps the gap a `Stack` +/// body leaves before the view, so a section shown in the view stands where it stands shown without it. 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. When the row narrows it, the width the row gives it is the room of its place; otherwise it shows as in no @@ -154,11 +157,13 @@ impl MemoVie // that body sizes from its content, whether its leaves fit their room and which axis it shares among the // children that fill it (`Scoped::start_view_room`), so the view takes what its content used there, as the // same content shown without the view would. + // Its direct children go on with the flow the view stands in (`layout::open_view_flow`). // Its key is its content's version: a body inside measures under it (`Scoped::view_version`). + let flow = crate::layout::open_view_flow(&ctx, &env, id); let installed = Env::install_for( ui, env.derive_local(|scoped| { - scoped.start_view_room(id); + scoped.start_view_room(id, flow); scoped.view_version = follows.then_some(key); }), ); diff --git a/crates/ui-contract/src/layout/body.rs b/crates/ui-contract/src/layout/body.rs index a98971add0..edf92773db 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -364,12 +364,31 @@ pub(crate) struct FlowStarts(IdMap); /// Where the next child placed in flow under `env` starts: `Some(edge)` while the body that places it has placed /// nothing yet in this sweep or run, with `after_rule` when a rule precedes that body's start (`Scoped::after_rule`); -/// `None` once a child took room there, and outside a body. +/// `None` once a child took room there, and outside a body. The children of a memoized view's content read the flow +/// they go on with (`Scoped::flow_of`). pub(crate) fn flow_start(ctx: &egui::Context, env: &Env) -> Option { - let body = env.flow_body?; + let body = env.flow_of()?; pass_state::with::(ctx, |starts| starts.0.get(&body).copied()) } +/// Opens the flow of the content of the memoized view `view`, which is placed in flow under `env`, and returns its id +/// (`Scoped::view_flow`); `None` when no flow places the view. The content's first child starts that flow when the +/// view starts the flow it stands in, after a rule when the view is; otherwise something precedes it there. +pub(crate) fn open_view_flow(ctx: &egui::Context, env: &Env, view: Id) -> Option { + env.flow_of()?; + let flow = sub(view, "flow"); + let start = flow_start(ctx, env); + pass_state::with::(ctx, |starts| match start { + Some(after_rule) => { + starts.0.insert(flow, after_rule); + } + None => { + starts.0.remove(&flow); + } + }); + Some(flow) +} + /// Whether the next child placed in flow under `env` starts its body's flow (`flow_start`). pub(crate) fn at_flow_start(ctx: &egui::Context, env: &Env) -> bool { flow_start(ctx, env).is_some() @@ -377,7 +396,7 @@ pub(crate) fn at_flow_start(ctx: &egui::Context, env: &Env) -> bool { /// A child took room in the flow of the body that places the children under `env`: its flow start closes. pub(crate) fn close_flow_start(ctx: &egui::Context, env: &Env) { - if let Some(body) = env.flow_body { + if let Some(body) = env.flow_of() { pass_state::with::(ctx, |starts| starts.0.remove(&body)); } } diff --git a/crates/ui-contract/src/layout/mod.rs b/crates/ui-contract/src/layout/mod.rs index 7912e0af94..05ffc0ef11 100644 --- a/crates/ui-contract/src/layout/mod.rs +++ b/crates/ui-contract/src/layout/mod.rs @@ -27,7 +27,7 @@ pub(crate) use body::run_body; pub(crate) use body::{ BodyLine, BracketTook, HeldKind, InFlow, ask_shortfall, at_flow_start, close_first_line, close_flow_start, count_fill_view, counting_fills, fill_share, fill_size, flow_fill_room, flow_region, flow_room, flow_start, grid_cell, held_kind, in_grid_content_column, leaf_fill_room, - members_line, open_first_lines, open_view_fill, place_in_cell, report_bracket_control, report_fill_least, report_held, report_least, - report_shrink, row_alone_room, run_body_measured, shares_y, shrink_cap, take_bracket, take_cell, take_in_flow, take_shortfall, take_view_fill, - view_room, + members_line, open_first_lines, open_view_fill, open_view_flow, place_in_cell, report_bracket_control, report_fill_least, report_held, + report_least, report_shrink, row_alone_room, run_body_measured, shares_y, shrink_cap, take_bracket, take_cell, take_in_flow, take_shortfall, + take_view_fill, view_room, }; diff --git a/crates/ui-contract/src/runtime/env.rs b/crates/ui-contract/src/runtime/env.rs index dea72c2f8a..1116ce6b91 100644 --- a/crates/ui-contract/src/runtime/env.rs +++ b/crates/ui-contract/src/runtime/env.rs @@ -80,10 +80,11 @@ pub(crate) struct FooterSlotOf { /// The flow facts of the body around a memoized view that its content is laid out by: whether that body is a /// content-sized or a given column, the axes it sizes from its content, the axis it shares among the children that fill /// it, whether the view runs in a least measure, the -/// axes a scrolling body leaves unbounded, whether it fills a beside label's cell and whether its leaves fit the view's -/// room (`Scoped::view_fits`). A measuring run of a container or a row lays the view out under other facts than its -/// real run, so they are a fact of the view's place, which each kind of run compares on its own (`lazy::Place`, -/// `lazy::Measured`), not of its key. +/// axes a scrolling body leaves unbounded, whether it fills a beside label's cell, whether its leaves fit the view's +/// room (`Scoped::view_fits`), and where the view stands in that body's flow: whether it starts the flow, after a rule +/// or not (`layout::flow_start`), and the gap a `Stack` body leaves before it (`Scoped::flow_gap`). A measuring run of a +/// container or a row lays the view out under other facts than its real run, so they are a fact of the view's place, +/// which each kind of run compares on its own (`lazy::Place`, `lazy::Measured`), not of its key. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub(crate) struct Flow { content_column: bool, @@ -95,6 +96,17 @@ pub(crate) struct Flow { cell_fills: bool, fits: bool, egui_cell: bool, + start: Option, + /// The bits of the gap, so the facts compare exactly. + gap: Option, +} + +impl Flow { + /// These facts for a view that stands at `start` in the flow of the body around it (`layout::flow_start`), with + /// `gap` the gap a `Stack` body leaves before it (`Scoped::flow_gap`). + pub(crate) fn in_flow(self, start: Option, gap: Option) -> Flow { + Flow { start, gap: gap.map(f32::to_bits), ..self } + } } /// The values of an env. @@ -173,8 +185,13 @@ pub struct Scoped { /// and the one a child placed in flow closes. pub(crate) flow_body: Option, /// The `Stack` body that places the scope's direct children in flow and the gap it leaves between them; it holds - /// only while that body is `flow_body`. + /// only while that body is the scope's flow (`flow_of`). pub(crate) stack_gap: Option<(Id, f32)>, + /// The flow of a memoized view's content, when the view stands in a body's flow: the scope's direct children go on + /// with that body's flow from where the view stands in it. The first one starts the flow when the view does + /// (`layout::open_view_flow`), and each one follows the gap a `Stack` body left before the view (`stack_gap`), as + /// the same children shown in that body without the view would. + pub(crate) view_flow: Option, /// A rule precedes the start of the body this scope opens: its container draws one under its header, or draws /// nothing of its own and itself follows one. The body's layout opens its flow start with it. pub(crate) after_rule: bool, @@ -488,6 +505,7 @@ impl Scoped { self.row_line = None; self.first_line = None; self.flow_body = None; + self.view_flow = None; self.column_row = false; self.fill_body = None; self.fill_share = None; @@ -508,6 +526,7 @@ impl Scoped { || self.row_line.is_some() || self.first_line.is_some() || self.flow_body.is_some() + || self.view_flow.is_some() || self.column_row || self.fill_body.is_some() || self.fill_share.is_some() @@ -529,10 +548,14 @@ impl Scoped { /// body, given its share as its room, as the child shown without the view would be. A view that is a unit of a /// shrinking `Row` that does not share its width 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, view: Id) { + /// + /// `flow` is the flow the content's direct children go on with (`view_flow`, from `layout::open_view_flow`): they + /// keep the gap a `Stack` body around the view leaves before it. + pub(crate) fn start_view_room(&mut self, view: Id, flow: Option) { let fits = self.view_fits(); let row_unit = matches!(self.shrink, Some(Shrink::Count(_) | Shrink::Least(_) | Shrink::Apply(_))); let shares = self.shares(); + let gap = self.flow_gap(); let (content_column, given_column, egui_cell) = (self.content_column, self.given_column, self.egui_cell); // The view stands on the line of the row around it only to fill it, as a levelled unit does: a fill-width row in // it (a bar in a shell slot) takes the line's height and centres on it, as shown without the view; its children @@ -541,6 +564,8 @@ impl Scoped { self.start_room(); (self.content_column, self.given_column, self.egui_cell) = (content_column, given_column, egui_cell); self.row_line = line; + self.view_flow = flow; + self.stack_gap = flow.zip(gap); self.fill_view = shares.map(|axis| (view, axis)); if fits { self.shrink = Some(Shrink::Fit); @@ -550,6 +575,21 @@ impl Scoped { } } + /// The flow that places the scope's direct children: the enclosing body's (`flow_body`), else the flow a memoized + /// view's content goes on with (`view_flow`). + pub(crate) const fn flow_of(&self) -> Option { + match self.flow_body { + Some(body) => Some(body), + None => self.view_flow, + } + } + + /// The gap a `Stack` body leaves before each of the scope's direct children it places in flow after its first + /// (`stack_gap`), while that body is the scope's flow (`flow_of`). + pub(crate) fn flow_gap(&self) -> Option { + self.stack_gap.filter(|(body, _)| self.flow_of() == Some(*body)).map(|(_, gap)| gap) + } + /// The axis the enclosing body shares among its direct children that fill it (`fill_body`). pub(crate) const fn shares(&self) -> Option { match self.fill_body { @@ -606,7 +646,8 @@ impl Scoped { }) } - /// The flow facts of the enclosing body a memoized view shown in this scope is laid out by (`Flow`). + /// The flow facts of the enclosing body a memoized view shown in this scope is laid out by (`Flow`), without where + /// the view stands in that body's flow (`Flow::in_flow`). pub(crate) const fn flow(&self) -> Flow { Flow { content_column: self.content_column, @@ -618,6 +659,8 @@ impl Scoped { cell_fills: self.cell_fills, fits: self.view_fits(), egui_cell: self.egui_cell, + start: None, + gap: None, } } @@ -758,6 +801,7 @@ impl Env { first_line: None, flow_body: None, stack_gap: None, + view_flow: None, after_rule: false, column_row: false, bracket: None, diff --git a/crates/ui-panels/tests/it/sections/rhythm.rs b/crates/ui-panels/tests/it/sections/rhythm.rs index 4fe61c1b70..5693fa493a 100644 --- a/crates/ui-panels/tests/it/sections/rhythm.rs +++ b/crates/ui-panels/tests/it/sections/rhythm.rs @@ -46,3 +46,96 @@ fn a_spaced_section_stands_its_rule_off_the_row_above_and_its_header() { assert!(spaced_above >= section_gap - 0.5, "the spaced rule stands a section gap ({section_gap}) off the row, not {spaced_above}"); assert!(spaced_below > tight_below, "the spaced header stands further below its rule ({spaced_below}) than the tight one ({tight_below})"); } + +/// A section that returns where its body's text landed, to show directly or as a memoized view's content. +fn section_at(title: &mut &'static str, ui: &mut Ui) -> Rect { + let mut body = Rect::NOTHING; + Section::fixed(*title).show(ui, |ui, _| body = Text::new("Width").show(ui).rect); + body +} + +/// Where the bodies of three sections land under `SectionRhythm::Spaced`: in a quiet-titled panel whose title heads its +/// body (`QuietHeader::Heads`) and in a plain stack after a row, each section shown directly or, with `memo`, in a +/// `MemoView` of its own. +fn section_bodies(memo: bool) -> Vec { + use craft_ui::contract::{MemoView, QuietHeader}; + use craft_ui::sections::{HeaderStrength, Panel}; + const TITLES: [&str; 3] = ["Transform", "Appearance", "Stroke"]; + let mut frames = Frames::new(); + let mut root = RootEnv::default(); + root.values.set(SectionRhythm::Spaced); + root.values.set(QuietHeader::Heads); + frames.root(root); + let sections = |ui: &mut Ui, bodies: &mut Vec| { + for (at, title) in TITLES.into_iter().enumerate() { + let body = if memo { MemoView::new(("section", at), title, section_at).show(ui) } else { Some(section_at(&mut { title }, ui)) }; + bodies.push(body.unwrap_or(Rect::NOTHING)); + } + }; + let mut bodies = Vec::new(); + let mut show = |ui: &mut Ui| { + bodies.clear(); + Panel::new("Properties").titled().strength(HeaderStrength::Quiet).show(ui, |ui, _| sections(ui, &mut bodies)); + Stack::new().show(ui, |ui, _| { + let _ = Text::new("Fill").show(ui); + sections(ui, &mut bodies); + }); + }; + frames.idle(4, &mut show); + bodies +} + +/// Regression: a section shown in a `MemoView` in a spaced stack lost the stack's gap and flow start, so under +/// `QuietHeader::Heads` the first one drew the rule above it with a section gap of room and each later one stood a +/// stack gap further down. A memoized section lands where the same section shown directly does, on its first run and +/// on its replays. +#[trial] +fn a_memoized_section_in_a_spaced_stack_lands_where_a_direct_one_does() { + let direct = section_bodies(false); + let memoized = section_bodies(true); + assert_eq!(direct.len(), 6, "every section shows its body"); + assert!(direct.iter().all(|body| body.is_positive()), "every section's body is placed: {direct:?}"); + assert_eq!(memoized, direct, "memoized sections land where direct ones do"); +} + +/// Where the bodies of two sections land in a spaced two-column grid of equal, top-aligned columns, in the last frame +/// after they swap cells, each shown directly or, with `memo`, in a `MemoView` of its own. The first cell starts the +/// grid's flow and the second does not, and both cells give a section a room of the same size. +fn bodies_after_a_swap(memo: bool) -> [Rect; 2] { + use craft_behavior::ColumnWidth; + use craft_ui::contract::MemoView; + use craft_ui::layout::CellAlign; + use craft_ui::layout_components::Grid; + static HALVES: [ColumnWidth; 2] = [ColumnWidth::fill(0.5), ColumnWidth::fill(0.5)]; + const TITLES: [&str; 2] = ["Stroke", "Fill"]; + let mut frames = Frames::new(); + let mut root = RootEnv::default(); + root.values.set(SectionRhythm::Spaced); + frames.root(root); + let mut bodies = [Rect::NOTHING; 2]; + let mut show = |ui: &mut Ui, swapped: bool| { + Grid::new(&HALVES).fill().cells(CellAlign::Top).show(ui, |ui, _| { + for cell in 0..2 { + let at = if swapped { 1 - cell } else { cell }; + bodies[at] = if memo { + MemoView::new(("section", at), TITLES[at], section_at).show(ui).unwrap_or(Rect::NOTHING) + } else { + section_at(&mut { TITLES[at] }, ui) + }; + } + }); + }; + frames.idle(3, |ui| show(ui, false)); + frames.idle(3, |ui| show(ui, true)); + bodies +} + +/// Regression: a memoized section that ran in the cell that starts its body's flow replayed that run in a cell of the +/// same size after it, without the room a section after another stands under, and the other the other way round. A view +/// whose place in its body's flow changed runs again. +#[trial] +fn a_memoized_section_runs_again_when_its_place_in_the_flow_changes() { + let direct = bodies_after_a_swap(false); + assert!(direct.iter().all(|body| body.is_positive()), "every section's body is placed: {direct:?}"); + assert_eq!(bodies_after_a_swap(true), direct, "memoized sections land where direct ones do"); +}