feat: implement nested border-box layout and alignment
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
//! Native border-box sizing. An unbounded axis cannot resolve `fill`.
|
||||
use crate::{layout::Size, ui_bridge::MAX_DIMENSION};
|
||||
use anyhow::Result;
|
||||
use endor_core::{Value, VmError};
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq)]
|
||||
pub enum Dimension {
|
||||
#[default]
|
||||
Auto,
|
||||
Fixed(f32),
|
||||
Fill,
|
||||
}
|
||||
|
||||
impl Dimension {
|
||||
pub fn value(self) -> Value {
|
||||
match self {
|
||||
Self::Auto => Value::Text("auto".into()),
|
||||
Self::Fill => Value::Text("fill".into()),
|
||||
Self::Fixed(value) => Value::Number((value as f64).into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(value: &Value) -> Result<Self, VmError> {
|
||||
match value {
|
||||
Value::Text(text) if text == "auto" => Ok(Self::Auto),
|
||||
Value::Text(text) if text == "fill" => Ok(Self::Fill),
|
||||
Value::Number(_) => Ok(Self::Fixed(crate::scroll::coordinate(value).and_then(
|
||||
|n| {
|
||||
if n >= 0.0 {
|
||||
Ok(n)
|
||||
} else {
|
||||
Err(VmError::InvalidArgument(
|
||||
"box dimensions must be nonnegative",
|
||||
))
|
||||
}
|
||||
},
|
||||
)?)),
|
||||
_ => Err(VmError::InvalidArgument(
|
||||
"box dimension expects auto, fill or a nonnegative number",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct Sizing {
|
||||
pub width: Dimension,
|
||||
pub height: Dimension,
|
||||
pub min_width: f32,
|
||||
pub min_height: f32,
|
||||
pub max_width: f32,
|
||||
pub max_height: f32,
|
||||
}
|
||||
|
||||
impl Default for Sizing {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
width: Dimension::Auto,
|
||||
height: Dimension::Auto,
|
||||
min_width: 0.0,
|
||||
min_height: 0.0,
|
||||
max_width: MAX_DIMENSION as f32,
|
||||
max_height: MAX_DIMENSION as f32,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Sizing {
|
||||
/// Constrain descendants using the actual finite box before wrapping text.
|
||||
pub fn available(self, mut constraints: [f32; 4]) -> [f32; 4] {
|
||||
constraints[2] = constraints[2].min(self.max_width);
|
||||
constraints[3] = constraints[3].min(self.max_height);
|
||||
if let Dimension::Fixed(width) = self.width {
|
||||
constraints[2] = constraints[2].min(width.max(self.min_width));
|
||||
}
|
||||
if let Dimension::Fixed(height) = self.height {
|
||||
constraints[3] = constraints[3].min(height.max(self.min_height));
|
||||
}
|
||||
constraints[0] = constraints[0].min(constraints[2]);
|
||||
constraints[1] = constraints[1].min(constraints[3]);
|
||||
constraints
|
||||
}
|
||||
|
||||
pub fn resolve(self, intrinsic: Size, constraints: [f32; 4]) -> Result<Size> {
|
||||
fn axis(
|
||||
policy: Dimension,
|
||||
intrinsic: f32,
|
||||
min: f32,
|
||||
max: f32,
|
||||
available: f32,
|
||||
) -> Result<f32> {
|
||||
let value = match policy {
|
||||
Dimension::Auto => intrinsic,
|
||||
Dimension::Fixed(value) => value,
|
||||
Dimension::Fill => {
|
||||
if available >= MAX_DIMENSION as f32 {
|
||||
return Err(VmError::InvalidArgument(
|
||||
"fill requires a finite parent allocation on this axis",
|
||||
)
|
||||
.into());
|
||||
}
|
||||
available
|
||||
}
|
||||
};
|
||||
Ok(value.clamp(min, max).min(available))
|
||||
}
|
||||
Ok(Size {
|
||||
width: axis(
|
||||
self.width,
|
||||
intrinsic.width,
|
||||
self.min_width,
|
||||
self.max_width,
|
||||
constraints[2],
|
||||
)?,
|
||||
height: axis(
|
||||
self.height,
|
||||
intrinsic.height,
|
||||
self.min_height,
|
||||
self.max_height,
|
||||
constraints[3],
|
||||
)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
+430
-38
@@ -10,6 +10,72 @@ use std::collections::{HashMap, HashSet};
|
||||
|
||||
const MAX_LAYOUT_VISITS: usize = 10_000;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq)]
|
||||
pub struct Insets {
|
||||
pub top: f32,
|
||||
pub right: f32,
|
||||
pub bottom: f32,
|
||||
pub left: f32,
|
||||
}
|
||||
|
||||
impl Insets {
|
||||
pub fn horizontal(self) -> f32 {
|
||||
self.left + self.right
|
||||
}
|
||||
pub fn vertical(self) -> f32 {
|
||||
self.top + self.bottom
|
||||
}
|
||||
fn outer(self, size: Size) -> Size {
|
||||
Size {
|
||||
width: size.width + self.horizontal(),
|
||||
height: size.height + self.vertical(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Alignment inside a finite cross-axis content box. Overflow remains start
|
||||
/// aligned so that content cannot become inaccessible beyond the leading edge.
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
||||
pub enum CrossAlign {
|
||||
#[default]
|
||||
Start,
|
||||
Center,
|
||||
End,
|
||||
Stretch,
|
||||
}
|
||||
|
||||
impl CrossAlign {
|
||||
pub fn name(self) -> &'static str {
|
||||
match self {
|
||||
Self::Start => "start",
|
||||
Self::Center => "center",
|
||||
Self::End => "end",
|
||||
Self::Stretch => "stretch",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(value: &str) -> Result<Self, endor_core::VmError> {
|
||||
match value {
|
||||
"start" => Ok(Self::Start),
|
||||
"center" => Ok(Self::Center),
|
||||
"end" => Ok(Self::End),
|
||||
"stretch" => Ok(Self::Stretch),
|
||||
_ => Err(endor_core::VmError::InvalidArgument(
|
||||
"crossAlign/alignSelf expects start, center, end or stretch (alignSelf also accepts auto)",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn offset(self, available: f32, occupied: f32) -> f32 {
|
||||
let spare = (available - occupied).max(0.0);
|
||||
match self {
|
||||
Self::Start | Self::Stretch => 0.0,
|
||||
Self::Center => spare / 2.0,
|
||||
Self::End => spare,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
||||
pub enum MainAlign {
|
||||
#[default]
|
||||
@@ -67,9 +133,11 @@ pub struct Size {
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
enum Shape {
|
||||
Sized(Box<Shape>, crate::box_model::Sizing, [f32; 4]),
|
||||
Text(String, u32, f32),
|
||||
Rect(f32, f32),
|
||||
Container(bool, f32, f32, Vec<Size>),
|
||||
Container(bool, Insets, f32, Vec<Size>),
|
||||
Padded(Box<Shape>, Insets),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
|
||||
@@ -122,7 +190,25 @@ pub fn root_constraints(viewport: Option<Size>) -> [f32; 4] {
|
||||
});
|
||||
[0.0, 0.0, size.width, size.height]
|
||||
}
|
||||
|
||||
fn content_limit(available: f32, inset: f32) -> f32 {
|
||||
if available >= MAX_DIMENSION as f32 {
|
||||
available
|
||||
} else {
|
||||
(available - inset).max(0.0)
|
||||
}
|
||||
}
|
||||
pub fn child_constraints(node: &UiNode, parent: [f32; 4]) -> [f32; 4] {
|
||||
let parent = node.sizing.available(parent);
|
||||
if let UiKind::Overlay { .. } = node.kind {
|
||||
let padding = insets(node);
|
||||
return [
|
||||
0.0,
|
||||
0.0,
|
||||
content_limit(parent[2], padding.horizontal()),
|
||||
content_limit(parent[3], padding.vertical()),
|
||||
];
|
||||
}
|
||||
if matches!(node.kind, UiKind::Canvas { .. }) {
|
||||
return root_constraints(None);
|
||||
}
|
||||
@@ -138,33 +224,88 @@ pub fn child_constraints(node: &UiNode, parent: [f32; 4]) -> [f32; 4] {
|
||||
0.0,
|
||||
0.0,
|
||||
MAX_DIMENSION as f32,
|
||||
(parent[3] - padding * 2.0).max(0.0),
|
||||
content_limit(parent[3], padding.vertical()),
|
||||
],
|
||||
Some((false, padding, _)) => [
|
||||
0.0,
|
||||
0.0,
|
||||
(parent[2] - padding * 2.0).max(0.0),
|
||||
content_limit(parent[2], padding.horizontal()),
|
||||
MAX_DIMENSION as f32,
|
||||
],
|
||||
None => parent,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn margin_constraints(child: &UiNode, mut constraints: [f32; 4]) -> [f32; 4] {
|
||||
for (axis, margin) in [child.margin.horizontal(), child.margin.vertical()]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
if constraints[axis + 2] < MAX_DIMENSION as f32 {
|
||||
constraints[axis + 2] = (constraints[axis + 2] - margin).max(0.0);
|
||||
constraints[axis] = constraints[axis].min(constraints[axis + 2]);
|
||||
}
|
||||
}
|
||||
constraints
|
||||
}
|
||||
|
||||
/// Main-axis fill first measures against a finite offer, then participates in
|
||||
/// weighted allocation. The unlimited-axis sentinel is never made finite by padding.
|
||||
pub fn occurrence_constraints(
|
||||
parent: &UiNode,
|
||||
child: &UiNode,
|
||||
mut base: [f32; 4],
|
||||
offer: Size,
|
||||
) -> [f32; 4] {
|
||||
let inset = insets(parent);
|
||||
for (axis, dimension, available, padding) in [
|
||||
(0, child.sizing.width, offer.width, inset.horizontal()),
|
||||
(1, child.sizing.height, offer.height, inset.vertical()),
|
||||
] {
|
||||
if dimension == crate::box_model::Dimension::Fill
|
||||
&& base[axis + 2] >= MAX_DIMENSION as f32
|
||||
&& available < MAX_DIMENSION as f32
|
||||
{
|
||||
base[axis + 2] = (available - padding).max(0.0);
|
||||
base[axis] = base[axis].min(base[axis + 2]);
|
||||
}
|
||||
}
|
||||
margin_constraints(child, base)
|
||||
}
|
||||
|
||||
pub fn initial_child_constraints(
|
||||
parent: &UiNode,
|
||||
child: &UiNode,
|
||||
constraints: [f32; 4],
|
||||
) -> [f32; 4] {
|
||||
let offer = parent.sizing.available(constraints);
|
||||
occurrence_constraints(
|
||||
parent,
|
||||
child,
|
||||
child_constraints(parent, constraints),
|
||||
Size {
|
||||
width: offer[2],
|
||||
height: offer[3],
|
||||
},
|
||||
)
|
||||
}
|
||||
/// Scroll cross-axis constraints come from this occurrence's actual viewport,
|
||||
/// including a source parent's allocation rather than only constructor size.
|
||||
pub fn arranged_child_constraints(node: &UiNode, available: [f32; 4], bounds: Size) -> [f32; 4] {
|
||||
if let UiKind::Scroll { padding, axis, .. } = node.kind {
|
||||
if let UiKind::Scroll { axis, .. } = node.kind {
|
||||
let padding = insets(node);
|
||||
return [
|
||||
0.0,
|
||||
0.0,
|
||||
if axis.horizontal() {
|
||||
MAX_DIMENSION as f32
|
||||
} else {
|
||||
(bounds.width - padding * 2.0).max(0.0)
|
||||
(bounds.width - padding.horizontal()).max(0.0)
|
||||
},
|
||||
if axis.vertical() {
|
||||
MAX_DIMENSION as f32
|
||||
} else {
|
||||
(bounds.height - padding * 2.0).max(0.0)
|
||||
(bounds.height - padding.vertical()).max(0.0)
|
||||
},
|
||||
];
|
||||
}
|
||||
@@ -178,12 +319,30 @@ pub fn constrained(size: Size, constraints: [f32; 4]) -> Size {
|
||||
}
|
||||
}
|
||||
|
||||
fn container(node: &UiNode) -> Option<(bool, f32, f32)> {
|
||||
/// Effective content inset, including borders, in border-box coordinates.
|
||||
pub fn insets(node: &UiNode) -> Insets {
|
||||
let fallback = match node.kind {
|
||||
UiKind::Row { padding, .. }
|
||||
| UiKind::Column { padding, .. }
|
||||
| UiKind::Button { padding, .. }
|
||||
| UiKind::Scroll { padding, .. }
|
||||
| UiKind::Overlay { padding, .. } => padding,
|
||||
_ => 0.0,
|
||||
};
|
||||
Insets {
|
||||
top: node.padding_edges[0].unwrap_or(fallback) + node.border.top,
|
||||
right: node.padding_edges[1].unwrap_or(fallback) + node.border.right,
|
||||
bottom: node.padding_edges[2].unwrap_or(fallback) + node.border.bottom,
|
||||
left: node.padding_edges[3].unwrap_or(fallback) + node.border.left,
|
||||
}
|
||||
}
|
||||
|
||||
fn container(node: &UiNode) -> Option<(bool, Insets, f32)> {
|
||||
match &node.kind {
|
||||
UiKind::Row { padding, gap, .. } => Some((true, *padding, *gap)),
|
||||
UiKind::Scroll { padding, gap, .. } => Some((false, *padding, *gap)),
|
||||
UiKind::Column { padding, gap, .. } | UiKind::Button { padding, gap, .. } => {
|
||||
Some((false, *padding, *gap))
|
||||
UiKind::Row { gap, .. } => Some((true, insets(node), *gap)),
|
||||
UiKind::Scroll { gap, .. } => Some((false, insets(node), *gap)),
|
||||
UiKind::Column { gap, .. } | UiKind::Button { gap, .. } => {
|
||||
Some((false, insets(node), *gap))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
@@ -195,6 +354,10 @@ pub fn main_axis(node: &UiNode) -> Option<usize> {
|
||||
|
||||
fn measure_shape(shape: &Shape) -> Result<Size> {
|
||||
let size = match shape {
|
||||
Shape::Sized(shape, sizing, constraints) => {
|
||||
sizing.resolve(measure_shape(shape)?, *constraints)?
|
||||
}
|
||||
Shape::Padded(shape, padding) => padding.outer(measure_shape(shape)?),
|
||||
Shape::Text(value, size, width) => {
|
||||
let style = TextStyle::ui(*size);
|
||||
let intrinsic = text::measure(value, style);
|
||||
@@ -213,13 +376,21 @@ fn measure_shape(shape: &Shape) -> Result<Size> {
|
||||
.iter()
|
||||
.map(|s| if *row { s.width } else { s.height })
|
||||
.sum::<f32>()
|
||||
+ padding * 2.0
|
||||
+ if *row {
|
||||
padding.horizontal()
|
||||
} else {
|
||||
padding.vertical()
|
||||
}
|
||||
+ gap * children.len().saturating_sub(1) as f32;
|
||||
let cross = children
|
||||
.iter()
|
||||
.map(|s| if *row { s.height } else { s.width })
|
||||
.fold(0.0_f32, f32::max)
|
||||
+ padding * 2.0;
|
||||
+ if *row {
|
||||
padding.vertical()
|
||||
} else {
|
||||
padding.horizontal()
|
||||
};
|
||||
if *row {
|
||||
Size {
|
||||
width: along,
|
||||
@@ -239,7 +410,25 @@ fn measure_shape(shape: &Shape) -> Result<Size> {
|
||||
}
|
||||
|
||||
fn shape(node: &UiNode, children: Vec<Size>, constraints: [f32; 4]) -> Result<Shape> {
|
||||
Ok(match &node.kind {
|
||||
let available = node.sizing.available(constraints);
|
||||
let shape = match &node.kind {
|
||||
UiKind::Overlay { .. } => {
|
||||
let padding = insets(node);
|
||||
let size = children.into_iter().zip(node.children()).fold(
|
||||
Size::default(),
|
||||
|size, (child, node)| {
|
||||
let child = node.margin.outer(child);
|
||||
Size {
|
||||
width: size.width.max(child.width),
|
||||
height: size.height.max(child.height),
|
||||
}
|
||||
},
|
||||
);
|
||||
Shape::Rect(
|
||||
size.width + padding.horizontal(),
|
||||
size.height + padding.vertical(),
|
||||
)
|
||||
}
|
||||
UiKind::Canvas { positions, .. } => {
|
||||
let mut size = Size::default();
|
||||
for (position, child) in positions.iter().zip(children) {
|
||||
@@ -248,14 +437,34 @@ fn shape(node: &UiNode, children: Vec<Size>, constraints: [f32; 4]) -> Result<Sh
|
||||
}
|
||||
Shape::Rect(size.width, size.height)
|
||||
}
|
||||
UiKind::Text { value, size, .. } => Shape::Text(value.clone(), *size, constraints[2]),
|
||||
UiKind::Text { value, size, .. } => {
|
||||
let padding = insets(node);
|
||||
Shape::Padded(
|
||||
Box::new(Shape::Text(
|
||||
value.clone(),
|
||||
*size,
|
||||
(available[2] - padding.horizontal()).max(0.0),
|
||||
)),
|
||||
padding,
|
||||
)
|
||||
}
|
||||
UiKind::Rect { width, height, .. } => Shape::Rect(*width, *height),
|
||||
UiKind::Spacer(size) => Shape::Rect(*size, *size),
|
||||
UiKind::Scroll { width, height, .. } => Shape::Rect(*width, *height),
|
||||
_ => {
|
||||
let (row, padding, gap) = container(node).context("unknown native visual protocol")?;
|
||||
let children = children
|
||||
.into_iter()
|
||||
.zip(node.children())
|
||||
.map(|(size, child)| child.margin.outer(size))
|
||||
.collect();
|
||||
Shape::Container(row, padding, gap, children)
|
||||
}
|
||||
};
|
||||
Ok(if node.sizing == crate::box_model::Sizing::default() {
|
||||
shape
|
||||
} else {
|
||||
Shape::Sized(Box::new(shape), node.sizing, constraints)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -275,14 +484,18 @@ pub struct Context {
|
||||
|
||||
pub fn paint_native(node: &UiNode, x: f32, y: f32, size: Size) -> Option<RenderCommand> {
|
||||
let size_for_text = size;
|
||||
let inset = insets(node);
|
||||
match &node.kind {
|
||||
UiKind::Text { value, size, color } => Some(RenderCommand::Text {
|
||||
x,
|
||||
y,
|
||||
x: x + inset.left,
|
||||
y: y + inset.top,
|
||||
value: value.clone(),
|
||||
size: *size,
|
||||
color: color.clone(),
|
||||
wrap_bounds: Some([size_for_text.width, size_for_text.height]),
|
||||
wrap_bounds: Some([
|
||||
(size_for_text.width - inset.horizontal()).max(0.0),
|
||||
(size_for_text.height - inset.vertical()).max(0.0),
|
||||
]),
|
||||
}),
|
||||
UiKind::Rect { radius, color, .. } => Some(RenderCommand::RoundedRect {
|
||||
x,
|
||||
@@ -304,19 +517,126 @@ pub fn paint_native(node: &UiNode, x: f32, y: f32, size: Size) -> Option<RenderC
|
||||
}
|
||||
}
|
||||
|
||||
pub fn paint_commands(node: &UiNode, x: f32, y: f32, size: Size) -> Vec<RenderCommand> {
|
||||
let mut commands: Vec<_> = paint_native(node, x, y, size).into_iter().collect();
|
||||
let border = node.border;
|
||||
for [x, y, width, height] in [
|
||||
[x, y, size.width, border.top.min(size.height)],
|
||||
[
|
||||
x,
|
||||
y + (size.height - border.bottom).max(0.0),
|
||||
size.width,
|
||||
border.bottom.min(size.height),
|
||||
],
|
||||
[x, y, border.left.min(size.width), size.height],
|
||||
[
|
||||
x + (size.width - border.right).max(0.0),
|
||||
y,
|
||||
border.right.min(size.width),
|
||||
size.height,
|
||||
],
|
||||
] {
|
||||
if width > 0.0 && height > 0.0 {
|
||||
commands.push(RenderCommand::RoundedRect {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
radius: 0.0,
|
||||
color: node.border_color.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
commands
|
||||
}
|
||||
|
||||
/// Allocate main-axis space with bounded growth and basis-weighted shrinking.
|
||||
/// Preferred sizes come from this occurrence's first measurement in the solve;
|
||||
/// remeasurement must not turn a prior allocation into a new intrinsic basis.
|
||||
fn main_fill(child: &UiNode, row: bool) -> bool {
|
||||
(if row {
|
||||
child.sizing.width
|
||||
} else {
|
||||
child.sizing.height
|
||||
}) == crate::box_model::Dimension::Fill
|
||||
}
|
||||
|
||||
fn main_limits(child: &UiNode, row: bool) -> (f32, f32) {
|
||||
let sizing = child.sizing;
|
||||
let (min, max) = if row {
|
||||
(sizing.min_width, sizing.max_width)
|
||||
} else {
|
||||
(sizing.min_height, sizing.max_height)
|
||||
};
|
||||
(
|
||||
child.flex_policy.min.clamp(min, max),
|
||||
child.flex_policy.max.clamp(min, max),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size], preferred: &[Size]) -> Vec<Size> {
|
||||
if matches!(node.kind, UiKind::Overlay { .. }) {
|
||||
let inset = insets(node);
|
||||
return children
|
||||
.iter()
|
||||
.zip(node.children())
|
||||
.map(|(size, child)| {
|
||||
let mut size = *size;
|
||||
if child.sizing.width == crate::box_model::Dimension::Fill {
|
||||
size.width = (bounds.width - inset.horizontal() - child.margin.horizontal())
|
||||
.max(0.0)
|
||||
.clamp(child.sizing.min_width, child.sizing.max_width);
|
||||
}
|
||||
if child.sizing.height == crate::box_model::Dimension::Fill
|
||||
|| (child.sizing.height == crate::box_model::Dimension::Auto
|
||||
&& child.align_self.unwrap_or(node.cross_align) == CrossAlign::Stretch)
|
||||
{
|
||||
size.height = (bounds.height - inset.vertical() - child.margin.vertical())
|
||||
.max(0.0)
|
||||
.clamp(child.sizing.min_height, child.sizing.max_height);
|
||||
}
|
||||
size
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
let Some((row, padding, gap)) = container(node) else {
|
||||
return children.to_vec();
|
||||
};
|
||||
let nodes = node.children();
|
||||
let mut output = children.to_vec();
|
||||
for (child, size) in nodes.iter().zip(&mut output) {
|
||||
if child.align_self.unwrap_or(node.cross_align) == CrossAlign::Stretch {
|
||||
let sizing = child.sizing;
|
||||
if row && sizing.height == crate::box_model::Dimension::Auto {
|
||||
size.height = (bounds.height - padding.vertical() - child.margin.vertical())
|
||||
.max(0.0)
|
||||
.clamp(sizing.min_height, sizing.max_height);
|
||||
} else if !row && sizing.width == crate::box_model::Dimension::Auto {
|
||||
size.width = (bounds.width - padding.horizontal() - child.margin.horizontal())
|
||||
.max(0.0)
|
||||
.clamp(sizing.min_width, sizing.max_width);
|
||||
}
|
||||
}
|
||||
}
|
||||
if !nodes.iter().any(|child| child.has_flex()) {
|
||||
return children.to_vec();
|
||||
return output;
|
||||
}
|
||||
let space = ((if row { bounds.width } else { bounds.height }) as f64
|
||||
- padding as f64 * 2.0
|
||||
- if row {
|
||||
padding.horizontal()
|
||||
} else {
|
||||
padding.vertical()
|
||||
} as f64
|
||||
- nodes
|
||||
.iter()
|
||||
.map(|child| {
|
||||
if row {
|
||||
child.margin.horizontal()
|
||||
} else {
|
||||
child.margin.vertical()
|
||||
}
|
||||
})
|
||||
.sum::<f32>() as f64
|
||||
- gap as f64 * children.len().saturating_sub(1) as f64)
|
||||
.max(0.0);
|
||||
let bases: Vec<f64> = nodes
|
||||
@@ -324,7 +644,7 @@ pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size], preferred: &[S
|
||||
.zip(preferred)
|
||||
.map(|(child, size)| {
|
||||
child.flex_policy.basis.unwrap_or_else(|| {
|
||||
if child.flex > 0.0 {
|
||||
if child.flex > 0.0 || main_fill(child, row) {
|
||||
0.0
|
||||
} else if row {
|
||||
size.width
|
||||
@@ -338,7 +658,8 @@ pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size], preferred: &[S
|
||||
.iter()
|
||||
.zip(&bases)
|
||||
.map(|(child, basis)| {
|
||||
basis.clamp(child.flex_policy.min as f64, child.flex_policy.max as f64)
|
||||
let (min, max) = main_limits(child, row);
|
||||
basis.clamp(min as f64, max as f64)
|
||||
})
|
||||
.collect();
|
||||
let free = space - sizes.iter().sum::<f64>();
|
||||
@@ -348,7 +669,11 @@ pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size], preferred: &[S
|
||||
.zip(&bases)
|
||||
.map(|(child, basis)| {
|
||||
if growing {
|
||||
child.flex as f64
|
||||
if main_fill(child, row) && child.flex == 0.0 {
|
||||
1.0
|
||||
} else {
|
||||
child.flex as f64
|
||||
}
|
||||
} else {
|
||||
child.flex_policy.shrink as f64 * basis
|
||||
}
|
||||
@@ -357,11 +682,8 @@ pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size], preferred: &[S
|
||||
let limits: Vec<f64> = nodes
|
||||
.iter()
|
||||
.map(|child| {
|
||||
if growing {
|
||||
child.flex_policy.max as f64
|
||||
} else {
|
||||
child.flex_policy.min as f64
|
||||
}
|
||||
let (min, max) = main_limits(child, row);
|
||||
if growing { max as f64 } else { min as f64 }
|
||||
})
|
||||
.collect();
|
||||
let mut active: Vec<usize> = (0..nodes.len())
|
||||
@@ -400,7 +722,6 @@ pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size], preferred: &[S
|
||||
}
|
||||
});
|
||||
}
|
||||
let mut output = children.to_vec();
|
||||
for (size, along) in output.iter_mut().zip(sizes) {
|
||||
if row {
|
||||
size.width = along as f32;
|
||||
@@ -418,6 +739,37 @@ pub fn arrange_native(
|
||||
bounds: Size,
|
||||
children: &[Size],
|
||||
) -> Result<Vec<(f32, f32)>> {
|
||||
if let UiKind::Overlay { .. } = node.kind {
|
||||
let padding = insets(node);
|
||||
let horizontal = match node.main_align {
|
||||
MainAlign::Center => CrossAlign::Center,
|
||||
MainAlign::End => CrossAlign::End,
|
||||
_ => CrossAlign::Start,
|
||||
};
|
||||
return children
|
||||
.iter()
|
||||
.zip(node.children())
|
||||
.map(|(size, child)| {
|
||||
let outer = child.margin.outer(*size);
|
||||
let point = (
|
||||
x + padding.left
|
||||
+ child.margin.left
|
||||
+ horizontal.offset(bounds.width - padding.horizontal(), outer.width),
|
||||
y + padding.top
|
||||
+ child.margin.top
|
||||
+ child
|
||||
.align_self
|
||||
.unwrap_or(node.cross_align)
|
||||
.offset(bounds.height - padding.vertical(), outer.height),
|
||||
);
|
||||
coordinate(point.0)?;
|
||||
coordinate(point.1)?;
|
||||
coordinate(point.0 + size.width)?;
|
||||
coordinate(point.1 + size.height)?;
|
||||
Ok(point)
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
if let UiKind::Canvas { positions, .. } = &node.kind {
|
||||
return Ok(positions
|
||||
.iter()
|
||||
@@ -429,11 +781,25 @@ pub fn arrange_native(
|
||||
};
|
||||
let occupied = children
|
||||
.iter()
|
||||
.map(|child| if row { child.width } else { child.height })
|
||||
.zip(node.children())
|
||||
.map(|(child, node)| {
|
||||
if row {
|
||||
child.width + node.margin.horizontal()
|
||||
} else {
|
||||
child.height + node.margin.vertical()
|
||||
}
|
||||
})
|
||||
.sum::<f32>()
|
||||
+ 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 spare = (available
|
||||
- if row {
|
||||
padding.horizontal()
|
||||
} else {
|
||||
padding.vertical()
|
||||
}
|
||||
- occupied)
|
||||
.max(0.0);
|
||||
let offset = match node.main_align {
|
||||
MainAlign::Center => spare / 2.0,
|
||||
MainAlign::End => spare,
|
||||
@@ -444,13 +810,34 @@ pub fn arrange_native(
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let mut cursor = padding + offset;
|
||||
let mut cursor = if row { padding.left } else { padding.top } + offset;
|
||||
let mut points = Vec::with_capacity(children.len());
|
||||
for child in children {
|
||||
for (index, child) in children.iter().enumerate() {
|
||||
let child_node = &node.children()[index];
|
||||
let margin = child_node.margin;
|
||||
let align = child_node.align_self.unwrap_or(node.cross_align);
|
||||
let cross = align.offset(
|
||||
if row {
|
||||
bounds.height - padding.vertical()
|
||||
} else {
|
||||
bounds.width - padding.horizontal()
|
||||
},
|
||||
if row {
|
||||
child.height + margin.vertical()
|
||||
} else {
|
||||
child.width + margin.horizontal()
|
||||
},
|
||||
);
|
||||
let point = if row {
|
||||
(x + cursor, y + padding)
|
||||
(
|
||||
x + cursor + margin.left,
|
||||
y + padding.top + cross + margin.top,
|
||||
)
|
||||
} else {
|
||||
(x + padding, y + cursor)
|
||||
(
|
||||
x + padding.left + cross + margin.left,
|
||||
y + cursor + margin.top,
|
||||
)
|
||||
};
|
||||
coordinate(point.0)?;
|
||||
coordinate(point.1)?;
|
||||
@@ -458,6 +845,11 @@ pub fn arrange_native(
|
||||
coordinate(point.1 + child.height)?;
|
||||
points.push(point);
|
||||
cursor += if row { child.width } else { child.height };
|
||||
cursor += if row {
|
||||
margin.horizontal()
|
||||
} else {
|
||||
margin.vertical()
|
||||
};
|
||||
cursor += gap + distributed_gap;
|
||||
}
|
||||
Ok(points)
|
||||
@@ -569,7 +961,7 @@ impl LayoutRuntime {
|
||||
child.as_ref(),
|
||||
Some(index),
|
||||
*slot,
|
||||
child_constraints(node, constraints),
|
||||
initial_child_constraints(node, child, constraints),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -617,8 +1009,8 @@ impl LayoutRuntime {
|
||||
height: size.height,
|
||||
interactive: item.node.interactive,
|
||||
});
|
||||
if !structural && let Some(command) = paint_native(item.node, x, y, size) {
|
||||
commands.push(command);
|
||||
if !structural {
|
||||
commands.extend(paint_commands(item.node, x, y, size));
|
||||
}
|
||||
if structural {
|
||||
continue;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// Module ownership follows the directory; crate-root aliases keep internal call sites stable.
|
||||
pub(crate) mod box_model;
|
||||
pub(crate) mod display_list;
|
||||
pub(crate) mod geometry;
|
||||
pub(crate) mod layout;
|
||||
|
||||
@@ -217,9 +217,14 @@ impl NativeCallable for Method {
|
||||
.map(|child| {
|
||||
size(
|
||||
child,
|
||||
layout::arranged_child_constraints(
|
||||
layout::occurrence_constraints(
|
||||
&node,
|
||||
[0.0, 0.0, width, height],
|
||||
child,
|
||||
layout::arranged_child_constraints(
|
||||
&node,
|
||||
[0.0, 0.0, width, height],
|
||||
Size { width, height },
|
||||
),
|
||||
Size { width, height },
|
||||
),
|
||||
)
|
||||
@@ -268,7 +273,7 @@ impl NativeCallable for Method {
|
||||
return Err(invalid("visual bounds exceed limits"));
|
||||
}
|
||||
let mut commands = Vec::new();
|
||||
if let Some(command) = layout::paint_native(&node, x, y, Size { width, height }) {
|
||||
for command in layout::paint_commands(&node, x, y, Size { width, height }) {
|
||||
let fields = match command {
|
||||
RenderCommand::Text {
|
||||
x,
|
||||
|
||||
+23
-13
@@ -76,11 +76,8 @@ pub fn project(vm: &mut Vm, table: &MountTable, root: &UiNode, frame: &mut Frame
|
||||
.mounts
|
||||
.iter()
|
||||
.map(|m| {
|
||||
let padding = match nodes[m.block_id.as_str()].kind {
|
||||
UiKind::Scroll { padding, .. } => padding,
|
||||
_ => 0.0,
|
||||
};
|
||||
(m.x + m.width - padding, m.y + m.height - padding)
|
||||
let inset = layout::insets(nodes[m.block_id.as_str()]);
|
||||
(m.x + m.width - inset.right, m.y + m.height - inset.bottom)
|
||||
})
|
||||
.collect();
|
||||
for i in (0..frame.mounts.len()).rev() {
|
||||
@@ -104,22 +101,20 @@ pub fn project(vm: &mut Vm, table: &MountTable, root: &UiNode, frame: &mut Frame
|
||||
.parent
|
||||
.and_then(|id| contexts.get(&id).copied())
|
||||
.unwrap_or_default();
|
||||
let padding = match nodes[m.block_id.as_str()].kind {
|
||||
UiKind::Scroll { padding, .. } => padding,
|
||||
_ => 0.0,
|
||||
};
|
||||
let node = nodes[m.block_id.as_str()];
|
||||
let padding = layout::insets(node);
|
||||
let axis = match nodes[m.block_id.as_str()].kind {
|
||||
UiKind::Scroll { axis, .. } => axis,
|
||||
_ => Axis::Both,
|
||||
};
|
||||
let limits = [
|
||||
if axis.horizontal() {
|
||||
(extent[i].0 + padding - m.x - m.width).max(0.0)
|
||||
(extent[i].0 + padding.right - m.x - m.width).max(0.0)
|
||||
} else {
|
||||
0.0
|
||||
},
|
||||
if axis.vertical() {
|
||||
(extent[i].1 + padding - m.y - m.height).max(0.0)
|
||||
(extent[i].1 + padding.bottom - m.y - m.height).max(0.0)
|
||||
} else {
|
||||
0.0
|
||||
},
|
||||
@@ -128,6 +123,19 @@ pub fn project(vm: &mut Vm, table: &MountTable, root: &UiNode, frame: &mut Frame
|
||||
m.y -= dy;
|
||||
m.clip = clip;
|
||||
let mut child = (dx, dy, clip);
|
||||
if node.clip_children {
|
||||
let border = node.border;
|
||||
let viewport = [
|
||||
m.x + border.left.min(m.width),
|
||||
m.y + border.top.min(m.height),
|
||||
(m.width - border.horizontal()).max(0.0),
|
||||
(m.height - border.vertical()).max(0.0),
|
||||
];
|
||||
child.2 = Some(
|
||||
clip.map(|clip| intersection(clip, viewport))
|
||||
.unwrap_or(viewport),
|
||||
);
|
||||
}
|
||||
if scrolling[i] {
|
||||
let weak = table
|
||||
.active
|
||||
@@ -149,13 +157,15 @@ pub fn project(vm: &mut Vm, table: &MountTable, root: &UiNode, frame: &mut Frame
|
||||
dx + x,
|
||||
dy + y,
|
||||
Some(
|
||||
clip.map(|clip| intersection(clip, viewport))
|
||||
child
|
||||
.2
|
||||
.map(|clip| intersection(clip, viewport))
|
||||
.unwrap_or(viewport),
|
||||
),
|
||||
);
|
||||
}
|
||||
contexts.insert(m.id, child);
|
||||
if let Some(command) = layout::paint_native(
|
||||
for command in layout::paint_commands(
|
||||
nodes[m.block_id.as_str()],
|
||||
m.x,
|
||||
m.y,
|
||||
|
||||
@@ -165,7 +165,11 @@ impl NativeCallable for Step {
|
||||
Some(
|
||||
plan.runtime
|
||||
.measure_node(&node, children.clone(), constraints)
|
||||
.map_err(|_| invalid("native measurement exceeds layout limits"))?,
|
||||
.map_err(|error| {
|
||||
error.downcast_ref::<VmError>().cloned().unwrap_or_else(|| {
|
||||
invalid("native measurement exceeds layout limits")
|
||||
})
|
||||
})?,
|
||||
)
|
||||
} else {
|
||||
if !intrinsic {
|
||||
@@ -211,8 +215,7 @@ impl NativeCallable for Step {
|
||||
if matches!(self.phase, Phase::Arrange) {
|
||||
plan.runtime.arranged += 1;
|
||||
plan.native_arrange[self.index] = intrinsic;
|
||||
plan.custom_arrange[self.index] =
|
||||
!intrinsic || node.children().iter().any(|child| child.has_flex());
|
||||
plan.custom_arrange[self.index] = !intrinsic || node.allocates_children();
|
||||
}
|
||||
(node, children, preferred, bounds, native_size)
|
||||
};
|
||||
@@ -532,7 +535,13 @@ impl NativeCallable for Settle {
|
||||
Size { width, height },
|
||||
);
|
||||
for child in children {
|
||||
let mut constraints = base;
|
||||
let natural = layout::occurrence_constraints(
|
||||
&plan.nodes[index],
|
||||
&plan.nodes[child],
|
||||
base,
|
||||
Size { width, height },
|
||||
);
|
||||
let mut constraints = natural;
|
||||
if let Some(width) = plan.allocations[child][0] {
|
||||
constraints[0] = width;
|
||||
constraints[2] = width;
|
||||
@@ -554,8 +563,8 @@ impl NativeCallable for Settle {
|
||||
// own replacement measurement. Source arrange
|
||||
// allocations remain authoritative.
|
||||
plan.allocations[child][main] = None;
|
||||
constraints[main] = base[main];
|
||||
constraints[main + 2] = base[main + 2];
|
||||
constraints[main] = natural[main];
|
||||
constraints[main + 2] = natural[main + 2];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -844,7 +853,7 @@ pub fn start(
|
||||
let mut nodes = Vec::new();
|
||||
let mut work = vec![root.clone()];
|
||||
while let Some(node) = work.pop() {
|
||||
custom |= node.has_flex();
|
||||
custom |= node.has_flex() || node.allocates_children();
|
||||
work.extend(node.children().iter().rev().cloned());
|
||||
nodes.push(node);
|
||||
}
|
||||
@@ -878,9 +887,12 @@ pub fn start(
|
||||
}
|
||||
let mut constraints = vec![runtime.root_constraints(); nodes.len()];
|
||||
for index in 0..nodes.len() {
|
||||
let next = layout::child_constraints(&nodes[index], constraints[index]);
|
||||
for child in &children[index] {
|
||||
constraints[*child] = next;
|
||||
constraints[*child] = layout::initial_child_constraints(
|
||||
&nodes[index],
|
||||
&nodes[*child],
|
||||
constraints[index],
|
||||
);
|
||||
}
|
||||
}
|
||||
let count = nodes.len();
|
||||
|
||||
@@ -425,7 +425,7 @@ fn finish(vm: &mut Vm, mut work: Work, reads: Vec<endor_core::ReadDependency>) -
|
||||
let node = nodes
|
||||
.get(mount.block_id.as_str())
|
||||
.ok_or_else(|| anyhow::anyhow!("paint projection has no node"))?;
|
||||
if let Some(command) = layout::paint_native(
|
||||
commands.extend(layout::paint_commands(
|
||||
node,
|
||||
mount.x,
|
||||
mount.y,
|
||||
@@ -433,9 +433,7 @@ fn finish(vm: &mut Vm, mut work: Work, reads: Vec<endor_core::ReadDependency>) -
|
||||
width: mount.width,
|
||||
height: mount.height,
|
||||
},
|
||||
) {
|
||||
commands.push(command);
|
||||
}
|
||||
));
|
||||
}
|
||||
clips.extend(std::iter::repeat_n(mount.clip, commands.len() - before));
|
||||
if commands.len() > 10_000 {
|
||||
|
||||
Reference in New Issue
Block a user