From 9a71823be691be37f545985e38c5887fd0e3a9bf Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 28 Nov 2026 20:09:29 +0100 Subject: [PATCH] thing-core: a Cells control is Bordered only when its laid content row fits inside the border Device::form(h, row) replaces form(h): Bordered needs 3 rows and row + 2 edge rows <= h. A Compact control's content is clipped to its rect by render. A content node with children keeps at least its children's height, so the row the form reads is the content's own. Co-Authored-By: Claude Opus 5.5 --- crates/thing-core/src/layout/measure.rs | 11 +++-- crates/thing-core/src/layout/mod.rs | 9 +++- crates/thing-core/src/layout/tree.rs | 8 +++- crates/thing-core/src/render/mod.rs | 19 +++++++- crates/thing-core/tests/layout.rs | 58 +++++++++++++++++++++++++ crates/thing-core/tests/render.rs | 24 ++++++++++ 6 files changed, 119 insertions(+), 10 deletions(-) diff --git a/crates/thing-core/src/layout/measure.rs b/crates/thing-core/src/layout/measure.rs index 2468487..c45167d 100644 --- a/crates/thing-core/src/layout/measure.rs +++ b/crates/thing-core/src/layout/measure.rs @@ -177,10 +177,13 @@ impl Device { if self.cells && px > 0.0 { length.max(1.0) } else { length } } - /// A control's form, read off its laid border-box height `h` and never fed back to the solve: in - /// Pixels always `Bordered`; in Cells `Bordered` from `BORDERED_ROWS` rows, `Compact` below. - pub(super) fn form(self, h: f32) -> ControlForm { - if !self.cells || h >= BORDERED_ROWS { ControlForm::Bordered } else { ControlForm::Compact } + /// A control's form, read off its laid border-box height `h` and the laid height `row` of its + /// content node, never fed back to the solve: in Pixels always `Bordered`; in Cells `Bordered` + /// from `BORDERED_ROWS` rows and only when the content row fits between the border rows, else + /// `Compact` (ruling cells-bordered-needs-content-fit). + pub(super) fn form(self, h: f32, row: f32) -> ControlForm { + let (_, edge) = self.inset(ControlForm::Bordered, f32::INFINITY, f32::INFINITY); + if !self.cells || (h >= BORDERED_ROWS && row + 2.0 * edge <= h) { ControlForm::Bordered } else { ControlForm::Compact } } /// The inset of a laid control's content from its border box, `(x, y)`, in device units: the one diff --git a/crates/thing-core/src/layout/mod.rs b/crates/thing-core/src/layout/mod.rs index 4a2eb90..169c1d1 100644 --- a/crates/thing-core/src/layout/mod.rs +++ b/crates/thing-core/src/layout/mod.rs @@ -90,7 +90,9 @@ pub enum ControlForm { /// A laid control: the form layout chose and the box its content is laid or drawn in, in device px /// (Pixels) or cells: the border box less the form's inset. A Pixels Button's content is laid -/// without an inset, so its content box is its border box. +/// without an inset, so its content box is its border box. `Compact` is the form of a control whose +/// content row does not fit between the border rows: its content stands from the first row and runs +/// past the last, so a painter clips a `Compact` control to its rect. #[derive(Clone, Copy, Debug, PartialEq)] pub struct LaidControl { pub form: ControlForm, @@ -205,7 +207,10 @@ fn solve<'a>( let (px, py) = entry.parent.map_or(origin, |parent| absolute[parent]); let (x, y) = (px + laid.location.x, py + laid.location.y); let (w, h) = (laid.size.width, laid.size.height); - let form = dev.form(h); + // The content row's laid height. A control without a content node (a Pixels Button, a + // non-control) passes `h`, which the form never reads. + let row = entry.control.and_then(|c| c.inner).map_or(h, |inner| taffy.layout(inner).expect("taffy: node is in the tree").size.height); + let form = dev.form(h, row); let (ix, iy) = entry.control.filter(|c| c.inset).map_or((0.0, 0.0), |_| dev.inset(form, w, h)); // The origin the children are placed from: a control's content node sits where the inset // says, not where Taffy put it. diff --git a/crates/thing-core/src/layout/tree.rs b/crates/thing-core/src/layout/tree.rs index d529ca0..6e7071c 100644 --- a/crates/thing-core/src/layout/tree.rs +++ b/crates/thing-core/src/layout/tree.rs @@ -126,14 +126,18 @@ pub(super) fn build<'a>( impl<'a> Builder<'a, '_> { /// The shell of a Cells control: a grid of three rows (an edge row of up to one row above and /// below, the content row between) one cell in on each side, the inset rule's numbers - /// (`Device::inset`). The content node takes the middle row and at least one row of it. The edge + /// (`Device::inset`). The content node takes the middle row, at least its children's height of it (one row if it has none). The edge /// rows give way first, so the shell lays anywhere from one row up and the form is read off the /// height it ends with (`Device::form`). fn shell(&mut self, content: taffy::NodeId) -> taffy::NodeId { use taffy::style_helpers::{auto, length, line, minmax}; let (across, down) = self.dev.inset(ControlForm::Bordered, f32::INFINITY, f32::INFINITY); let mut seat = self.taffy.style(content).expect("taffy: node is in the tree").clone(); - seat.min_size.height = taffy::LengthPercentageAuto::length(1.0); + // A content node with children is never laid shorter than they are (its automatic minimum), + // so the row the form reads is the content's own height; a leaf or an empty Button holds one row. + if self.taffy.children(content).expect("taffy: node is in the tree").is_empty() { + seat.min_size.height = taffy::LengthPercentageAuto::length(1.0); + } seat.grid_row = taffy::Line { start: line(2), end: auto() }; self.taffy.set_style(content, seat).expect("taffy: style"); let edge = || -> taffy::GridTemplateComponent { minmax::(length(0.0), length(down)).into() }; diff --git a/crates/thing-core/src/render/mod.rs b/crates/thing-core/src/render/mod.rs index 2d3a08f..847e57b 100644 --- a/crates/thing-core/src/render/mod.rs +++ b/crates/thing-core/src/render/mod.rs @@ -19,7 +19,7 @@ pub use scroll::{ScrollOffsets, clamp}; use crate::bind::{Bound, BoundTree, Field}; use crate::diagnostic::{Code, Diagnostic}; -use crate::layout::{LaidControl, LaidTree, MAX_DEVICE, Measure, Metrics, Rect}; +use crate::layout::{ControlForm, LaidControl, LaidNode, LaidTree, MAX_DEVICE, Measure, Metrics, Rect}; use crate::resolve::grow; use crate::theme::{Theme, fallback}; @@ -138,6 +138,11 @@ fn meet(a: Rect, b: Rect) -> Option { (x1 >= x0 && y1 >= y0).then_some(Rect { x: x0, y: y0, w: x1 - x0, h: y1 - y0 }) } +/// Whether layout states the node a `Compact` control, whose content is clipped to its rect. +fn is_compact(node: &LaidNode) -> bool { + node.control.is_some_and(|c| c.form == ControlForm::Compact) +} + fn radius(value: f64) -> f32 { let value = value as f32; if value.is_finite() && value > 0.0 { value } else { 0.0 } @@ -228,6 +233,10 @@ impl<'a> Painter<'a> { let border = self.border(b).map_or(0.0, |(_, width)| width); self.out.commands.push(RenderCommand::ClipPush { rect: clip, radius: (radius(b.paint.radius) - border).max(0.0) }); self.clips.push(clip); + } else if is_compact(laid) { + // A Compact control's content may run past its last row: cut it to the rect the form states. + self.out.commands.push(RenderCommand::ClipPush { rect, radius: 0.0 }); + self.clips.push(rect); } } @@ -296,7 +305,13 @@ impl<'a> Painter<'a> { fn close(&mut self, i: usize) { let laid = self.laid; let node = &laid.nodes[i]; - let Some(padding) = node.clip else { return }; + let Some(padding) = node.clip else { + if is_compact(node) { + self.clips.pop(); + self.out.commands.push(RenderCommand::ClipPop); + } + return; + }; self.clips.pop(); self.out.commands.push(RenderCommand::ClipPop); let Some(max) = node.max_scroll else { return }; diff --git a/crates/thing-core/tests/layout.rs b/crates/thing-core/tests/layout.rs index 4b10a5c..ba09f15 100644 --- a/crates/thing-core/tests/layout.rs +++ b/crates/thing-core/tests/layout.rs @@ -1462,6 +1462,64 @@ fn a_content_sized_control_shrinks_to_one_compact_row_in_a_short_column() { assert_eq!((label.0, label.1, label.3), (button.0 + 1.0, button.1, 1.0), "a 1-row label on the first row of the 2-row Button"); } +/// A Button holding a block 8 cells wide and `lines` rows tall (its content row is that tall, whatever +/// the grid leaves it), alone in a root `rows` rows tall: a `column` the Button may only shrink in, or a +/// row that stretches it to the root's height. +fn button_of_lines_in(rows: f64, column: bool, lines: f64) -> LaidTree { + let content = vec![bx("blk", sized(64.0, lines * ROW), vec![])]; + let button = Node::Button(ButtonNode { common: common("btn"), content, on_activate: command("dismiss"), enabled: lit_bool(true) }); + let direction = if column { Direction::Column } else { Direction::Row }; + lay_in_cells(bx("root", BoxStyle { direction, size: size2(px(80.0), px(rows * ROW)), ..plain() }, vec![button]), 40.0, 20.0) +} + +#[test] +fn a_squeezed_control_whose_content_does_not_fit_the_border_is_compact() { + // Soul's case: 3 rows of content directly in a 4-row column. The Button lays 4 rows around a + // content row that does not fit between two border rows: Compact, content from the first row, + // clipped to the rect (render pins the clip). + let laid = button_of_lines_in(4.0, true, 3.0); + let (x, y, w, h) = rect(&laid, "btn"); + assert_eq!(h, 4.0); + assert_eq!(control(&laid, "btn"), (ControlForm::Compact, (x + 1.0, y, w - 2.0, 4.0))); + assert_eq!(rect(&laid, "blk"), (x + 1.0, y, 8.0, 3.0), "the content on the first row, three rows tall"); +} + +#[test] +fn a_control_exactly_its_content_rows_tall_is_compact() { + let laid = button_of_lines_in(3.0, true, 3.0); + let (x, y, w, h) = rect(&laid, "btn"); + assert_eq!(h, 3.0); + assert_eq!(control(&laid, "btn"), (ControlForm::Compact, (x + 1.0, y, w - 2.0, 3.0))); + assert_eq!(rect(&laid, "blk"), (x + 1.0, y, 8.0, 3.0)); +} + +#[test] +fn a_two_row_label_at_four_rows_stays_bordered() { + let laid = button_of_lines_in(4.0, false, 2.0); + let (x, y, w, h) = rect(&laid, "btn"); + assert_eq!(h, 4.0); + assert_eq!(control(&laid, "btn"), (ControlForm::Bordered, (x + 1.0, y + 1.0, w - 2.0, 2.0))); + assert_eq!(rect(&laid, "blk"), (x + 1.0, y + 1.0, 8.0, 2.0)); +} + +#[test] +fn a_control_under_three_rows_is_compact_even_when_its_content_row_is_empty() { + // Content with no height: row 0 plus two border rows is 2, which fits, but under 3 rows is Compact. + let laid = button_of_lines_in(8.0, true, 0.0); + let (x, y, w, h) = rect(&laid, "btn"); + assert_eq!(h, 2.0); + assert_eq!(control(&laid, "btn"), (ControlForm::Compact, (x + 1.0, y, w - 2.0, 2.0))); +} + +#[test] +fn a_control_whose_content_plus_its_border_is_its_height_is_bordered() { + let laid = button_of_lines_in(5.0, false, 3.0); + let (x, y, w, h) = rect(&laid, "btn"); + assert_eq!(h, 5.0); + assert_eq!(control(&laid, "btn"), (ControlForm::Bordered, (x + 1.0, y + 1.0, w - 2.0, 3.0))); + assert_eq!(rect(&laid, "blk"), (x + 1.0, y + 1.0, 8.0, 3.0)); +} + #[test] fn a_pixels_control_is_always_bordered_with_a_two_px_inset_and_a_pixels_button_has_none() { // The compact form is a Cells form: a short Pixels control keeps its chrome. diff --git a/crates/thing-core/tests/render.rs b/crates/thing-core/tests/render.rs index 64ac294..1ab0153 100644 --- a/crates/thing-core/tests/render.rs +++ b/crates/thing-core/tests/render.rs @@ -625,6 +625,30 @@ fn a_control_layout_states_no_form_for_paints_the_placeholder() { assert_eq!(rendered.commands, expected); } +#[test] +fn a_compact_buttons_overflowing_content_is_clipped_to_its_rect() { + // A 3-row label in a Button stretched to 2 rows: Compact, so its label runs past the last row and + // render cuts it to the Button's rect. Bordered controls, whose content fits, push no clip. + let button = Node::Button(ButtonNode { + common: common("btn"), + content: vec![text("label", "abcd efgh ijkl")], + on_activate: command("dismiss"), + enabled: lit_bool(true), + }); + let root = flat("root", BoxStyle { size: size2(px(80.0), px(32.0)), ..plain() }, vec![button]); + let (laid, out) = inline(root, &frame(20.0, 10.0, 1.0, Metrics::Cells), &ScrollOffsets::default()); + let node = laid.nodes.iter().find(|n| n.id.as_str() == "btn").unwrap(); + assert_eq!((node.rect.h, node.control.map(|c| c.form)), (2.0, Some(thing_core::layout::ControlForm::Compact))); + let rect = placed(&out, "btn").rect; + let at = |want: fn(&RenderCommand) -> bool| out.commands.iter().position(want).expect("a command"); + let (push, pop) = (at(|c| matches!(c, RenderCommand::ClipPush { .. })), at(|c| matches!(c, RenderCommand::ClipPop))); + assert_eq!(out.commands[push], RenderCommand::ClipPush { rect, radius: 0.0 }); + let texts: Vec = (0..out.commands.len()).filter(|&i| matches!(out.commands[i], RenderCommand::Text { .. })).collect(); + assert!(!texts.is_empty() && texts.iter().all(|&i| push < i && i < pop), "every label line between the push and the pop: {:?}", out.commands); + assert_eq!(out.commands.iter().filter(|c| matches!(c, RenderCommand::ClipPush { .. })).count(), 1); + assert!(balanced(&out.commands)); +} + #[test] fn an_oversize_asset_extent_through_bind_is_an_invalid_measure_and_renders() { let provider = ProviderId::new("provider.render");