Skip to content

Commit

Permalink
add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharktheone committed Dec 26, 2024
1 parent 775152b commit cd735f9
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 20 deletions.
31 changes: 25 additions & 6 deletions crates/gosub_instance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<C: ModuleConfiguration> {
pub title: String,
pub url: Url,
Expand Down Expand Up @@ -74,7 +75,7 @@ impl<C: ModuleConfiguration> EngineInstance<C> {
})
}

/// 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<C>) -> Result<InstanceHandle>
where
C::Layouter: Send + 'static,
Expand All @@ -98,6 +99,7 @@ impl<C: ModuleConfiguration> EngineInstance<C> {
Ok(InstanceHandle { tx })
}

/// Runs the instance on the current thread with the given `Runtime`
fn run(&mut self, rt: &Runtime) {
let set = LocalSet::new();

Expand All @@ -110,6 +112,7 @@ impl<C: ModuleConfiguration> EngineInstance<C> {
});
}

/// Handles a message sent to the instance
async fn handle_message(&mut self, message: InstanceMessage) -> Result<()> {
match message {
InstanceMessage::Redraw(size) => {
Expand Down Expand Up @@ -191,18 +194,15 @@ impl<C: ModuleConfiguration> EngineInstance<C> {
self.redraw();
}
}
_ => {}
_ => {} //TODO: send all events to the WebEventLoop
},
}

Ok(())
}

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);
}
Expand All @@ -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),
}

Expand Down Expand Up @@ -257,18 +265,29 @@ impl<C: ModuleConfiguration> El<C> {
}

pub enum InternalInstanceMessage<C: HasTreeDrawer> {
/// Add an image to the cache
Image(Url, ImageBuffer<C::RenderBackend>, Option<SizeU32>),
/// 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<NodeDesc>),
/// 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<NodeDesc>),
/// 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,
}
6 changes: 2 additions & 4 deletions crates/gosub_interface/src/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C: HasRenderBackend>: Send + Clone {
type WindowId;

fn draw_scene(&self, scene: <C::RenderBackend as RenderBackend>::Scene, size: SizeU32, instance: InstanceId);

fn set_window(&mut self, window_id: Self::WindowId);
}
3 changes: 3 additions & 0 deletions crates/gosub_interface/src/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ use gosub_shared::geo::SizeU32;
use url::Url;

pub trait EventLoopHandle<C: HasDrawComponents>: 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<C::RenderBackend>, size: Option<SizeU32>);

/// Reload the instance from the given render tree
fn reload_from(&self, rt: C::RenderTree);
}
8 changes: 6 additions & 2 deletions crates/gosub_interface/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
1 change: 1 addition & 0 deletions crates/gosub_interface/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C: HasChrome> {
pub chrome: C::ChromeHandle,
Expand Down
2 changes: 1 addition & 1 deletion crates/gosub_interface/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#[derive(Clone)]
pub struct RequestServerHandle;
pub struct RequestServerHandle; //TODO
2 changes: 1 addition & 1 deletion examples/vello-renderer/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<C: ModuleConfiguration<ChromeHandle = WinitEventLoopHandle<C>>> 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:?}");
Expand Down
5 changes: 0 additions & 5 deletions examples/vello-renderer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,11 @@ impl<C: HasRenderBackend> Clone for WinitEventLoopHandle<C> {
}

impl<C: HasRenderBackend> ChromeHandle<C> for WinitEventLoopHandle<C> {
type WindowId = WindowId;
fn draw_scene(&self, scene: <C::RenderBackend as RenderBackend>::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<()> {
Expand Down
2 changes: 1 addition & 1 deletion examples/vello-renderer/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<'a, C: ModuleConfiguration<ChromeHandle = WinitEventLoopHandle<C>>> Window<
) -> Result<Self> {
let window = create_window(event_loop)?;

handles.chrome.set_window(window.id());
handles.chrome.window = window.id();

#[cfg(target_arch = "wasm32")]
{
Expand Down

0 comments on commit cd735f9

Please sign in to comment.