feat: implement nested border-box layout and alignment

This commit is contained in:
2026-11-10 22:17:19 +08:00
parent 525097a0b2
commit 32d7ada3ab
21 changed files with 1368 additions and 74 deletions
+421
View File
@@ -3993,6 +3993,427 @@ fn main_alignment_updates_live_geometry_without_changing_sizes() {
}
}
#[test]
fn cross_alignment_is_live_and_specific_to_each_parent_occurrence() {
let (mut session, initial) = ReplSession::load(r#"
class Tall: Row {
function measure(self: Tall, mount: Mount, constraints: Constraints): MeasureResult { return MeasureResult(200, 100) }
}
class Wide: Column {
function measure(self: Wide, mount: Mount, constraints: Constraints): MeasureResult { return MeasureResult(200, 100) }
}
shared = RectBlock(20, 20)
row = Tall(10, 0, shared)
column = Wide(10, 0, shared)
row.crossAlign = "center"
column.crossAlign = "end"
page = Page()
page.render(Column(0, 0, row, column))
"#, "app.main", 294, 1).unwrap();
initial.validate().unwrap();
for (expression, expected) in [
("shared.mounts().at(0).bounds().x", "10"),
("shared.mounts().at(0).bounds().y", "40"),
("shared.mounts().at(1).bounds().x", "170"),
("shared.mounts().at(1).bounds().y", "110"),
] {
assert_eq!(
session.execute(expression, "app.main").unwrap().value,
expected
);
}
session
.execute("shared.alignSelf = \"start\"", "app.main")
.unwrap();
assert_eq!(
session
.execute("shared.mounts().at(0).bounds().y", "app.main")
.unwrap()
.value,
"10"
);
assert_eq!(
session
.execute("shared.mounts().at(1).bounds().x", "app.main")
.unwrap()
.value,
"10"
);
session
.execute(
"shared.alignSelf = \"auto\"\nrow.crossAlign = \"end\"",
"app.main",
)
.unwrap();
assert_eq!(
session
.execute("shared.mounts().at(0).bounds().y", "app.main")
.unwrap()
.value,
"70"
);
assert_eq!(
session
.execute("shared.mounts().at(1).bounds().x", "app.main")
.unwrap()
.value,
"170"
);
}
#[test]
fn cross_alignment_preserves_source_placements_and_starts_overflow() {
let (mut session, initial) = ReplSession::load(r#"
class Placed: Row {
function arrange(self: Placed, mount: Mount, geometry: Bounds): Arrangement {
result = Row.arrange(self, mount, geometry)
result.children.at(0).y = 7
return result
}
}
class Small: Column {
function measure(self: Small, mount: Mount, constraints: Constraints): MeasureResult { return MeasureResult(10, 30) }
}
first = RectBlock(20, 20)
second = RectBlock(40, 20)
placed = Placed(0, 0, first)
placed.crossAlign = "end"
small = Small(0, 0, second)
small.crossAlign = "center"
page = Page()
page.render(Column(0, 0, placed, small))
"#, "app.main", 295, 1).unwrap();
initial.validate().unwrap();
assert_eq!(
session
.execute("first.mounts().at(0).bounds().y", "app.main")
.unwrap()
.value,
"7"
);
assert_eq!(
session
.execute("second.mounts().at(0).bounds().x", "app.main")
.unwrap()
.value,
"0"
);
}
#[test]
fn margins_participate_in_intrinsic_size_alignment_and_flex_allocation() {
let (mut session, initial) = ReplSession::load(r#"
class Wide: Row {
function measure(self: Wide, mount: Mount, constraints: Constraints): MeasureResult { return MeasureResult(200, 100) }
}
first = RectBlock(20, 20)
first.marginLeft = 3
first.marginRight = 7
first.marginTop = 5
first.marginBottom = 15
second = RectBlock(20, 20)
second.flex = 1
row = Wide(10, 10, first, second)
row.crossAlign = "center"
intrinsic = Row(10, 10, first, RectBlock(30, 30))
page = Page()
page.render(Column(0, 0, row, intrinsic))
"#, "app.main", 296, 1).unwrap();
initial.validate().unwrap();
for (expression, expected) in [
("first.mounts().at(0).bounds().x", "13"),
("first.mounts().at(0).bounds().y", "35"),
("second.mounts().at(0).bounds().x", "50"),
("second.mounts().at(0).bounds().width", "140"),
("intrinsic.mounts().at(0).bounds().width", "90"),
("intrinsic.mounts().at(0).bounds().height", "60"),
] {
assert_eq!(
session.execute(expression, "app.main").unwrap().value,
expected,
"{expression}"
);
}
session
.execute("first.marginRight = 17", "app.main")
.unwrap();
assert_eq!(
session
.execute("second.mounts().at(0).bounds().width", "app.main")
.unwrap()
.value,
"130"
);
assert_eq!(
session
.execute("intrinsic.mounts().at(0).bounds().width", "app.main")
.unwrap()
.value,
"100"
);
}
#[test]
fn nested_overlay_uses_border_boxes_and_asymmetric_content_insets() {
let (mut session, initial) = ReplSession::load(
r##"
child = RectBlock(20, 10)
child.marginLeft = 5
child.marginTop = 4
column = Column(0, 0, child)
column.boxWidth = 200
column.boxHeight = 120
column.paddingLeft = 11
column.paddingTop = 7
column.borderLeft = 3
column.borderTop = 2
column.borderColor = "#FF0000"
overlay = Overlay(10, 0, column)
overlay.boxWidth = 300
overlay.boxHeight = 200
overlay.mainAlign = "center"
overlay.crossAlign = "end"
page = Page()
page.render(overlay)
"##,
"app.main",
297,
1,
)
.unwrap();
initial.validate().unwrap();
for (expression, expected) in [
("overlay.mounts().at(0).bounds().width", "300"),
("column.mounts().at(0).bounds().x", "50"),
("column.mounts().at(0).bounds().y", "70"),
("column.mounts().at(0).bounds().width", "200"),
("child.mounts().at(0).bounds().x", "69"),
("child.mounts().at(0).bounds().y", "83"),
] {
assert_eq!(
session.execute(expression, "app.main").unwrap().value,
expected,
"{expression}"
);
}
assert!(initial.frame.commands.iter().any(|command| matches!(command, RenderCommand::RoundedRect { x, y, width, height, color, .. } if *x == 50.0 && *y == 70.0 && *width == 3.0 && *height == 120.0 && color == "#FF0000")));
session
.execute("column.maxWidth = 160\ncolumn.minHeight = 140", "app.main")
.unwrap();
assert_eq!(
session
.execute("column.mounts().at(0).bounds().width", "app.main")
.unwrap()
.value,
"160"
);
assert_eq!(
session
.execute("column.mounts().at(0).bounds().height", "app.main")
.unwrap()
.value,
"140"
);
}
#[test]
fn fill_wraps_text_inside_finite_column_and_rejects_unbounded_axis() {
let (mut session, initial) = ReplSession::load(r#"
text = TextBlock("Text wrapping follows the finite border box rather than the original unbounded column width.")
text.boxWidth = "fill"
text.paddingLeft = 8
text.paddingRight = 12
column = Column(0, 0, text)
column.boxWidth = 160
page = Page()
page.render(column)
"#, "app.main", 298, 1).unwrap();
initial.validate().unwrap();
assert_eq!(
session
.execute("text.mounts().at(0).bounds().width", "app.main")
.unwrap()
.value,
"160"
);
assert!(initial.frame.commands.iter().any(|command| matches!(command, RenderCommand::Text { x, wrap_bounds: Some([width, _]), .. } if *x == 8.0 && *width == 140.0)));
let error = ReplSession::load(
r#"
rect = RectBlock(10, 10)
rect.boxHeight = "fill"
page = Page()
page.render(Column(0, 0, rect))
"#,
"app.main",
299,
1,
)
.err()
.expect("unbounded fill must fail");
assert!(
format!("{error:#}").contains("fill requires a finite parent allocation"),
"{error:#}"
);
}
#[test]
fn clipping_a_box_limits_descendant_paint_and_hit_geometry() {
let (_, initial) = ReplSession::load(r#"
class Overflow: RectBlock {
function measure(self: Overflow, mount: Mount, constraints: Constraints): MeasureResult { return MeasureResult(100, 20) }
}
child = Button("outside")
child.boxWidth = 100
row = Row(0, 0, Overflow(100, 20), child)
row.boxWidth = 60
row.boxHeight = 40
row.borderLeft = 2
row.overflow = "clip"
page = Page()
page.render(row)
"#, "app.main", 300, 1).unwrap();
initial.validate().unwrap();
let child = initial
.frame
.mounts
.iter()
.find(|mount| mount.interactive)
.unwrap();
assert_eq!(child.clip, Some([2.0, 0.0, 58.0, 40.0]));
assert!(child.x >= 100.0);
assert!(
initial
.frame
.hit_test(crate::geometry::Point::new(child.x + 1.0, child.y + 1.0))
.is_none()
);
}
#[test]
fn main_axis_fill_shares_remaining_space_and_stretch_respects_box_limits() {
let (mut session, initial) = ReplSession::load(
r#"
first = RectBlock(10, 10)
first.boxWidth = "fill"
first.marginLeft = 5
first.maxHeight = 30
second = RectBlock(10, 10)
second.boxWidth = "fill"
second.maxWidth = 50
fixed = RectBlock(40, 20)
fixed.alignSelf = "center"
row = Row(10, 5, first, second, fixed)
row.boxWidth = 240
row.boxHeight = 80
row.crossAlign = "stretch"
page = Page()
page.render(row)
"#,
"app.main",
301,
1,
)
.unwrap();
initial.validate().unwrap();
for (expression, expected) in [
("first.mounts().at(0).bounds().width", "115"),
("second.mounts().at(0).bounds().width", "50"),
("first.mounts().at(0).bounds().height", "30"),
("second.mounts().at(0).bounds().height", "60"),
("fixed.mounts().at(0).bounds().y", "30"),
] {
assert_eq!(
session.execute(expression, "app.main").unwrap().value,
expected,
"{expression}"
);
}
session.execute("row.boxWidth = 280", "app.main").unwrap();
assert_eq!(
session
.execute("first.mounts().at(0).bounds().width", "app.main")
.unwrap()
.value,
"155"
);
}
#[test]
fn box_layout_example_runs_through_the_real_source_pipeline() {
let (mut session, initial) = ReplSession::load(
include_str!("../../../examples/box-layout.endor"),
"app.main",
302,
1,
)
.unwrap();
initial.validate().unwrap();
assert_eq!(
session
.execute("shared.mounts().count", "app.main")
.unwrap()
.value,
"2"
);
let left = session
.execute("shared.mounts().at(0).bounds().width", "app.main")
.unwrap()
.value
.parse::<f32>()
.unwrap();
let right = session
.execute("shared.mounts().at(1).bounds().width", "app.main")
.unwrap()
.value
.parse::<f32>()
.unwrap();
assert!(left > right);
}
#[test]
fn overlay_stretch_allocates_each_occurrence_without_reflowing_its_siblings() {
let (mut session, initial) = ReplSession::load(
r#"
child = RectBlock(20, 10)
child.marginTop = 3
child.maxHeight = 50
overlay = Overlay(5, 0, child, RectBlock(10, 10))
overlay.boxWidth = 100
overlay.boxHeight = 80
overlay.crossAlign = "stretch"
page = Page()
page.render(overlay)
"#,
"app.main",
303,
1,
)
.unwrap();
initial.validate().unwrap();
assert_eq!(
session
.execute("child.mounts().at(0).bounds().height", "app.main")
.unwrap()
.value,
"50"
);
assert_eq!(
session
.execute("child.mounts().at(0).bounds().y", "app.main")
.unwrap()
.value,
"8"
);
session.execute("child.boxHeight = 15", "app.main").unwrap();
assert_eq!(
session
.execute("child.mounts().at(0).bounds().height", "app.main")
.unwrap()
.value,
"15"
);
}
#[test]
fn scroll_axes_wrap_shared_text_and_clamp_only_the_enabled_offsets() {
let (mut session, initial) = ReplSession::load(