From 77e66090ddb79af5e79c1391e52e9e8401aeb418 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 28 Nov 2026 13:01:53 +0100 Subject: [PATCH] thing-core: the bordered-control inset is one rule, owned by layout Findings inset-two-owners-and-unpinned-edges and button-multirow-label-on-border. Device::inset is the one rule for Button, TextInput, Select and Toggle: in Cells one cell on every side once the box is 3 rows tall, none below (a control too short for a border and a content row paints no border and its content box is its whole box); in Pixels 2 logical px. Layout reserves it as padding in Cells, so a content-sized control's box includes its border rows and a wrapped label never lands on a border, and publishes the laid content box as LaidNode.inside. Render draws in that box, computes no inset, and strokes a border only when the box is inset. cells_inset and its public export are gone. The 2-row and exactly-3-row edges are pinned for all four controls in layout and render. A one-row control is now a 3-row box (27x3 for a TextInput); f01, f02, f04, f05 and f09 Cells goldens re-blessed. Co-Authored-By: Claude Sonnet 5.5 --- crates/thing-core/src/layout/measure.rs | 27 ++++--- crates/thing-core/src/layout/mod.rs | 13 ++- crates/thing-core/src/layout/tree.rs | 35 +++++--- crates/thing-core/src/render/leaf.rs | 34 ++++---- crates/thing-core/src/render/mod.rs | 13 +-- .../layout-f01-swarm-dashboard-cells.txt | 6 +- .../render-f01-swarm-dashboard-cells.txt | 8 +- crates/thing-core/tests/layout.rs | 80 +++++++++++++++++++ crates/thing-core/tests/render.rs | 36 ++++++++- .../screen-f01-swarm-dashboard-cells.txt | 20 ++--- .../goldens/screen-f02-login-effect-cells.txt | 36 ++++----- .../tests/goldens/screen-f04-form-cells.txt | 60 +++++++------- .../screen-f05-modal-overlay-cells.txt | 42 +++++----- .../screen-f09-composition-stack-cells.txt | 56 +++++++------ 14 files changed, 307 insertions(+), 159 deletions(-) diff --git a/crates/thing-core/src/layout/measure.rs b/crates/thing-core/src/layout/measure.rs index 4683fef..235d25f 100644 --- a/crates/thing-core/src/layout/measure.rs +++ b/crates/thing-core/src/layout/measure.rs @@ -105,15 +105,6 @@ pub(crate) const MAX_DEVICE: f32 = 2_097_152.0; /// leaves. A smaller side is below the resolution layout keeps. pub(super) const MIN_SIDE: f32 = 0.125; -/// The inset of a bordered control's content from its border in a cell grid, `(x, y)`: the border -/// stands in the edge cells, so content keeps one cell clear left and right, and above and below -/// when the control is tall enough (3 rows) to have an inside. A one-row control draws its border as -/// `[` and `]` in its end cells. Layout applies it to a Button's content and render to every other -/// bordered control's; Pixels insets are render's own. -pub fn cells_inset(height: f32) -> (f32, f32) { - (1.0, if height >= 3.0 { 1.0 } else { 0.0 }) -} - /// Device px per cell, `(across, down)`: the metric a terminal cell stands for. In Cells mode a Px /// length converts at it (8 wide, 16 tall), and an Image's pixel extent is read in cells at it. pub(crate) const CELL_PX: (f32, f32) = (8.0, 16.0); @@ -175,6 +166,24 @@ impl Device { } } + /// The inset of a laid bordered control's content (Button, TextInput, Select, Toggle) from its + /// border box, `(x, y)`, in device units: the one rule, owned here. Layout reserves it as padding + /// in Cells (so a content-sized control's box includes its border rows) and publishes the laid + /// content box; render reads that box and computes no inset. In Pixels: the 2 px + /// of chrome, never more than half a side. In Cells the border stands in the edge cells, so + /// content keeps one cell clear on every side when the box is tall enough (3 rows) to hold the + /// border and a content row; a shorter box paints no border and its content takes the whole box. + pub(super) fn inset(self, w: f32, h: f32) -> (f32, f32) { + let (x, y) = if !self.cells { + (self.px(2.0), self.px(2.0)) + } else if h >= 3.0 { + (1.0, 1.0) + } else { + (0.0, 0.0) + }; + (x.min(w / 2.0).max(0.0), y.min(h / 2.0).max(0.0)) + } + fn logical(self, device: f32) -> f32 { device / self.k } diff --git a/crates/thing-core/src/layout/mod.rs b/crates/thing-core/src/layout/mod.rs index a69b158..d47a4d8 100644 --- a/crates/thing-core/src/layout/mod.rs +++ b/crates/thing-core/src/layout/mod.rs @@ -18,7 +18,7 @@ use taffy::{AvailableSpace, Size, compute_leaf_layout}; use measure::Device; use thing_contract::{NodeId, OverlayNode}; -pub use measure::{Avail, CellStyle, cells_inset, Constraint, FontId, Glyph, Leaf, Measure, Run, TextLayout, TextLine, TextRequest}; +pub use measure::{Avail, CellStyle, Constraint, FontId, Glyph, Leaf, Measure, Run, TextLayout, TextLine, TextRequest}; pub(crate) use measure::{CELL_PX, MAX_DEVICE}; use crate::bind::BoundTree; @@ -69,6 +69,12 @@ pub struct LaidNode { /// it. Layout never sees a scroll position, so a scroll never re-runs layout. pub max_scroll: Option<(f32, f32)>, pub clip: Option, + /// The box a bordered control's content (Button, TextInput, Select, Toggle) is laid or drawn + /// in: the border box less the control's inset, in device px (Pixels) or cells. Layout owns + /// the inset; render draws in this box and computes none. A box with no inset (a Cells control + /// under 3 rows tall, which paints no border) has the border box itself. `None` for every other + /// node and for a Button in Pixels, whose content is laid without an inset. + pub inside: Option, } /// Every laid node in paint order (document order, Repeat instances spliced), and the style @@ -202,6 +208,10 @@ fn solve<'a>( w: clean(w - laid.border.left - laid.border.right), h: clean(h - laid.border.top - laid.border.bottom), }); + let inside = entry.control.then(|| { + let (ix, iy) = dev.inset(w, h); + Rect { x: clean(x + ix), y: clean(y + iy), w: clean(w - 2.0 * ix), h: clean(h - 2.0 * iy) } + }); nodes.push(LaidNode { id: entry.id.clone(), parent: entry.parent.map(|parent| (base + parent) as u32), @@ -209,6 +219,7 @@ fn solve<'a>( content, max_scroll, clip, + inside, }); } diagnostics.append(own); diff --git a/crates/thing-core/src/layout/tree.rs b/crates/thing-core/src/layout/tree.rs index 5ca7285..31884b9 100644 --- a/crates/thing-core/src/layout/tree.rs +++ b/crates/thing-core/src/layout/tree.rs @@ -3,7 +3,7 @@ use taffy::{Style, TaffyTree}; use thing_contract::{Direction, Node, NodeId, Overflow, OverlayNode}; -use super::measure::{Axis, Device, Leaf, MAX_DEVICE, cells_inset, intrinsic_ratio}; +use super::measure::{Axis, Device, Leaf, MAX_DEVICE, intrinsic_ratio}; use super::style::{Invalid, box_style, embed_style}; use crate::bind::{Bound, BoundTree, Field}; use crate::diagnostic::{Code, Diagnostic}; @@ -18,6 +18,8 @@ pub(super) struct Entry<'a> { pub(super) parent: Option, pub(super) clip: bool, pub(super) scroll: bool, + /// A bordered control (Button, TextInput, Select, Toggle): its content box is published. + pub(super) control: bool, } pub(super) struct Built<'a> { @@ -111,6 +113,12 @@ pub(super) fn build<'a>( } impl<'a> Builder<'a, '_> { + /// A bordered control's padding in Cells: one cell on every side, where its border stands. + fn chrome_padding(&self) -> taffy::Rect { + let one = taffy::LengthPercentage::length(1.0); + taffy::Rect { left: one, right: one, top: one, bottom: one } + } + fn hidden(&self, id: &NodeId) -> bool { matches!(self.tree.value(id, Field::Visible), Some(Bound::Bool(false))) } @@ -133,7 +141,7 @@ impl<'a> Builder<'a, '_> { fn entry(&mut self, id: &'a NodeId, parent: Option, overflow: &Overflow) -> usize { let clip = !matches!(overflow, Overflow::Visible); let scroll = matches!(overflow, Overflow::Scroll); - self.entries.push(Entry { id, node: taffy::NodeId::from(0u64), parent, clip, scroll }); + self.entries.push(Entry { id, node: taffy::NodeId::from(0u64), parent, clip, scroll, control: false }); self.entries.len() - 1 } @@ -153,7 +161,12 @@ impl<'a> Builder<'a, '_> { Err(()) => self.report(id, vec!["measure"]), } } + let control = matches!(leaf, Leaf::TextInput { .. } | Leaf::Toggle { .. } | Leaf::Select { .. }); + if control && self.dev.cells() { + style.padding = self.chrome_padding(); + } let index = self.entry(id, parent, &Overflow::Visible); + self.entries[index].control = control; let context = self.leaves.len(); self.leaves.push(leaf); let node = self.taffy.new_leaf_with_context(style, context).expect("taffy: insert"); @@ -200,21 +213,19 @@ 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`). 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. + // In Cells the border stands in the edge cells: the content keeps clear of it on + // every side (the inset rule, `Device::inset`), so a content-sized Button's box + // includes its border rows. The content is a row as in Pixels, its items centred + // between the borders of a tall Button 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 { - align_items: taffy::AlignItems::CENTER, - padding: taffy::Rect { left: side, right: side, top: none, bottom: none }, - ..Style::default() - } + Style { align_items: taffy::AlignItems::CENTER, padding: self.chrome_padding(), ..Style::default() } } else { Style::default() }; + let index = self.entries.len(); self.container(&b.common.id, parent, (style, Vec::new()), &Overflow::Visible, Axis::X, &b.content, out); + self.entries[index].control = self.dev.cells(); } } // A Repeat has no box: its instances are its parent's children, in instance order. diff --git a/crates/thing-core/src/render/leaf.rs b/crates/thing-core/src/render/leaf.rs index 8114e83..933e4c6 100644 --- a/crates/thing-core/src/render/leaf.rs +++ b/crates/thing-core/src/render/leaf.rs @@ -8,7 +8,7 @@ use super::Painter; use super::command::RenderCommand; use crate::bind::{Bound, Field}; use crate::diagnostic::{Code, Diagnostic}; -use crate::layout::{Measure, Metrics, Rect, TextRequest, cells_inset}; +use crate::layout::{Measure, Metrics, Rect, TextRequest}; /// Where an Image lands inside `rect` for an extent of `(w, h)` asset px (1 asset px = 1 logical /// px), and whether it must be clipped to `rect`. @@ -70,25 +70,25 @@ impl<'a> Painter<'a> { self.out.commands.push(RenderCommand::StrokeRect { rect, radius: 0.0, width: 1.0, color }); } - /// The content box of a control: `rect` less the inset of its border, 2 logical px all round - /// or, in a cell grid, `cells_inset`. - fn content_box(&self, rect: Rect) -> Rect { - let (x, y) = if self.input.metrics == Metrics::Cells { cells_inset(rect.h) } else { (2.0, 2.0) }; - let (x, y) = (x.min(rect.w / 2.0).max(0.0), y.min(rect.h / 2.0).max(0.0)); - Rect { x: rect.x + x, y: rect.y + y, w: rect.w - 2.0 * x, h: rect.h - 2.0 * y } + /// A control's border: stroked unless layout left it no inset (its content box is its whole + /// box), which is a control too short to hold a border and a row. `inner` is the laid content + /// box; a Button laid without one (Pixels) always has its border. + fn control_border(&mut self, id: &NodeId, rect: Rect, inner: Option) { + if inner != Some(rect) { + self.stroke(id, rect); + } } - pub(super) fn button(&mut self, n: &ButtonNode, rect: Rect) { + pub(super) fn button(&mut self, n: &ButtonNode, rect: Rect, inner: Option) { let id = &n.common.id; let enabled = self.flag(id, Field::Enabled) != Some(false); self.fill(id, rect, if enabled { "thing.control.fill" } else { "thing.control.disabled" }); - self.stroke(id, rect); + self.control_border(id, rect, inner); } - pub(super) fn toggle(&mut self, n: &ToggleNode, rect: Rect) { + pub(super) fn toggle(&mut self, n: &ToggleNode, rect: Rect, inner: Rect) { let id = &n.common.id; - self.stroke(id, rect); - let inner = self.content_box(rect); + self.control_border(id, rect, Some(inner)); let side = inner.h.min(inner.w / 2.0).max(0.0); let checked = self.flag(id, Field::Checked) == Some(true); let x = if checked { inner.x + inner.w - side } else { inner.x }; @@ -118,22 +118,21 @@ impl<'a> Painter<'a> { self.fill(id, Rect { x, y: rect.y, w: side, h: side }, "thing.control.accent"); } - pub(super) fn select(&mut self, n: &SelectNode, rect: Rect, text: &mut dyn Measure) { + pub(super) fn select(&mut self, n: &SelectNode, rect: Rect, inner: Rect, text: &mut dyn Measure) { let id = &n.common.id; - self.stroke(id, rect); + self.control_border(id, rect, Some(inner)); let Some(value) = self.bound_text(id, Field::Selected) else { return }; let tree = self.tree; let label = match tree.value(id, Field::Options) { Some(Bound::Options(options)) => options.iter().find(|o| o.value == value).map_or(value, |o| o.label.as_str()), _ => value, }; - let inner = self.content_box(rect); self.draw_text(id, label, "thing.text", false, Some(1), inner, inner.h, text); } - pub(super) fn text_input(&mut self, n: &TextInputNode, rect: Rect, text: &mut dyn Measure) { + pub(super) fn text_input(&mut self, n: &TextInputNode, rect: Rect, inner: Rect, text: &mut dyn Measure) { let id = &n.common.id; - self.stroke(id, rect); + self.control_border(id, rect, Some(inner)); let (content, token) = match self.bound_text(id, Field::Initial).filter(|t| !t.is_empty()) { Some(initial) => (initial, "thing.text"), None => match self.bound_text(id, Field::Placeholder) { @@ -141,7 +140,6 @@ impl<'a> Painter<'a> { None => return, }, }; - let inner = self.content_box(rect); self.draw_text(id, content, token, n.multiline, if n.multiline { None } else { Some(1) }, inner, inner.h, text); } diff --git a/crates/thing-core/src/render/mod.rs b/crates/thing-core/src/render/mod.rs index e24b0eb..537f0db 100644 --- a/crates/thing-core/src/render/mod.rs +++ b/crates/thing-core/src/render/mod.rs @@ -221,7 +221,8 @@ impl<'a> Painter<'a> { self.out.placed.push(Placed { id: laid.id.clone(), rect, visible }); self.stack.push(i); let Some(node) = node else { return }; - self.paint(node, rect, text); + let inner = laid.inside.map(|inside| self.logical(inside, shift)); + self.paint(node, rect, inner, text); if let (Some(padding), Node::Box(b)) = (laid.clip, node) { let clip = self.logical(padding, shift); let border = self.border(b).map_or(0.0, |(_, width)| width); @@ -230,7 +231,7 @@ impl<'a> Painter<'a> { } } - fn paint(&mut self, node: &'a Node, rect: Rect, text: &mut dyn Measure) { + fn paint(&mut self, node: &'a Node, rect: Rect, inner: Option, text: &mut dyn Measure) { match node { Node::Box(b) => { let id = &b.common.id; @@ -267,11 +268,11 @@ impl<'a> Painter<'a> { _ => {} } } - Node::Button(n) => self.button(n, rect), - Node::Toggle(n) => self.toggle(n, rect), + Node::Button(n) => self.button(n, rect, inner), + Node::Toggle(n) => self.toggle(n, rect, inner.unwrap_or(rect)), Node::Slider(n) => self.slider(n, rect), - Node::Select(n) => self.select(n, rect, text), - Node::TextInput(n) => self.text_input(n, rect, text), + Node::Select(n) => self.select(n, rect, inner.unwrap_or(rect), text), + Node::TextInput(n) => self.text_input(n, rect, inner.unwrap_or(rect), text), Node::Image(n) => self.image(n, rect, text), Node::Plot(n) => self.plot(n, rect), Node::Overlay(_) | Node::Repeat(_) | Node::Slot(_) => {} 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 44c563e..5a28c2d 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 @@ -1,8 +1,8 @@ surface thing:surface:provider.swarm.dashboard viewport 100x37 scale 1 cells -root 0 0 100 4 +root 0 0 100 6 load 0 0 50 1 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 7 1 +restart 0 3 50 3 +restart-label 1 4 7 1 diff --git a/crates/thing-core/tests/goldens/render-f01-swarm-dashboard-cells.txt b/crates/thing-core/tests/goldens/render-f01-swarm-dashboard-cells.txt index ff2ced7..f847bf7 100644 --- a/crates/thing-core/tests/goldens/render-f01-swarm-dashboard-cells.txt +++ b/crates/thing-core/tests/goldens/render-f01-swarm-dashboard-cells.txt @@ -1,9 +1,9 @@ surface thing:surface:provider.swarm.dashboard viewport 100x37 scale 1 cells -fill 0 0 100 4 r8 #1e1e2eff +fill 0 0 100 6 r8 #1e1e2eff text 0 0 #ffffffff cells "42.0%" bold=false italic=false underline=false text 51 0 #eeeef4ff cells "Eureka" bold=false italic=false underline=false text 0 2 #eeeef4ff cells "Libby" bold=false italic=false underline=false text 51 2 #eeeef4ff cells "Kiko" bold=false italic=false underline=false -fill 0 3 50 1 r0 #2c2c3eff -stroke 0 3 50 1 r0 w1 #4a4a60ff -text 1 3 #eeeef4ff cells "Restart" bold=false italic=false underline=false +fill 0 3 50 3 r0 #2c2c3eff +stroke 0 3 50 3 r0 w1 #4a4a60ff +text 1 4 #eeeef4ff cells "Restart" bold=false italic=false underline=false diff --git a/crates/thing-core/tests/layout.rs b/crates/thing-core/tests/layout.rs index 3f00392..41d399c 100644 --- a/crates/thing-core/tests/layout.rs +++ b/crates/thing-core/tests/layout.rs @@ -1294,3 +1294,83 @@ fn a_buttons_content_runs_in_a_row_in_both_metrics_and_is_centred_by_alignment() 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"); } + +/// One bordered control of each kind, by name. +fn bordered(kind: &str) -> Node { + match kind { + "button" => Node::Button(ButtonNode { common: common(kind), content: vec![text("label", "Go")], on_activate: command("dismiss"), enabled: lit_bool(true) }), + "toggle" => Node::Toggle(ToggleNode { common: common(kind), checked: lit_bool(true), on_change: command("dismiss") }), + "input" => Node::TextInput(TextInputNode { + common: common(kind), + multiline: false, + input_kind: InputKind::Text, + placeholder: lit_text("type"), + initial: lit_text("hi"), + submit: command("dismiss"), + }), + "select" => { + let options = OptionList::Literal(LiteralOptions { options: vec![SelectOption { value: "a".into(), label: "Alpha".into() }] }); + let value = Bind { source: StateSource::Record(StateRef { provider: Some(ProviderId::new("provider.layout")), schema: SchemaId::new("gamecult.thing.test.swarm.v1"), key: RecordKey::new("row") }), member: path(&["Name"]), format: None }; + Node::Select(SelectNode { common: common(kind), options, value, on_change: command("dismiss") }) + } + other => panic!("no control {other}"), + } +} + +const BORDERED: [&str; 4] = ["button", "toggle", "input", "select"]; + +fn lay_control(kind: &str, size: Option<(f64, f64)>, frame: &LayoutFrame) -> LaidTree { + let style = size.map_or_else(plain, |(w, h)| sized(w, h)); + let tree = bind(&mut MemState::new(), &surface_of(bx("root", style, vec![bordered(kind)]))).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() +} + +fn inside(laid: &LaidTree, id: &str) -> Option<(f32, f32, f32, f32)> { + let node = laid.nodes.iter().find(|n| n.id.as_str() == id).unwrap_or_else(|| panic!("no node {id}")); + node.inside.map(|r| (r.x, r.y, r.w, r.h)) +} + +#[test] +fn the_cells_inset_is_one_rule_for_every_bordered_control_with_its_edges_at_2_and_3_rows() { + // The border stands in the edge cells and the content box is one cell in on every side, once + // the box is 3 rows tall: a border and a content row. At 2 rows there is no room for both, so + // the control paints no border and its content box is its whole box. + for kind in BORDERED { + for (rows, border) in [(2.0_f32, false), (3.0, true), (4.0, true), (7.0, true)] { + let laid = lay_control(kind, Some((20.0 * 8.0, f64::from(rows) * 16.0)), &cells(40.0, 10.0)); + let (x, y, w, h) = rect(&laid, kind); + assert_eq!(h, rows, "{kind}: stretched to the root's {rows} rows"); + let want = if border { (x + 1.0, y + 1.0, w - 2.0, h - 2.0) } else { (x, y, w, h) }; + assert_eq!(inside(&laid, kind), Some(want), "{kind} at {rows} rows"); + } + } +} + +#[test] +fn a_content_sized_cells_control_includes_its_border_rows() { + for kind in BORDERED { + let laid = lay_control(kind, None, &cells(40.0, 10.0)); + let (x, y, w, h) = rect(&laid, kind); + assert!(h >= 3.0, "{kind}: a border row above and below at least one content row"); + assert_eq!(inside(&laid, kind), Some((x + 1.0, y + 1.0, w - 2.0, h - 2.0)), "{kind}"); + } + // A label that wraps to three lines in a Button 8 cells wide: 3 rows of label between 2 of border. + let hugging = laid_button(&[("label", "abcd efgh ijkl")], None, &cells(8.0, 10.0)); + let (button, label) = (rect(&hugging, "btn"), rect(&hugging, "label")); + assert_eq!((button.3, label.3), (label.3 + 2.0, 3.0), "3 label rows and 2 border rows"); + assert_eq!((label.0, label.1), (button.0 + 1.0, button.1 + 1.0), "the label starts inside the border"); +} + +#[test] +fn a_pixels_control_inset_is_two_logical_px_and_a_pixels_button_has_none() { + let laid = lay_control("input", Some((200.0, 100.0)), &pixels(800.0, 600.0, 2.0)); + let (x, y, w, h) = rect(&laid, "input"); + assert_eq!(inside(&laid, "input"), Some((x + 4.0, y + 4.0, w - 8.0, h - 8.0)), "2 logical px is 4 device px at scale 2"); + let button = lay_control("button", None, &pixels(800.0, 600.0, 2.0)); + assert_eq!(inside(&button, "button"), None, "Pixels lays a Button's content without an inset"); + assert_eq!(inside(&button, "label"), None, "and a node that is no control has no content box"); +} diff --git a/crates/thing-core/tests/render.rs b/crates/thing-core/tests/render.rs index 396c405..a7d93a5 100644 --- a/crates/thing-core/tests/render.rs +++ b/crates/thing-core/tests/render.rs @@ -1195,7 +1195,8 @@ fn cells_text_origins(root: Node) -> Vec<(f32, f32)> { #[test] fn cells_content_keeps_clear_of_every_bordered_controls_border() { - // A one-row control: one cell in from each end. A TextInput's text starts at x 1, not on `[`. + // A content-sized control is a box with its border rows: one cell in on every side. A + // TextInput's text starts at (1, 1) of its box, never on the border. let input = Node::TextInput(TextInputNode { common: common("i"), multiline: false, @@ -1204,7 +1205,7 @@ fn cells_content_keeps_clear_of_every_bordered_controls_border() { initial: lit_text("hi"), submit: command("dismiss"), }); - assert_eq!(cells_text_origins(flat("root", plain(), vec![input])), [(1.0, 0.0)]); + assert_eq!(cells_text_origins(flat("root", plain(), vec![input])), [(1.0, 1.0)]); // A Button's content likewise, with the label centred between the borders of a tall Button. let button = |height: f64| { let mut style = plain(); @@ -1212,7 +1213,7 @@ fn cells_content_keeps_clear_of_every_bordered_controls_border() { let node = Node::Button(ButtonNode { common: common("b"), content: vec![text("label", "Go")], on_activate: command("dismiss"), enabled: lit_bool(true) }); flat("root", style, vec![node]) }; - assert_eq!(cells_text_origins(button(1.0)), [(1.0, 0.0)]); + assert_eq!(cells_text_origins(button(3.0)), [(1.0, 1.0)]); assert_eq!(cells_text_origins(button(5.0)), [(1.0, 2.0)]); // Pixels are unchanged: content sits on the content box, 2 px in. let pixels_input = Node::TextInput(TextInputNode { @@ -1240,3 +1241,32 @@ fn an_image_extent_and_a_px_box_of_the_same_size_are_the_same_cells() { assert_eq!(xywh(placed(&rendered, "pic")), (0.0, 0.0, 8.0, 2.0)); assert_eq!(xywh(placed(&rendered, "twin")), (8.0, 0.0, 8.0, 2.0)); } + +#[test] +fn a_cells_control_too_short_for_a_border_and_a_row_paints_no_border() { + // Layout publishes the content box; a box that is the whole border box has no border to paint. + // 3 rows is the least that holds the border and a content row. + let strokes = |kind: &str, rows: f64| { + let mut style = plain(); + style.size = size2(px(20.0 * 8.0), px(rows * 16.0)); + let node = match kind { + "input" => Node::TextInput(TextInputNode { + common: common("c"), + multiline: false, + input_kind: InputKind::Text, + placeholder: lit_text("type"), + initial: lit_text("hi"), + submit: command("dismiss"), + }), + "toggle" => Node::Toggle(ToggleNode { common: common("c"), checked: lit_bool(true), on_change: command("dismiss") }), + _ => Node::Button(ButtonNode { common: common("c"), content: vec![text("label", "Go")], on_activate: command("dismiss"), enabled: lit_bool(true) }), + }; + let (_, rendered) = inline(flat("root", style, vec![node]), &frame(30.0, 12.0, 1.0, Metrics::Cells), &ScrollOffsets::default()); + rendered.commands.iter().filter(|c| matches!(c, RenderCommand::StrokeRect { .. })).count() + }; + for kind in ["input", "toggle", "button"] { + assert_eq!(strokes(kind, 2.0), 0, "{kind} at 2 rows"); + assert_eq!(strokes(kind, 3.0), 1, "{kind} at 3 rows"); + assert_eq!(strokes(kind, 6.0), 1, "{kind} at 6 rows"); + } +} diff --git a/crates/thing-tui/tests/goldens/screen-f01-swarm-dashboard-cells.txt b/crates/thing-tui/tests/goldens/screen-f01-swarm-dashboard-cells.txt index 2465ff8..c5ed2fd 100644 --- a/crates/thing-tui/tests/goldens/screen-f01-swarm-dashboard-cells.txt +++ b/crates/thing-tui/tests/goldens/screen-f01-swarm-dashboard-cells.txt @@ -2,9 +2,9 @@ surface thing:surface:provider.swarm.dashboard 100x37 42.0% Eureka Libby Kiko -[Restart ] - - +┌────────────────────────────────────────────────┐ +│Restart │ +└────────────────────────────────────────────────┘ @@ -45,13 +45,15 @@ style 2 0 5 #eeeef4 #1e1e2e - style 2 5 46 - #1e1e2e - style 2 51 4 #eeeef4 #1e1e2e - style 2 55 45 - #1e1e2e - -style 3 0 1 #4a4a60 #2c2c3e - -style 3 1 7 #eeeef4 #2c2c3e - -style 3 8 41 - #2c2c3e - -style 3 49 1 #4a4a60 #2c2c3e - +style 3 0 50 #4a4a60 #2c2c3e - style 3 50 50 - #1e1e2e - -style 4 0 100 - - - -style 5 0 100 - - - +style 4 0 1 #4a4a60 #2c2c3e - +style 4 1 7 #eeeef4 #2c2c3e - +style 4 8 41 - #2c2c3e - +style 4 49 1 #4a4a60 #2c2c3e - +style 4 50 50 - #1e1e2e - +style 5 0 50 #4a4a60 #2c2c3e - +style 5 50 50 - #1e1e2e - style 6 0 100 - - - style 7 0 100 - - - style 8 0 100 - - - diff --git a/crates/thing-tui/tests/goldens/screen-f02-login-effect-cells.txt b/crates/thing-tui/tests/goldens/screen-f02-login-effect-cells.txt index 5e9e394..04ef29a 100644 --- a/crates/thing-tui/tests/goldens/screen-f02-login-effect-cells.txt +++ b/crates/thing-tui/tests/goldens/screen-f02-login-effect-cells.txt @@ -1,7 +1,11 @@ surface thing:surface:provider.login.login 100x37 -[username ] +┌──────────────────────────────────────────────────────────────────────────────────────────────────┐ +│username │ +└──────────────────────────────────────────────────────────────────────────────────────────────────┘ -[Sign in ] +┌──────────────────────────────────────────────────────────────────────────────────────────────────┐ +│Sign in │ +└──────────────────────────────────────────────────────────────────────────────────────────────────┘ @@ -32,23 +36,19 @@ surface thing:surface:provider.login.login 100x37 - - - - -style 0 0 1 #4a4a60 - - -style 0 1 8 #6e6e80 - - -style 0 9 90 - - - -style 0 99 1 #4a4a60 - - -style 1 0 100 - - - -style 2 0 1 #4a4a60 #2c2c3e - -style 2 1 7 #eeeef4 #2c2c3e - -style 2 8 91 - #2c2c3e - -style 2 99 1 #4a4a60 #2c2c3e - +style 0 0 100 #4a4a60 - - +style 1 0 1 #4a4a60 - - +style 1 1 8 #6e6e80 - - +style 1 9 90 - - - +style 1 99 1 #4a4a60 - - +style 2 0 100 #4a4a60 - - style 3 0 100 - - - -style 4 0 100 - - - -style 5 0 100 - - - -style 6 0 100 - - - +style 4 0 100 #4a4a60 #2c2c3e - +style 5 0 1 #4a4a60 #2c2c3e - +style 5 1 7 #eeeef4 #2c2c3e - +style 5 8 91 - #2c2c3e - +style 5 99 1 #4a4a60 #2c2c3e - +style 6 0 100 #4a4a60 #2c2c3e - style 7 0 100 - - - style 8 0 100 - - - style 9 0 100 - - - diff --git a/crates/thing-tui/tests/goldens/screen-f04-form-cells.txt b/crates/thing-tui/tests/goldens/screen-f04-form-cells.txt index a7b13f3..e8051e7 100644 --- a/crates/thing-tui/tests/goldens/screen-f04-form-cells.txt +++ b/crates/thing-tui/tests/goldens/screen-f04-form-cells.txt @@ -1,10 +1,16 @@ surface thing:surface:provider.settings.form 100x37 -[ ] +┌──────────────────────────────────────────────────────────────────────────────────────────────────┐ +│ │ +└──────────────────────────────────────────────────────────────────────────────────────────────────┘ -[Dark ] +┌──────────────────────────────────────────────────────────────────────────────────────────────────┐ +│Dark │ +└──────────────────────────────────────────────────────────────────────────────────────────────────┘ -[8080 ] +┌──────────────────────────────────────────────────────────────────────────────────────────────────┐ +│8080 │ +└──────────────────────────────────────────────────────────────────────────────────────────────────┘ @@ -30,34 +36,28 @@ surface thing:surface:provider.settings.form 100x37 - - - - - - -style 0 0 1 #4a4a60 - - -style 0 1 97 - - - -style 0 98 1 - #7c9cff - -style 0 99 1 #4a4a60 - - -style 1 0 100 - - - -style 2 0 99 - #4a4a60 - -style 2 99 1 - #7c9cff - -style 3 0 1 #4a4a60 - - -style 3 1 4 #eeeef4 - - -style 3 5 94 - - - -style 3 99 1 #4a4a60 - - -style 4 0 100 - - - -style 5 0 1 #4a4a60 - - -style 5 1 4 #eeeef4 - - -style 5 5 94 - - - -style 5 99 1 #4a4a60 - - -style 6 0 100 - - - -style 7 0 100 - - - +style 0 0 100 #4a4a60 - - +style 1 0 1 #4a4a60 - - +style 1 1 97 - - - +style 1 98 1 - #7c9cff - +style 1 99 1 #4a4a60 - - +style 2 0 100 #4a4a60 - - +style 3 0 100 - - - +style 4 0 99 - #4a4a60 - +style 4 99 1 - #7c9cff - +style 5 0 100 #4a4a60 - - +style 6 0 1 #4a4a60 - - +style 6 1 4 #eeeef4 - - +style 6 5 94 - - - +style 6 99 1 #4a4a60 - - +style 7 0 100 #4a4a60 - - style 8 0 100 - - - -style 9 0 100 - - - -style 10 0 100 - - - -style 11 0 100 - - - +style 9 0 100 #4a4a60 - - +style 10 0 1 #4a4a60 - - +style 10 1 4 #eeeef4 - - +style 10 5 94 - - - +style 10 99 1 #4a4a60 - - +style 11 0 100 #4a4a60 - - style 12 0 100 - - - style 13 0 100 - - - style 14 0 100 - - - diff --git a/crates/thing-tui/tests/goldens/screen-f05-modal-overlay-cells.txt b/crates/thing-tui/tests/goldens/screen-f05-modal-overlay-cells.txt index 4c5a226..beeccbf 100644 --- a/crates/thing-tui/tests/goldens/screen-f05-modal-overlay-cells.txt +++ b/crates/thing-tui/tests/goldens/screen-f05-modal-overlay-cells.txt @@ -14,14 +14,14 @@ The archive holds 12 items. - - Delete the archive? -[Delete ] - -[Cancel ] - +┌──────────────────────────────────────────────────────────────────────────────────────────────────┐ +│Delete │ +└──────────────────────────────────────────────────────────────────────────────────────────────────┘ +┌──────────────────────────────────────────────────────────────────────────────────────────────────┐ +│Cancel │ +└──────────────────────────────────────────────────────────────────────────────────────────────────┘ @@ -51,22 +51,22 @@ style 10 0 100 - #000000 - style 11 0 100 - #000000 - style 12 0 100 - #000000 - style 13 0 100 - #000000 - -style 14 0 100 - #000000 - -style 15 0 100 - #000000 - -style 16 0 100 - #2c2c3e - -style 17 0 19 #eeeef4 #2c2c3e - -style 17 19 81 - #2c2c3e - -style 18 0 1 #4a4a60 #2c2c3e - -style 18 1 6 #eeeef4 #2c2c3e - -style 18 7 92 - #2c2c3e - -style 18 99 1 #4a4a60 #2c2c3e - +style 14 0 100 - #2c2c3e - +style 15 0 19 #eeeef4 #2c2c3e - +style 15 19 81 - #2c2c3e - +style 16 0 100 #4a4a60 #2c2c3e - +style 17 0 1 #4a4a60 #2c2c3e - +style 17 1 6 #eeeef4 #2c2c3e - +style 17 7 92 - #2c2c3e - +style 17 99 1 #4a4a60 #2c2c3e - +style 18 0 100 #4a4a60 #2c2c3e - style 19 0 100 - #2c2c3e - -style 20 0 1 #4a4a60 #2c2c3e - -style 20 1 6 #eeeef4 #2c2c3e - -style 20 7 92 - #2c2c3e - -style 20 99 1 #4a4a60 #2c2c3e - -style 21 0 100 - #000000 - -style 22 0 100 - #000000 - +style 20 0 100 #4a4a60 #2c2c3e - +style 21 0 1 #4a4a60 #2c2c3e - +style 21 1 6 #eeeef4 #2c2c3e - +style 21 7 92 - #2c2c3e - +style 21 99 1 #4a4a60 #2c2c3e - +style 22 0 100 #4a4a60 #2c2c3e - style 23 0 100 - #000000 - style 24 0 100 - #000000 - style 25 0 100 - #000000 - diff --git a/crates/thing-tui/tests/goldens/screen-f09-composition-stack-cells.txt b/crates/thing-tui/tests/goldens/screen-f09-composition-stack-cells.txt index 35349dd..a13fa87 100644 --- a/crates/thing-tui/tests/goldens/screen-f09-composition-stack-cells.txt +++ b/crates/thing-tui/tests/goldens/screen-f09-composition-stack-cells.txt @@ -4,10 +4,14 @@ Load Card - [Open ] + ┌─────────────────────────────────────────────────────────────────────────────────────────────────┐ + │Open │ + └─────────────────────────────────────────────────────────────────────────────────────────────────┘ Card - [Open ] + ┌────────────────────────────────────────────────────────────────────────────────────────────────┐ + │Open │ + └────────────────────────────────────────────────────────────────────────────────────────────────┘ nested @@ -27,10 +31,6 @@ Load - - - - @@ -45,27 +45,33 @@ style 3 1 4 #ffffff - - style 3 5 95 - - - style 4 0 100 - - - style 5 0 1 - - - -style 5 1 1 #4a4a60 #2c2c3e - -style 5 2 4 #eeeef4 #2c2c3e - -style 5 6 93 - #2c2c3e - -style 5 99 1 #4a4a60 #2c2c3e - +style 5 1 99 #4a4a60 #2c2c3e - style 6 0 1 - - - -style 6 1 4 #ffffff - - -style 6 5 95 - - - -style 7 0 100 - - - +style 6 1 1 #4a4a60 #2c2c3e - +style 6 2 4 #eeeef4 #2c2c3e - +style 6 6 93 - #2c2c3e - +style 6 99 1 #4a4a60 #2c2c3e - +style 7 0 1 - - - +style 7 1 99 #4a4a60 #2c2c3e - style 8 0 1 - - - -style 8 1 1 #4a4a60 #2c2c3e - -style 8 2 4 #eeeef4 #2c2c3e - -style 8 6 92 - #2c2c3e - -style 8 98 1 #4a4a60 #2c2c3e - -style 8 99 1 - - - -style 9 0 1 - - - -style 9 1 6 #eeeef4 - - -style 9 7 93 - - - -style 10 0 100 - - - -style 11 0 100 - - - -style 12 0 100 - - - -style 13 0 100 - - - +style 8 1 4 #ffffff - - +style 8 5 95 - - - +style 9 0 100 - - - +style 10 0 1 - - - +style 10 1 98 #4a4a60 #2c2c3e - +style 10 99 1 - - - +style 11 0 1 - - - +style 11 1 1 #4a4a60 #2c2c3e - +style 11 2 4 #eeeef4 #2c2c3e - +style 11 6 92 - #2c2c3e - +style 11 98 1 #4a4a60 #2c2c3e - +style 11 99 1 - - - +style 12 0 1 - - - +style 12 1 98 #4a4a60 #2c2c3e - +style 12 99 1 - - - +style 13 0 1 - - - +style 13 1 6 #eeeef4 - - +style 13 7 93 - - - style 14 0 100 - - - style 15 0 100 - - - style 16 0 100 - - -