From e080fa2098a081382d91a0a95f82c35d0a5bcd42 Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Thu, 19 Nov 2026 13:31:11 +0000 Subject: [PATCH] look-backstage: Sizing::FillHeight, a container as wide as its content and as tall as the room (a side menu beside a page), with its trial and story Co-Authored-By: Claude Opus 5.5 --- crates/ui-contract/src/contract/body/hooks.rs | 9 +++- .../ui-contract/src/contract/body/regions.rs | 6 ++- crates/ui-contract/src/contract/body/size.rs | 14 ++--- crates/ui-contract/src/contract/container.rs | 16 ++++++ .../ui-contract/src/contract/items/hooks.rs | 6 +-- .../ui-contract/src/contract/items/measure.rs | 2 +- .../src/layout_components/stories.rs | 17 +++++- .../ui-widgets/tests/it/layout/fill_height.rs | 52 +++++++++++++++++++ crates/ui-widgets/tests/it/layout/mod.rs | 1 + 9 files changed, 107 insertions(+), 16 deletions(-) create mode 100644 crates/ui-widgets/tests/it/layout/fill_height.rs diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 7587ba2e5a..5cb485611c 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -516,12 +516,17 @@ impl ShapeHooks for BodyHooks<'_> { }; Vec2::new(crate::layout::fill_size(ui, id, craft_behavior::Axis::X, size.x, room, self.given.0 || held), height) } + // Its content's width, and the height as `Fill` takes it. + Sizing::FillHeight => { + let room = crate::layout::flow_room(ui); + Vec2::new(size.x, crate::layout::fill_size(ui, id, craft_behavior::Axis::Y, size.y, room.y, self.given.1)) + } Sizing::Content => size, } } fn fills_width(&self, comp: &C) -> bool { - self.sizing(comp.props(Internal::new())) != Sizing::Content + !self.sizing(comp.props(Internal::new())).content_width() } fn fills_width_only(&self, comp: &C) -> bool { @@ -825,7 +830,7 @@ impl ShapeHooks for BodyHooks<'_> { owner: id, parent: node.then_some(id), row: regions.body, - room: if self.sizing(&props) == Sizing::Content { + room: if self.sizing(&props).content_width() { layout::ambient_room(&ctx).map_or(room.x, |offered| room.x.max(offered)) } else { regions.body.width() diff --git a/crates/ui-contract/src/contract/body/regions.rs b/crates/ui-contract/src/contract/body/regions.rs index 21266ae84f..f2d28b2bc0 100644 --- a/crates/ui-contract/src/contract/body/regions.rs +++ b/crates/ui-contract/src/contract/body/regions.rs @@ -364,8 +364,10 @@ impl<'b> BodyHooks<'b> { let top = inner.min.y + stacked_h; // Along an axis the container is not given, its body is offered the room the container was offered and the // container takes what the body took. - let content_sized = self.sizing(props) == Sizing::Content; - let content_y = content_sized || ((self.sizing(props) == Sizing::Fill || !inner.max.y.is_finite()) && !self.given.1); + // `content_sized` is the width's rule (`FillHeight` takes its content's width); the height has its own. + let content_sized = self.sizing(props).content_width(); + let content_y = + self.sizing(props) == Sizing::Content || ((self.sizing(props).fills_height() || !inner.max.y.is_finite()) && !self.given.1); let (bottom, footer_top) = if content_y { let body_room = (room.y - stacked_h - across_h).max(0.0); // A body that fills records the band it was given, which may predate the footer's measure: the line is diff --git a/crates/ui-contract/src/contract/body/size.rs b/crates/ui-contract/src/contract/body/size.rs index c5d682cb99..30a93c3599 100644 --- a/crates/ui-contract/src/contract/body/size.rs +++ b/crates/ui-contract/src/contract/body/size.rs @@ -112,7 +112,7 @@ impl<'b> BodyHooks<'b> { /// Whether the container fills the width of a content-sized column it sits in: the column holds it at its measured /// width and shows it across the width the column settles on (step 6). pub(super) fn held(&self, props: &ContainerProps, env: &Env) -> bool { - env.content_column && matches!(props.mount, Mount::Inline) && self.sizing(props) != Sizing::Content + env.content_column && matches!(props.mount, Mount::Inline) && !self.sizing(props).content_width() } /// The axes the container is given where `env` is its parent's: a rect it is shown at gives both; an inline @@ -125,7 +125,7 @@ impl<'b> BodyHooks<'b> { let sizing = self.sizing(props); let fills_line = sizing == Sizing::FillWidth && unwrapped_row(props) && env.row_line.is_some(); let fits_bar = props.content_in_bar && env.values.get::().0; - let (x, y) = (sizing != Sizing::Content && !fits_bar, sizing == Sizing::Fill || fills_line); + 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), @@ -137,14 +137,14 @@ impl<'b> BodyHooks<'b> { /// is its parent's: in a `Stack` body given its width, a child of its body that fills takes that width, and the /// container takes what its content took. A content-sized mount, a row or a grid stops it. pub(super) fn passes_width(&self, props: &ContainerProps, env: &Env, at: ShowAt) -> bool { - matches!((at, &props.mount), (ShowAt::Flow, Mount::Inline)) && self.sizing(props) == Sizing::Content && env.given_column + matches!((at, &props.mount), (ShowAt::Flow, Mount::Inline)) && self.sizing(props).content_width() && env.given_column } /// Whether the height the container is given (`given_axes`) is a fixed extent: a rect it is shown at, a pane's, or /// the row line a fill-width row fills. An inline container that fills takes a share of its parent's room, which /// follows the content. pub(super) fn fixed_height(&self, props: &ContainerProps, at: ShowAt) -> bool { - self.given.1 && !(matches!((at, &props.mount), (ShowAt::Flow, Mount::Inline)) && self.sizing(props) == Sizing::Fill) + self.given.1 && !(matches!((at, &props.mount), (ShowAt::Flow, Mount::Inline)) && self.sizing(props).fills_height()) } /// Whether the container centres its body's line in the height it is given: its body is a row that does not scroll @@ -175,7 +175,7 @@ impl<'b> BodyHooks<'b> { /// its content height, at most that room. A body that scrolls along the height keeps to the room by scrolling. pub(super) fn capped(&self, props: &ContainerProps, env: &Env, at: ShowAt) -> bool { matches!((at, &props.mount), (ShowAt::Flow, Mount::Inline)) - && self.sizing(props) == Sizing::Fill + && self.sizing(props).fills_height() && env.content.1 && self.room.y.is_finite() && !scroll_of(props).is_some_and(|scroll| scroll.vertical_on()) @@ -298,14 +298,14 @@ impl<'b> BodyHooks<'b> { let room = crate::layout::flow_room(ui); // A row that shrinks its children to fit gives a body among them a width: it measures and shows // in that room. A body that fills a bounded row takes the rest of it instead. - let fills = self.sizing(props) != Sizing::Content && room.x.is_finite(); + let fills = !self.sizing(props).content_width() && room.x.is_finite(); let cap = match Env::of(ui).shrink { Some(crate::layout::Shrink::Apply(row)) if !fills => crate::layout::shrink_cap(ui.ctx(), row, id).map(|cap| cap.width), _ => None, }; // A body that fills the height of a `Stack` body that scrolls along y takes a share of what the view // shows (`layout::view_room`): it runs in that room, bounded like a body in a room that does not scroll. - let shares_view = !room.y.is_finite() && self.sizing(props) == Sizing::Fill && crate::layout::shares_y(ui); + let shares_view = !room.y.is_finite() && self.sizing(props).fills_height() && crate::layout::shares_y(ui); let y = shares_view.then(|| crate::layout::view_room(ui, craft_behavior::Axis::Y)).flatten().unwrap_or(room.y); Vec2::new(cap.map_or(room.x, |cap| room.x.min(cap)), props.room_height.unwrap_or(y)) } diff --git a/crates/ui-contract/src/contract/container.rs b/crates/ui-contract/src/contract/container.rs index a0559703ad..f8513467c6 100644 --- a/crates/ui-contract/src/contract/container.rs +++ b/crates/ui-contract/src/contract/container.rs @@ -168,6 +168,22 @@ pub enum Sizing { /// fills takes it, and the container with it. A content-sized mount, a row or a grid stops that. #[default] Content, + /// The width its content took last pass and the available height: a side menu beside a page, as wide as its + /// widest item and as tall as the row it stands in, with its footer at the bottom. Across the width it is + /// `Content`, down the height `Fill`. + FillHeight, +} + +impl Sizing { + /// Whether the width follows the content: `Content` and `FillHeight`. + pub const fn content_width(self) -> bool { + matches!(self, Sizing::Content | Sizing::FillHeight) + } + + /// Whether the height is the available height: `Fill` and `FillHeight`. + pub const fn fills_height(self) -> bool { + matches!(self, Sizing::Fill | Sizing::FillHeight) + } } /// A size role that sets the least width of a container's content or of a button. diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index 3083878ee8..d25c229dfb 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -257,7 +257,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { match comp.props(Internal::new()).sizing { // Under a body sized from its content along the height, the rows take up to the room, as a content-sized // collection's do: the body has no room of its own to hand it. - Sizing::Fill => Vec2::new(wide, if self.from_content.1 || self.capped { height.min(room.y) } else { 0.0 }), + Sizing::Fill | Sizing::FillHeight => Vec2::new(wide, if self.from_content.1 || self.capped { height.min(room.y) } else { 0.0 }), Sizing::Content | Sizing::FillWidth => Vec2::new(wide, height.min(room.y)), } } @@ -275,8 +275,8 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { crate::layout::body::fill_size(ui, id, Axis::X, size.x, self.room.x, true) }; let height = match (self.scrolls && self.room.y.is_finite(), sizing) { - (true, Sizing::Fill) if !self.from_content.1 && !self.capped => self.room.y, - (true, Sizing::Fill) => size.y.min(self.room.y), + (true, Sizing::Fill | Sizing::FillHeight) if !self.from_content.1 && !self.capped => self.room.y, + (true, Sizing::Fill | Sizing::FillHeight) => size.y.min(self.room.y), (true, Sizing::Content | Sizing::FillWidth) => size.y.min(self.room.y), (false, _) => size.y, }; diff --git a/crates/ui-contract/src/contract/items/measure.rs b/crates/ui-contract/src/contract/items/measure.rs index a29b9bcaad..52227aa231 100644 --- a/crates/ui-contract/src/contract/items/measure.rs +++ b/crates/ui-contract/src/contract/items/measure.rs @@ -20,7 +20,7 @@ impl<'s, 'r, C: Collection> ItemsHooks<'s, 'r, C> { /// Whether the collection takes its width from its widest row: `Sizing::Content`. A row's width holds its /// column cells at the widths the header resolves for them. pub(super) fn content_sized(&self, comp: &C) -> bool { - comp.props(Internal::new()).sizing == Sizing::Content + comp.props(Internal::new()).sizing.content_width() } /// The padding the collection's props add on every side of its rows (`ShapeHooks::pad`). diff --git a/crates/ui-widgets/src/layout_components/stories.rs b/crates/ui-widgets/src/layout_components/stories.rs index 0788646d28..83a05fb5db 100644 --- a/crates/ui-widgets/src/layout_components/stories.rs +++ b/crates/ui-widgets/src/layout_components/stories.rs @@ -1,7 +1,7 @@ //! Stories of the layout components `Row`, `Stack` and `Grid`. use super::{Grid, Row, Stack}; -use crate::contract::{Gap, Pad, ShowContainer as _}; +use crate::contract::{Gap, Pad, Props as _, ShowContainer as _, Sizing}; use crate::story_kit::sample::{outlined, words}; use crate::testing::{StoryData, StoryOutput, story}; use craft_behavior::ColumnWidth; @@ -31,6 +31,21 @@ fn stack_footer(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { outlined(ui, |ui| Stack::new().gap(Gap::Stack).pad(Pad::Regular).footer(|ui, _| words(ui, 2..3)).show(ui, |ui, _| words(ui, 0..2)).into()) } +/// A side menu beside a page: the menu as wide as its widest item and as tall as the row, its footer at the bottom. +#[story(Stack<'static>, covers = [])] +fn stack_fill_height(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + outlined(ui, |ui| { + ui.allocate_ui(egui::vec2(360.0, 200.0), |ui| { + Row::new().fill().show(ui, |ui, _| { + Stack::new().sizing(Sizing::FillHeight).gap(Gap::Tight).pad(Pad::Regular).footer(|ui, _| words(ui, 2..3)).show(ui, |ui, _| words(ui, 0..2)); + Stack::new().fill().pad(Pad::Regular).show(ui, |ui, _| words(ui, 3..4)); + }) + }) + .inner + .into() + }) +} + static COLUMNS: [ColumnWidth; 2] = [ColumnWidth::Content, ColumnWidth::Fixed(60.0)]; #[story(Grid, covers = [Default])] diff --git a/crates/ui-widgets/tests/it/layout/fill_height.rs b/crates/ui-widgets/tests/it/layout/fill_height.rs new file mode 100644 index 0000000000..43a5bc8c24 --- /dev/null +++ b/crates/ui-widgets/tests/it/layout/fill_height.rs @@ -0,0 +1,52 @@ +//! `Sizing::FillHeight` in a row given its height: the child takes its content's width and the row's height, so a side +//! menu beside a page is as tall as the page with its footer at the bottom, and the page beside it takes the rest of +//! the width (the File page's menu, as tall as the window, About at its foot). + +use craft_ui::buttons::Button; +use craft_ui::contract::{Props as _, Show as _, ShowContainer as _, Sizing}; +use craft_ui::layout_components::{Row, Stack}; +use craft_ui::testing::{Fixture, trial}; +use egui::{Rect, Vec2}; +use std::cell::Cell; +use std::rc::Rc; + +const SNAP: f32 = 1.0; + +#[trial] +fn a_fill_height_menu_takes_its_content_width_and_the_rows_height() { + // The row, the menu, the menu's footer button, its widest item and the page, from the last real run. + let seen = Rc::new(Cell::new([Rect::NOTHING; 5])); + let out = Rc::clone(&seen); + let mut fixture = Fixture::new(move |ui| { + let out = Rc::clone(&out); + let parts = Cell::new([Rect::NOTHING; 4]); + let row = Row::new().fill().show(ui, |ui, _| { + let footer = Cell::new(Rect::NOTHING); + let widest = Cell::new(Rect::NOTHING); + let menu = Stack::new() + .sizing(Sizing::FillHeight) + .footer(|ui, _| footer.set(Button::new("About").show(ui).rect)) + .show(ui, |ui, _| { + Button::new("Home").show(ui); + widest.set(Button::new("Save As a copy").show(ui).rect); + }); + let page = Stack::new().fill().show(ui, |ui, _| { + Button::new("Browse").show(ui); + }); + parts.set([menu.output.rect, footer.get(), widest.get(), page.output.rect]); + }); + if !ui.is_sizing_pass() { + let [menu, footer, widest, page] = parts.get(); + out.set([row.output.rect, menu, footer, widest, page]); + } + }); + fixture.room(Vec2::new(600.0, 400.0)).settle(); + let [row, menu, footer, widest, page] = seen.get(); + assert!(row != Rect::NOTHING, "the row showed"); + assert!(row.height() > 300.0, "the row fills the room: {row:?}"); + assert!((menu.top() - row.top()).abs() <= SNAP && (menu.bottom() - row.bottom()).abs() <= SNAP, "the menu {menu:?} spans the row {row:?}"); + assert!(menu.width() <= widest.width() + 40.0, "the menu {menu:?} is about as wide as its widest item {widest:?}"); + assert!(menu.width() < row.width() / 2.0, "the menu takes no share of the width: {menu:?} in {row:?}"); + assert!(row.bottom() - footer.bottom() < 40.0, "the footer {footer:?} sits at the menu's foot {menu:?}"); + assert!(page.left() >= menu.right() - SNAP && (page.right() - row.right()).abs() <= SNAP, "the page {page:?} takes the rest of {row:?}"); +} diff --git a/crates/ui-widgets/tests/it/layout/mod.rs b/crates/ui-widgets/tests/it/layout/mod.rs index ff427c664a..ed6c32e169 100644 --- a/crates/ui-widgets/tests/it/layout/mod.rs +++ b/crates/ui-widgets/tests/it/layout/mod.rs @@ -1,5 +1,6 @@ mod cross_fill; mod fill_column; +mod fill_height; mod fill_share; mod fonts; mod grid_labels;