use std::{
cell::RefCell,
collections::{BTreeSet, HashMap},
};
#[cfg(debug_assertions)]
use std::env;
use crate::vdom::{
Align, Edges, NodeKind, Offset, Overflow, Position, Resize, Size, Style, VElement, WhiteSpace,
};
pub const FONT_SIZE: f32 = 16.0;
pub const SPACING: f32 = 6.0;
pub const BUTTON_PAD_X: f32 = 14.0;
pub const BUTTON_PAD_Y: f32 = 9.0;
pub const BUTTON_MIN_H: f32 = 34.0;
pub const FIELD_WIDTH: f32 = 240.0;
pub const FIELD_PAD_X: f32 = 8.0;
pub const FIELD_MIN_H: f32 = 34.0;
pub const FIELD_LINE_H: f32 = 22.0;
pub const RESIZE_GRIP: f32 = 12.0;
pub const CHECKBOX_BOX: f32 = 18.0;
pub const CHECKBOX_GAP: f32 = 8.0;
pub const SELECT_WIDTH: f32 = 240.0;
pub const SELECT_PAD_X: f32 = 8.0;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Point {
pub x: f32,
pub y: f32,
}
impl Point {
pub fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Rect {
pub x: f32,
pub y: f32,
pub w: f32,
pub h: f32,
}
impl Rect {
pub fn new(x: f32, y: f32, w: f32, h: f32) -> Self {
Self { x, y, w, h }
}
pub fn contains(&self, point: Point) -> bool {
point.x >= self.x
&& point.x <= self.x + self.w
&& point.y >= self.y
&& point.y <= self.y + self.h
}
pub fn intersection(self, other: Self) -> Self {
let x = self.x.max(other.x);
let y = self.y.max(other.y);
let right = (self.x + self.w).min(other.x + other.w);
let bottom = (self.y + self.h).min(other.y + other.h);
Self::new(x, y, (right - x).max(0.0), (bottom - y).max(0.0))
}
pub fn union(self, other: Self) -> Self {
if self.w <= 0.0 || self.h <= 0.0 {
return other;
}
if other.w <= 0.0 || other.h <= 0.0 {
return self;
}
let x = self.x.min(other.x);
let y = self.y.min(other.y);
let right = (self.x + self.w).max(other.x + other.w);
let bottom = (self.y + self.h).max(other.y + other.h);
Self::new(x, y, right - x, bottom - y)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ContentBox {
pub left: f32,
pub top: f32,
pub width: f32,
pub height: f32,
}
impl ContentBox {
pub fn from_rect(rect: Rect, inset: Edges) -> Self {
Self {
left: rect.x + inset.left,
top: rect.y + inset.top,
width: (rect.w - inset.left - inset.right).max(0.0),
height: (rect.h - inset.top - inset.bottom).max(0.0),
}
}
}
fn button_extra_padding(vnode: &VElement, style: &Style) -> Edges {
let explicit = vnode.attr("padding").is_some()
|| vnode.attr("padding-top").is_some()
|| vnode.attr("padding-right").is_some()
|| vnode.attr("padding-bottom").is_some()
|| vnode.attr("padding-left").is_some()
|| !style.padding.is_zero();
if explicit {
Edges::ZERO
} else {
Edges {
top: BUTTON_PAD_Y,
right: BUTTON_PAD_X,
bottom: BUTTON_PAD_Y,
left: BUTTON_PAD_X,
}
}
}
fn is_heading(vnode: &VElement) -> bool {
matches!(vnode.kind, NodeKind::Column) && matches!(vnode.tag, "h1" | "h2" | "h3" | "h4" | "h5")
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Axis {
Width,
Height,
}
#[derive(Debug, Clone, Copy)]
struct FlowContext {
content: ContentBox,
width_known: bool,
height_known: bool,
stretch: Option<Axis>,
forced_w: Option<f32>,
forced_h: Option<f32>,
}
pub trait TextMetrics {
fn measure(&self, text: &str, font_size: f32) -> (f32, f32);
}
type TextMeasureCache = RefCell<HashMap<(usize, usize, u32), (String, (f32, f32))>>;
struct MeasuredText<'a> {
inner: &'a dyn TextMetrics,
cache: TextMeasureCache,
}
impl<'a> MeasuredText<'a> {
fn new(inner: &'a dyn TextMetrics) -> Self {
Self {
inner,
cache: RefCell::new(HashMap::new()),
}
}
}
impl TextMetrics for MeasuredText<'_> {
fn measure(&self, text: &str, font_size: f32) -> (f32, f32) {
let key = (text.as_ptr() as usize, text.len(), font_size.to_bits());
if let Some((cached, size)) = self.cache.borrow().get(&key)
&& cached == text
{
return *size;
}
let size = self.inner.measure(text, font_size);
self.cache.borrow_mut().insert(key, (text.to_owned(), size));
size
}
}
#[derive(Debug, Clone)]
pub struct PlacedNode {
pub key: &'static str,
pub kind: NodeKind,
pub style: Style,
pub rect: Rect,
pub children: Vec<usize>,
pub overflow_x: Overflow,
pub overflow_y: Overflow,
pub scroll_width: f32,
pub scroll_height: f32,
pub scroll_x: f32,
pub scroll_y: f32,
pub clip_rect: Option<Rect>,
}
impl PlacedNode {
pub fn content_rect(&self) -> Rect {
let inset = self.style.inset();
Rect::new(
self.rect.x + inset.left,
self.rect.y + inset.top,
(self.rect.w - inset.horizontal()).max(0.0),
(self.rect.h - inset.vertical()).max(0.0),
)
}
pub fn max_scroll_x(&self) -> f32 {
(self.scroll_width - self.rect.w).max(0.0)
}
pub fn max_scroll_y(&self) -> f32 {
(self.scroll_height - self.rect.h).max(0.0)
}
pub fn is_interactive(&self) -> bool {
match self.kind {
NodeKind::Button { .. } | NodeKind::TextField { .. } => true,
#[cfg(feature = "elements-checkbox")]
NodeKind::Checkbox { .. } => true,
#[cfg(feature = "elements-radio")]
NodeKind::Radio { .. } => true,
#[cfg(feature = "elements-select")]
NodeKind::Select { .. } => true,
_ => false,
}
}
}
#[derive(Debug, Clone, Copy)]
struct Inherited {
size: f32,
color: Option<u32>,
white_space: WhiteSpace,
}
#[derive(Debug, Clone)]
pub struct Layout {
nodes: Vec<PlacedNode>,
}
impl Layout {
pub fn build(root: &VElement, viewport: (f32, f32), metrics: &dyn TextMetrics) -> Self {
let mut layout = Self { nodes: Vec::new() };
let view = Rect::new(0.0, 0.0, viewport.0, viewport.1);
let metrics = MeasuredText::new(metrics);
layout.place(
root,
Point::new(0.0, 0.0),
FlowContext {
content: ContentBox {
left: 0.0,
top: 0.0,
width: viewport.0,
height: viewport.1,
},
width_known: true,
height_known: true,
stretch: Some(Axis::Width),
forced_w: None,
forced_h: None,
},
None,
&metrics,
Inherited {
size: FONT_SIZE,
color: None,
white_space: WhiteSpace::Normal,
},
);
if let Some(node) = layout.nodes.first_mut() {
node.rect = view;
}
layout.reorder_by_z();
layout
}
pub fn nodes(&self) -> &[PlacedNode] {
&self.nodes
}
pub fn rect_of(&self, key: &str) -> Option<Rect> {
self.nodes
.iter()
.find(|node| node.key == key)
.map(|node| node.rect)
}
pub fn node(&self, key: &str) -> Option<&PlacedNode> {
self.nodes.iter().find(|node| node.key == key)
}
pub fn apply_scroll_offsets(&mut self, offsets: &HashMap<&'static str, Point>) {
if self.nodes.is_empty() {
return;
}
self.nodes[0].scroll_x = 0.0;
self.nodes[0].scroll_y = 0.0;
for index in 1..self.nodes.len() {
let offset = offsets
.get(&self.nodes[index].key)
.copied()
.unwrap_or(Point { x: 0.0, y: 0.0 });
let node = &mut self.nodes[index];
node.scroll_x = if node.overflow_x.scrollable() {
offset.x.clamp(0.0, node.max_scroll_x())
} else {
0.0
};
node.scroll_y = if node.overflow_y.scrollable() {
offset.y.clamp(0.0, node.max_scroll_y())
} else {
0.0
};
}
self.nodes[0].clip_rect = None;
let end = self.nodes.len();
self.apply_clip_and_shift(0, end);
}
pub fn scroll_target(&self, point: Point, horizontal: bool) -> Option<&PlacedNode> {
self.hit_stack(point).into_iter().find(|node| {
if horizontal {
node.overflow_x.scrollable() && node.max_scroll_x() > 0.5
} else {
node.overflow_y.scrollable() && node.max_scroll_y() > 0.5
}
})
}
pub fn scroll_offset(&self, key: &str) -> Point {
self.node(key)
.map(|node| Point::new(node.scroll_x, node.scroll_y))
.unwrap_or(Point { x: 0.0, y: 0.0 })
}
fn apply_clip_and_shift(&mut self, index: usize, end: usize) {
let node = &self.nodes[index];
let scroll_x = node.scroll_x;
let scroll_y = node.scroll_y;
let own_clip = if node.overflow_x.clips() || node.overflow_y.clips() {
let clip = node.content_rect();
Some(match node.clip_rect {
Some(parent) => parent.intersection(clip),
None => clip,
})
} else {
node.clip_rect
};
let children = node.children.clone();
for (position, child) in children.iter().enumerate() {
let child = *child;
let child_end = children.get(position + 1).copied().unwrap_or(end);
if scroll_x != 0.0 || scroll_y != 0.0 {
for node in &mut self.nodes[child..child_end] {
node.rect.x -= scroll_x;
node.rect.y -= scroll_y;
}
}
self.nodes[child].clip_rect = own_clip;
self.apply_clip_and_shift(child, child_end);
}
}
pub fn hit_interactive(&self, point: Point) -> Option<&PlacedNode> {
if self.nodes.is_empty() {
return None;
}
self.hit(0, point)
}
pub fn hit_stack(&self, point: Point) -> Vec<&PlacedNode> {
let mut stack = Vec::new();
if !self.nodes.is_empty() {
self.collect_hit(0, point, &mut stack);
}
stack
}
fn collect_hit<'a>(
&'a self,
index: usize,
point: Point,
stack: &mut Vec<&'a PlacedNode>,
) -> bool {
let node = &self.nodes[index];
if !node.rect.contains(point) || node.clip_rect.is_some_and(|clip| !clip.contains(point)) {
return false;
}
for &child in node.children.iter().rev() {
if self.collect_hit(child, point, stack) {
break;
}
}
stack.push(node);
true
}
pub fn hit_titlebar(&self, point: Point) -> bool {
self.nodes
.iter()
.any(|node| matches!(node.kind, NodeKind::TitleBar) && node.rect.contains(point))
}
pub fn hit_resize(&self, point: Point) -> Option<(&PlacedNode, Resize)> {
self.nodes.iter().rev().find_map(|node| {
let NodeKind::TextField {
resize,
disabled: false,
..
} = &node.kind
else {
return None;
};
if *resize == Resize::None || !node.rect.contains(point) {
return None;
}
let right = node.rect.x + node.rect.w;
let bottom = node.rect.y + node.rect.h;
let near_right = point.x >= right - RESIZE_GRIP;
let near_bottom = point.y >= bottom - RESIZE_GRIP;
let hit = match resize {
Resize::Horizontal => near_right,
Resize::Vertical => near_bottom,
Resize::Both => near_right && near_bottom,
Resize::None => false,
};
hit.then_some((node, *resize))
})
}
fn hit(&self, index: usize, point: Point) -> Option<&PlacedNode> {
let node = &self.nodes[index];
if !node.rect.contains(point) || node.clip_rect.is_some_and(|clip| !clip.contains(point)) {
return None;
}
for &child in node.children.iter().rev() {
if let Some(found) = self.hit(child, point) {
return Some(found);
}
}
if node.is_interactive() && !is_disabled(&node.kind) {
Some(node)
} else {
None
}
}
fn place(
&mut self,
vnode: &VElement,
origin: Point,
flow: FlowContext,
parent: Option<usize>,
metrics: &dyn TextMetrics,
inherited: Inherited,
) -> (f32, f32) {
let style = vnode.style;
if !style.visible {
return (0.0, 0.0);
}
let inset = style.inset();
let margin = style.margin;
let flow_x = origin.x + margin.left;
let flow_y = origin.y + margin.top;
let font_size = style.font_size.unwrap_or(inherited.size);
let font_color = style.font_color.or(inherited.color);
let white_space = style.white_space.unwrap_or(inherited.white_space);
let avail_w = flow.content.width;
let avail_h = flow.content.height;
let step_w = margin.left + margin.right;
let step_h = margin.top + margin.bottom;
let start = self.nodes.len();
let root = parent.is_none() || matches!(vnode.kind, NodeKind::Window);
let is_container = matches!(
vnode.kind,
NodeKind::Column | NodeKind::Row | NodeKind::TitleBar | NodeKind::Window
);
let stretch_w = is_container && flow.stretch == Some(Axis::Width);
let stretch_h = is_container && flow.stretch == Some(Axis::Height);
let definite_w = flow.forced_w.unwrap_or(if root {
avail_w
} else if matches!(style.width, Size::Auto) {
if stretch_w { avail_w } else { 0.0 }
} else {
style.resolve_width(0.0, avail_w)
});
let definite_h = flow.forced_h.unwrap_or(if root {
avail_h
} else if matches!(style.height, Size::Auto) {
if stretch_h { avail_h } else { 0.0 }
} else {
style.resolve_height(0.0, avail_h)
});
let width_known = flow.forced_w.is_some()
|| root
|| matches!(style.width, Size::Px(_))
|| (matches!(style.width, Size::Percent(_)) && flow.width_known)
|| (matches!(style.width, Size::Auto) && stretch_w);
let height_known = flow.forced_h.is_some()
|| root
|| matches!(style.height, Size::Px(_))
|| (matches!(style.height, Size::Percent(_)) && flow.height_known)
|| (matches!(style.height, Size::Auto) && stretch_h);
let child_content = ContentBox {
left: flow_x + inset.left,
top: flow_y + inset.top,
width: (definite_w - inset.left - inset.right).max(0.0),
height: (definite_h - inset.top - inset.bottom).max(0.0),
};
let child_stretch = if style.align_items == Align::Start {
match vnode.kind {
NodeKind::Row | NodeKind::TitleBar if height_known => Some(Axis::Height),
NodeKind::Column | NodeKind::Window if width_known => Some(Axis::Width),
_ => None,
}
} else {
None
};
let child_flow = FlowContext {
content: child_content,
width_known,
height_known,
stretch: child_stretch,
forced_w: None,
forced_h: None,
};
let (w, h) = match &vnode.kind {
NodeKind::Text { content } => {
let explicit_w = flow.forced_w.or_else(|| {
(!matches!(style.width, Size::Auto)).then(|| style.resolve_width(0.0, avail_w))
});
let limit = if let Some(width) = explicit_w {
width
} else if let Some(max) = style.max_width {
max.resolve(0.0, avail_w)
} else if flow.width_known {
avail_w
} else {
f32::INFINITY
};
let limit = if let Some(max) = style.max_width {
limit.min(max.resolve(0.0, avail_w))
} else {
limit
};
let limit = (limit - inset.horizontal()).max(1.0);
let (content_w, content_h) = match white_space {
WhiteSpace::Normal => {
let lines =
crate::text_wrap::wrap_words_with_widths(content, limit, |line| {
metrics.measure(line, font_size).0
});
let width = lines.iter().map(|(_, width)| *width).fold(0.0, f32::max);
let row_h = metrics.measure("", font_size).1.max(1.0);
(width, lines.len() as f32 * row_h)
}
WhiteSpace::Nowrap => metrics.measure(content, font_size),
};
let w = flow.forced_w.unwrap_or_else(|| {
style.resolve_width(content_w + inset.horizontal(), avail_w)
});
let h = flow
.forced_h
.unwrap_or_else(|| style.resolve_height(content_h + inset.vertical(), avail_h));
self.push(vnode, Rect::new(flow_x, flow_y, w, h), parent);
(w, h)
}
#[cfg(feature = "image-support")]
NodeKind::Image { width, height, .. } => {
let intrinsic_w = *width as f32;
let intrinsic_h = *height as f32;
let explicit_w = flow.forced_w.or_else(|| {
(!matches!(style.width, Size::Auto))
.then(|| style.resolve_width(intrinsic_w, avail_w))
});
let explicit_h = flow.forced_h.or_else(|| {
(!matches!(style.height, Size::Auto))
.then(|| style.resolve_height(intrinsic_h, avail_h))
});
let (w, h) = match (explicit_w, explicit_h) {
(Some(w), Some(h)) => (w, h),
(Some(w), None) => {
let inner_w = (w - inset.horizontal()).max(0.0);
let inner_h = if intrinsic_w > 0.0 {
inner_w * intrinsic_h / intrinsic_w
} else {
0.0
};
(w, inner_h + inset.vertical())
}
(None, Some(h)) => {
let inner_h = (h - inset.vertical()).max(0.0);
let inner_w = if intrinsic_h > 0.0 {
inner_h * intrinsic_w / intrinsic_h
} else {
0.0
};
(inner_w + inset.horizontal(), h)
}
(None, None) => (
intrinsic_w + inset.horizontal(),
intrinsic_h + inset.vertical(),
),
};
self.push(vnode, Rect::new(flow_x, flow_y, w, h), parent);
(w, h)
}
#[cfg(feature = "image-support")]
NodeKind::RuntimeImage { image, .. } => {
let intrinsic_w = image.width() as f32;
let intrinsic_h = image.height() as f32;
let explicit_w = flow.forced_w.or_else(|| {
(!matches!(style.width, Size::Auto))
.then(|| style.resolve_width(intrinsic_w, avail_w))
});
let explicit_h = flow.forced_h.or_else(|| {
(!matches!(style.height, Size::Auto))
.then(|| style.resolve_height(intrinsic_h, avail_h))
});
let (w, h) = match (explicit_w, explicit_h) {
(Some(w), Some(h)) => (w, h),
(Some(w), None) => {
let inner_w = (w - inset.horizontal()).max(0.0);
let inner_h = if intrinsic_w > 0.0 {
inner_w * intrinsic_h / intrinsic_w
} else {
0.0
};
(w, inner_h + inset.vertical())
}
(None, Some(h)) => {
let inner_h = (h - inset.vertical()).max(0.0);
let inner_w = if intrinsic_h > 0.0 {
inner_h * intrinsic_w / intrinsic_h
} else {
0.0
};
(inner_w + inset.horizontal(), h)
}
(None, None) => (
intrinsic_w + inset.horizontal(),
intrinsic_h + inset.vertical(),
),
};
self.push(vnode, Rect::new(flow_x, flow_y, w, h), parent);
(w, h)
}
NodeKind::Button { .. } => {
let index = self.push(vnode, Rect::default(), parent);
let extra = button_extra_padding(vnode, &style);
let btn_inset = inset.plus(extra);
let inner_left = flow_x + btn_inset.left;
let inner_top = flow_y + btn_inset.top;
let inner_w = if matches!(style.width, Size::Auto) {
f32::INFINITY
} else {
(definite_w - btn_inset.horizontal()).max(0.0)
};
let inner_h = (definite_h - btn_inset.vertical()).max(0.0);
let content = ContentBox {
left: inner_left,
top: inner_top,
width: inner_w,
height: inner_h,
};
let mut cursor_x = inner_left;
let mut content_w = 0.0_f32;
let mut content_h = 0.0_f32;
let mut any_child = false;
for child in &vnode.children {
if !child.style.visible || child.style.position == Position::Absolute {
continue;
}
let (child_w, child_h) = self.place(
child,
Point::new(cursor_x, inner_top),
FlowContext {
content,
width_known: inner_w > 0.0,
height_known: inner_h > 0.0,
stretch: None,
forced_w: None,
forced_h: None,
},
Some(index),
metrics,
Inherited {
size: font_size,
color: font_color,
white_space,
},
);
cursor_x += child_w + SPACING;
content_h = content_h.max(child_h);
any_child = true;
}
if any_child {
content_w = cursor_x - inner_left - SPACING;
}
let w = match (style.width, flow.forced_w) {
(Size::Auto, _) => content_w + btn_inset.horizontal(),
(_, Some(fw)) => fw,
(_, None) => style.resolve_width(content_w + btn_inset.horizontal(), avail_w),
};
let h = flow.forced_h.unwrap_or_else(|| {
style.resolve_height(
(content_h + btn_inset.vertical()).max(BUTTON_MIN_H),
avail_h,
)
});
let node = &mut self.nodes[index];
node.rect = Rect::new(flow_x, flow_y, w, h);
#[cfg(debug_assertions)]
debug_trace(vnode, node.rect, child_flow);
if any_child {
let pad_box = ContentBox {
left: node.rect.x + btn_inset.left,
top: node.rect.y + btn_inset.top,
width: (node.rect.w - btn_inset.horizontal()).max(0.0),
height: (node.rect.h - btn_inset.vertical()).max(0.0),
};
let first = self.nodes[index].children[0];
let dx = pad_box.left + ((pad_box.width - content_w) / 2.0).max(0.0)
- self.nodes[first].rect.x;
let dy = pad_box.top + ((pad_box.height - content_h) / 2.0).max(0.0)
- self.nodes[first].rect.y;
if dx != 0.0 || dy != 0.0 {
for child in &mut self.nodes[index + 1..] {
child.rect.x += dx;
child.rect.y += dy;
}
}
}
(w, h)
}
NodeKind::TextField { multiline, .. } => {
let default_h = if *multiline {
FIELD_MIN_H + 2.0 * FIELD_LINE_H
} else {
FIELD_MIN_H
};
let w = flow.forced_w.unwrap_or_else(|| {
style.resolve_width(FIELD_WIDTH + inset.horizontal(), avail_w)
});
let h = flow
.forced_h
.unwrap_or_else(|| style.resolve_height(default_h + inset.vertical(), avail_h));
self.push(vnode, Rect::new(flow_x, flow_y, w, h), parent);
(w, h)
}
#[cfg(feature = "elements-checkbox")]
NodeKind::Checkbox { label, .. } => {
let (label_w, label_h) = metrics.measure(label, font_size);
let content_w = CHECKBOX_BOX + CHECKBOX_GAP + label_w;
let content_h = CHECKBOX_BOX.max(label_h);
let w = flow.forced_w.unwrap_or_else(|| {
style.resolve_width(content_w + inset.horizontal(), avail_w)
});
let h = flow
.forced_h
.unwrap_or_else(|| style.resolve_height(content_h + inset.vertical(), avail_h));
self.push(vnode, Rect::new(flow_x, flow_y, w, h), parent);
(w, h)
}
#[cfg(feature = "elements-radio")]
NodeKind::Radio { label, .. } => {
let (label_w, label_h) = metrics.measure(label, font_size);
let content_w = CHECKBOX_BOX + CHECKBOX_GAP + label_w;
let content_h = CHECKBOX_BOX.max(label_h);
let w = flow.forced_w.unwrap_or_else(|| {
style.resolve_width(content_w + inset.horizontal(), avail_w)
});
let h = flow
.forced_h
.unwrap_or_else(|| style.resolve_height(content_h + inset.vertical(), avail_h));
self.push(vnode, Rect::new(flow_x, flow_y, w, h), parent);
(w, h)
}
#[cfg(feature = "elements-select")]
NodeKind::Select { options, .. } => {
let widest = options
.iter()
.map(|option| metrics.measure(&option.label, font_size).0)
.fold(0.0_f32, f32::max);
let content_w = SELECT_WIDTH.max(widest + 2.0 * SELECT_PAD_X + CHECKBOX_BOX);
let w = flow.forced_w.unwrap_or_else(|| {
style.resolve_width(content_w + inset.horizontal(), avail_w)
});
let h = flow.forced_h.unwrap_or_else(|| {
style.resolve_height(FIELD_MIN_H + inset.vertical(), avail_h)
});
self.push(vnode, Rect::new(flow_x, flow_y, w, h), parent);
(w, h)
}
NodeKind::Column | NodeKind::Row | NodeKind::TitleBar | NodeKind::Window => {
let index = self.push(vnode, Rect::default(), parent);
let horizontal = matches!(vnode.kind, NodeKind::Row | NodeKind::TitleBar);
let is_head = !root && is_heading(vnode) && matches!(style.width, Size::Auto);
let measure_content = if is_head {
ContentBox {
left: flow_x + inset.left,
top: flow_y + inset.top,
width: f32::INFINITY,
height: f32::INFINITY,
}
} else {
child_content
};
let mut cursor_x = measure_content.left;
let mut cursor_y = measure_content.top;
let mut content_w = 0.0_f32;
let mut content_h = 0.0_f32;
let mut child_sizes = Vec::with_capacity(vnode.children.len());
let mut any_flow = false;
for child in &vnode.children {
if !child.style.visible || child.style.position == Position::Absolute {
continue;
}
any_flow = true;
let (child_w, child_h) = if is_head {
let mut copy = child.clone();
copy.style.width = Size::Auto;
let ctx = FlowContext {
content: measure_content,
width_known: false,
height_known: true,
stretch: None,
forced_w: None,
forced_h: None,
};
self.place(
©,
Point::new(cursor_x, cursor_y),
ctx,
Some(index),
metrics,
Inherited {
size: font_size,
color: font_color,
white_space,
},
)
} else {
self.place(
child,
Point::new(cursor_x, cursor_y),
child_flow,
Some(index),
metrics,
Inherited {
size: font_size,
color: font_color,
white_space,
},
)
};
child_sizes.push((child_w, child_h));
if horizontal {
cursor_x += child_w + SPACING;
content_h = content_h.max(child_h);
} else {
cursor_y += child_h + SPACING;
content_w = content_w.max(child_w);
}
}
if any_flow {
if horizontal {
content_w = cursor_x - child_content.left - SPACING;
} else {
content_h = cursor_y - child_content.top - SPACING;
}
}
let main_avail = if horizontal {
child_content.width
} else {
child_content.height
};
let used_main = if horizontal { content_w } else { content_h };
let main_known = if horizontal {
width_known
} else {
height_known
};
let scrolls_main = if horizontal {
style.overflow_x.scrollable()
} else {
style.overflow_y.scrollable()
};
let raw_free = main_avail - used_main;
let free = if main_known && (raw_free >= 0.0 || !scrolls_main) {
raw_free
} else {
0.0
};
let total_flex: f32 = vnode
.children
.iter()
.filter(|child| child.style.visible && child.style.position == Position::Flow)
.map(|child| flex_weight(child, free > 0.0))
.sum();
if free != 0.0 && total_flex > 0.0 {
let forced: Vec<(Option<f32>, Option<f32>)> = vnode
.children
.iter()
.filter(|child| {
child.style.visible && child.style.position == Position::Flow
})
.zip(child_sizes.iter())
.map(|(child, &(child_w, child_h))| {
let weight = flex_weight(child, free > 0.0);
let extra = free * weight / total_flex;
if horizontal {
let step = child.style.margin.left + child.style.margin.right;
(Some((child_w - step + extra).max(0.0)), None)
} else {
let step = child.style.margin.top + child.style.margin.bottom;
(None, Some((child_h - step + extra).max(0.0)))
}
})
.collect();
self.nodes.truncate(index + 1);
self.nodes[index].children.clear();
cursor_x = child_content.left;
cursor_y = child_content.top;
content_w = 0.0;
content_h = 0.0;
let mut visible = vnode
.children
.iter()
.filter(|child| {
child.style.visible && child.style.position == Position::Flow
})
.zip(forced.iter());
for (child, &(forced_w, forced_h)) in &mut visible {
let (child_w, child_h) = self.place(
child,
Point::new(cursor_x, cursor_y),
FlowContext {
forced_w,
forced_h,
..child_flow
},
Some(index),
metrics,
Inherited {
size: font_size,
color: font_color,
white_space,
},
);
if horizontal {
cursor_x += child_w + SPACING;
content_h = content_h.max(child_h);
} else {
cursor_y += child_h + SPACING;
content_w = content_w.max(child_w);
}
}
if any_flow {
if horizontal {
content_w = cursor_x - child_content.left - SPACING;
} else {
content_h = cursor_y - child_content.top - SPACING;
}
}
}
let w = if is_head {
if let Some(fw) = flow.forced_w {
fw
} else {
content_w + inset.horizontal()
}
} else {
flow.forced_w.unwrap_or(if root {
avail_w
} else if stretch_w {
style.resolve_width(avail_w, avail_w)
} else {
style.resolve_width(content_w + inset.horizontal(), avail_w)
})
};
let h = flow.forced_h.unwrap_or(if root {
avail_h
} else if stretch_h {
style.resolve_height(avail_h, avail_h)
} else {
style.resolve_height(content_h + inset.vertical(), avail_h)
});
self.nodes[index].rect = Rect::new(flow_x, flow_y, w, h);
let content = ContentBox::from_rect(self.nodes[index].rect, inset);
if is_head {
let dx = content.left - measure_content.left;
let dy = content.top - measure_content.top;
for ci in self.nodes[index].children.clone() {
let lnode = &mut self.nodes[ci];
lnode.rect.x += dx;
lnode.rect.y += dy;
if matches!(lnode.kind, NodeKind::Text { .. })
&& matches!(lnode.style.width, Size::Percent(_))
{
lnode.rect.w = (content.width).max(0.0);
}
}
}
if horizontal && !height_known && style.align_items == Align::Start {
(content_w, content_h) = self.stretch_row_children(
vnode,
index,
content,
metrics,
Inherited {
size: font_size,
color: font_color,
white_space,
},
);
}
for child in &vnode.children {
if !child.style.visible || child.style.position != Position::Absolute {
continue;
}
self.place(
child,
Point::new(content.left, content.top),
FlowContext {
content,
width_known: true,
height_known: true,
stretch: None,
forced_w: None,
forced_h: None,
},
Some(index),
metrics,
Inherited {
size: font_size,
color: font_color,
white_space,
},
);
}
self.nodes[index].scroll_width = (content_w + inset.horizontal()).max(w);
self.nodes[index].scroll_height = (content_h + inset.vertical()).max(h);
let used_main = if horizontal { content_w } else { content_h };
let cross_attr = if horizontal {
"position-y"
} else {
"position-x"
};
let explicit_cross: Vec<bool> = vnode
.children
.iter()
.filter(|child| child.style.visible && child.style.position == Position::Flow)
.map(|child| child.attr(cross_attr).is_some())
.collect();
self.align_container_content(
index,
content,
horizontal,
used_main,
&explicit_cross,
);
self.align_children(index, content);
(w, h)
}
};
if !is_container {
self.nodes[start].scroll_width = w;
self.nodes[start].scroll_height = h;
}
self.nodes[start].style.font_size = Some(font_size);
self.nodes[start].style.white_space = Some(white_space);
if let Some(color) = font_color
&& self.nodes[start].style.font_color.is_none()
{
self.nodes[start].style.font_color = Some(color);
}
(w + step_w, h + step_h)
}
fn stretch_row_children(
&mut self,
vnode: &VElement,
index: usize,
content: ContentBox,
metrics: &dyn TextMetrics,
inherited: Inherited,
) -> (f32, f32) {
let Inherited {
size: font_size,
color: font_color,
white_space,
} = inherited;
let needed = self.nodes[index].children.iter().any(|&child| {
let node = &self.nodes[child];
let explicit_position = vnode
.children
.iter()
.find(|entry| entry.key == node.key)
.is_some_and(|entry| entry.attr("position-y").is_some());
let stretch = node.style.height == Size::Auto
&& node.style.align_y == Align::Start
&& !explicit_position;
stretch && (content.height - node.style.margin.vertical() - node.rect.h).abs() > 0.5
});
if !needed {
return self.row_content_size(index);
}
let saved: Vec<(&'static str, f32)> = self.nodes[index]
.children
.iter()
.map(|&child| (self.nodes[child].key, self.nodes[child].rect.w))
.collect();
self.nodes.truncate(index + 1);
self.nodes[index].children.clear();
let mut cursor_x = content.left;
for child in &vnode.children {
if !child.style.visible || child.style.position == Position::Absolute {
continue;
}
let Some(&(_, width)) = saved.iter().find(|(key, _)| *key == child.key) else {
continue;
};
let explicit_position = child.attr("position-y").is_some();
let height = if child.style.height == Size::Auto
&& child.style.align_y == Align::Start
&& !explicit_position
{
Some((content.height - child.style.margin.vertical()).max(0.0))
} else {
None
};
let (child_w, _) = self.place(
child,
Point::new(cursor_x, content.top),
FlowContext {
content,
width_known: true,
height_known: true,
stretch: None,
forced_w: Some(width),
forced_h: height,
},
Some(index),
metrics,
Inherited {
size: font_size,
color: font_color,
white_space,
},
);
cursor_x += child_w + SPACING;
}
self.row_content_size(index)
}
fn row_content_size(&self, index: usize) -> (f32, f32) {
let children = &self.nodes[index].children;
let mut width = 0.0_f32;
let mut height = 0.0_f32;
for &child in children {
let node = &self.nodes[child];
width += node.rect.w + node.style.margin.horizontal();
height = height.max(node.rect.h + node.style.margin.vertical());
}
if children.len() > 1 {
width += SPACING * (children.len() - 1) as f32;
}
(width, height)
}
fn align_container_content(
&mut self,
index: usize,
content: ContentBox,
horizontal: bool,
used_main: f32,
explicit_cross: &[bool],
) {
let style = self.nodes[index].style;
let justify = style.justify_content;
let align_items = style.align_items;
if justify == Align::Start && align_items == Align::Start {
return;
}
let (main_avail, cross_avail) = if horizontal {
(content.width, content.height)
} else {
(content.height, content.width)
};
let free_main = main_avail - used_main;
let delta_main = match justify {
Align::Start => 0.0,
Align::Center => free_main / 2.0,
Align::End => free_main,
};
let children = self.nodes[index].children.clone();
let end = self.nodes.len();
let mut flow_position = 0;
for (position, &child) in children.iter().enumerate() {
if self.nodes[child].style.position == Position::Absolute {
continue;
}
let child_end = children.get(position + 1).copied().unwrap_or(end);
let rect = self.nodes[child].rect;
let explicit = explicit_cross.get(flow_position).copied().unwrap_or(false);
flow_position += 1;
let delta_cross = if explicit {
0.0
} else {
let free_cross = if horizontal {
cross_avail - rect.h
} else {
cross_avail - rect.w
};
match align_items {
Align::Start => 0.0,
Align::Center => free_cross / 2.0,
Align::End => free_cross,
}
};
let (dx, dy) = if horizontal {
(delta_main, delta_cross)
} else {
(delta_cross, delta_main)
};
if dx != 0.0 || dy != 0.0 {
for node in &mut self.nodes[child..child_end] {
node.rect.x += dx;
node.rect.y += dy;
}
}
}
}
fn align_children(&mut self, index: usize, content: ContentBox) {
let children = self.nodes[index].children.clone();
let end = self.nodes.len();
for (position, &child) in children.iter().enumerate() {
let child_end = children.get(position + 1).copied().unwrap_or(end);
let rect = self.nodes[child].rect;
let style = self.nodes[child].style;
let final_x = aligned(
rect.x,
rect.w,
content.left,
content.width,
style.align_x,
style.offset_x,
);
let final_y = aligned(
rect.y,
rect.h,
content.top,
content.height,
style.align_y,
style.offset_y,
);
let dx = final_x - rect.x;
let dy = final_y - rect.y;
if dx != 0.0 || dy != 0.0 {
for node in &mut self.nodes[child..child_end] {
node.rect.x += dx;
node.rect.y += dy;
}
}
}
}
fn reorder_by_z(&mut self) {
if self.nodes.is_empty() {
return;
}
let z: Vec<i32> = self.nodes.iter().map(|node| node.style.z).collect();
for node in &mut self.nodes {
node.children.sort_by_key(|&child| z[child]);
}
let mut order = Vec::with_capacity(self.nodes.len());
let mut stack = vec![0usize];
while let Some(index) = stack.pop() {
order.push(index);
for &child in self.nodes[index].children.iter().rev() {
stack.push(child);
}
}
if order.len() != self.nodes.len() {
return;
}
let mut remap = vec![0usize; self.nodes.len()];
for (new, &old) in order.iter().enumerate() {
remap[old] = new;
}
let mut old_nodes: Vec<Option<PlacedNode>> = self.nodes.drain(..).map(Some).collect();
let mut nodes = Vec::with_capacity(order.len());
for &old in &order {
let Some(mut node) = old_nodes[old].take() else {
return;
};
for child in &mut node.children {
*child = remap[*child];
}
nodes.push(node);
}
self.nodes = nodes;
}
fn push(&mut self, vnode: &VElement, rect: Rect, parent: Option<usize>) -> usize {
self.nodes.push(PlacedNode {
key: vnode.key,
kind: vnode.kind.clone(),
style: vnode.style,
rect,
children: Vec::new(),
overflow_x: vnode.style.overflow_x,
overflow_y: vnode.style.overflow_y,
scroll_width: rect.w,
scroll_height: rect.h,
scroll_x: 0.0,
scroll_y: 0.0,
clip_rect: None,
});
let index = self.nodes.len() - 1;
if let Some(parent) = parent {
self.nodes[parent].children.push(index);
}
index
}
}
fn is_disabled(kind: &NodeKind) -> bool {
match kind {
NodeKind::Button { disabled, .. } | NodeKind::TextField { disabled, .. } => *disabled,
#[cfg(feature = "elements-checkbox")]
NodeKind::Checkbox { disabled, .. } => *disabled,
#[cfg(feature = "elements-radio")]
NodeKind::Radio { disabled, .. } => *disabled,
#[cfg(feature = "elements-select")]
NodeKind::Select { disabled, .. } => *disabled,
_ => false,
}
}
pub fn damage_regions(
old_rects: &HashMap<&'static str, Rect>,
new: &Layout,
changed: &BTreeSet<&'static str>,
) -> Vec<Rect> {
let mut regions: HashMap<&'static str, Rect> = HashMap::new();
for node in new.nodes() {
let previous = old_rects.get(node.key).copied();
let moved = previous != Some(node.rect);
let content_changed = changed.contains(node.key);
if moved || content_changed {
let union = match previous {
Some(old) => old.union(node.rect),
None => node.rect,
};
regions
.entry(node.key)
.and_modify(|rect| *rect = rect.union(union))
.or_insert(union);
}
}
for (&key, &rect) in old_rects {
if new.rect_of(key).is_none() {
regions
.entry(key)
.and_modify(|r| *r = r.union(rect))
.or_insert(rect);
}
}
regions.into_values().collect()
}
#[cfg(feature = "window")]
pub(crate) fn coalesce_regions(regions: Vec<Rect>) -> Vec<Rect> {
let mut merged = Vec::<Rect>::with_capacity(regions.len());
for rect in regions {
if rect.w <= 0.0 || rect.h <= 0.0 {
continue;
}
let mut current = rect;
loop {
let mut found = false;
let mut index = 0;
while index < merged.len() {
if overlaps(merged[index], current) {
current = merged.swap_remove(index).union(current);
found = true;
} else {
index += 1;
}
}
if !found {
break;
}
}
merged.push(current);
}
merged
}
#[cfg(feature = "window")]
fn overlaps(a: Rect, b: Rect) -> bool {
a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h
}
pub fn snapshot_rects(layout: &Layout) -> HashMap<&'static str, Rect> {
layout
.nodes()
.iter()
.map(|node| (node.key, node.rect))
.collect()
}
fn flex_weight(child: &VElement, growing: bool) -> f32 {
if growing {
child.style.flex_grow
} else if matches!(child.kind, NodeKind::TitleBar) {
0.0
} else {
child.style.flex_shrink
}
}
fn aligned(
flow: f32,
size: f32,
bound_start: f32,
bound_size: f32,
align: Align,
offset: Offset,
) -> f32 {
let base = match align {
Align::Start => flow,
Align::Center => bound_start + (bound_size - size) / 2.0,
Align::End => bound_start + bound_size - size,
};
base + offset.resolve(bound_size)
}
#[cfg(debug_assertions)]
fn debug_trace(vnode: &VElement, rect: Rect, flow: FlowContext) {
let dbg = env::var("LIGHT_DEBUG_LAYOUT").unwrap_or_default();
if dbg != "1" && dbg != "true" && dbg != "on" {
return;
}
eprintln!(
"[LAYOUT] tag={} kind={:?} key={} parent_content=(x={} y={} w={} h={}) pos_x={:?} pos_y={:?} resolved_rect=(x={} y={} w={} h={})",
vnode.tag,
vnode.kind,
vnode.key,
flow.content.left,
flow.content.top,
flow.content.width,
flow.content.height,
vnode.style.align_x,
vnode.style.align_y,
rect.x,
rect.y,
rect.w,
rect.h
);
}
#[cfg(all(test, feature = "window"))]
mod tests {
use super::{Rect, coalesce_regions};
#[test]
fn coalesces_overlapping_regions() {
let regions = vec![
Rect::new(0.0, 0.0, 10.0, 10.0),
Rect::new(5.0, 5.0, 20.0, 20.0),
Rect::new(40.0, 40.0, 5.0, 5.0),
];
let merged = coalesce_regions(regions);
assert_eq!(merged.len(), 2);
assert!(merged.contains(&Rect::new(0.0, 0.0, 25.0, 25.0)));
assert!(merged.contains(&Rect::new(40.0, 40.0, 5.0, 5.0)));
}
#[test]
fn coalesces_union_induced_overlaps() {
let regions = vec![
Rect::new(0.0, 0.0, 10.0, 10.0),
Rect::new(20.0, 0.0, 10.0, 10.0),
Rect::new(5.0, 5.0, 20.0, 3.0),
];
let merged = coalesce_regions(regions);
assert_eq!(merged, vec![Rect::new(0.0, 0.0, 30.0, 10.0)]);
}
#[test]
fn drops_empty_regions() {
assert!(coalesce_regions(vec![Rect::new(0.0, 0.0, 0.0, 10.0)]).is_empty());
}
}