feat: expose native visual protocols and explicit invalidation
Verify Tessera GUI / verify (push) Failing after 7m23s

This commit is contained in:
2026-10-21 02:42:16 +08:00
parent d2f167e71f
commit dc61f5e33c
13 changed files with 691 additions and 140 deletions
+103 -2
View File
@@ -32,6 +32,14 @@ pub const TYPE_NAMES: &[&str] = &[
"Mount",
"Event",
"Subscription",
"ObjectList",
"Constraints",
"MeasureResult",
"Bounds",
"Point",
"Arrangement",
"DisplayList",
"PaintCommand",
];
pub const QUERY_NAMES: &[&str] = &["isInstance", "isSubtype"];
@@ -168,6 +176,7 @@ struct Session {
layout: crate::layout::LayoutRuntime,
mounts: crate::mount::MountTable,
published_version: u64,
force_frame: bool,
snapshot_visited: usize,
snapshot_reused: usize,
}
@@ -210,6 +219,7 @@ enum Operation {
On,
Dispatch,
OnEvent,
Invalidate,
OnMount,
OnUnmount,
Click,
@@ -492,7 +502,7 @@ impl NativeCallable for Function {
Button => (1, 1),
Replace | Move => (2, 2),
Page | Unmount | Open | Close | Activate | Back | Forward | Children => (0, 0),
Click => (0, 1),
Click | Invalidate => (0, 1),
On => (2, 3),
Dispatch => (1, 2),
OnEvent | OnMount | OnUnmount => (1, 1),
@@ -510,6 +520,56 @@ impl NativeCallable for Function {
};
return Ok(NativeResult::Return(Value::Bool(value)));
}
if matches!(self.operation, Invalidate) {
let reason = args
.first()
.map(|value| text(Some(value)))
.transpose()?
.unwrap_or_else(|| "layout".into());
if !matches!(reason.as_str(), "paint" | "layout" | "content") {
return Err(invalid(
"invalidation reason must be paint, layout or content",
));
}
let owner = self.owner.ok_or(invalid("invalidation requires a Block"))?;
let (data, _) = block::resolve(cx.store(), owner)?;
let mut session = self
.session
.lock()
.map_err(|_| invalid("UI session poisoned"))?;
let mut affected = std::collections::HashSet::new();
for weak in &session.nodes {
if let Some(block) = cx.upgrade(*weak)?
&& block::resolve(cx.store(), block)?.0 == data
{
affected.insert(block);
}
}
if reason != "paint" {
for block in &affected {
session.layout.invalidate(&block.to_string());
}
}
if reason == "content" {
session
.render_cache
.invalidate_objects(affected.iter().copied());
}
let mut visible = false;
for weak in session.mounts.active.values() {
if let Some(mount) = cx.upgrade(*weak)?
&& affected.contains(
&cx.store()
.payload::<crate::mount::Mount>(mount, "Mount")?
.block,
)
{
visible = true;
}
}
session.force_frame |= visible;
return Ok(NativeResult::Return(Value::Nil));
}
if matches!(self.operation, Mounts) {
let owner = self.owner.ok_or(invalid("mount query requires a Block"))?;
let session = self
@@ -532,6 +592,7 @@ impl NativeCallable for Function {
let set = crate::mount::collection(
cx,
positions.into_iter().map(|(_, object)| object).collect(),
session.desktop,
)?;
return Ok(NativeResult::Return(Value::Object(set)));
}
@@ -1077,7 +1138,7 @@ impl UiBridge {
vm.get_attr(state.desktop, "focusRequested"),
Ok(Value::Bool(true))
);
let changed = state.pending.is_some() || !same || focus_changed;
let changed = state.pending.is_some() || !same || focus_changed || state.force_frame;
if changed {
state.snapshot_visited = state.render_cache.visited;
state.snapshot_reused = state.render_cache.reused;
@@ -1107,6 +1168,7 @@ impl UiBridge {
}
vm.set_attr(state.desktop, "pending", Value::Nil)?;
vm.set_attr(state.desktop, "focusRequested", Value::Bool(false))?;
state.force_frame = false;
state.last_snapshot = snapshot;
state.published_version = frame.layout_version;
// Hooks may call Page/container APIs; never hold the session mutex
@@ -1181,6 +1243,7 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
layout: crate::layout::LayoutRuntime::default(),
mounts: crate::mount::MountTable::default(),
published_version: 0,
force_frame: false,
snapshot_visited: 0,
snapshot_reused: 0,
}));
@@ -1235,6 +1298,7 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
("Mount", "Object", None),
("Event", "Object", Some("event")),
("Subscription", "Object", None),
("ObjectList", "Object", None),
] {
let base = object(Some(&vm.get_attr(ui, base_name)?))?;
let constructor = match constructor_name {
@@ -1280,6 +1344,40 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
vm.set_attr(ui, name, Value::Object(definition))?;
definitions.push((name, definition));
}
for (name, fields) in [
(
"Constraints",
vec!["minWidth", "minHeight", "maxWidth", "maxHeight"],
),
("MeasureResult", vec!["width", "height"]),
("Bounds", vec!["x", "y", "width", "height"]),
("Point", vec!["x", "y"]),
("Arrangement", vec!["children"]),
("DisplayList", vec!["commands"]),
("PaintCommand", vec![]),
] {
let fields = fields
.into_iter()
.map(|name| endor_core::ClassField {
name: name.into(),
type_name: if matches!(name, "children" | "commands") {
"Object"
} else {
"Number"
}
.into(),
})
.collect();
let definition = vm.new_class(
name,
root_types.object,
fields,
std::collections::BTreeMap::new(),
)?;
roots.push(definition);
vm.set_attr(ui, name, Value::Object(definition))?;
definitions.push((name, definition));
}
// The desktop owns the registry through ordinary traced attributes. The
// Rust session only borrows handles and adds no hidden lifetime roots.
let types = vm.new_dict()?;
@@ -1312,6 +1410,7 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
}
for (type_name, name, operation) in [
("Block", "mounts", Operation::Mounts),
("Block", "invalidate", Operation::Invalidate),
("Block", "onClick", Operation::OnClick),
("Block", "on", Operation::On),
("Block", "dispatch", Operation::Dispatch),
@@ -1350,6 +1449,8 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
vm.attach_type(method, function_type)?;
vm.define_method(expected, name, method)?;
}
let block_type = object(Some(&vm.get_attr(ui, "Block")?))?;
crate::native_visual::install(vm, roots, desktop, block_type, function_type)?;
let event_type = object(Some(&vm.get_attr(ui, "Event")?))?;
let token_type = object(Some(&vm.get_attr(ui, "Subscription")?))?;
crate::event_dispatch::install(vm, roots, event_type, token_type, function_type)?;