From 9332e1c0c83a712a7f97dd9613d638b7539bedca Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 18:16:48 +0000 Subject: [PATCH] Fill children of a content-sized body take their measured width: a fill container not given the width offers its body the width it measured, unless it is held in a content column or passes its column's width on; a collection not given the width takes its content width in the real run too, unless held; trial: a content-sized row's fill leaf, held container and list leave the next member after them, inside the row --- crates/ui-contract/src/contract/body.rs | 4 ++ crates/ui-contract/src/contract/body/hooks.rs | 9 +-- .../ui-contract/src/contract/body/inherit.rs | 23 ++++---- crates/ui-contract/src/contract/items.rs | 3 +- .../ui-contract/src/contract/items/hooks.rs | 8 ++- crates/ui-contract/tests/it/layout/fill.rs | 58 ++++++++++++++++++- 6 files changed, 83 insertions(+), 22 deletions(-) diff --git a/crates/ui-contract/src/contract/body.rs b/crates/ui-contract/src/contract/body.rs index b9736dcb7..d984f7b64 100644 --- a/crates/ui-contract/src/contract/body.rs +++ b/crates/ui-contract/src/contract/body.rs @@ -150,6 +150,9 @@ pub(super) struct BodyHooks<'b> { /// A content-sized container that passes the width its column gives it on to its own body (`passes`): it is not /// given the width, but its body is laid out as if it were. passes: bool, + /// A fill-width container held in the content-sized column it sits in (`held`): it shows across the width the + /// column settles on, so its body is offered the room. + held: bool, /// A held fill-width container (`held`) whose content column measured its held children across its width /// (`ColumnSweep::Column`): in the column's second sweep and in the real run after one. Its body is laid out /// across that width as if it were given it. @@ -224,6 +227,7 @@ impl<'b> BodyHooks<'b> { given: (false, false), fixed_height: false, passes: false, + held: false, across: false, in_column: true, follows_end: false, diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 4b6ff0ca2..7b08a5230 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -279,7 +279,8 @@ impl ShapeHooks for BodyHooks<'_> { self.given = self.given_axes(props, &env, at); self.fixed_height = self.fixed_height(props, at); self.passes = self.passes_width(props, &env, at); - self.across = self.held(props, &env) && matches!(env.column, Some(ColumnSweep::Column(_))); + self.held = self.held(props, &env); + self.across = self.held && matches!(env.column, Some(ColumnSweep::Column(_))); self.in_column = label::in_column(ui); self.given.1 |= self.over_room(props, &env, at, ui.ctx(), id); let measured = self.measured_body.unwrap_or(Vec2::ZERO); @@ -336,7 +337,8 @@ impl ShapeHooks for BodyHooks<'_> { self.given = self.given_axes(&props, at.env, at.at); self.fixed_height = self.fixed_height(&props, at.at); self.passes = self.passes_width(&props, at.env, at.at); - self.across = self.held(&props, at.env) && matches!(at.env.column, Some(ColumnSweep::Column(_))); + self.held = self.held(&props, at.env); + self.across = self.held && matches!(at.env.column, Some(ColumnSweep::Column(_))); // A held container across its column measures at the column's width. if let Some(ColumnSweep::Column(width)) = at.env.column.filter(|_| self.across && at.width.is_none()) { self.room.x = width; @@ -786,7 +788,6 @@ impl ShapeHooks for BodyHooks<'_> { // A stack is a content column where its width comes from its content: a content-sized stack in flow, or a // held fill-width container. A stack given its width (a rect it is shown at, or a column's width it passes // on) is a given column, so the width reaches down through nested stacks. - let sizing = self.sizing(&props); let content_column = !self.wide() && matches!(props.layout, layout::BodyLayout::Stack); let layout_id = sub(id, "layout"); // The body is measured in the line the real run gives it: what the header and the end region leave. The @@ -851,7 +852,7 @@ impl ShapeHooks for BodyHooks<'_> { && env.values.get::() == label::LabelScheme::Inspector; let body_env = line_env.derive_local(|scoped| { let label_width = label_extra.x; - self.scope_body(scoped, &props, id, BodyScope { room, real, sizing, content_column, scroll, label_width, beside: regions.beside }); + self.scope_body(scoped, &props, id, BodyScope { room, real, content_column, scroll, label_width, beside: regions.beside }); scoped.inside_label = inside.map(|inside| inside as crate::runtime::env::Held); scoped.cell_fills = cell_fills; // A scrolling body shows its first child wherever the view is: the container has no first line there. diff --git a/crates/ui-contract/src/contract/body/inherit.rs b/crates/ui-contract/src/contract/body/inherit.rs index d668877f0..8d0c00b6e 100644 --- a/crates/ui-contract/src/contract/body/inherit.rs +++ b/crates/ui-contract/src/contract/body/inherit.rs @@ -2,7 +2,7 @@ use super::BodyHooks; use super::reorder::REORDER_SCOPE; -use crate::contract::{Container, ContainerProps, Cx, DragBand, Mount, Scroll, Sizing}; +use crate::contract::{Container, ContainerProps, Cx, DragBand, Mount, Scroll}; use crate::runtime::commands::{self, CommandSet as _, WindowCommand}; use crate::runtime::env::{AppValues, ArenaId, Scoped, ScrollOwner}; use crate::style::{ComponentStyle as _, SurfaceKind}; @@ -13,7 +13,6 @@ use egui::{Context, Id, Vec2}; pub(super) struct BodyScope { pub(super) room: Vec2, pub(super) real: bool, - pub(super) sizing: Sizing, pub(super) content_column: bool, pub(super) scroll: Option, /// The width a side label beside the body took from the container's row. @@ -99,7 +98,7 @@ impl BodyHooks<'_> { /// What the body inherits on top of the line's env: arena, transform, the room it is offered and its scroll axes. pub(super) fn scope_body(&self, scoped: &mut Scoped, props: &ContainerProps, id: Id, body: BodyScope) { - let BodyScope { room, real, sizing, content_column, scroll, label_width, beside } = body; + let BodyScope { room, real, content_column, scroll, label_width, beside } = body; if props.arena { scoped.arena = Some(ArenaId::new(id)); } @@ -109,19 +108,17 @@ impl BodyHooks<'_> { scoped.row_room = None; scoped.row_line = None; scoped.content = (!self.wide(), !self.given.1); - // A content-sized container offers its children, in the real run, the width this pass measured it at: a - // child that fits itself into its room (a bar moving items into "More") fits the width the container - // takes, not the room it could have taken. A fill container that is not given the width still takes at - // least the room (`regions`), so it offers the room, and so does a container that passes its column's width - // on: its measuring run offered the room too. + // A container not given the width offers its children, in the real run, the width this pass measured it at: + // its parent sizes from its content, so a fill container there is shown at its measured width like a + // content-sized one, and a child that fits itself into its room (a bar moving items into "More") fits the + // width the container takes, not the room it could have taken. A fill container held in a content-sized + // column shows across the width the column settles on, so it offers the room, and so does a container that + // passes its column's width on: its measuring run offered the room too. // The regions beside the body on its line and a side label's column are not the body's: the children are // offered what is left beside them. scoped.offered = (!self.given.0 && room.x.is_finite()).then(|| { - let offered = if real && sizing == Sizing::Content && !self.passes { - self.wanted_width(props).map_or(room.x, |wanted| wanted.min(room.x)) - } else { - room.x - }; + let offered = + if real && !self.held && !self.passes { self.wanted_width(props).map_or(room.x, |wanted| wanted.min(room.x)) } else { room.x }; (offered - beside - label_width).max(0.0) }); if scroll.is_some() || props.scroll_owner { diff --git a/crates/ui-contract/src/contract/items.rs b/crates/ui-contract/src/contract/items.rs index 5c454f16c..d2ce1b11e 100644 --- a/crates/ui-contract/src/contract/items.rs +++ b/crates/ui-contract/src/contract/items.rs @@ -148,7 +148,8 @@ pub(super) struct ItemsHooks<'s, 'r, C: Collection> { /// One step of a tree's indent: the icon side at the collection's size, as its rows indent. indent_step: f32, /// The axes (x, y) the collection takes from its content under a body sized from its content: the height in every - /// run, the width in a measuring run (the real run shows a collection that fills across the column it is held in). + /// run, the width in every run except the real run of a collection held in a content-sized column, which shows + /// across the width the column settles on. from_content: (bool, bool), scrolls: bool, /// The collection senses the wheel itself (a tab strip stepping its tabs): the wheel is its own, not the rows' diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index acbcd37ab..4b234c126 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -178,9 +178,11 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { self.indent_step = scx.control().icon; let env = Env::of(ui); let room = Self::inner_room(comp, &env.tokens, room); - // Where the enclosing body does not give the width, a measuring run takes the collection's width from its - // content, whether it fills or fills the width, as a container does. - self.from_content = (!env.gives_width() && crate::runtime::measure::active(ui.ctx()), env.content.1); + // Where the enclosing body does not give the width, the collection takes its width from its content, whether + // it fills or fills the width, as a container does. Held in a content-sized column it measures so, and shows + // across the width the column settles on. + let held = env.content_column || crate::layout::in_grid_content_column(ui); + self.from_content = (!env.gives_width() && (crate::runtime::measure::active(ui.ctx()) || !held), env.content.1); self.header.measure(scx); self.header.read_content(ui.ctx(), id); let bounded = room.x.is_finite(); diff --git a/crates/ui-contract/tests/it/layout/fill.rs b/crates/ui-contract/tests/it/layout/fill.rs index 84557b9ab..635f450d5 100644 --- a/crates/ui-contract/tests/it/layout/fill.rs +++ b/crates/ui-contract/tests/it/layout/fill.rs @@ -2,8 +2,12 @@ //! that scrolls along y the children that fill its height share what the view leaves, at least their content. A child //! that fills a bounded room never takes less than its least; in an unbounded one it keeps its ideal. +use crate::collection::kit::{Count, List, Row as ListRow}; use craft_behavior::{Proposal, Sense}; -use craft_ui::contract::{Common, Component, ContainerProps, Cx, MeasureCx, Props as _, Role, Scroll, Show as _, ShowContainer as _, Sizing}; +use craft_ui::contract::{ + Common, Component, ContainerProps, Cx, MeasureCx, Props as _, Role, Scroll, SelectMode, Show as _, ShowCollection as _, ShowContainer as _, + Sizing, +}; use craft_ui::layout::BodyLayout; use craft_ui::style::StyleCx; use craft_ui::testing::layout::Block; @@ -266,3 +270,55 @@ fn fill_children_measure_once_per_sweep() { assert_eq!(sweeps.get(), 0, "{name}: unchanged inputs ran the body's measuring run"); } } + +/// What fills the start of the `content_row` rig. +#[derive(Clone, Copy, Debug)] +enum First { + /// A leaf that fills the width. + Leaf, + /// A fill-width container whose body holds that leaf. + Held, + /// A list that fills. + List, +} + +/// The row, what fills its start and the member after it, in a content-sized `Row` inside a content-sized `Stack`. +fn content_row(first: First) -> [Rect; 3] { + let seen = Rc::new(Cell::new([Rect::NOTHING; 3])); + let out = Rc::clone(&seen); + let mut fixture = Fixture::new(move |ui| { + let (sink, after) = (RefCell::new(Seen::default()), RefCell::new(Seen::default())); + let asked = Asked::default(); + let rects = Panel::new().id_salt("column").show(ui, |ui, _| { + let row = Panel::new().layout(BodyLayout::row()).id_salt("row").show(ui, |ui, _| { + let leaf = |ui: &mut egui::Ui| Stretch::new(&sink, &asked).sizing(Sizing::FillWidth).id_salt("fill").show(ui).rect; + let filled = match first { + First::Leaf => leaf(ui), + First::Held => Panel::new().sizing(Sizing::FillWidth).id_salt("held").show(ui, |ui, _| leaf(ui)).inner.unwrap_or(Rect::NOTHING), + First::List => List::new(SelectMode::None).id_salt("list").show_items(ui, &Count(8), &mut |_, _| ListRow::new()).response.rect, + }; + let next = Probe::new(&after).size(Vec2::new(100.0, LINE)).id_salt("after").show(ui).rect; + [filled, next] + }); + let [filled, next] = row.inner.unwrap_or([Rect::NOTHING; 2]); + [row.output.rect, filled, next] + }); + out.set(rects.inner.unwrap_or([Rect::NOTHING; 3])); + }); + fixture.room(Vec2::new(900.0, 400.0)).settle(); + seen.get() +} + +// A row sized from its content gives a child that fills its width the width it measured, not the row's room: the +// member after it follows it, and both stay inside the row. +#[trial] +fn a_fill_child_of_a_content_sized_row_takes_its_measured_width_and_the_next_member_follows_it() { + for first in [First::Leaf, First::Held, First::List] { + let [row, fill, after] = content_row(first); + assert!(fill.width() > 0.0, "{first:?}: the fill child shows: {fill:?}"); + assert!(fill.max.x <= after.min.x + 0.5, "{first:?}: the member after the fill child follows it: {fill:?}, {after:?}"); + assert!(row.contains_rect(fill) && row.contains_rect(after), "{first:?}: both stay inside the row {row:?}: {fill:?}, {after:?}"); + } + let [_, leaf, _] = content_row(First::Held); + assert!(leaf.width() >= IDEAL - 0.5, "the held leaf shows at least its ideal: {leaf:?}"); +}