Merge pull request #4359 from dotCooCoo/fix/memo-view-stack-gap

ui-contract: a memoized view's content keeps the flow start and gap of the Stack around it
This commit is contained in:
BFlat authored and GitHub committed 2026-11-30 09:58:15 +07:00
commit 9e01ebc239
8 files changed
+187 -19

No files matched your search

@@ -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
@@ -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>() == 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,
+8 -2
View File
@@ -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)
+8 -3
View File
@@ -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<P: MemoProps, R: Clone + PartialEq + Shift + Send + Sync + 'static> 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);
}),
);
+22 -3
View File
@@ -364,12 +364,31 @@ pub(crate) struct FlowStarts(IdMap<bool>);
/// 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<bool> {
let body = env.flow_body?;
let body = env.flow_of()?;
pass_state::with::<FlowStarts, _>(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<Id> {
env.flow_of()?;
let flow = sub(view, "flow");
let start = flow_start(ctx, env);
pass_state::with::<FlowStarts, _>(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::<FlowStarts, _>(ctx, |starts| starts.0.remove(&body));
}
}
+3 -3
View File
@@ -26,7 +26,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,
};
+51 -7
View File
@@ -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<bool>,
/// The bits of the gap, so the facts compare exactly.
gap: Option<u32>,
}
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<bool>, gap: Option<f32>) -> 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<Id>,
/// 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<Id>,
/// 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<Id>) {
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<Id> {
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<f32> {
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<craft_behavior::Axis> {
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,
@@ -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<Rect> {
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<Rect>| {
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");
}