Files
craft-libs/crates/ui-widgets/tests/it/layout/stack_header.rs
T
dotCooCoo 01af858386 ui-contract: a fill-width row in a content-sized stack's header or footer spans the band
A stack that takes its width from its content, such as the column in a
popover, held a fill-width child of its header or footer band at its
measured width only in the measuring run. In the real run the child took
its content width, so a RowAlign::SpaceBetween or RowAlign::End row there
placed its children at the start of the band, as RowAlign::Start does.

The header band, and a footer in its own band, now keep the content
column in the real run too, as the body and a footer in the end region
or the body already did. The row shows across the band and places its
children by its alignment.

The new test shows a SpaceBetween and an End row in the header and in
the footer of a popover's stack and checks the children's x positions.
2026-11-29 10:45:17 -08:00

104 lines
5.0 KiB
Rust

//! A stack's header and footer are its first and last rows: the stack's gap separates them from the body, and a
//! fill-width row in them spans their band.
use craft_ui::buttons::Button;
use craft_ui::contract::{Bind, BodyCx, Gap, Pad, Props as _, RowAlign, Show as _, ShowContainer as _};
use craft_ui::display::Text;
use craft_ui::layout_components::{Row, Stack};
use craft_ui::popover::Popover;
use craft_ui::testing::{Fixture, trial};
use egui::{Rect, Ui};
use std::cell::Cell;
use std::rc::Rc;
/// Half a pixel at the scales the box runs.
const ROUNDING: f32 = 0.5;
/// The gaps in a stack with a two-line header, a body line and a footer line: between the header's lines, header to
/// body, body to footer. `gap` is set after the header and footer.
fn gaps(gap: Option<Gap>) -> [f32; 3] {
let mut fixture = Fixture::new(move |ui| {
let mut stack = Stack::new()
.header(|ui, _| {
Text::new("Head one").show(ui);
Text::new("Head two").show(ui);
})
.footer(|ui, _| {
Text::new("Foot").show(ui);
});
if let Some(gap) = gap {
stack = stack.gap(gap);
}
stack.show(ui, |ui, _| {
Text::new("Body").show(ui);
});
});
fixture.settle();
let rect =
|text: &str| -> Rect { fixture.texts().iter().find(|painted| painted.text == text).unwrap_or_else(|| panic!("no {text} painted")).rect };
let (one, two, body, foot) = (rect("Head one"), rect("Head two"), rect("Body"), rect("Foot"));
[two.top() - one.bottom(), body.top() - two.bottom(), foot.top() - body.bottom()]
}
/// The rects of the stack, a fill-width `align` row in its header (`header`) or footer, and the row's first and last
/// children, from the last real run of a popover whose stack takes its width from a body line wider than the row.
fn region_row(header: bool, align: RowAlign) -> [Rect; 4] {
let seen = Rc::new(Cell::new([Rect::NOTHING; 4]));
let out = Rc::clone(&seen);
let mut fixture = Fixture::new(move |ui| {
let [column, row, first, last] = [(); 4].map(|()| Cell::new(Rect::NOTHING));
let region = |ui: &mut Ui, _: &mut BodyCx<'_>| {
let shown = Row::new().fill_width().align(align).show(ui, |ui, _| {
first.set(Text::new("Title").show(ui).rect);
let _ = Button::new("Menu").id_salt("menu").show(ui);
last.set(Button::new("Close").id_salt("close").show(ui).rect);
});
row.set(shown.output.rect);
};
Popover::new("Box", Bind::given(true)).show(ui, |ui, _| {
let stack = Stack::new().fill();
let stack = if header { stack.header(region).header_inset(Pad::Control) } else { stack.footer(region).footer_rule() };
let shown = stack.show(ui, |ui, _| {
Text::new("A body line much wider than the title and the two buttons together").show(ui);
});
column.set(shown.output.rect);
});
if !ui.is_sizing_pass() {
out.set([column.get(), row.get(), first.get(), last.get()]);
}
});
fixture.settle();
seen.get()
}
/// A stack that takes its width from its body (a popover's) shows a fill-width row in its header or footer across the
/// band, so a `SpaceBetween` row puts its last child at the band's end and its first at the start, and an `End` row
/// puts its children together at the end.
#[trial]
fn a_spread_or_end_row_in_a_content_sized_stacks_header_or_footer_spans_the_band() {
for header in [true, false] {
for align in [RowAlign::SpaceBetween, RowAlign::End] {
let [stack, row, first, last] = region_row(header, align);
let dump = format!("header {header}, {align:?}: stack {stack:?}, row {row:?}, first {first:?}, last {last:?}");
assert!(stack.is_positive() && row.is_positive(), "the popover shows the stack and the row: {dump}");
let (start, end) = (row.min.x - stack.min.x, stack.max.x - row.max.x);
assert!((start - end).abs() <= ROUNDING, "the row spans the band, inset alike at both ends: {dump}");
assert!((last.max.x - row.max.x).abs() <= ROUNDING, "the last child ends at the band's end: {dump}");
match align {
RowAlign::SpaceBetween => assert!((first.min.x - row.min.x).abs() <= ROUNDING, "the first child starts the band: {dump}"),
_ => assert!(first.min.x > row.center().x, "the children sit together at the band's end: {dump}"),
}
}
}
}
#[trial]
fn a_stack_spaces_its_header_and_footer_from_the_body_by_its_gap() {
for gap in [None, Some(Gap::Section)] {
let [rows, header, footer] = gaps(gap);
assert!(rows > 0.0, "the header's lines are spaced ({gap:?})");
assert!((header - rows).abs() <= ROUNDING, "header to body {header}, between header lines {rows} ({gap:?})");
assert!((footer - rows).abs() <= ROUNDING, "body to footer {footer}, between header lines {rows} ({gap:?})");
}
}