diff --git a/README.md b/README.md index b976b8e..968356a 100644 --- a/README.md +++ b/README.md @@ -487,3 +487,5 @@ Native layout/paint fast paths and the default event classification inspect the Block.click returns the generated Event and Block.dispatch returns the delivered Event, preserving the event object from the dispatch pipeline. Native return signatures match these actual results, including GUI activation. Event dispatch/click and lifecycle entry points publish External as their allowed effect bound: handlers may mutate application state or use host I/O APIs. The built-in no-op bodies do not make the overridable handler contract ReadOnly. These entry points still reject invocation inside read-only rendering. + +ContainerBlock.mainAlign is a managed attribute with start (default), center, end and space-between policies. It positions children in the allocated main-axis space after padding and the configured minimum gap. Center/end use remaining positive space; space-between adds it between two or more children. Overflow keeps start positions and existing sizes; alignment does not imply flex sizing. Native arrange queries and rendering use the same actual Bounds. Source arrange overrides retain their explicit placements. diff --git a/src/README.md b/src/README.md index b2e7a0a..9a6c28e 100644 --- a/src/README.md +++ b/src/README.md @@ -137,3 +137,5 @@ Native TextBlock measurement and painting now share width-sensitive shaping. Col Layout convergence uses exact finite snapshots of constraints/sizes/bounds and source-selected limits. The same immutable driver loops within one shared VM budget, clears all provisional measurement geometry between passes and rejects nonconsecutive repeated states. Aster ReadAccess distinguishes annotation validation from normal reads; the bridge skips only validation-only Mount reads after independently validating the live Mount/owner context. Actual Mount reads remain volatile; all other recorded dependencies are retained. ScrollBlock.axis is stored in its managed properties and projected into the visual snapshot. Axis-aware child constraints use constructor/clamped size for initial measurement and actual occurrence bounds after arrangement. Scroll projection clamps disabled offsets/limits; the same geometry feeds paint and input. + +Container mainAlign projects from managed properties into UiNode and its shallow equality. Native arrangement receives the actual allocated Size, computes positive remaining main-axis space and keeps child sizes unchanged. diff --git a/src/api.rs b/src/api.rs index 7973565..a990cd9 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1117,6 +1117,8 @@ managed click/focus methods used by code. Events appear in Network, not Console ScrollBlock.axis is a managed visual attribute: "both" (default), "vertical" or "horizontal". The default preserves two-axis unconstrained content. Vertical restricts content width to the actual occurrence viewport minus padding; horizontal restricts height. Source-arranged viewport dimensions are used in subsequent measurement. The disabled scroll axis has zero offset/limit and is clamped after updates. Composition remains vertical; a Row child can arrange horizontal content. See examples/scroll-axis.endor. +ContainerBlock.mainAlign accepts "start" (default), "center", "end" or "space-between". Native arrangement uses allocated Bounds after padding/minimum gap, distributes positive remaining main-axis space and keeps child sizes unchanged. Overflow keeps start placement. Explicit source arrange placements remain authoritative; this is alignment, not flex sizing. + ## UI host object Positional arguments only: diff --git a/src/block.rs b/src/block.rs index 8fbf308..a1c629f 100644 --- a/src/block.rs +++ b/src/block.rs @@ -117,6 +117,7 @@ pub struct Data { pub children: Vec, pub padding: f32, pub gap: f32, + pub main_align: crate::layout::MainAlign, pub scroll_axis: crate::scroll::Axis, pub next_slot: u64, pub history: Vec, @@ -143,6 +144,7 @@ impl Data { .collect(), padding, gap, + main_align: crate::layout::MainAlign::Start, scroll_axis: crate::scroll::Axis::Both, next_slot, history: vec![], @@ -340,7 +342,11 @@ pub fn native_properties(data: &Data) -> Vec<(&'static str, Value)> { _ => {} } if data.is_container() { - fields.extend([("padding", number(data.padding)), ("gap", number(data.gap))]); + fields.extend([ + ("padding", number(data.padding)), + ("gap", number(data.gap)), + ("mainAlign", Value::Text(data.main_align.name().into())), + ]); } fields } @@ -370,6 +376,13 @@ pub fn apply_visual_property(data: &mut Data, name: &str, value: &Value) -> Resu Ok(n) }; let container = data.is_container(); + if container && name == "mainAlign" { + let Value::Text(value) = value else { + return Err(bad()); + }; + data.main_align = crate::layout::MainAlign::parse(value)?; + return Ok(()); + } match (&mut data.kind, name) { (Kind::Text { value: text, .. }, "text") => { let Value::Text(value) = value else { diff --git a/src/endor_host.rs b/src/endor_host.rs index a23ddfe..739db38 100644 --- a/src/endor_host.rs +++ b/src/endor_host.rs @@ -3889,6 +3889,52 @@ page.render(block) ); } + #[test] + fn main_alignment_updates_live_geometry_without_changing_sizes() { + let (mut session, initial) = ReplSession::load(r#" + class Wide: Row { + function measure(self: Wide, mount: Mount, constraints: Constraints): MeasureResult { return MeasureResult(300, 60) } + } + first = RectBlock(20, 20) + second = RectBlock(40, 40) + row = Wide(10, 10, first, second) + page = Page() + page.render(row) + "#, "app.main", 264, 1).unwrap(); + initial.validate().unwrap(); + for (mode, first, second) in [ + ("start", "10", "40"), + ("center", "115", "145"), + ("end", "220", "250"), + ("space-between", "10", "250"), + ] { + session + .execute(&format!("row.mainAlign = \"{mode}\""), "app.main") + .unwrap(); + assert_eq!( + session + .execute("first.mounts().at(0).bounds().x", "app.main") + .unwrap() + .value, + first + ); + assert_eq!( + session + .execute("second.mounts().at(0).bounds().x", "app.main") + .unwrap() + .value, + second + ); + assert_eq!( + session + .execute("first.mounts().at(0).bounds().width", "app.main") + .unwrap() + .value, + "20" + ); + } + } + #[test] fn scroll_axes_wrap_shared_text_and_clamp_only_the_enabled_offsets() { let (mut session, initial) = ReplSession::load( diff --git a/src/layout.rs b/src/layout.rs index 96b68f3..aac2702 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -10,6 +10,36 @@ use std::collections::{HashMap, HashSet}; const MAX_LAYOUT_VISITS: usize = 10_000; +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub enum MainAlign { + #[default] + Start, + Center, + End, + SpaceBetween, +} +impl MainAlign { + pub fn name(self) -> &'static str { + match self { + Self::Start => "start", + Self::Center => "center", + Self::End => "end", + Self::SpaceBetween => "space-between", + } + } + pub fn parse(value: &str) -> Result { + match value { + "start" => Ok(Self::Start), + "center" => Ok(Self::Center), + "end" => Ok(Self::End), + "space-between" => Ok(Self::SpaceBetween), + _ => Err(endor_core::VmError::InvalidArgument( + "mainAlign expects start, center, end or space-between", + )), + } + } +} + #[derive(Default, Debug, Clone, Copy, PartialEq)] pub struct Size { pub width: f32, @@ -230,11 +260,34 @@ pub fn paint_native(node: &UiNode, x: f32, y: f32, size: Size) -> Option Result> { +pub fn arrange_native( + node: &UiNode, + x: f32, + y: f32, + bounds: Size, + children: &[Size], +) -> Result> { let Some((row, padding, gap)) = container(node) else { return Ok(vec![]); }; - let mut cursor = padding; + let occupied = children + .iter() + .map(|child| if row { child.width } else { child.height }) + .sum::() + + gap * children.len().saturating_sub(1) as f32; + let available = if row { bounds.width } else { bounds.height }; + let spare = (available - padding * 2.0 - occupied).max(0.0); + let offset = match node.main_align { + MainAlign::Center => spare / 2.0, + MainAlign::End => spare, + _ => 0.0, + }; + let distributed_gap = if node.main_align == MainAlign::SpaceBetween && children.len() > 1 { + spare / (children.len() - 1) as f32 + } else { + 0.0 + }; + let mut cursor = padding + offset; let mut points = Vec::with_capacity(children.len()); for child in children { let point = if row { @@ -248,7 +301,7 @@ pub fn arrange_native(node: &UiNode, x: f32, y: f32, children: &[Size]) -> Resul bounded(point.1 + child.height)?; points.push(point); cursor += if row { child.width } else { child.height }; - cursor += gap; + cursor += gap + distributed_gap; } Ok(points) } @@ -410,7 +463,7 @@ impl LayoutRuntime { for (child, point) in item.children .iter() - .zip(arrange_native(item.node, x, y, &child_sizes)?) + .zip(arrange_native(item.node, x, y, size, &child_sizes)?) { points[*child] = point; } diff --git a/src/native_visual.rs b/src/native_visual.rs index 33d4ece..46b6a14 100644 --- a/src/native_visual.rs +++ b/src/native_visual.rs @@ -222,7 +222,7 @@ impl NativeCallable for Method { }) .collect::, _>>()? }; - let points = layout::arrange_native(&node, x, y, &children) + let points = layout::arrange_native(&node, x, y, Size { width, height }, &children) .map_err(|_| invalid("native arrangement exceeds limits"))?; let mut result = Vec::new(); for ((slot, size), (x, y)) in node.slots.iter().zip(children).zip(points) { diff --git a/src/presentation.rs b/src/presentation.rs index 805ce87..c6d09ea 100644 --- a/src/presentation.rs +++ b/src/presentation.rs @@ -117,6 +117,7 @@ mod tests { height: 160.0, }); let root = UiNode { + main_align: crate::layout::MainAlign::Start, block_id: "shared".into(), protocol_key: "rect".into(), slots: Vec::new(), diff --git a/src/snapshot.rs b/src/snapshot.rs index 50b99c1..ca348a1 100644 --- a/src/snapshot.rs +++ b/src/snapshot.rs @@ -273,6 +273,7 @@ impl RenderCache { }, }; let next = UiNode { + main_align: data.main_align, block_id: block.to_string(), protocol_key, slots: data.children.iter().map(|c| c.slot).collect(), diff --git a/src/ui_bridge.rs b/src/ui_bridge.rs index f862894..1aad8d3 100644 --- a/src/ui_bridge.rs +++ b/src/ui_bridge.rs @@ -60,6 +60,7 @@ pub const QUERY_NAMES: &[&str] = &[ #[derive(Debug, Clone, PartialEq)] pub struct UiNode { + pub main_align: crate::layout::MainAlign, pub block_id: String, pub protocol_key: String, pub slots: Vec, @@ -79,7 +80,8 @@ impl UiNode { } pub fn shallow_eq(&self, other: &Self) -> bool { - if self.block_id != other.block_id + if self.main_align != other.main_align + || self.block_id != other.block_id || self.protocol_key != other.protocol_key || self.slots != other.slots || self.interactive != other.interactive