thing-core: a nonzero border side is at least one cell in Cells

Ruling cells-box-border-min-cell. Device::border is the one conversion of a border side:
in Cells a nonzero Px width is at least one cell on its axis, zero stays zero, Pixels is
unchanged. A 20x4 Box with a 1 px border held its content on the stroke's perimeter
cells; the content now starts one cell in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-28 15:43:08 +01:00
commit 06264314ed
3 changed files with 28 additions and 3 deletions

View file

@ -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

View file

@ -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))
}
};

View file

@ -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})");
}
}