Skip to content

Desktop: Final wasm crate #2940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ specta-macros = { opt-level = 1 }
syn = { opt-level = 1 }

[profile.release]
lto = "thin"
debug = true

[profile.profiling]
Expand Down
20 changes: 11 additions & 9 deletions desktop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ edition = "2024"
rust-version = "1.87"

[features]
# default = ["gpu"]
# gpu = ["graphite-editor/gpu"]
default = ["gpu"]
gpu = ["graphite-editor/gpu"]

[dependencies]
# Local dependencies
# graphite-editor = { path = "../editor", features = [
# "gpu",
# "ron",
# "vello",
# "decouple-execution",
# ] }
# # Local dependencies
graphite-editor = { path = "../editor", features = [
"gpu",
"ron",
"vello",
"decouple-execution",
] }

wgpu = { workspace = true }
winit = { workspace = true, features = ["serde"] }
thiserror = { workspace = true }
Expand All @@ -29,3 +30,4 @@ include_dir = { workspace = true }
tracing-subscriber = { workspace = true }
tracing = { workspace = true }
dirs = {workspace = true}
serde_json = { workspace = true }
17 changes: 17 additions & 0 deletions desktop/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use crate::CustomEvent;
use crate::WindowSize;
use crate::render::GraphicsState;
use crate::render::WgpuContext;
use graphite_editor::application::Editor;
use graphite_editor::dispatcher::Dispatcher;
use graphite_editor::messages::prelude::Message;
use std::collections::VecDeque;
use std::sync::Arc;
use std::sync::mpsc::Sender;
use std::time::Duration;
Expand All @@ -21,11 +25,13 @@ pub(crate) struct WinitApp {
pub(crate) cef_context: cef::Context<cef::Initialized>,
pub(crate) window: Option<Arc<Window>>,
cef_schedule: Option<Instant>,
// Cached frame buffer from CEF, used to check if mouse is on a transparent pixel
_ui_frame_buffer: Option<wgpu::Texture>,
window_size_sender: Sender<WindowSize>,
_viewport_frame_buffer: Option<wgpu::Texture>,
graphics_state: Option<GraphicsState>,
wgpu_context: WgpuContext,
pub(crate) editor: Editor,
}

