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
+200 -9
View File
@@ -207,10 +207,10 @@ page.render(Column(0,8,Column(0,8,paragraph,tail),Column(50,8,paragraph)))
})
.collect();
assert_eq!(commands.len(), 2);
// The 240px window reserves 56px for the rail and 8px for its inset.
// The second occurrence additionally has 50px padding on each side.
assert_eq!(commands[0][0], 176.0);
assert_eq!(commands[1][0], 76.0);
// The viewport is already the available page area; there is no implicit
// rail/inset subtraction. The second occurrence has 50px padding per side.
assert_eq!(commands[0][0], 240.0);
assert_eq!(commands[1][0], 140.0);
assert!(commands[1][1] > commands[0][1]);
let tail = initial
.frame
@@ -251,7 +251,7 @@ page.render(Column(0,8,Column(0,8,paragraph,tail),Column(50,8,paragraph)))
_ => None,
})
.collect();
assert_eq!(widths, [336.0, 236.0]);
assert_eq!(widths, [400.0, 300.0]);
let heights: Vec<_> = resized
.frame
.commands
@@ -1774,14 +1774,14 @@ fn viewport_resize_changes_constraints_and_preserves_mount_identity_without_sour
}),
)
.unwrap();
// Page content excludes the 56px rail, 8px inset and 24px root padding
// on each side, both initially and after resizing the outer window.
// The viewport is the actual page area; only the Endor root's 24px padding
// on each side is removed, both initially and after resizing.
assert!(
initial
.frame
.mounts
.iter()
.any(|mount| mount.width == 688.0 && mount.height == 24.0)
.any(|mount| mount.width == 752.0 && mount.height == 24.0)
);
let ids: Vec<_> = initial.frame.mounts.iter().map(|mount| mount.id).collect();
let document = session
@@ -1805,7 +1805,7 @@ fn viewport_resize_changes_constraints_and_preserves_mount_identity_without_sour
.frame
.mounts
.iter()
.any(|mount| mount.width == 248.0 && mount.height == 24.0)
.any(|mount| mount.width == 312.0 && mount.height == 24.0)
);
assert_eq!(session.inputs, 0);
assert!(
@@ -5675,3 +5675,194 @@ fn shutdown_hooks_cannot_create_new_background_work() {
assert!(format!("{error:#}").contains("runtime is exiting; new tasks are forbidden"));
assert!(session.is_faulted());
}
#[test]
fn page_modes_grow_on_the_declared_axis_and_follow_live_children() {
for (mode, expected) in [
("window", (400.0, 300.0)),
("vertical", (400.0, 710.0)),
("horizontal", (910.0, 300.0)),
] {
let source = format!(
r#"page=Page()
page.mode="{mode}"
page.gap=10
page.render(RectBlock(450,350))
page.append(RectBlock(450,350))"#
);
let (mut session, document) = ReplSession::load_with_viewport(
&source,
"app.main",
1,
300,
Some(crate::layout::Size {
width: 400.0,
height: 300.0,
}),
)
.unwrap();
let root = &document.frame.mounts[0];
assert_eq!((root.width, root.height), expected, "{mode}");
if mode == "vertical" {
let next = session
.execute("page.append(RectBlock(450,350))", "app.main")
.unwrap()
.document
.unwrap();
assert_eq!(next.frame.mounts[0].height, 1070.0);
}
if mode == "horizontal" {
let next = session
.execute("page.append(RectBlock(450,350))", "app.main")
.unwrap()
.document
.unwrap();
assert_eq!(next.frame.mounts[0].width, 1370.0);
}
}
}
#[test]
fn canvas_positions_are_live_signed_and_shared_without_copying_objects() {
let (mut session, initial) = ReplSession::load_with_viewport(
r#"
item=Button("same object")
item.x=-120
item.y=90
page=Page()
page.mode="canvas"
page.render(item)
page.append(item)
page.panX=-160
page.zoom=2
"#,
"app.main",
1,
301,
Some(crate::layout::Size {
width: 400.0,
height: 300.0,
}),
)
.unwrap();
let mounts: Vec<_> = initial
.frame
.mounts
.iter()
.filter(|mount| mount.interactive)
.collect();
assert_eq!(mounts.len(), 2);
assert_eq!(mounts[0].block_id, mounts[1].block_id);
assert_ne!(mounts[0].id, mounts[1].id);
assert_eq!((mounts[0].x, mounts[0].y), (-120.0, 90.0));
assert_eq!(initial.frame.surface.pan.x, -160.0);
assert_eq!(initial.frame.surface.zoom, 2.0);
let next = session
.execute("item.x=500\nitem.y=-60", "app.main")
.unwrap()
.document
.unwrap();
assert!(
next.frame
.mounts
.iter()
.filter(|m| m.interactive)
.all(|m| m.x == 500.0 && m.y == -60.0)
);
let old_page = next.frame.surface.page.clone();
let window = session
.execute(
"other=Page()\nother.render(TextBlock(\"other\"))",
"app.main",
)
.unwrap()
.document
.unwrap();
assert_eq!(window.frame.surface.mode, crate::page::Mode::Window);
let restored = session
.execute("page.activate()", "app.main")
.unwrap()
.document
.unwrap();
assert_eq!(restored.frame.surface.page, old_page);
assert_eq!(restored.frame.surface.pan.x, -160.0);
assert_eq!(restored.frame.surface.zoom, 2.0);
}
#[test]
fn bundled_start_page_creates_all_four_real_page_modes() {
let mut session = ReplSession::new_with_viewport(
302,
Some(crate::layout::Size {
width: 1024.0,
height: 800.0,
}),
)
.unwrap();
let mut document = session
.execute("ui.newTab()", "app.main")
.unwrap()
.document
.unwrap();
assert_eq!(document.frame.surface.mode, crate::page::Mode::Canvas);
for (label, mode) in [
("窗口页面", crate::page::Mode::Window),
("纵向延伸", crate::page::Mode::Vertical),
("横向延伸", crate::page::Mode::Horizontal),
] {
let home_tab = document
.frame
.tabs
.iter()
.find(|tab| tab.title == "自由画布")
.unwrap()
.id;
let point = document
.frame
.commands
.iter()
.find_map(|command| match command {
RenderCommand::Text { x, y, value, .. } if value == label => {
Some(crate::geometry::Point::new(x + 2.0, y + 2.0))
}
_ => None,
})
.unwrap();
let mount = document
.frame
.hit_test(point)
.expect("button label must be clickable")
.id;
document = session
.dispatch(&crate::events::Input {
window: 1,
session: document.frame.session,
layout_version: document.frame.layout_version,
mount,
kind: "click".into(),
point: Some([point.x, point.y]),
press_point: Some([point.x, point.y]),
delta: None,
tab_drop: None,
screen_position: None,
})
.unwrap()
.document
.unwrap();
assert_eq!(document.frame.surface.mode, mode, "{label}");
assert!(document.frame.fault.is_none());
let input = crate::events::Input {
window: 1,
session: document.frame.session,
layout_version: document.frame.layout_version,
mount: home_tab,
kind: "tab-activate".into(),
point: None,
press_point: None,
delta: None,
tab_drop: None,
screen_position: None,
};
document = session.dispatch(&input).unwrap().document.unwrap();
}
}