diff --git a/crates/thing-core/src/layout/measure.rs b/crates/thing-core/src/layout/measure.rs index 34dc0c6..72e9fe5 100644 --- a/crates/thing-core/src/layout/measure.rs +++ b/crates/thing-core/src/layout/measure.rs @@ -170,6 +170,13 @@ impl Device { } } + /// A border side of `px` along `axis`: its Px length, and in Cells at least one cell when it is + /// nonzero, so the terminal shows the border and content never sits under it. Zero stays zero. + pub(super) fn border(self, px: f32, axis: Axis) -> f32 { + let length = self.length(px, axis); + if self.cells && px > 0.0 { length.max(1.0) } else { length } + } + /// The inset of a laid control's content from its border box, `(x, y)`, in device units: the one /// rule, owned here. In Pixels: the 2 px of chrome. In Cells a `Bordered` control keeps one cell /// clear on every side, a `Compact` one a cell on each side across and none down. Never more than diff --git a/crates/thing-core/src/layout/style.rs b/crates/thing-core/src/layout/style.rs index 23e6eb2..6f6f4dc 100644 --- a/crates/thing-core/src/layout/style.rs +++ b/crates/thing-core/src/layout/style.rs @@ -211,9 +211,9 @@ pub(super) fn box_style(s: &c::BoxStyle, paint: &c::Paint, dev: Device, main: Ax let (border_x, border_y) = match &paint.border { None => (LengthPercentage::ZERO, LengthPercentage::ZERO), Some(b) => { - let width = c::Scalar { value: b.width }; - let across = slots.take("border", length(&width, dev, Axis::X, false), 0.0); - let down = length(&width, dev, Axis::Y, false).unwrap_or(0.0); + let side = |axis| number(b.width, false).map(|v| dev.border(v, axis)).filter(|v| v.abs() <= MAX_DEVICE); + let across = slots.take("border", side(Axis::X), 0.0); + let down = side(Axis::Y).unwrap_or(0.0); (LengthPercentage::length(across), LengthPercentage::length(down)) } }; diff --git a/crates/thing-core/tests/layout.rs b/crates/thing-core/tests/layout.rs index edd1d21..5f4fc08 100644 --- a/crates/thing-core/tests/layout.rs +++ b/crates/thing-core/tests/layout.rs @@ -1419,3 +1419,21 @@ fn a_control_smaller_than_its_inset_gives_up_half_of_each_side_at_most() { assert_eq!(rect(&laid, "input"), (0.0, 0.0, 2.0, 2.0)); assert_eq!(control(&laid, "input"), (ControlForm::Bordered, (1.0, 1.0, 0.0, 0.0))); } + +#[test] +fn a_nonzero_border_side_is_at_least_one_cell_in_cells_so_content_never_sits_under_the_stroke() { + // A 20x4-cell Box holding four text lines: whatever the hairline's width, the stroke is one cell + // wide on the perimeter and the first line starts inside it. Zero stays zero. + let framed = |width: f64| { + let border = Paint { background: None, border: Some(Border { color: ColorRef::Rgba(Rgba { r: 0, g: 0, b: 0, a: 255 }), width }), radius: 0.0 }; + let lines = ["one", "two", "three", "four"].map(|id| text(id, id)).to_vec(); + let root = bx_paint("framed", BoxStyle { direction: Direction::Column, ..sized(160.0, 64.0) }, border, lines); + lay_with(&mut MemState::new(), root, &cells(40.0, 10.0)) + }; + for (width, across, down) in [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0), (4.0, 1.0, 1.0), (8.0, 1.0, 1.0), (16.0, 2.0, 1.0)] { + let laid = framed(width); + assert_eq!(rect(&laid, "framed"), (0.0, 0.0, 20.0, 4.0)); + let one = rect(&laid, "one"); + assert_eq!((one.0, one.1), (across, down), "a {width} px border: content starts at ({across}, {down})"); + } +}