impl WinitApp {
Expand All @@ -39,6 +45,7 @@ impl WinitApp {
graphics_state: None,
window_size_sender,
wgpu_context,
editor: Editor::new(),
}
}
}
Expand Down Expand Up @@ -97,6 +104,16 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
self.cef_schedule = Some(instant);
}
}
CustomEvent::MessageReceived { message } => {
let Ok(message) = serde_json::from_str::<Message>(&message) else {
tracing::error!("Message could not be deserialized: {:?}", message);
return;
};
println!("Message received: {message:?}");
let responses = self.editor.handle_message(message);
println!("responses: {:?}", responses);
// Send response to CEF
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions desktop/src/cef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub(crate) trait CefEventHandler: Clone {
/// Scheudule the main event loop to run the cef event loop after the timeout
/// [`_cef_browser_process_handler_t::on_schedule_message_pump_work`] for more documentation.
fn schedule_cef_message_loop_work(&self, scheduled_time: Instant);

fn send_message_to_editior(&self, message: String);
}

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -116,4 +118,7 @@ impl CefEventHandler for CefHandler {
fn schedule_cef_message_loop_work(&self, scheduled_time: std::time::Instant) {
let _ = self.event_loop_proxy.send_event(CustomEvent::ScheduleBrowserWork(scheduled_time));
}
fn send_message_to_editior(&self, message: String) {
let _ = self.event_loop_proxy.send_event(CustomEvent::MessageReceived { message });
}
}
4 changes: 2 additions & 2 deletions desktop/src/cef/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ impl Context<Setup> {
return Err(InitError::InitializationFailed);
}

let render_handler = RenderHandlerImpl::new(event_handler.clone());
let mut client = Client::new(ClientImpl::new(RenderHandler::new(render_handler)));
let render_handler = RenderHandler::new(RenderHandlerImpl::new(event_handler.clone()));
let mut client = Client::new(ClientImpl::new(render_handler, event_handler.clone()));

let url = CefString::from(format!("{GRAPHITE_SCHEME}://{FRONTEND_DOMAIN}/").as_str());

Expand Down
2 changes: 2 additions & 0 deletions desktop/src/cef/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ mod app;
mod browser_process_handler;
mod client;
mod non_browser_app;
mod non_browser_render_process_handler;
mod non_browser_v8_handler;
mod render_handler;

pub(crate) use app::AppImpl;
Expand Down
1 change: 1 addition & 0 deletions desktop/src/cef/internal/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cef::sys::{_cef_app_t, cef_base_ref_counted_t};
use cef::{BrowserProcessHandler, CefString, ImplApp, ImplCommandLine, SchemeRegistrar, WrapApp};

use crate::cef::CefEventHandler;

use crate::cef::scheme_handler::GraphiteSchemeHandlerFactory;

use super::browser_process_handler::BrowserProcessHandlerImpl;
Expand Down
45 changes: 37 additions & 8 deletions desktop/src/cef/internal/client.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
use cef::rc::{Rc, RcImpl};
use cef::sys::{_cef_client_t, cef_base_ref_counted_t};
use cef::{ImplClient, RenderHandler, WrapClient};
use cef::{ImplClient, ImplProcessMessage, RenderHandler, WrapClient};

pub(crate) struct ClientImpl {
use crate::cef::CefEventHandler;

pub(crate) struct ClientImpl<H: CefEventHandler> {
object: *mut RcImpl<_cef_client_t, Self>,
render_handler: RenderHandler,
event_handler: H,
}
impl ClientImpl {
pub(crate) fn new(render_handler: RenderHandler) -> Self {
impl<H: CefEventHandler> ClientImpl<H> {
pub(crate) fn new(render_handler: RenderHandler, event_handler: H) -> Self {
Self {
object: std::ptr::null_mut(),
render_handler,
event_handler,
}
}
}

impl ImplClient for ClientImpl {
impl<H: CefEventHandler> ImplClient for ClientImpl<H> {
fn render_handler(&self) -> Option<RenderHandler> {
Some(self.render_handler.clone())
}

fn get_raw(&self) -> *mut _cef_client_t {
self.object.cast()
}

fn on_process_message_received(
&self,
browser: Option<&mut cef::Browser>,
frame: Option<&mut cef::Frame>,
source_process: cef::ProcessId,
message: Option<&mut cef::ProcessMessage>,
) -> ::std::os::raw::c_int {
let Some(message) = message else {
tracing::event!(tracing::Level::ERROR, "No message in RenderProcessHandlerImpl::on_process_message_received");
return 1;
};

let pointer: *mut cef::sys::_cef_string_utf16_t = message.name().into();
let message = unsafe {
let str = (*pointer).str_;
let len = (*pointer).length;
let slice = std::slice::from_raw_parts(str, len as usize);
String::from_utf16(slice).unwrap()
};

let _ = self.event_handler.send_message_to_editior(message);
0
}
}

impl Clone for ClientImpl {
impl<H: CefEventHandler> Clone for ClientImpl<H> {
fn clone(&self) -> Self {
unsafe {
let rc_impl = &mut *self.object;
Expand All @@ -34,18 +62,19 @@ impl Clone for ClientImpl {
Self {
object: self.object,
render_handler: self.render_handler.clone(),
event_handler: self.event_handler.clone(),
}
}
}
impl Rc for ClientImpl {
impl<H: CefEventHandler> Rc for ClientImpl<H> {
fn as_base(&self) -> &cef_base_ref_counted_t {
unsafe {
let base = &*self.object;
std::mem::transmute(&base.cef_object)
}
}
}
impl WrapClient for ClientImpl {
impl<H: CefEventHandler> WrapClient for ClientImpl<H> {
fn wrap_rc(&mut self, object: *mut RcImpl<_cef_client_t, Self>) {
self.object = object;
}
Expand Down
5 changes: 5 additions & 0 deletions desktop/src/cef/internal/non_browser_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use cef::rc::{Rc, RcImpl};
use cef::sys::{_cef_app_t, cef_base_ref_counted_t};
use cef::{App, ImplApp, SchemeRegistrar, WrapApp};

use crate::cef::internal::non_browser_render_process_handler::NonBrowserRenderProcessHandlerImpl;
use crate::cef::scheme_handler::GraphiteSchemeHandlerFactory;

pub(crate) struct NonBrowserAppImpl {
Expand All @@ -14,6 +15,10 @@ impl NonBrowserAppImpl {
}

impl ImplApp for NonBrowserAppImpl {
fn render_process_handler(&self) -> Option<cef::RenderProcessHandler> {
Some(cef::RenderProcessHandler::new(NonBrowserRenderProcessHandlerImpl::new()))
}

fn on_register_custom_schemes(&self, registrar: Option<&mut SchemeRegistrar>) {
GraphiteSchemeHandlerFactory::register_schemes(registrar);
}
Expand Down
61 changes: 61 additions & 0 deletions desktop/src/cef/internal/non_browser_render_process_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use cef::rc::{Rc, RcImpl};
use cef::sys::{_cef_render_process_handler_t, cef_base_ref_counted_t};
use cef::{CefString, ImplRenderProcessHandler, ImplV8Context, ImplV8Value, V8Handler, V8Propertyattribute, WrapRenderProcessHandler, v8_value_create_function};

use crate::cef::internal::non_browser_v8_handler::NonBrowserV8HandlerImpl;

pub(crate) struct NonBrowserRenderProcessHandlerImpl {
object: *mut RcImpl<_cef_render_process_handler_t, Self>,
}
impl NonBrowserRenderProcessHandlerImpl {
pub(crate) fn new() -> Self {
Self { object: std::ptr::null_mut() }
}
}

impl ImplRenderProcessHandler for NonBrowserRenderProcessHandlerImpl {
fn on_context_created(&self, _browser: Option<&mut cef::Browser>, _frame: Option<&mut cef::Frame>, context: Option<&mut cef::V8Context>) {
let Some(context) = context else {
tracing::event!(tracing::Level::ERROR, "No browser in RenderProcessHandlerImpl::on_context_created");
return;
};
let mut v8_handler = V8Handler::new(NonBrowserV8HandlerImpl::new());
let Some(mut function) = v8_value_create_function(Some(&CefString::from("sendMessageToCef")), Some(&mut v8_handler)) else {
tracing::event!(tracing::Level::ERROR, "Failed to create V8 function");
return;
};
let Some(global) = context.global() else {
tracing::event!(tracing::Level::ERROR, "No global object in RenderProcessHandlerImpl::on_context_created");
return;
};

global.set_value_bykey(Some(&CefString::from("sendMessageToCef")), Some(&mut function), V8Propertyattribute::default());
}

fn get_raw(&self) -> *mut _cef_render_process_handler_t {
self.object.cast()
}
}

impl Clone for NonBrowserRenderProcessHandlerImpl {
fn clone(&self) -> Self {
unsafe {
let rc_impl = &mut *self.object;
rc_impl.interface.add_ref();
}
Self { object: self.object }
}
}
impl Rc for NonBrowserRenderProcessHandlerImpl {
fn as_base(&self) -> &cef_base_ref_counted_t {
unsafe {
let base = &*self.object;
std::mem::transmute(&base.cef_object)
}
}
}
impl WrapRenderProcessHandler for NonBrowserRenderProcessHandlerImpl {
fn wrap_rc(&mut self, object: *mut RcImpl<_cef_render_process_handler_t, Self>) {
self.object = object;
}
}
Loading
Loading