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
+29 -19
View File
@@ -2,6 +2,7 @@
mod bootstrap;
mod calls;
mod developer_pages;
mod page_input;
mod signatures;
mod tree;
@@ -658,6 +659,7 @@ pub enum LayoutProgress {
}
struct LayoutContext {
surface: crate::page::Surface,
root: Arc<UiNode>,
tabs: Vec<crate::tabs::Tab>,
full_viewport: Option<crate::layout::Size>,
@@ -1189,13 +1191,16 @@ impl UiBridge {
vm.release_value(page)?;
return Ok(());
}
if matches!(input.kind.as_str(), "page-pan" | "page-zoom") {
return self.dispatch_page_view(vm, input);
}
if matches!(
input.kind.as_str(),
"tab-activate" | "tab-close" | "tab-detach" | "tab-drop"
) {
return self.dispatch_tab(vm, input);
}
let (weak, desktop, candidates, viewport) = {
let (weak, desktop, candidates, viewport, surface) = {
let state = self
.session
.lock()
@@ -1211,13 +1216,8 @@ impl UiBridge {
.ok_or_else(|| anyhow::anyhow!("Mount is no longer active"))?,
state.desktop,
state.mounts.ordered(),
state.layout.viewport.map(|size| crate::layout::Size {
width: (size.width - crate::tabs::RAIL - crate::tabs::INSET).max(0.0),
height: (size.height
- crate::tabs::INSET
- crate::tabs::height(&state.last_tabs))
.max(0.0),
}),
state.layout.viewport,
state.surface.clone(),
)
};
if input.kind == "scroll" {
@@ -1231,8 +1231,7 @@ impl UiBridge {
.ok_or_else(|| anyhow::anyhow!("scroll has no pointer"))?;
if !data.mounted
|| data.version != input.layout_version
|| viewport
.is_some_and(|size| point[0] >= size.width || point[1] >= size.height)
|| !surface.contains(point, viewport)
|| !data
.geometry
.contains(crate::geometry::Point::new(point[0], point[1]))
@@ -1265,11 +1264,10 @@ impl UiBridge {
return result;
}
let mount = if let (Some(press), Some(release)) = (input.press_point, input.point) {
if viewport.is_some_and(|size| {
[press, release]
.iter()
.any(|point| point[0] >= size.width || point[1] >= size.height)
}) {
if [press, release]
.iter()
.any(|point| !surface.contains(*point, viewport))
{
return Ok(());
}
let Some(mount) = crate::source_hit::resolve(vm, desktop, candidates, press, release)?
@@ -1477,6 +1475,7 @@ impl UiBridge {
state.force_frame = false;
state.last_snapshot = snapshot;
state.last_tabs = frame.tabs.clone();
state.surface = frame.surface.clone();
state.published_version = frame.layout_version;
// Hooks may call Page/container APIs; never hold the session mutex
// across user code. Publish only after the refresh loop settles.
@@ -1649,10 +1648,8 @@ impl UiBridge {
project_tabs(vm, &mut state)?
};
let full_viewport = layout.viewport;
if let Some(size) = &mut layout.viewport {
size.width = (size.width - crate::tabs::RAIL - crate::tabs::INSET).max(0.0);
size.height = (size.height - crate::tabs::height(&tabs) - crate::tabs::INSET).max(0.0);
}
let surface = root.surface.clone().unwrap_or_default();
layout.page_mode = root.surface.as_ref().map(|surface| surface.mode);
{
let state = self
.session
@@ -1688,6 +1685,7 @@ impl UiBridge {
(hooks, state.mounts.ordered(), checkpoint)
};
let context = LayoutContext {
surface,
root: root.clone(),
tabs,
full_viewport,
@@ -1736,6 +1734,7 @@ impl UiBridge {
fn discard_layout_context(&self, vm: &mut Vm, context: LayoutContext) -> Result<()> {
let LayoutContext {
surface: _,
desktop,
window,
hooks,
@@ -1824,6 +1823,7 @@ impl UiBridge {
draft: crate::source_layout::Draft,
) -> Result<crate::document::Frame> {
let LayoutContext {
mut surface,
root,
tabs,
full_viewport,
@@ -1851,6 +1851,16 @@ impl UiBridge {
// after either layout path has produced the final page geometry.
frame.tabs = tabs;
frame.window = window;
if let Some(root) = frame.mounts.first() {
surface.extent = crate::layout::Size {
width: root.width,
height: root.height,
};
}
if let Some(viewport) = full_viewport {
surface.clamp(viewport);
}
frame.surface = surface;
// Capture versions before lifecycle hooks can change a dependency.
let desktops = self
.session
+66
View File
@@ -0,0 +1,66 @@
//! Camera mutations run on the owning VM worker, just like other page input.
use super::*;
impl UiBridge {
pub(super) fn dispatch_page_view(
&self,
vm: &mut Vm,
input: &crate::events::Input,
) -> Result<()> {
let (page, mut surface, viewport) = {
let state = self
.session
.lock()
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?;
anyhow::ensure!(
state.published_version == input.layout_version,
"obsolete page view"
);
(
state
.active
.ok_or_else(|| anyhow::anyhow!("no active Page"))?,
state.surface.clone(),
state.layout.viewport,
)
};
let delta = input
.delta
.ok_or_else(|| anyhow::anyhow!("page view requires delta"))?;
if input.kind == "page-zoom" {
anyhow::ensure!(
surface.mode == crate::page::Mode::Canvas,
"zoom requires canvas mode"
);
let pointer = input
.point
.ok_or_else(|| anyhow::anyhow!("zoom requires anchor"))?;
let pointer = crate::geometry::Point::new(pointer[0], pointer[1]);
anyhow::ensure!(
pointer.x >= 0.0
&& pointer.y >= 0.0
&& viewport.is_none_or(|v| pointer.x < v.width && pointer.y < v.height),
"zoom anchor is outside viewport"
);
let world = surface.to_world(pointer);
surface.zoom = (surface.zoom * delta[0]).clamp(0.1, 4.0);
surface.pan = crate::geometry::Point::new(
world.x - pointer.x / surface.zoom,
world.y - pointer.y / surface.zoom,
);
} else {
surface.pan.x += delta[0] / surface.zoom;
surface.pan.y += delta[1] / surface.zoom;
}
if let Some(viewport) = viewport {
surface.clamp(viewport);
}
let limit = MAX_DIMENSION as f32;
surface.pan.x = surface.pan.x.clamp(-limit, limit);
surface.pan.y = surface.pan.y.clamp(-limit, limit);
vm.set_attr(page, "panX", crate::native_visual::value(surface.pan.x))?;
vm.set_attr(page, "panY", crate::native_visual::value(surface.pan.y))?;
vm.set_attr(page, "zoom", crate::native_visual::value(surface.zoom))?;
Ok(())
}
}
+88 -57
View File
@@ -1,39 +1,63 @@
// Tessera's start page is an ordinary Endor Page, including its layout and actions.
// This module has its own globals; creating tabs does not replace user bindings.
class StartPage: Page {
function measure(self: StartPage, mount: Mount, constraints: Constraints): MeasureResult {
return MeasureResult(constraints.maxWidth, constraints.maxHeight)
// The page, cards and actions belong to Endor. The host only owns its sidebar.
class Note: Column {
tint: String
function measure(self: Note, mount: Mount, constraints: Constraints): MeasureResult {
measured = Column.measure(self, mount, constraints)
return MeasureResult(224, measured.height)
}
function paint(self: Note, mount: Mount, geometry: Bounds): DisplayList {
fill = PaintCommand()
fill.kind = "roundedRect"
fill.x = geometry.x
fill.y = geometry.y
fill.width = geometry.width
fill.height = geometry.height
fill.radius = 14
fill.color = self.tint
return DisplayList(ObjectList(fill))
}
}
class StartRow: Row {
function measure(self: StartRow, mount: Mount, constraints: Constraints): MeasureResult {
measured = Row.measure(self, mount, constraints)
return MeasureResult(constraints.maxWidth, measured.height)
}
}
// Quiet action rows retain native hit targets and keyboard focus without card chrome.
class StartButton: Button {
function measure(self: StartButton, mount: Mount, constraints: Constraints): MeasureResult {
return MeasureResult(360, 40)
}
function paint(self: StartButton, mount: Mount, geometry: Bounds): DisplayList {
class QuietButton: Button {
function paint(self: QuietButton, mount: Mount, geometry: Bounds): DisplayList {
return DisplayList(ObjectList())
}
}
class StartLine: Row {
function measure(self: StartLine, mount: Mount, constraints: Constraints): MeasureResult {
measured = Row.measure(self, mount, constraints)
return MeasureResult(360, measured.height)
}
}
class StartActions {
home: Page
function blank(self: StartActions, event: Event): Unit {
page = Page()
page.title = "未命名页面"
back = Button("返回开始页")
page.title = "窗口页面"
page.mode = "window"
back = Button("返回画布")
back.onClick(self.home.activate)
page.render(Column(32, 16, TextBlock("未命名页面", 2), TextBlock("一个新的 Page 对象,已加入当前窗口。"), TextBlock("用 Endor 的 page.render(...) 组织内容。"), back))
page.render(Column(56, 20, TextBlock("恰好一个窗口", 3), TextBlock("页面跟随可用区域调整。内容和工具栏由 Endor 自己组织。"), back))
}
function vertical(self: StartActions, event: Event): Unit {
page = Page()
page.title = "纵向页面"
page.mode = "vertical"
page.padding = 48
page.gap = 28
page.render(Column(0, 12, TextBlock("向下,继续生长", 3), TextBlock("宽度跟随窗口,高度随子组件增长。滚轮向下浏览。")))
page.append(Note(24, 16, TextBlock("01 / 收集想法", 2), RectBlock(176, 130, 10, "#E2EADB"), TextBlock("从一个小小的念头开始。"), "#F0F3E9"))
page.append(Note(24, 16, TextBlock("02 / 连接片段", 2), RectBlock(176, 130, 10, "#E6DBEF"), TextBlock("内容保留在同一条连续的页面里。"), "#F4EEF7"))
page.append(Note(24, 16, TextBlock("03 / 慢慢展开", 2), RectBlock(176, 130, 10, "#EDDCC5"), TextBlock("追加一个子组件,页面自然延伸。"), "#FAF2E7"))
page.append(Note(24, 16, TextBlock("04 / 留一点空白", 2), TextBlock("不必把所有想法都放在第一屏。"), "#EDF1F6"))
}
function horizontal(self: StartActions, event: Event): Unit {
page = Page()
page.title = "横向页面"
page.mode = "horizontal"
page.padding = 48
page.gap = 32
page.render(Note(24, 16, TextBlock("从左,走向右", 2), RectBlock(176, 180, 10, "#DDE8F1"), TextBlock("高度跟随窗口,宽度由子组件决定。"), "#EDF3F8"))
page.append(Note(24, 16, TextBlock("收集", 2), RectBlock(176, 210, 10, "#D7E5DA"), TextBlock("一张卡片,一个想法。"), "#EFF4EC"))
page.append(Note(24, 16, TextBlock("整理", 2), RectBlock(176, 170, 10, "#E5D9EB"), TextBlock("横向滚动,保留思考的顺序。"), "#F4EFF7"))
page.append(Note(24, 16, TextBlock("继续", 2), RectBlock(176, 230, 10, "#EFDFC7"), TextBlock("滚轮或 Shift + 滚轮浏览更多内容。"), "#FAF3E8"))
page.append(Note(24, 16, TextBlock("不止一屏", 2), TextBlock("右侧仍有空间。"), "#EEF1F5"))
}
function canvas(self: StartActions, event: Event): Unit {
page = newTab()
}
function counter(self: StartActions, event: Event): Unit {
count = ui.state(0)
@@ -41,40 +65,47 @@ class StartActions {
add.onClick(count.increment)
page = Page()
page.title = "共享对象"
page.render(Column(32, 16, TextBlock("一个对象,两个视图", 2), TextBlock("点击一次,两处展示同时更新。切换标签后,状态仍然保留。"), Row(0, 32, ui.textOf(count, "value", 3), ui.textOf(count, "value", 3)), add))
}
function compose(self: StartActions, event: Event): Unit {
label = TextBlock("对象可以被多处引用")
page = Page()
page.title = "组合页面"
page.render(Column(32, 20, TextBlock("把对象组合成界面", 2), TextBlock("Column 组织纵向内容,Row 组织横向内容。"), Row(0, 16, RectBlock(100, 64, 12, "#DCE8FF"), RectBlock(100, 64, 12, "#E3ECDC"), RectBlock(100, 64, 12, "#F3E4DB")), label, Row(0, 24, label, label)))
page.render(Column(48, 24, TextBlock("一个对象,两个视图", 2), Row(0, 32, ui.textOf(count, "value", 3), ui.textOf(count, "value", 3)), add))
}
}
function newTab(): Page {
page = StartPage()
page.title = "新标签页"
page = Page()
page.title = "自由画布"
page.mode = "canvas"
actions = StartActions(page)
blank = StartButton("+ 创建页面")
counter = StartButton("↗ 共享对象示例")
compose = StartButton("◇ 界面组合示例")
blank.on("click", actions.blank)
counter.on("click", actions.counter)
compose.on("click", actions.compose)
heading = StartLine(0, 0, TextBlock("Endor", 2, "#30343B"))
heading.mainAlign = "center"
subtitle = StartLine(0, 0, TextBlock("创建页面,或从一个示例开始。", 1, "#80858D"))
subtitle.mainAlign = "center"
newHint = StartLine(0, 0, TextBlock("新建标签页", 1, "#8A8F97"), TextBlock("⌘ T / Ctrl + T", 1, "#747B85"))
newHint.mainAlign = "space-between"
closeHint = StartLine(0, 0, TextBlock("关闭标签页", 1, "#8A8F97"), TextBlock("⌘ W / Ctrl + W", 1, "#747B85"))
closeHint.mainAlign = "space-between"
content = Column(0, 8,
heading, subtitle, Spacer(20),
blank, counter, compose, Spacer(20),
newHint, closeHint)
row = StartRow(24, 0, content)
row.mainAlign = "center"
page.mainAlign = "center"
page.render(row)
fixed = Button("窗口页面")
vertical = Button("纵向延伸")
horizontal = Button("横向延伸")
canvas = Button("新的画布")
shared = QuietButton("试试:一个对象,两个视图 ↗")
fixed.on("click", actions.blank)
vertical.on("click", actions.vertical)
horizontal.on("click", actions.horizontal)
canvas.on("click", actions.canvas)
shared.on("click", actions.counter)
hero = Column(0, 16, TextBlock("给想法,一点空间。", 3, "#303941"), TextBlock("页面可以是一扇窗口,也可以一直延伸。", 1, "#7D858C"), Spacer(8), Row(0, 10, fixed, vertical), Row(0, 10, horizontal, canvas), shared)
hero.x = 316
hero.y = 254
page.render(hero)
left = Note(24, 14, TextBlock("随手记下", 2), RectBlock(176, 100, 10, "#D5E4DA"), TextBlock("想法不必排成一条直线。"), "#EDF3E9")
left.x = 36
left.y = 92
page.append(left)
top = Note(24, 14, TextBlock("四种页面,同一个世界"), TextBlock("窗口 · 纵向 · 横向 · 自由画布", 1, "#7D858C"), "#EFF1F7")
top.x = 466
top.y = 58
page.append(top)
right = Note(24, 14, RectBlock(176, 138, 12, "#DDDAEA"), TextBlock("把片段放在一起", 2), TextBlock("位置属于组件,视角属于页面。"), "#F1EDF5")
right.x = 826
right.y = 190
page.append(right)
bottom = Note(24, 14, TextBlock("不止眼前这一屏", 2), TextBlock("触控板或滚轮平移\n空格拖动 / 鼠标中键拖动\n双指捏合 / ⌘ Ctrl + 滚轮缩放"), "#F7F0E5")
bottom.x = 104
bottom.y = 524
page.append(bottom)
end = Note(24, 14, TextBlock("继续向外探索", 2), RectBlock(176, 100, 10, "#CEDFE7"), TextBlock("画布上的对象仍是普通 Endor 组件。"), "#EAF1F5")
end.x = 716
end.y = 614
page.append(end)
return page
}
+11 -1
View File
@@ -3,6 +3,7 @@ use std::sync::Arc;
#[derive(Debug, Clone, PartialEq)]
pub struct UiNode {
pub surface: Option<crate::page::Surface>,
pub flex: f32,
pub flex_policy: crate::layout::FlexPolicy,
pub main_align: crate::layout::MainAlign,
@@ -17,6 +18,7 @@ impl UiNode {
pub fn children(&self) -> &[Arc<UiNode>] {
match &self.kind {
UiKind::Column { children, .. }
| UiKind::Canvas { children, .. }
| UiKind::Row { children, .. }
| UiKind::Button { children, .. }
| UiKind::Scroll { children, .. } => children,
@@ -29,7 +31,8 @@ impl UiNode {
}
pub fn shallow_eq(&self, other: &Self) -> bool {
if self.flex_policy != other.flex_policy
if self.surface != other.surface
|| self.flex_policy != other.flex_policy
|| self.flex != other.flex
|| self.main_align != other.main_align
|| self.block_id != other.block_id
@@ -48,6 +51,9 @@ impl UiNode {
.all(|(a, b)| Arc::ptr_eq(a, b))
};
match (&self.kind, &other.kind) {
(UiKind::Canvas { positions: a, .. }, UiKind::Canvas { positions: b, .. }) => {
a == b && same_children()
}
(
UiKind::Text {
value: a,
@@ -124,6 +130,10 @@ impl UiNode {
#[derive(Debug, Clone, PartialEq)]
pub enum UiKind {
Canvas {
positions: Vec<[f32; 2]>,
children: Vec<Arc<UiNode>>,
},
Button {
padding: f32,
gap: f32,