Files
craft-libs/crates/ui-widgets/tests/it/layout/stack_widths.rs
T
dotCooCoo cc3172c606 ui: a wrapping spread row on one line sets its children on an unwrapped row's line
A wrapping SpaceBetween row whose children fit one line recorded its line at
the middle of its tallest child. When the real run spread the children, a
child whose line lies above its middle (a nested row whose line is its first
button's, beside a column of two buttons) sat its line on that middle and
reached below the row's measured height, over the next sibling.

The measuring run now records what each child takes about a line, as an
unwrapped row's children do, and the row records that line and its height.
The measuring block takes that height when the real run may spread the
children, so the container measures the row as tall as it shows.
2026-11-28 13:22:26 -08:00

295 lines
16 KiB
Rust

//! 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`), and breaks a wrapping
//! row there too.
use craft_ui::buttons::Button;
use craft_ui::choice::{Radio, RadioGroup};
use craft_ui::contract::{
Bind, FieldLayout, Gap, LeastWidth, Pad, Props as _, RowAlign, Scroll, Show as _, ShowContainer as _, SideLabel, Sizing, WrapWidth,
};
use craft_ui::display::Text;
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;
/// 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:?}");
}
/// In a row beside a page that fills, a full-height side menu keeps its least width: the row places its children by
/// their measure, and that measure held only the menu's content (a File page's rail came out at its widest name).
#[trial]
fn a_side_menu_in_a_row_keeps_its_least_width() {
let shown = |least: bool| {
let seen = Rc::new(Cell::new((Rect::NOTHING, Rect::NOTHING)));
let out = Rc::clone(&seen);
let mut fixture = Fixture::new(move |ui| {
let (menu, page) = (Cell::new(Rect::NOTHING), Cell::new(Rect::NOTHING));
let _ = Row::new().fill().show(ui, |ui, _| {
let mut stack = Stack::new().sizing(Sizing::FillHeight).gap(Gap::None).scroll(Scroll::vertical()).footer(|ui, _| {
let _ = Button::new("About").sizing(Sizing::FillWidth).show(ui);
});
if least {
stack = stack.least_width(LeastWidth::SideMenu);
}
menu.set(
stack
.show(ui, |ui, _| {
let _ = Button::new("Home").sizing(Sizing::FillWidth).show(ui);
})
.output
.rect,
);
page.set(
Stack::new()
.fill()
.show(ui, |ui, _| {
let _ = Text::new("Page").show(ui);
})
.output
.rect,
);
});
if !ui.is_sizing_pass() {
out.set((menu.get(), page.get()));
}
});
fixture.room(Vec2::new(1440.0, 900.0)).settle();
seen.get()
};
let (plain, _) = shown(false);
let (menu, page) = shown(true);
assert!(plain != Rect::NOTHING && menu != Rect::NOTHING && page != Rect::NOTHING, "the menu and the page showed");
assert!(menu.width() > plain.width() + 60.0, "the side menu's least width holds in the row: {menu:?} against {plain:?}");
assert!(page.left() >= menu.right() - 0.5, "the page starts after the menu: {page:?} after {menu:?}");
assert!(menu.height() > 800.0, "the menu is as tall as the row: {menu:?}");
}
/// A page's padding stands its content off the edges by two and a half roomy pads: a File page's margin.
#[trial]
fn a_pages_padding_is_two_and_a_half_roomy_pads() {
let inset = |pad: Pad| {
let seen = Rc::new(Cell::new(Vec2::ZERO));
let out = Rc::clone(&seen);
let mut fixture = Fixture::new(move |ui| {
let child = Cell::new(Rect::NOTHING);
let shown = Stack::new().pad(pad).show(ui, |ui, _| child.set(Button::new("Home").show(ui).rect));
if !ui.is_sizing_pass() {
out.set(child.get().min - shown.output.rect.min);
}
});
fixture.room(Vec2::new(800.0, 600.0)).settle();
seen.get()
};
let (roomy, page) = (inset(Pad::Roomy), inset(Pad::Page));
assert!(roomy.x > 0.0, "a roomy stack pads its content: {roomy:?}");
assert!((page.x - roomy.x * 2.5).abs() < 0.6 && (page.y - roomy.y * 2.5).abs() < 0.6, "a page pads 2.5 roomy pads: {page:?} for {roomy:?}");
}
#[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:?}");
}
/// Labels whose row is far wider than a column.
const KINDS: [&str; 6] = ["No grid", "Lines only", "Lines and characters", "Text snaps to characters", "Left-to-right", "Right-to-left"];
/// A wrapping row in a stack with a wrap width breaks at the stack's line length, as its paragraphs do (a dialog's
/// row of radios takes a second line), instead of widening the stack to keep its children on one line.
#[trial]
fn a_wrapping_row_breaks_at_the_stacks_wrap_width() {
let shown = |wrap: Option<WrapWidth>| {
let seen = Rc::new(Cell::new((Rect::NOTHING, Rect::NOTHING, Rect::NOTHING)));
let out = Rc::clone(&seen);
let mut fixture = Fixture::new(move |ui| {
let (first, last) = (Cell::new(Rect::NOTHING), Cell::new(Rect::NOTHING));
let stack = match wrap {
Some(wrap) => Stack::new().wrap_width(wrap),
None => Stack::new(),
};
let shown = stack.show(ui, |ui, _| {
Row::new().wrap(true).show(ui, |ui, _| {
for (at, kind) in KINDS.into_iter().enumerate() {
let rect = Button::new(kind).id_salt(kind).show(ui).rect;
if at == 0 {
first.set(rect);
}
last.set(rect);
}
});
});
if !ui.is_sizing_pass() {
out.set((shown.output.rect, first.get(), last.get()));
}
});
fixture.room(Vec2::new(1600.0, 600.0)).settle();
seen.get()
};
let (plain, plain_first, plain_last) = shown(None);
let (column, first, last) = shown(Some(WrapWidth::Column));
assert!((plain_first.center().y - plain_last.center().y).abs() < 1.0, "without a wrap width the row keeps one line: {plain:?}");
assert!(last.top() >= first.bottom(), "the row breaks onto a second line: {first:?} then {last:?}");
assert!(column.width() < plain.width() * 0.75, "the wrap width keeps the stack narrow: {column:?} against {plain:?}");
}
/// Labelled radios (a group's `labelled` children, label after the circle) in a wrapping row break onto further lines
/// as buttons do: a radio whose label would not fit what is left of the line starts the next one, at the row's start
/// edge, and keeps its label on one line. The label step places a labelled leaf in the rest of the line before egui
/// allocates its space, so the row used to keep every radio on its first line and squeeze the last label into the
/// sliver left at the end (Page Setup's "Text snaps to characters").
#[trial]
fn labelled_radios_in_a_wrapping_row_break_onto_a_new_line() {
let mut fixture = Fixture::new(move |ui| {
let _ = Stack::new().wrap_width(WrapWidth::Column).show(ui, |ui, _| {
let after = SideLabel::new().layout(FieldLayout::After);
let _ = RadioGroup::new("Grid", Bind::given(0u8)).show(ui, |ui, cx| {
Row::new().wrap(true).show(ui, |ui, _| {
for (value, kind) in (0u8..).zip(KINDS) {
let _ = cx.labelled(ui, kind, kind.into(), after.clone(), Radio::new(value));
}
});
});
});
});
fixture.room(Vec2::new(1600.0, 600.0)).settle();
let rect = |kind: &str| fixture.by_label(kind).unwrap_or_else(|| panic!("the {kind} radio")).rect;
let (first, last) = (rect(KINDS[0]), rect(KINDS[KINDS.len() - 1]));
assert!(last.top() >= first.bottom(), "the row breaks onto a further line: {first:?} then {last:?}");
let starts = KINDS.iter().filter(|kind| (rect(kind).left() - first.left()).abs() < 0.5).count();
assert!(starts >= 2, "a radio that wraps starts its line at the row's start edge: {starts} radios do");
}
/// A wrapping `SpaceBetween` row that fills a stack its least width makes wider than its wrap width, and whose children
/// fit one line, spreads them over the wrap width, the line length it wraps them at. Without a wrap width it spreads
/// them over the whole stack.
#[trial]
fn a_spread_wrapping_row_keeps_to_the_stacks_wrap_width() {
use craft_ui::runtime::env::Env;
let shown = |wrap_width: bool| {
let seen = Rc::new(Cell::new((Rect::NOTHING, Rect::NOTHING, Rect::NOTHING, 0.0)));
let out = Rc::clone(&seen);
let mut fixture = Fixture::new(move |ui| {
let (first, last, wrap) = (Cell::new(Rect::NOTHING), Cell::new(Rect::NOTHING), Cell::new(0.0));
let stack = Stack::new().least_width(LeastWidth::WideDialog);
let stack = if wrap_width { stack.wrap_width(WrapWidth::Dialog) } else { stack };
let shown = stack.show(ui, |ui, _| {
wrap.set(WrapWidth::Dialog.points(Env::of(ui).tokens.tokens()));
Row::new().wrap(true).align(RowAlign::SpaceBetween).sizing(Sizing::FillWidth).show(ui, |ui, _| {
first.set(Button::new("Home").id_salt("home").show(ui).rect);
last.set(Button::new("Next").id_salt("next").show(ui).rect);
});
});
if !ui.is_sizing_pass() {
out.set((shown.output.rect, first.get(), last.get(), wrap.get()));
}
});
fixture.room(Vec2::new(1600.0, 600.0)).settle();
seen.get()
};
let (stack, first, last, wrap) = shown(true);
let dump = format!("stack {stack:?}, first {first:?}, last {last:?}, wrap width {wrap}");
assert!(stack.width() > wrap + 1.0, "the least width makes the stack wider than its wrap width: {dump}");
assert!((first.center().y - last.center().y).abs() < 1.0, "the children share one line: {dump}");
assert!(last.max.x <= first.min.x + wrap + 0.5, "the last child ends within the wrap width: {dump}");
assert!(last.max.x > first.min.x + wrap - 0.5, "the last child ends at the wrap width's end: {dump}");
let (stack, first, last, _) = shown(false);
let dump = format!("stack {stack:?}, first {first:?}, last {last:?}");
assert!((first.center().y - last.center().y).abs() < 1.0, "without a wrap width the children share one line: {dump}");
assert!((last.max.x - stack.max.x).abs() < 0.5, "without a wrap width the last child ends at the stack's end: {dump}");
}
/// A wrapping `SpaceBetween` row whose children fit one line sets them on the line an unwrapped row sets them on, and
/// takes that line's height: a nested row whose line is its first button's, above a column of two buttons beside it,
/// stays inside the row and clear of the button below it.
#[trial]
fn a_spread_wrapping_row_keeps_a_child_whose_line_is_above_its_middle_inside_it() {
let seen = Rc::new(Cell::new((Rect::NOTHING, Rect::NOTHING, Rect::NOTHING, Rect::NOTHING)));
let out = Rc::clone(&seen);
let mut fixture = Fixture::new(move |ui| {
let (row, nested, next, below) = (Cell::new(Rect::NOTHING), Cell::new(Rect::NOTHING), Cell::new(Rect::NOTHING), Cell::new(Rect::NOTHING));
let _ = Stack::new().gap(Gap::None).show(ui, |ui, _| {
let shown = Row::new().wrap(true).align(RowAlign::SpaceBetween).show(ui, |ui, _| {
let shown = Row::new().show(ui, |ui, _| {
let _ = Button::new("Home").id_salt("home").show(ui);
let _ = Stack::new().show(ui, |ui, _| {
let _ = Button::new("Up").id_salt("up").show(ui);
let _ = Button::new("Down").id_salt("down").show(ui);
});
});
nested.set(shown.output.rect);
next.set(Button::new("Next").id_salt("next").show(ui).rect);
});
row.set(shown.output.rect);
below.set(Button::new("Below").id_salt("below").show(ui).rect);
});
if !ui.is_sizing_pass() {
out.set((row.get(), nested.get(), next.get(), below.get()));
}
});
fixture.room(Vec2::new(800.0, 600.0)).settle();
let (row, nested, next, below) = seen.get();
let dump = format!("row {row:?}, nested row {nested:?}, next {next:?}, below {below:?}");
assert!(nested.height() > next.height() * 1.5, "the nested row is two buttons tall: {dump}");
assert!(next.min.x > nested.max.x, "the children share one line: {dump}");
assert!(nested.max.y <= row.max.y + 0.5, "the nested row stays inside the row: {dump}");
assert!(nested.max.y <= below.min.y + 0.5, "the nested row stays clear of the button below: {dump}");
assert!((next.center().y - (nested.min.y + next.height() / 2.0)).abs() < 1.0, "the button sits on the nested row's line: {dump}");
}
/// A body that scrolls keeps the bar's lane clear: a child that fills the width stops where the bar starts, not under it
/// (a Print page's buttons ran under the settings column's bar).
#[trial]
fn a_fill_width_child_of_a_scrolling_stack_leaves_the_bars_lane() {
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::new().sizing(Sizing::FillHeight).least_width(LeastWidth::Dialog).scroll(Scroll::vertical()).show(ui, |ui, _| {
for _ in 0..30 {
child.set(Button::new("Save these pages as PDF…").sizing(Sizing::FillWidth).show(ui).rect);
}
});
if !ui.is_sizing_pass() {
out.set((shown.output.rect, child.get()));
}
});
fixture.room(Vec2::new(1440.0, 300.0)).settle();
let (stack, button) = seen.get();
assert!(stack != Rect::NOTHING && button != Rect::NOTHING, "the stack showed");
assert!(button.max.x <= stack.max.x - 4.0, "the button leaves the bar's lane: {button:?} in {stack:?}");
}