diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 8dfa74c621..dce04dbb3e 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -294,7 +294,7 @@ impl ShapeHooks for BodyHooks<'_> { 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); + self.given.1 |= self.given_room(props, &env, at, ui.ctx(), id); let measured = self.measured_body.unwrap_or(Vec2::ZERO); let body = if self.presence.visible { Vec2::new(measured.x, measured.y * self.presence.t) } else { Vec2::ZERO }; let room = self.room.x - 2.0 * self.pads(props, scx.tokens).x; @@ -317,7 +317,7 @@ impl ShapeHooks for BodyHooks<'_> { let plan = self.measure_plan(at, cx.core.id, scrolled_share); let (vertical, pad) = (plan.vertical, plan.pad); let took = match plan.reused { - Some(reused) => self.take_reused(at, plan, reused), + Some(reused) => self.take_reused(plan, reused), // A share of a scrolling view is laid out as given while the content fits it, so the children that fill // share it; taller, the body takes its content height (the children that fill at their least) and the view // scrolls. @@ -1808,6 +1808,7 @@ impl BodyHooks<'_> { None => self.mount_room(&props, at.ui, id, at.at, at.scx.tokens), }; self.given = self.given_axes(&props, at.env, at.at); + self.given.1 |= self.given_room(&props, at.env, at.at, at.ui.ctx(), id); self.fixed_height = self.fixed_height(&props, at.at); self.passes = self.passes_width(&props, at.env, at.at); self.held = self.held(&props, at.env); @@ -1871,14 +1872,9 @@ impl BodyHooks<'_> { } /// A record that serves: it stands for the run, and a kept one is noted under this run's inputs too. - fn take_reused(&mut self, at: &Measuring<'_, '_>, plan: MeasurePlan, (took, found): (Vec2, bool)) -> Vec2 { - let (ctx, id) = (&plan.ctx, plan.id); + fn take_reused(&mut self, plan: MeasurePlan, (took, found): (Vec2, bool)) -> Vec2 { if found { - layout_memory::kept_also_under(ctx, id, plan.key); - } - // A capped body whose content outgrew its room was laid out as given it: the record of its content says so. - if self.over_room(&self.props, at.env, at.at, ctx, id) { - self.given.1 = true; + layout_memory::kept_also_under(&plan.ctx, plan.id, plan.key); } took } @@ -1893,6 +1889,8 @@ impl BodyHooks<'_> { } /// The measuring run of a capped body: its content height while that fits the room, else laid out as given the room. + /// A body already given its room (`given_room`: on its row's settled line, which holds the content the row measured) + /// fits it as laid out. fn measure_capped(&mut self, comp: &mut C, at: &Measuring<'_, '_>, cx: &mut Cx<'_, C>, plan: MeasurePlan) -> Vec2 { let fits = self.room.y + plan.tolerance; let (content, kept) = crate::contract::draft(&plan.ctx, || self.measure_run(comp, at, cx, plan.key.clone(), None), |took| took.y <= fits); diff --git a/crates/ui-contract/src/contract/body/size.rs b/crates/ui-contract/src/contract/body/size.rs index 835952520f..a3fa5d83df 100644 --- a/crates/ui-contract/src/contract/body/size.rs +++ b/crates/ui-contract/src/contract/body/size.rs @@ -119,8 +119,8 @@ impl<'b> BodyHooks<'b> { /// A container that fits its content in a bar (`ContainerProps::content_in_bar`) is not given the width there. /// A fill-width container whose body is a row, shown on the line of a row given its height, is given that height /// too: the line is a fixed cross extent, and the container fills it (a bar in a shell slot). A container that - /// fills its height on the line of a row that takes its height from its content is given the line's height once - /// the row's measuring run settled it (`line_height`). + /// fills its height where its parent sizes from its content is capped (`capped`), and `given_room` decides whether + /// it is given its room. pub(super) fn given_axes(&self, props: &ContainerProps, env: &Env, at: ShowAt) -> (bool, bool) { let sizing = self.sizing(props); let fills_line = sizing == Sizing::FillWidth && unwrapped_row(props) && env.row_line.is_some(); @@ -128,22 +128,23 @@ impl<'b> BodyHooks<'b> { let (x, y) = (!sizing.content_width() && !fits_bar, sizing.fills_height() || fills_line); match (at, &props.mount) { (ShowAt::Rect(_), _) => (true, true), - (ShowAt::Flow, Mount::Inline) => (x && env.gives_width(), y && (!env.content.1 || self.line_height(props, env, at).is_some())), + (ShowAt::Flow, Mount::Inline) => (x && env.gives_width(), y && !env.content.1), (ShowAt::Flow, _) => (x, y), } } /// The height of the line an inline container that fills its height stands on, where `env` is its parent's: the /// line of an unwrapped row that takes its height from its content, once this pass's measuring run of the row - /// settled it. The container takes that height (`layout::fill_size`) and its body runs in it, so the children in - /// it that fill share it: a ribbon group as tall as the tallest group, its title at its foot. In the row's - /// measuring run there is no line yet, and the container measures its content. + /// settled it. The container's room is that height (`mount_room`), and it is given the room (`given_room`): its + /// body runs in it, so the children in it that fill share it (a ribbon group as tall as the tallest group, its + /// title at its foot). In the row's measuring run there is no settled line, not even where the row shares a line + /// with an inline header or an end region, and the container measures its content. pub(super) fn line_height(&self, props: &ContainerProps, env: &Env, at: ShowAt) -> Option { let inline = matches!((at, &props.mount), (ShowAt::Flow, Mount::Inline)); if !(inline && self.sizing(props).fills_height() && env.content.1) { return None; } - env.row_line.and_then(|line| line.height).filter(|height| height.is_finite() && *height > 0.0) + env.row_line.filter(|line| line.settled).and_then(|line| line.height).filter(|height| height.is_finite() && *height > 0.0) } /// Whether a content-sized inline container passes the width its column gives it on to its own body, where `env` @@ -208,13 +209,15 @@ impl<'b> BodyHooks<'b> { && !scroll_of(props).is_some_and(|scroll| scroll.vertical_on()) } - /// Whether a capped container's content is taller than its room, as this pass's measure found it (the record - /// `content` its measuring run leaves then): it takes the room and lays its children out as given the height there, - /// so the children that fill share it. - pub(super) fn over_room(&self, props: &ContainerProps, env: &Env, at: ShowAt, ctx: &Context, id: Id) -> bool { + /// Whether a capped container is given its room, so it lays its children out as given the height there and the + /// children that fill share it: the room is the settled line of the row it stands on (`line_height`), or its + /// content is taller than the room, as this pass's measure found it (the record `content` its measuring run leaves + /// then). Otherwise it takes its content height. + pub(super) fn given_room(&self, props: &ContainerProps, env: &Env, at: ShowAt, ctx: &Context, id: Id) -> bool { let tolerance = measure_tolerance(crate::runtime::egui::scale::pixels_per_point(ctx)); self.capped(props, env, at) - && layout_memory::measured(ctx, sub(id, "content")).is_some_and(|content| content.took.y > self.room.y + tolerance) + && (self.line_height(props, env, at).is_some() + || layout_memory::measured(ctx, sub(id, "content")).is_some_and(|content| content.took.y > self.room.y + tolerance)) } /// What the regions took together, padding included, from the records a measuring run left in this pass, when they diff --git a/crates/ui-contract/src/layout/body.rs b/crates/ui-contract/src/layout/body.rs index 8198b215f7..267021ea4f 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -1535,11 +1535,13 @@ pub(crate) fn members_line(ctx: &egui::Context, body: Id) -> Option { body, centre: Some(took.x), height: Some(took.y), + settled: true, }) } /// The line the unwrapped `Row` body `body` sets its children on: its own (`centre`, `height`), or the line `shared` -/// high it shares with its container's inline header or end region, centred, when that centre is lower. +/// high it shares with its container's inline header or end region, centred, when that centre is lower. Only its own +/// line, which this pass's measuring run recorded, is settled. fn on_line(body: Id, own: Option<(f32, f32)>, shared: Option) -> RowLine { let (centre, height) = match (own, shared) { (Some((centre, height)), Some(shared)) => { @@ -1550,7 +1552,7 @@ fn on_line(body: Id, own: Option<(f32, f32)>, shared: Option) -> RowLine { (None, Some(shared)) => (Some(shared / 2.0), Some(shared)), (None, None) => (None, None), }; - RowLine { body, centre, height } + RowLine { body, centre, height, settled: own.is_some() } } /// Starts a sweep of a body's children in a measuring run: the body's first-line `target` and, for the unwrapped diff --git a/crates/ui-contract/src/runtime/env.rs b/crates/ui-contract/src/runtime/env.rs index 38a7d029ba..545c5518c0 100644 --- a/crates/ui-contract/src/runtime/env.rs +++ b/crates/ui-contract/src/runtime/env.rs @@ -289,6 +289,10 @@ pub(crate) struct RowLine { pub(crate) centre: Option, /// The row's height once its children sit on the line, at least the shared line's. `None` with `centre`. pub(crate) height: Option, + /// Whether this pass's measuring run of the row settled the line (the row's own record): only then does a + /// container that fills its height take the line (`line_height`). While the row is measured, the line it shares + /// gives `centre` and `height` but is not settled. + pub(crate) settled: bool, } /// What a collection records for one of its items in a pass: the row flags the item folds into its state (step 12) diff --git a/crates/ui-contract/tests/it/layout/line_fill.rs b/crates/ui-contract/tests/it/layout/line_fill.rs index 5e7436e4cb..aed05edb75 100644 --- a/crates/ui-contract/tests/it/layout/line_fill.rs +++ b/crates/ui-contract/tests/it/layout/line_fill.rs @@ -1,8 +1,9 @@ //! Filling the line of a row that takes its height from its content: a levelled unit stands on the row's line, a -//! container that fills its height there runs its body in the line's height from the first frame, and a centred row in -//! a unit of a row that scrolls centres across its own column, not in the view. +//! container that fills its height there runs its body in the line's height from the first frame and measures its +//! content while the row measures, and a centred row in a unit of a row that scrolls centres across its own column, not +//! in the view. -use craft_ui::contract::{ContainerProps, Props as _, Scroll, Show as _, ShowContainer as _, Sizing}; +use craft_ui::contract::{ContainerProps, HeaderRegion, Props as _, Scroll, Show as _, ShowContainer as _, Sizing}; use craft_ui::layout::{BodyLayout, RowAlign, levelled_unit}; use craft_ui::testing::{Frames, trial}; use craft_ui_test_kit::body::Slab; @@ -49,6 +50,32 @@ fn a_levelled_unit_stands_on_its_rows_line() { } } +/// A container that fills its height, on a row that shares its line with an inline header, measures its content while +/// the row measures: the shared line, shorter than the content, never caps it, and both its lines show in the row. +#[trial] +fn a_fill_height_container_on_a_shared_line_shows_all_its_content() { + let (column, first, second) = (RefCell::new(Seen::default()), RefCell::new(Seen::default()), RefCell::new(Seen::default())); + let props = ContainerProps::new().layout(BodyLayout::row()).header(HeaderRegion::new().inline(true)); + let mut frames = Frames::new(); + let mut show = |ui: &mut egui::Ui| { + Slab::new(props.clone()).id_salt("bar").show(ui, |ui, _| { + Panel::new().layout(BodyLayout::stack()).sizing(Sizing::FillHeight).seen(&column).id_salt("column").show(ui, |ui, _| { + Probe::new(&first).id_salt("first").show(ui); + Probe::new(&second).id_salt("second").show(ui); + }); + }); + }; + for (frames_run, when) in [(1, "in the first frame"), (4, "once settled")] { + frames.idle(frames_run, &mut show); + let (column, first, second) = (column.borrow().rect, first.borrow().rect, second.borrow().rect); + assert!(second.top() >= first.bottom() - SNAP, "{when}: the second line {second:?} sits under the first {first:?}"); + assert!( + first.top() >= column.top() - SNAP && second.bottom() <= column.bottom() + SNAP, + "{when}: the column {column:?} holds both its lines, {first:?} and {second:?}" + ); + } +} + #[trial] fn a_fill_height_container_on_a_content_height_row_runs_its_body_in_the_line() { let seen = line_and_fill(|ui, fill| {