diff --git a/crates/thing-core/src/layout/tree.rs b/crates/thing-core/src/layout/tree.rs index c91e1ec..5ca7285 100644 --- a/crates/thing-core/src/layout/tree.rs +++ b/crates/thing-core/src/layout/tree.rs @@ -200,15 +200,14 @@ impl<'a> Builder<'a, '_> { } Node::Button(b) => { if !self.hidden(&b.common.id) { - // In Cells a Button's content keeps clear of its border (`cells_inset`), centred - // between the top and bottom borders of a tall one. Pixels: render draws the chrome behind it. + // In Cells a Button's content keeps clear of its border (`cells_inset`). Its axis is a + // row as in Pixels; a tall Button centres its items between the borders by alignment. + // Pixels: render draws the chrome behind it. let style = if self.dev.cells() { let side = taffy::LengthPercentage::length(cells_inset(0.0).0); let none = taffy::LengthPercentage::length(0.0); Style { - display: taffy::Display::Flex, - flex_direction: taffy::FlexDirection::Column, - justify_content: taffy::AlignContent::CENTER, + align_items: taffy::AlignItems::CENTER, padding: taffy::Rect { left: side, right: side, top: none, bottom: none }, ..Style::default() } diff --git a/crates/thing-core/tests/goldens/layout-f01-swarm-dashboard-cells.txt b/crates/thing-core/tests/goldens/layout-f01-swarm-dashboard-cells.txt index ab92c79..44c563e 100644 --- a/crates/thing-core/tests/goldens/layout-f01-swarm-dashboard-cells.txt +++ b/crates/thing-core/tests/goldens/layout-f01-swarm-dashboard-cells.txt @@ -5,4 +5,4 @@ minds#6:mind-1/mind-name 51 0 49 1 minds#6:mind-2/mind-name 0 2 50 1 minds#6:mind-3/mind-name 51 2 49 1 restart 0 3 50 1 -restart-label 1 3 48 1 +restart-label 1 3 7 1 diff --git a/crates/thing-core/tests/layout.rs b/crates/thing-core/tests/layout.rs index 6af081d..3f00392 100644 --- a/crates/thing-core/tests/layout.rs +++ b/crates/thing-core/tests/layout.rs @@ -1265,3 +1265,32 @@ fn a_px_length_in_cells_is_cells_at_the_cell_metric_per_axis() { assert_eq!((rect(&laid, "a").2, rect(&laid, "a").3), (3.0, 2.0), "tracks: 24 px across, 32 px down"); assert_eq!(rect(&laid, "b").0, 5.0, "the gap across is 16 px: 2 cells"); } + +/// A Button of `label` Text children inside a root of `size` (None: the root hugs it), laid in +/// `frame` with the engine the frame names. +fn laid_button(labels: &[(&str, &str)], size: Option<(f64, f64)>, frame: &LayoutFrame) -> LaidTree { + let content = labels.iter().map(|(id, content)| text(id, content)).collect(); + let button = Node::Button(ButtonNode { common: common("btn"), content, on_activate: command("dismiss"), enabled: lit_bool(true) }); + let style = size.map_or_else(plain, |(w, h)| sized(w, h)); + let tree = bind(&mut MemState::new(), &surface_of(bx("root", style, vec![button]))).expect("resolves"); + let mut measure: Box = match frame.metrics { + Metrics::Cells => Box::new(FixedMeasure::cells()), + Metrics::Pixels => Box::new(FixedMeasure::pixels()), + }; + layout(&tree, frame, &mut *measure).unwrap() +} + +#[test] +fn a_buttons_content_runs_in_a_row_in_both_metrics_and_is_centred_by_alignment() { + // Two items side by side, as in Pixels; the label is one item, so a tall Button centres it + // between the borders by alignment, never by changing the axis. + for frame in [cells(40.0, 10.0), pixels(640.0, 160.0, 1.0)] { + let laid = laid_button(&[("ab", "ab"), ("cd", "cd")], None, &frame); + let (ab, cd) = (rect(&laid, "ab"), rect(&laid, "cd")); + assert_eq!(ab.1, cd.1, "{:?}: one row", frame.metrics); + assert_eq!(cd.0, ab.0 + ab.2, "{:?}: cd follows ab across", frame.metrics); + } + let tall = laid_button(&[("go", "Go")], Some((160.0, 80.0)), &cells(40.0, 10.0)); + assert_eq!(rect(&tall, "btn").3, 5.0); + assert_eq!(rect(&tall, "go").1, 2.0, "the label is centred between the borders of a 5-row Button"); +}