From 9e3d7bbc1fdf1664a8ccb013a346e09acf9ad774 Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Thu, 19 Nov 2026 13:55:17 +0000 Subject: [PATCH] look-rest-2: WrapWidth::Column (size.dock.column) and Stack::wrap_width, a settings column beside a preview breaks its paragraphs instead of widening; trial and story --- crates/ui-contract/src/contract/container.rs | 3 ++ .../ui-widgets/src/layout_components/mod.rs | 12 ++++- .../src/layout_components/stories.rs | 27 +++++++++- crates/ui-widgets/tests/it/layout/mod.rs | 1 + .../tests/it/layout/stack_widths.rs | 52 +++++++++++++++++++ 5 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 crates/ui-widgets/tests/it/layout/stack_widths.rs diff --git a/crates/ui-contract/src/contract/container.rs b/crates/ui-contract/src/contract/container.rs index a59a07e53..c0d16b99e 100644 --- a/crates/ui-contract/src/contract/container.rs +++ b/crates/ui-contract/src/contract/container.rs @@ -226,6 +226,8 @@ impl LeastWidth { pub enum WrapWidth { /// `size.dialog.wrap`: a dialog's paragraphs. Dialog, + /// `size.dock.column`: a page's column of settings beside a preview (a Print page), as wide as a dock column. + Column, } impl WrapWidth { @@ -233,6 +235,7 @@ impl WrapWidth { pub fn points(self, tokens: &craft_theme::Tokens) -> f32 { match self { WrapWidth::Dialog => tokens.size.dialog.wrap, + WrapWidth::Column => tokens.size.dock.column, } } } diff --git a/crates/ui-widgets/src/layout_components/mod.rs b/crates/ui-widgets/src/layout_components/mod.rs index bc53a6731..5326396a0 100644 --- a/crates/ui-widgets/src/layout_components/mod.rs +++ b/crates/ui-widgets/src/layout_components/mod.rs @@ -2,8 +2,8 @@ //! lays its children out (6.3). use crate::contract::{ - Access, BodyCx, Common, Component, Container, ContainerProps, Cx, EndRegion, FooterPlace, FooterRegion, Gap, HeaderRegion, LabelScheme, LeastWidth, Pad, - Role, Scroll, Sizing, ThemeScope, + Access, BodyCx, Common, Component, Container, ContainerProps, Cx, EndRegion, FooterPlace, FooterRegion, Gap, HeaderRegion, LabelScheme, LeastWidth, + Pad, Role, Scroll, Sizing, ThemeScope, WrapWidth, }; use crate::layout::{BodyLayout, RowAlign}; use crate::style::{ComponentStyle, StyleCx}; @@ -269,6 +269,14 @@ impl<'a> Stack<'a> { self } + /// The line length `wrap` of the paragraphs in a stack sized from its content: a wrapping text breaks its lines + /// there instead of widening the stack (a Print page's settings column, `WrapWidth::Column`). + #[must_use] + pub fn wrap_width(mut self, wrap: WrapWidth) -> Self { + self.props = self.props.wrap_width(wrap); + self + } + /// The padding `pad` on the header's two sides, none above or below it: a field over a table starts where the /// table's text does. Set it after [`header`](Stack::header). #[must_use] diff --git a/crates/ui-widgets/src/layout_components/stories.rs b/crates/ui-widgets/src/layout_components/stories.rs index 83a05fb5d..ac6d6ceee 100644 --- a/crates/ui-widgets/src/layout_components/stories.rs +++ b/crates/ui-widgets/src/layout_components/stories.rs @@ -1,7 +1,8 @@ //! Stories of the layout components `Row`, `Stack` and `Grid`. use super::{Grid, Row, Stack}; -use crate::contract::{Gap, Pad, Props as _, ShowContainer as _, Sizing}; +use crate::contract::{Gap, LeastWidth, Pad, Props as _, Show as _, ShowContainer as _, Sizing, WrapWidth}; +use crate::display::Text; use crate::story_kit::sample::{outlined, words}; use crate::testing::{StoryData, StoryOutput, story}; use craft_behavior::ColumnWidth; @@ -46,7 +47,29 @@ fn stack_fill_height(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { }) } -static COLUMNS: [ColumnWidth; 2] = [ColumnWidth::Content, ColumnWidth::Fixed(60.0)]; +/// The two width roles of a content-sized stack: a side menu held at its least width with short items, and a column +/// of settings whose paragraph breaks at the column's line length instead of widening it. +#[story(Stack<'static>, covers = [], title = "Width roles")] +fn stack_width_roles(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + outlined(ui, |ui| { + Row::new() + .gap(Gap::Section) + .show(ui, |ui, _| { + Stack::new().least_width(LeastWidth::SideMenu).gap(Gap::Tight).pad(Pad::Regular).show(ui, |ui, _| words(ui, 0..2)); + Stack::new().wrap_width(WrapWidth::Column).gap(Gap::Stack).pad(Pad::Regular).show(ui, |ui, _| { + let _ = Text::new( + "Save a copy in another format. Printing goes through a PDF you can print from any viewer, and the pages \ + picked here are the ones that print.", + ) + .show(ui); + words(ui, 2..3); + }); + }) + .into() + }) +} + +static COLUMNS: [ColumnWidth; 2] =[ColumnWidth::Content, ColumnWidth::Fixed(60.0)]; #[story(Grid, covers = [Default])] fn grid(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { diff --git a/crates/ui-widgets/tests/it/layout/mod.rs b/crates/ui-widgets/tests/it/layout/mod.rs index ed6c32e16..345f8b837 100644 --- a/crates/ui-widgets/tests/it/layout/mod.rs +++ b/crates/ui-widgets/tests/it/layout/mod.rs @@ -13,4 +13,5 @@ mod rtl_scroll; mod scroll_region; mod shrink_order; mod stack_header; +mod stack_widths; mod zero_width; diff --git a/crates/ui-widgets/tests/it/layout/stack_widths.rs b/crates/ui-widgets/tests/it/layout/stack_widths.rs new file mode 100644 index 000000000..7a910b976 --- /dev/null +++ b/crates/ui-widgets/tests/it/layout/stack_widths.rs @@ -0,0 +1,52 @@ +//! A content-sized `Stack`'s width roles: `least_width` keeps it at least the role's width with narrow content (a +//! File page's side menu, `LeastWidth::SideMenu`), and `wrap_width` breaks a long paragraph at the role's line length +//! instead of letting it widen the stack (a Print page's settings column, `WrapWidth::Column`). + +use craft_ui::buttons::Button; +use craft_ui::contract::{LeastWidth, Show as _, ShowContainer as _, WrapWidth}; +use craft_ui::display::Text; +use craft_ui::layout_components::Stack; +use craft_ui::testing::{Fixture, trial}; +use egui::{Rect, Vec2}; +use std::cell::Cell; +use std::rc::Rc; + +/// A paragraph far longer than any column. +const LONG: &str = "Save a copy in another format. Printing goes through a PDF you can print from any viewer, and the pages you pick \ + here are the ones that print, at the size and orientation set below."; + +/// The rects of the stack and its one child from the last real run, the stack built by `stack`. +fn shown(stack: fn() -> Stack<'static>, long: bool) -> (Rect, Rect) { + let seen = Rc::new(Cell::new((Rect::NOTHING, Rect::NOTHING))); + let out = Rc::clone(&seen); + let mut fixture = Fixture::new(move |ui| { + let child = Cell::new(Rect::NOTHING); + let shown = stack().show(ui, |ui, _| { + let rect = if long { Text::new(LONG).show(ui).rect } else { Button::new("Home").show(ui).rect }; + child.set(rect); + }); + if !ui.is_sizing_pass() { + out.set((shown.output.rect, child.get())); + } + }); + fixture.room(Vec2::new(1600.0, 600.0)).settle(); + seen.get() +} + +#[trial] +fn a_stacks_least_width_holds_it_wider_than_narrow_content() { + let (plain, button) = shown(Stack::new, false); + let (menu, _) = shown(|| Stack::new().least_width(LeastWidth::SideMenu), false); + assert!(plain != Rect::NOTHING && menu != Rect::NOTHING, "both stacks showed"); + assert!(plain.width() < button.width() + 2.0, "a plain stack is as wide as its button: {plain:?} for {button:?}"); + assert!(menu.width() > plain.width() + 60.0, "the side menu's least width holds it wider: {menu:?} against {plain:?}"); +} + +#[trial] +fn a_stacks_wrap_width_breaks_a_long_paragraph_at_the_column() { + let (plain, one_line) = shown(Stack::new, true); + let (column, wrapped) = shown(|| Stack::new().wrap_width(WrapWidth::Column), true); + assert!(plain.width() > 600.0, "without a wrap width the paragraph widens the stack to the room: {plain:?}"); + assert!(column.width() < plain.width() / 2.0, "the column's wrap width keeps the stack narrow: {column:?} against {plain:?}"); + assert!(wrapped.height() > one_line.height() * 2.0, "the paragraph breaks into lines: {wrapped:?} against {one_line:?}"); +}