diff --git a/crates/gosub_instance/src/lib.rs b/crates/gosub_instance/src/lib.rs index b73e1398..43261058 100644 --- a/crates/gosub_instance/src/lib.rs +++ b/crates/gosub_instance/src/lib.rs @@ -9,7 +9,7 @@ use gosub_interface::render_backend::{ImageBuffer, NodeDesc}; use gosub_net::http::fetcher::Fetcher; use gosub_shared::geo::SizeU32; use gosub_shared::types::Result; -use log::{info, warn}; +use log::warn; use std::sync::mpsc::Sender as SyncSender; use std::sync::Arc; use tokio::runtime::{Builder, Handle, Runtime}; @@ -18,6 +18,7 @@ use tokio::task; use tokio::task::LocalSet; use url::Url; +/// Represents a running instance of the engine. This can be a tab in a browser or a webview pub struct EngineInstance { pub title: String, pub url: Url, @@ -74,7 +75,7 @@ impl EngineInstance { }) } - /// Spawns a new `EngineInstance` on a new thread, returning the `InstanceHandle` to communicate with it. + /// Spawns a new `EngineInstance` on a new thread, returning the `InstanceHandle` to communicate with it pub fn new_on_thread(url: Url, layouter: C::Layouter, id: InstanceId, handles: Handles) -> Result where C::Layouter: Send + 'static, @@ -98,6 +99,7 @@ impl EngineInstance { Ok(InstanceHandle { tx }) } + /// Runs the instance on the current thread with the given `Runtime` fn run(&mut self, rt: &Runtime) { let set = LocalSet::new(); @@ -110,6 +112,7 @@ impl EngineInstance { }); } + /// Handles a message sent to the instance async fn handle_message(&mut self, message: InstanceMessage) -> Result<()> { match message { InstanceMessage::Redraw(size) => { @@ -191,7 +194,7 @@ impl EngineInstance { self.redraw(); } } - _ => {} + _ => {} //TODO: send all events to the WebEventLoop }, } @@ -199,10 +202,7 @@ impl EngineInstance { } fn redraw(&mut self) { - let now = std::time::Instant::now(); let scene = self.data.draw(self.size, &self.el); - let elapsed = now.elapsed(); - info!("Redraw took {:?}", elapsed); self.handles.chrome.draw_scene(scene, self.size, self.id); } @@ -213,15 +213,23 @@ pub struct InstanceHandle { } pub enum InstanceMessage { + /// Redraw the instance with the given size Redraw(SizeU32), + /// Navigate to the given URL Navigate(Url), + /// Navigate back in history Back, + /// Navigate forward in history Forward, + /// Reload the current page Reload, + /// Close the instance Close, + /// Input event (mouse, keyboard, etc.) Input(InputEvent), + /// Debug event (send nodes, select element, etc.) Debug(DebugEvent), } @@ -257,18 +265,29 @@ impl El { } pub enum InternalInstanceMessage { + /// Add an image to the cache Image(Url, ImageBuffer, Option), + /// Redraw the instance Redraw, + /// Reload the instance from the given tree ReloadFrom(C::RenderTree), } pub enum DebugEvent { + /// Send a NodeDescription of the root node to the given sender SendNodes(SyncSender), + /// Visually select the element with the given ID SelectElement(u64), + /// Send a NodeDescription of the element with the given ID to the given sender Info(u64, SyncSender), + /// Deselect the currently selected element (visually) Deselect, + /// Toggle the debug mode Toggle, + /// Enable the debug mode Enable, + /// Disable the debug mode Disable, + /// Clear the debug buffers so the next draw will be a full redraw ClearBuffers, } diff --git a/crates/gosub_interface/src/chrome.rs b/crates/gosub_interface/src/chrome.rs index ef636734..362ada68 100644 --- a/crates/gosub_interface/src/chrome.rs +++ b/crates/gosub_interface/src/chrome.rs @@ -3,10 +3,8 @@ use crate::instance::InstanceId; use crate::render_backend::RenderBackend; use gosub_shared::geo::SizeU32; +/// A `ChromeHandle` is a trait that allows a potential instance of the engine to call back to the Chrome/Useragent +/// this can include drawing the scene pub trait ChromeHandle: Send + Clone { - type WindowId; - fn draw_scene(&self, scene: ::Scene, size: SizeU32, instance: InstanceId); - - fn set_window(&mut self, window_id: Self::WindowId); } diff --git a/crates/gosub_interface/src/eventloop.rs b/crates/gosub_interface/src/eventloop.rs index cf817521..35fdce5c 100644 --- a/crates/gosub_interface/src/eventloop.rs +++ b/crates/gosub_interface/src/eventloop.rs @@ -5,9 +5,12 @@ use gosub_shared::geo::SizeU32; use url::Url; pub trait EventLoopHandle: WasmNotSendSync + Clone + 'static { + /// Request a redraw of the scene fn redraw(&self); + /// Add an image to the cache fn add_img_cache(&self, url: Url, buf: ImageBuffer, size: Option); + /// Reload the instance from the given render tree fn reload_from(&self, rt: C::RenderTree); } diff --git a/crates/gosub_interface/src/input.rs b/crates/gosub_interface/src/input.rs index 74b8ca58..518dee0a 100644 --- a/crates/gosub_interface/src/input.rs +++ b/crates/gosub_interface/src/input.rs @@ -2,13 +2,17 @@ use gosub_shared::geo::Point; #[derive(Debug, Clone, Copy, PartialEq)] pub enum InputEvent { + /// The mouse moved to a new position MouseMove(Point), - MouseEnter, - MouseLeave, + /// The mouse wheel was scrolled with the given delta MouseScroll(Point), + /// A mouse button was pressed MouseDown(MouseButton), + /// A mouse button was released MouseUp(MouseButton), + /// A key was pressed KeyboardDown(char), + /// A key was released KeyboardUp(char), } diff --git a/crates/gosub_interface/src/instance.rs b/crates/gosub_interface/src/instance.rs index 667acb2e..4c4f5a0c 100644 --- a/crates/gosub_interface/src/instance.rs +++ b/crates/gosub_interface/src/instance.rs @@ -4,6 +4,7 @@ use crate::request::RequestServerHandle; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct InstanceId(pub u64); +/// holds handles to the various systems of the engine #[derive(Clone)] pub struct Handles { pub chrome: C::ChromeHandle, diff --git a/crates/gosub_interface/src/request.rs b/crates/gosub_interface/src/request.rs index d032f223..b045ce96 100644 --- a/crates/gosub_interface/src/request.rs +++ b/crates/gosub_interface/src/request.rs @@ -1,2 +1,2 @@ #[derive(Clone)] -pub struct RequestServerHandle; +pub struct RequestServerHandle; //TODO diff --git a/examples/vello-renderer/application.rs b/examples/vello-renderer/application.rs index ffbfebf7..3a686afe 100644 --- a/examples/vello-renderer/application.rs +++ b/examples/vello-renderer/application.rs @@ -119,7 +119,7 @@ impl>> Application return; }; - handles.chrome.set_window(window.id()); + handles.chrome.window = window.id(); if let Err(e) = window.tabs.open(url, self.layouter.clone(), handles) { error!("Error opening tab: {e:?}"); diff --git a/examples/vello-renderer/main.rs b/examples/vello-renderer/main.rs index 7d7a7135..bf45d6e3 100644 --- a/examples/vello-renderer/main.rs +++ b/examples/vello-renderer/main.rs @@ -88,16 +88,11 @@ impl Clone for WinitEventLoopHandle { } impl ChromeHandle for WinitEventLoopHandle { - type WindowId = WindowId; fn draw_scene(&self, scene: ::Scene, size: SizeU32, instance: InstanceId) { let _ = self .proxy .send_event(CustomEventInternal::DrawScene(scene, size, instance, self.window)); } - - fn set_window(&mut self, window_id: WindowId) { - self.window = window_id; - } } fn main() -> Result<()> { diff --git a/examples/vello-renderer/window.rs b/examples/vello-renderer/window.rs index 07f66cdd..f88ee947 100644 --- a/examples/vello-renderer/window.rs +++ b/examples/vello-renderer/window.rs @@ -69,7 +69,7 @@ impl<'a, C: ModuleConfiguration>> Window< ) -> Result { let window = create_window(event_loop)?; - handles.chrome.set_window(window.id()); + handles.chrome.window = window.id(); #[cfg(target_arch = "wasm32")] {