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
+4
View File
@@ -43,6 +43,7 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
("spacer", Operation::Spacer),
("row", Operation::Row),
("column", Operation::Column),
("overlay", Operation::Overlay),
("button", Operation::Button),
("page", Operation::Page),
("present", Operation::Present),
@@ -86,6 +87,7 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
("ScrollBlock", "ContainerBlock", Some("scroll")),
("Row", "ContainerBlock", Some("row")),
("Column", "ContainerBlock", Some("column")),
("Overlay", "ContainerBlock", Some("overlay")),
("Button", "ContainerBlock", Some("button")),
("Page", "ContainerBlock", Some("page")),
("Window", "Object", Some("window-create")),
@@ -130,6 +132,7 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
"scroll" => Operation::Scroll,
"row" => Operation::Row,
"column" => Operation::Column,
"overlay" => Operation::Overlay,
"button" => Operation::Button,
"page" => Operation::Page,
"window-create" => Operation::NewWindow,
@@ -232,6 +235,7 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
"scroll" => Some(Operation::Scroll),
"row" => Some(Operation::Row),
"column" => Some(Operation::Column),
"overlay" => Some(Operation::Overlay),
"button" => Some(Operation::Button),
"page" => Some(Operation::Page),
"window-create" => Some(Operation::NewWindow),
+3 -1
View File
@@ -346,9 +346,11 @@ impl NativeCallable for Function {
0.0,
0.0,
)),
Row | Column => Some(Data::new(
Row | Column | Overlay => Some(Data::new(
if matches!(self.operation, Row) {
Kind::Row
} else if matches!(self.operation, Overlay) {
Kind::Overlay
} else {
Kind::Column
},
+3 -1
View File
@@ -133,11 +133,13 @@ pub(super) fn operation_signature(
nominal("ScrollBlock")?,
CallEffect::ReadOnly,
),
Row | Column => (
Row | Column | Overlay => (
vec![Number, Number],
Some(nominal("Block")?),
nominal(if matches!(operation, Row) {
"Row"
} else if matches!(operation, Overlay) {
"Overlay"
} else {
"Column"
})?,
+36 -1
View File
@@ -7,6 +7,14 @@ pub struct UiNode {
pub flex: f32,
pub flex_policy: crate::layout::FlexPolicy,
pub main_align: crate::layout::MainAlign,
pub cross_align: crate::layout::CrossAlign,
pub align_self: Option<crate::layout::CrossAlign>,
pub margin: crate::layout::Insets,
pub sizing: crate::box_model::Sizing,
pub padding_edges: [Option<f32>; 4],
pub border: crate::layout::Insets,
pub border_color: String,
pub clip_children: bool,
pub block_id: String,
pub protocol_key: String,
pub slots: Vec<u64>,
@@ -18,6 +26,7 @@ impl UiNode {
pub fn children(&self) -> &[Arc<UiNode>] {
match &self.kind {
UiKind::Column { children, .. }
| UiKind::Overlay { children, .. }
| UiKind::Canvas { children, .. }
| UiKind::Row { children, .. }
| UiKind::Button { children, .. }
@@ -27,7 +36,18 @@ impl UiNode {
}
pub fn has_flex(&self) -> bool {
self.flex > 0.0 || self.flex_policy != crate::layout::FlexPolicy::default()
self.flex > 0.0
|| self.flex_policy != crate::layout::FlexPolicy::default()
|| self.sizing.width == crate::box_model::Dimension::Fill
|| self.sizing.height == crate::box_model::Dimension::Fill
}
pub fn allocates_children(&self) -> bool {
self.children().iter().any(|child| {
child.has_flex()
|| child.align_self.unwrap_or(self.cross_align)
== crate::layout::CrossAlign::Stretch
})
}
pub fn shallow_eq(&self, other: &Self) -> bool {
@@ -35,6 +55,14 @@ impl UiNode {
|| self.flex_policy != other.flex_policy
|| self.flex != other.flex
|| self.main_align != other.main_align
|| self.cross_align != other.cross_align
|| self.align_self != other.align_self
|| self.margin != other.margin
|| self.sizing != other.sizing
|| self.padding_edges != other.padding_edges
|| self.border != other.border
|| self.border_color != other.border_color
|| self.clip_children != other.clip_children
|| self.block_id != other.block_id
|| self.protocol_key != other.protocol_key
|| self.slots != other.slots
@@ -51,6 +79,9 @@ impl UiNode {
.all(|(a, b)| Arc::ptr_eq(a, b))
};
match (&self.kind, &other.kind) {
(UiKind::Overlay { padding: a, .. }, UiKind::Overlay { padding: b, .. }) => {
a == b && same_children()
}
(UiKind::Canvas { positions: a, .. }, UiKind::Canvas { positions: b, .. }) => {
a == b && same_children()
}
@@ -130,6 +161,10 @@ impl UiNode {
#[derive(Debug, Clone, PartialEq)]
pub enum UiKind {
Overlay {
padding: f32,
children: Vec<Arc<UiNode>>,
},
Canvas {
positions: Vec<[f32; 2]>,
children: Vec<Arc<UiNode>>,