feat: build functional Endor start page and balance compact tabs

This commit is contained in:
2026-10-24 20:12:02 +08:00
parent 3cb44b96eb
commit 952fc2eff2
11 changed files with 205 additions and 82 deletions
+53 -4
View File
@@ -4,6 +4,7 @@ use super::*;
#[derive(Debug)]
struct OpenTool {
session: Shared,
ui: ObjectRef,
}
impl NativeCallable for OpenTool {
fn arity(&self) -> Option<endor_core::CallArity> {
@@ -16,6 +17,13 @@ impl NativeCallable for OpenTool {
if !["Console", "Sources", "Network", "Settings", "New tab"].contains(&name.as_str()) {
return Err(invalid("unknown developer tool"));
}
if name == "New tab" {
let factory = object(Some(&cx.store().get_attr(self.ui, "newTab")?))?;
return Ok(NativeResult::Invoke {
function: factory,
arguments: vec![],
});
}
let nodes = self
.session
.lock()
@@ -23,9 +31,6 @@ impl NativeCallable for OpenTool {
.nodes
.clone();
for weak in nodes {
if name == "New tab" {
break;
}
if let Some(page) = cx.upgrade(weak)?
&& cx.store().get_attr(page, "_tesseraTool").ok() == Some(Value::Text(name.clone()))
{
@@ -63,10 +68,54 @@ pub(super) fn install(vm: &mut Vm, ui: ObjectRef, session: &Shared) -> Result<()
"developerTools",
OpenTool {
session: session.clone(),
ui,
},
vec![],
vec![ui],
))?;
vm.set_attr(ui, "developerTools", Value::Object(function))?;
vm.release(function)?;
Ok(())
}
/// Compile the bundled Endor page once per VM, in a private module namespace.
/// Its functions/classes and callbacks remain ordinary managed VM objects.
pub(super) fn install_start_page(vm: &mut Vm, bridge: &UiBridge) -> Result<()> {
let source = frontend::EndorReader::from_text(
"tessera:start-page.endor",
include_str!("start_page.endor"),
);
let ast = frontend::parse_source_ast(&source, frontend::ParseMode::Module)?;
let compiled = compiler::compile_ast(
&ast,
&source,
&compiler::CompileOptions {
globals: std::iter::once("ui")
.chain(TYPE_NAMES.iter().copied())
.map(str::to_owned)
.collect(),
},
)
.map_err(|error| source.annotate(error))?;
let globals = vm.new_dict()?;
let result = (|| -> Result<()> {
bridge.install_globals(vm, globals)?;
let code = compiled.load(vm)?;
let value = vm.execute_with_limits(
code,
globals,
endor_core::ExecutionLimits {
max_frames: 128,
max_instructions: 100_000,
},
);
vm.release(code)?;
vm.release_value(value?)?;
let factory = vm
.dict_get(globals, &Value::Text("newTab".into()))?
.ok_or_else(|| anyhow::anyhow!("start page module requires newTab"))?;
vm.set_attr(bridge.ui, "newTab", factory)?;
Ok(())
})();
vm.release(globals)?;
result
}
+59
View File
@@ -0,0 +1,59 @@
// 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)
}
}
class StartRow: Row {
function measure(self: StartRow, mount: Mount, constraints: Constraints): MeasureResult {
measured = Row.measure(self, mount, constraints)
return MeasureResult(constraints.maxWidth, measured.height)
}
}
class StartActions {
home: Page
function blank(self: StartActions, event: Event): Unit {
page = Page()
page.title = "未命名页面"
back = Button("返回开始页")
back.onClick(self.home.activate)
page.render(Column(32, 16, TextBlock("未命名页面", 2), TextBlock("一个新的 Page 对象,已加入当前窗口。"), TextBlock("用 Endor 的 page.render(...) 组织内容。"), back))
}
function counter(self: StartActions, event: Event): Unit {
count = ui.state(0)
add = Button("增加计数")
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)))
}
}
function newTab(): Page {
page = StartPage()
page.title = "新标签页"
actions = StartActions(page)
blank = Button("+ 创建页面")
counter = Button("↗ 试试共享对象")
compose = Button("▦ 探索界面组合")
blank.on("click", actions.blank)
counter.on("click", actions.counter)
compose.on("click", actions.compose)
content = Column(0, 14,
TextBlock("Endor", 3, "#292B2D"),
TextBlock("从一个对象开始。", 2, "#484B50"),
TextBlock("用代码组织对象,让同一份内容成为可交互的界面。", 1, "#7D8085"),
Spacer(16), blank, counter, compose, Spacer(16),
TextBlock("⌘ / Ctrl + T 新建标签页 · ⌘ / Ctrl + W 关闭", 1, "#8B8E93"))
row = StartRow(24, 0, content)
row.mainAlign = "center"
page.mainAlign = "center"
page.render(row)
return page
}