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
+38
View File
@@ -65,6 +65,44 @@ pub struct DisplayList {
}
impl DisplayList {
pub fn scale(&mut self, scale: f32) {
for command in &mut self.commands {
let rect = match command {
DrawCommand::WrappedText { rect, style, .. } => {
*style = style.scaled(scale);
Some(rect)
}
DrawCommand::PushClip(rect) => Some(rect),
DrawCommand::FillRoundedRect { rect, radius, .. } => {
*radius *= scale;
Some(rect)
}
DrawCommand::StrokeRoundedRect {
rect,
radius,
width,
..
} => {
*radius *= scale;
*width *= scale;
Some(rect)
}
DrawCommand::Text { origin, style, .. } => {
origin.x *= scale;
origin.y *= scale;
*style = style.scaled(scale);
None
}
_ => None,
};
if let Some(rect) = rect {
rect.x *= scale;
rect.y *= scale;
rect.width *= scale;
rect.height *= scale;
}
}
}
pub fn push(&mut self, command: DrawCommand) {
self.commands.push(command);
}
+1 -1
View File
@@ -1,4 +1,4 @@
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Point {
pub x: f32,
pub y: f32,
+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,
+1
View File
@@ -3,6 +3,7 @@ pub(crate) mod display_list;
pub(crate) mod geometry;
pub(crate) mod layout;
pub(crate) mod native_visual;
pub(crate) mod page;
pub(crate) mod scene;
pub(crate) mod scroll;
pub(crate) mod software_renderer;
+2 -2
View File
@@ -202,8 +202,8 @@ impl NativeCallable for Method {
)?)
}
Query::Arrange => {
let x = number(cx, input, "x", false)?;
let y = number(cx, input, "y", false)?;
let x = number(cx, input, "x", true)?;
let y = number(cx, input, "y", true)?;
let width = number(cx, input, "width", false)?;
let height = number(cx, input, "height", false)?;
if x + width > MAX_DIMENSION as f32 || y + height > MAX_DIMENSION as f32 {
+190
View File
@@ -0,0 +1,190 @@
//! Page space is independent of the native window. The camera is applied once
//! for painting and inverted once for input; layout and Mounts stay in world units.
use crate::{geometry::Point, layout::Size};
use endor_core::VmError;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Mode {
#[default]
Window,
Vertical,
Horizontal,
Canvas,
}
impl Mode {
pub fn name(self) -> &'static str {
match self {
Self::Window => "window",
Self::Vertical => "vertical",
Self::Horizontal => "horizontal",
Self::Canvas => "canvas",
}
}
pub fn parse(name: &str) -> Result<Self, VmError> {
match name {
"window" => Ok(Self::Window),
"vertical" => Ok(Self::Vertical),
"horizontal" => Ok(Self::Horizontal),
"canvas" => Ok(Self::Canvas),
_ => Err(VmError::InvalidArgument(
"Page.mode expects window, vertical, horizontal or canvas",
)),
}
}
pub fn constraints(self, viewport: Option<Size>) -> [f32; 4] {
let Some(size) = viewport else {
return crate::layout::root_constraints(None);
};
let limit = crate::ui_bridge::MAX_DIMENSION as f32;
match self {
Self::Window => [size.width, size.height, size.width, size.height],
Self::Vertical => [size.width, size.height, size.width, limit],
Self::Horizontal => [size.width, size.height, limit, size.height],
Self::Canvas => [0.0, 0.0, limit, limit],
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Surface {
pub mode: Mode,
pub page: String,
pub pan: Point,
#[serde(default = "unit")]
pub zoom: f32,
pub extent: Size,
}
fn unit() -> f32 {
1.0
}
impl Default for Surface {
fn default() -> Self {
Self {
mode: Mode::Window,
page: String::new(),
pan: Point::default(),
zoom: 1.0,
extent: Size::default(),
}
}
}
impl Surface {
pub fn new(mode: Mode) -> Self {
Self {
mode,
zoom: 1.0,
..Self::default()
}
}
pub fn to_world(&self, point: Point) -> Point {
Point::new(
point.x / self.zoom + self.pan.x,
point.y / self.zoom + self.pan.y,
)
}
pub fn to_view(&self, point: Point) -> Point {
Point::new(
(point.x - self.pan.x) * self.zoom,
(point.y - self.pan.y) * self.zoom,
)
}
pub fn contains(&self, point: [f32; 2], viewport: Option<Size>) -> bool {
let point = self.to_view(Point::new(point[0], point[1]));
point.x >= 0.0
&& point.y >= 0.0
&& viewport.is_none_or(|size| point.x < size.width && point.y < size.height)
}
pub fn clamp(&mut self, viewport: Size) {
match self.mode {
Mode::Window => {
self.pan = Point::default();
self.zoom = 1.0;
}
Mode::Vertical => {
self.zoom = 1.0;
self.pan.x = 0.0;
self.pan.y = self
.pan
.y
.clamp(0.0, (self.extent.height - viewport.height).max(0.0));
}
Mode::Horizontal => {
self.zoom = 1.0;
self.pan.y = 0.0;
self.pan.x = self
.pan
.x
.clamp(0.0, (self.extent.width - viewport.width).max(0.0));
}
Mode::Canvas => {}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn camera_inverse_and_cursor_anchored_zoom_preserve_world_position() {
let mut view = Surface::new(Mode::Canvas);
view.pan = Point::new(-240.0, 120.0);
view.zoom = 0.75;
let pointer = Point::new(300.0, 180.0);
let world = view.to_world(pointer);
assert_eq!(view.to_view(world), pointer);
view.zoom = 2.0;
view.pan = Point::new(
world.x - pointer.x / view.zoom,
world.y - pointer.y / view.zoom,
);
assert_eq!(view.to_view(world), pointer);
assert!(view.contains(
[world.x, world.y],
Some(Size {
width: 500.0,
height: 400.0
})
));
assert!(!view.contains(
[-1000.0, -1000.0],
Some(Size {
width: 500.0,
height: 400.0
})
));
}
#[test]
fn finite_pages_clamp_after_content_shrinks() {
let mut view = Surface::new(Mode::Vertical);
view.pan = Point::new(25.0, 900.0);
view.extent = Size {
width: 400.0,
height: 800.0,
};
view.clamp(Size {
width: 400.0,
height: 600.0,
});
assert_eq!(view.pan, Point::new(0.0, 200.0));
view.extent.height = 100.0;
view.clamp(Size {
width: 400.0,
height: 600.0,
});
assert_eq!(view.pan.y, 0.0);
}
}
+38 -52
View File
@@ -1,8 +1,9 @@
use crate::{
activity::Activity,
developer_tools::{self, DeveloperToolsLayout, Workspace as DeveloperToolsState},
display_list::DisplayList,
display_list::{DisplayList, DrawCommand},
document::RenderDocument,
geometry::{Point, Rect},
};
#[derive(Clone, Debug, Default)]
@@ -17,7 +18,7 @@ pub fn build_scene(
document: &RenderDocument,
developer_tools_state: &mut DeveloperToolsState,
activity: &Activity,
tab_chrome: crate::tabs::Chrome,
sidebar: &mut crate::sidebar::State,
) -> DisplayScene {
let tool = document
.frame
@@ -27,69 +28,54 @@ pub fn build_scene(
.and_then(|tab| tab.developer_tool.as_deref());
developer_tools_state.select(tool);
developer_tools_state.document_updated(document);
let mut display_list = document.display_list(scale).unwrap_or_else(|_| {
let left = sidebar.left();
let surface = &document.frame.surface;
let content = Rect::new(
left * scale,
0.0,
(width as f32 - left * scale).max(0.0),
height as f32,
);
let zoom = if tool.is_some() { 1.0 } else { surface.zoom };
let mut page = document.display_list(scale * zoom).unwrap_or_else(|_| {
RenderDocument::default()
.display_list(scale)
.expect("default document is valid")
});
let offset = crate::tabs::height(&document.frame.tabs);
let inset = crate::tabs::INSET;
let left = crate::tabs::RAIL;
let content = crate::geometry::Rect::new(
left * scale,
offset * scale,
(width as f32 - (left + inset) * scale).max(0.0),
(height as f32 - (offset + inset) * scale).max(0.0),
);
display_list.translate(crate::geometry::Point::new(left * scale, offset * scale));
let mut framed = DisplayList::default();
use crate::display_list::DrawCommand;
framed.push(DrawCommand::Clear(crate::system_ui::theme::BAR));
framed.push(DrawCommand::PushClip(content));
for command in display_list.commands() {
if let DrawCommand::Clear(color) = command {
framed.push(DrawCommand::FillRoundedRect {
rect: content,
radius: 10.0 * scale,
color: *color,
});
} else {
framed.push(command.clone());
page.translate(Point::new(
(left - surface.pan.x * zoom) * scale,
-surface.pan.y * zoom * scale,
));
let mut display_list = DisplayList::default();
display_list.push(DrawCommand::Clear(
crate::document::parse_color(&document.frame.background)
.unwrap_or(crate::system_ui::theme::BG),
));
display_list.push(DrawCommand::PushClip(content));
for command in page.commands() {
if !matches!(command, DrawCommand::Clear(_)) {
display_list.push(command.clone());
}
}
framed.push(DrawCommand::PopClip);
let mut display_list = framed;
crate::tabs::draw(
tab_chrome.offset,
&mut display_list,
&document.frame.tabs,
width as f32 / scale,
scale,
tab_chrome.pointer,
&tab_chrome.visual,
);
if let Some(index) = tab_chrome.insertion {
crate::tabs::draw_insertion(
tab_chrome.offset,
&mut display_list,
&document.frame.tabs,
width as f32 / scale,
index,
scale,
);
}
display_list.push(DrawCommand::PopClip);
let (mut developer_list, mut developer_layout) = developer_tools::build(
width.saturating_sub(((left + inset) * scale) as u32),
height.saturating_sub(((offset + inset) * scale) as u32),
width.saturating_sub((left * scale) as u32),
height,
scale,
document,
developer_tools_state,
activity,
);
developer_list.translate(crate::geometry::Point::new(left * scale, offset * scale));
developer_layout.translate(left, offset);
developer_list.translate(Point::new(left * scale, 0.0));
developer_layout.translate(left, 0.0);
display_list.extend(developer_list);
let mut chrome = sidebar.draw(
&document.frame.tabs,
height as f32 / scale,
developer_tools_state.accent,
);
chrome.scale(scale);
display_list.extend(chrome);
DisplayScene {
display_list,
layout: SceneLayout {
+3 -3
View File
@@ -458,8 +458,8 @@ impl NativeCallable for Collect {
return Err(invalid("duplicate arrangement slot"));
}
let bounds = [
number(cx, *item, "x", false)?,
number(cx, *item, "y", false)?,
number(cx, *item, "x", true)?,
number(cx, *item, "y", true)?,
number(cx, *item, "width", false)?,
number(cx, *item, "height", false)?,
];
@@ -876,7 +876,7 @@ pub fn start(
children[indices[&parent]].push(index);
}
}
let mut constraints = vec![layout::root_constraints(runtime.viewport); nodes.len()];
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] {