Support shared-VM native windows and Page detachment
Verify Tessera GUI / verify (push) Failing after 7m35s

This commit is contained in:
2026-10-21 18:11:58 +08:00
parent f8173168fd
commit e3e46068eb
13 changed files with 993 additions and 145 deletions
+191 -31
View File
@@ -30,6 +30,10 @@ pub enum AppEvent {
}
pub struct TesseraApp {
resource: u64,
native_session: u64,
children: std::collections::BTreeMap<u64, TesseraApp>,
proxy: EventLoopProxy<AppEvent>,
window: Option<Arc<Window>>,
context: Option<Context<Arc<Window>>>,
surface: Option<Surface<Arc<Window>, Arc<Window>>>,
@@ -42,6 +46,7 @@ pub struct TesseraApp {
event_client: crate::events::Client,
guest_pressed: Option<crate::events::Input>,
tab_pressed: Option<crate::events::Input>,
tab_drag_origin: Option<Point>,
guest_focus: Option<crate::events::Input>,
guest_key: Option<(NamedKey, crate::events::Input)>,
guest_keyboard: bool,
@@ -55,6 +60,10 @@ impl TesseraApp {
) -> anyhow::Result<Self> {
let event_client = crate::events::Client::new(proxy.clone())?;
Ok(Self {
resource: crate::window::MAIN_WINDOW,
native_session: 0,
children: std::collections::BTreeMap::new(),
proxy: proxy.clone(),
window: None,
context: None,
surface: None,
@@ -71,36 +80,114 @@ impl TesseraApp {
event_client,
guest_pressed: None,
tab_pressed: None,
tab_drag_origin: None,
guest_focus: None,
guest_key: None,
guest_keyboard: true,
})
}
fn apply_window_commands(&self) {
let Some(window) = &self.window else {
fn reconcile_windows(&mut self, event_loop: &ActiveEventLoop) {
if self.resource != crate::window::MAIN_WINDOW {
return;
}
let Ok(namespace) = self.namespace.read() else {
return;
};
let session = namespace.runtime_session;
let revision = namespace.revision;
let submission = namespace.submission;
let documents = namespace.document.windows.clone();
drop(namespace);
if session != self.native_session {
// Console reset preserves the old projection with input disabled.
// A real replacement changes frame sessions and drops old OS views.
let replaces = documents
.values()
.any(|document| document.frame.session == session && session != 0);
if replaces || (session != 0 && documents.is_empty()) {
self.children.clear();
}
self.native_session = session;
}
self.children.retain(|id, child| {
documents.get(id).is_some_and(|document| {
document.frame.session == 0 || child.native_session == document.frame.session
})
});
for (id, document) in documents {
if let Some(child) = self.children.get_mut(&id) {
if let Ok(mut namespace) = child.namespace.write() {
namespace.document = document;
namespace.runtime_session = session;
namespace.revision = revision;
namespace.submission = submission;
}
child.document_updated(event_loop);
} else {
let namespace = Arc::new(RwLock::new(NamespaceState {
document,
runtime_session: session,
revision,
submission,
..NamespaceState::default()
}));
match Self::new(namespace, self.activity.clone(), self.proxy.clone()) {
Ok(mut child) => {
child.resource = id;
child.native_session = session;
child.resumed(event_loop);
child.document_updated(event_loop);
self.children.insert(id, child);
}
Err(error) => {
self.developer_tools.input_error = format!("Window {id}: {error}");
self.developer_tools.open = true;
}
}
}
}
}
fn apply_window_commands(&mut self, event_loop: &ActiveEventLoop) {
let Ok(mut namespace) = self.namespace.write() else {
return;
};
let session = namespace.runtime_session;
let commands = std::mem::take(&mut namespace.window_commands);
drop(namespace);
for envelope in commands {
if envelope.session == 0
|| envelope.session != namespace.runtime_session
|| envelope.command.window != crate::window::MAIN_WINDOW
{
if envelope.session == 0 || envelope.session != session {
continue;
}
let id = envelope.command.window;
if matches!(envelope.command.action, crate::window::Action::Close) {
if id == crate::window::MAIN_WINDOW {
event_loop.exit();
} else {
self.children.remove(&id);
}
continue;
}
let target = if id == self.resource {
self.window.as_ref()
} else {
self.children
.get(&id)
.and_then(|child| child.window.as_ref())
};
let Some(window) = target else {
continue;
};
match envelope.command.action {
crate::window::Action::Resize { width, height } => {
let _ = window.request_inner_size(LogicalSize::new(width, height));
}
crate::window::Action::SetTitle(title) => window.set_title(&title),
crate::window::Action::Focus => window.focus_window(),
crate::window::Action::Close => {}
}
}
drop(namespace);
self.publish_viewport();
}
@@ -117,10 +204,24 @@ impl TesseraApp {
return;
}
if let Ok(mut namespace) = self.namespace.write() {
namespace.viewport = Some(crate::layout::Size {
let size = crate::layout::Size {
width: size.width.min(crate::ui_bridge::MAX_DIMENSION as f32),
height: size.height.min(crate::ui_bridge::MAX_DIMENSION as f32),
});
};
namespace.viewport = Some(size);
namespace.window_viewports.insert(self.resource, size);
if self.resource == crate::window::MAIN_WINDOW {
namespace.window_viewports.retain(|id, _| {
*id == crate::window::MAIN_WINDOW || self.children.contains_key(id)
});
for (id, child) in &self.children {
if let Ok(local) = child.namespace.read()
&& let Some(size) = local.viewport
{
namespace.window_viewports.insert(*id, size);
}
}
}
}
}
@@ -138,8 +239,9 @@ impl TesseraApp {
self.request_redraw();
}
fn document_updated(&mut self) {
self.apply_window_commands();
fn document_updated(&mut self, event_loop: &ActiveEventLoop) {
self.reconcile_windows(event_loop);
self.apply_window_commands(event_loop);
if let Ok(namespace) = self.namespace.read() {
self.developer_tools.document_updated(&namespace.document);
let frame = &namespace.document.frame;
@@ -148,7 +250,7 @@ impl TesseraApp {
.focus
.filter(|_| frame.fault.is_none())
.map(|mount| crate::events::Input {
window: crate::window::MAIN_WINDOW,
window: self.resource,
session: frame.session,
layout_version: frame.layout_version,
mount,
@@ -276,7 +378,7 @@ impl TesseraApp {
let point = Point::new(self.pointer.x, self.pointer.y - offset);
let mount = frame.hit_test(point)?;
Some(crate::events::Input {
window: crate::window::MAIN_WINDOW,
window: self.resource,
session: frame.session,
layout_version: frame.layout_version,
mount: mount.id,
@@ -301,7 +403,7 @@ impl TesseraApp {
(active + 1) % frame.tabs.len()
};
Some(crate::events::Input {
window: crate::window::MAIN_WINDOW,
window: self.resource,
session: frame.session,
layout_version: frame.layout_version,
mount: frame.tabs[index].id,
@@ -314,6 +416,7 @@ impl TesseraApp {
self.guest_pressed = None;
self.guest_key = None;
self.tab_pressed = None;
self.tab_drag_origin = None;
self.send_guest(input);
true
} else {
@@ -334,7 +437,7 @@ impl TesseraApp {
let width = window.inner_size().width as f32 / window.scale_factor() as f32;
let (id, close) = crate::tabs::hit(&frame.tabs, width, self.pointer)?;
Some(crate::events::Input {
window: crate::window::MAIN_WINDOW,
window: self.resource,
session: frame.session,
layout_version: frame.layout_version,
mount: id,
@@ -382,7 +485,7 @@ impl TesseraApp {
current.map(|i| (i + 1) % targets.len()).unwrap_or(0)
};
Some(crate::events::Input {
window: crate::window::MAIN_WINDOW,
window: self.resource,
session: frame.session,
layout_version: frame.layout_version,
mount: targets[index].id,
@@ -476,8 +579,16 @@ impl ApplicationHandler<AppEvent> for TesseraApp {
}
let attributes = WindowAttributes::default()
.with_title("Tessera")
.with_inner_size(LogicalSize::new(1_280.0, 800.0))
.with_title(if self.resource == crate::window::MAIN_WINDOW {
"Tessera".into()
} else {
format!("Endor Window {}", self.resource)
})
.with_inner_size(if self.resource == crate::window::MAIN_WINDOW {
LogicalSize::new(1_280.0, 800.0)
} else {
LogicalSize::new(960.0, 640.0)
})
.with_min_inner_size(LogicalSize::new(720.0, 480.0))
.with_max_inner_size(LogicalSize::new(
crate::window::MAX_WINDOW_DIMENSION,
@@ -496,12 +607,12 @@ impl ApplicationHandler<AppEvent> for TesseraApp {
self.window = Some(window.clone());
self.context = Some(context);
self.surface = Some(surface);
self.apply_window_commands();
self.apply_window_commands(event_loop);
self.publish_viewport();
window.request_redraw();
}
fn user_event(&mut self, _event_loop: &ActiveEventLoop, event: AppEvent) {
fn user_event(&mut self, event_loop: &ActiveEventLoop, event: AppEvent) {
match event {
AppEvent::RuntimeFailed { message } => {
self.developer_tools.input_error = message;
@@ -510,7 +621,7 @@ impl ApplicationHandler<AppEvent> for TesseraApp {
self.guest_key = None;
self.request_redraw();
}
AppEvent::DocumentUpdated => self.document_updated(),
AppEvent::DocumentUpdated => self.document_updated(event_loop),
AppEvent::ConsoleFailed { message, source } => {
self.developer_tools.input_error = format!("Request failed: {message}");
if self.developer_tools.input.is_empty() {
@@ -533,11 +644,41 @@ impl ApplicationHandler<AppEvent> for TesseraApp {
return;
};
if window.id() != window_id {
if let Some(child) = self.children.values_mut().find(|child| {
child
.window
.as_ref()
.is_some_and(|window| window.id() == window_id)
}) {
child.window_event(event_loop, window_id, event);
self.publish_viewport();
}
return;
}
match event {
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::CloseRequested => {
if self.resource == crate::window::MAIN_WINDOW {
event_loop.exit();
} else {
let input = self
.namespace
.read()
.ok()
.map(|namespace| crate::events::Input {
window: self.resource,
session: namespace.document.frame.session,
layout_version: namespace.document.frame.layout_version,
mount: 0,
kind: "window-close".into(),
point: None,
press_point: None,
});
if let Some(input) = input {
self.send_guest(input);
}
}
}
WindowEvent::Resized(_) | WindowEvent::ScaleFactorChanged { .. } => {
self.publish_viewport();
window.request_redraw();
@@ -566,7 +707,9 @@ impl ApplicationHandler<AppEvent> for TesseraApp {
}
WindowEvent::CursorLeft { .. } => {
self.guest_pressed = None;
self.tab_pressed = None;
if self.tab_drag_origin.is_none() {
self.tab_pressed = None;
}
self.developer_tools.pointer_left();
window.set_cursor(winit::window::CursorIcon::Default);
window.request_redraw();
@@ -579,6 +722,11 @@ impl ApplicationHandler<AppEvent> for TesseraApp {
if state == ElementState::Pressed {
self.guest_keyboard = !self.guest_covered(self.pointer);
self.tab_pressed = self.tab_hit();
self.tab_drag_origin = self
.tab_pressed
.as_ref()
.filter(|input| input.kind == "tab-activate")
.map(|_| self.pointer);
self.guest_pressed = self.guest_hit();
if self.guest_keyboard {
self.developer_tools.blur();
@@ -589,14 +737,26 @@ impl ApplicationHandler<AppEvent> for TesseraApp {
&self.activity,
);
} else {
if let Some(pressed) = self.tab_pressed.take()
&& let Some(released) = self.tab_hit()
&& released.session == pressed.session
&& released.layout_version == pressed.layout_version
&& released.mount == pressed.mount
&& released.kind == pressed.kind
{
self.send_guest(released);
if let Some(mut pressed) = self.tab_pressed.take() {
let width = window.inner_size().width as f32 / window.scale_factor() as f32;
let dragged = self.tab_drag_origin.take().is_some_and(|origin| {
(self.pointer.x - origin.x).hypot(self.pointer.y - origin.y) > 12.0
&& (self.pointer.y < 0.0
|| self.pointer.y >= crate::tabs::HEIGHT + 12.0
|| self.pointer.x < 0.0
|| self.pointer.x >= width)
});
if dragged {
pressed.kind = "tab-detach".into();
self.send_guest(pressed);
} else if let Some(released) = self.tab_hit()
&& released.session == pressed.session
&& released.layout_version == pressed.layout_version
&& released.mount == pressed.mount
&& released.kind == pressed.kind
{
self.send_guest(released);
}
}
if let Some(pressed) = self.guest_pressed.take()
&& let Some(mut released) = self.guest_hit()