feat: add page surfaces and collapsible sidebar

This commit is contained in:
2026-11-10 16:18:47 +08:00
parent e0eff7755f
commit 82f9256a84
35 changed files with 1480 additions and 1751 deletions
+41 -10
View File
@@ -59,7 +59,7 @@ impl Default for FlexPolicy {
}
}
#[derive(Default, Debug, Clone, Copy, PartialEq)]
#[derive(Default, Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Size {
pub width: f32,
pub height: f32,
@@ -90,6 +90,7 @@ pub struct LayoutRuntime {
pub settle_passes: usize,
pub slices: usize,
pub viewport: Option<Size>,
pub page_mode: Option<crate::page::Mode>,
}
struct Item<'a> {
@@ -107,6 +108,13 @@ fn bounded(value: f32) -> Result<f32> {
Ok(value)
}
fn coordinate(value: f32) -> Result<f32> {
if !value.is_finite() || value.abs() > MAX_DIMENSION as f32 {
bail!("UI coordinate exceeds the 1000000 limit");
}
Ok(value)
}
pub fn root_constraints(viewport: Option<Size>) -> [f32; 4] {
let size = viewport.unwrap_or(Size {
width: MAX_DIMENSION as f32,
@@ -115,6 +123,9 @@ pub fn root_constraints(viewport: Option<Size>) -> [f32; 4] {
[0.0, 0.0, size.width, size.height]
}
pub fn child_constraints(node: &UiNode, parent: [f32; 4]) -> [f32; 4] {
if matches!(node.kind, UiKind::Canvas { .. }) {
return root_constraints(None);
}
if let UiKind::Scroll { width, height, .. } = node.kind {
return arranged_child_constraints(
node,
@@ -229,6 +240,14 @@ fn measure_shape(shape: &Shape) -> Result<Size> {
fn shape(node: &UiNode, children: Vec<Size>, constraints: [f32; 4]) -> Result<Shape> {
Ok(match &node.kind {
UiKind::Canvas { positions, .. } => {
let mut size = Size::default();
for (position, child) in positions.iter().zip(children) {
size.width = size.width.max(position[0] + child.width);
size.height = size.height.max(position[1] + child.height);
}
Shape::Rect(size.width, size.height)
}
UiKind::Text { value, size, .. } => Shape::Text(value.clone(), *size, constraints[2]),
UiKind::Rect { width, height, .. } => Shape::Rect(*width, *height),
UiKind::Spacer(size) => Shape::Rect(*size, *size),
@@ -399,6 +418,12 @@ pub fn arrange_native(
bounds: Size,
children: &[Size],
) -> Result<Vec<(f32, f32)>> {
if let UiKind::Canvas { positions, .. } = &node.kind {
return Ok(positions
.iter()
.map(|point| (x + point[0], y + point[1]))
.collect());
}
let Some((row, padding, gap)) = container(node) else {
return Ok(vec![]);
};
@@ -427,10 +452,10 @@ pub fn arrange_native(
} else {
(x + padding, y + cursor)
};
bounded(point.0)?;
bounded(point.1)?;
bounded(point.0 + child.width)?;
bounded(point.1 + child.height)?;
coordinate(point.0)?;
coordinate(point.1)?;
coordinate(point.0 + child.width)?;
coordinate(point.1 + child.height)?;
points.push(point);
cursor += if row { child.width } else { child.height };
cursor += gap + distributed_gap;
@@ -439,6 +464,12 @@ pub fn arrange_native(
}
impl LayoutRuntime {
pub fn root_constraints(&self) -> [f32; 4] {
self.page_mode.map_or_else(
|| root_constraints(self.viewport),
|mode| mode.constraints(self.viewport),
)
}
pub fn invalidate(&mut self, block: &str) {
self.sizes.remove(block);
}
@@ -495,7 +526,7 @@ impl LayoutRuntime {
self.settle_passes = usize::from(!structural);
self.slices = usize::from(!structural);
let mut items = Vec::<Item<'_>>::new();
let mut work = vec![(root, None::<usize>, 0, root_constraints(self.viewport))];
let mut work = vec![(root, None::<usize>, 0, self.root_constraints())];
let mut used = HashSet::new();
while let Some((node, parent, slot, constraints)) = work.pop() {
if items.len() >= MAX_LAYOUT_VISITS {
@@ -568,10 +599,10 @@ impl LayoutRuntime {
self.arranged += usize::from(!structural);
let (x, y) = points[index];
let size = sizes[index];
bounded(x)?;
bounded(y)?;
bounded(x + size.width)?;
bounded(y + size.height)?;
coordinate(x)?;
coordinate(y)?;
coordinate(x + size.width)?;
coordinate(y + size.height)?;
mounts.push(MountGeometry {
id: item.id,
clip: None,