Execute editor natively

This commit is contained in:
Adam
2025-07-24 04:23:33 -07:00
parent 4fec24893e
commit 0c60ebaabc
21 changed files with 296 additions and 29 deletions

View File

@@ -12,6 +12,7 @@
"production": "npm run setup && npm run wasm:build-production && concurrently -k -n \"VITE,RUST\" \"vite\" \"npm run wasm:watch-production\"",
"---------- BUILDS ----------": "",
"build-dev": "npm run wasm:build-dev && vite build",
"build-native": "npm run native:build-dev && vite build",
"build-profiling": "npm run wasm:build-profiling && vite build",
"build": "npm run wasm:build-production && vite build",
"---------- UTILITIES ----------": "",
@@ -19,6 +20,7 @@
"lint-fix": "eslint . --fix && tsc --noEmit",
"---------- INTERNAL ----------": "",
"setup": "node package-installer.js",
"native:build-dev": "wasm-pack build ./wasm --dev --target=web --features native",
"wasm:build-dev": "wasm-pack build ./wasm --dev --target=web",
"wasm:build-profiling": "wasm-pack build ./wasm --profiling --target=web",
"wasm:build-production": "wasm-pack build ./wasm --release --target=web",

View File

@@ -5,6 +5,8 @@
import Editor from "@graphite/components/Editor.svelte";
import { send_message_to_cef } from "/wasm/pkg/graphite_wasm";
let editor: GraphiteEditor | undefined = undefined;
onMount(async () => {
@@ -17,6 +19,9 @@
// Destroy the WASM editor handle
editor?.handle.free();
});
console.log("Test from app.svelte javascript");
sendMessageToCef("Test from app direct");
</script>
{#if editor !== undefined}

View File

@@ -48,6 +48,7 @@
onMount(() => {
// Initialize certain setup tasks required by the editor backend to be ready for the user now that the frontend is ready
console.log("init after frontend ready from js");
editor.handle.initAfterFrontendReady(operatingSystem());
});

View File

@@ -171,7 +171,7 @@
function canvasPointerDown(e: PointerEvent) {
const onEditbox = e.target instanceof HTMLDivElement && e.target.contentEditable;
console.log("Canvas pointer down", e, onEditbox);
if (!onEditbox) viewport?.setPointerCapture(e.pointerId);
if (window.document.activeElement instanceof HTMLElement) {
window.document.activeElement.blur();

View File

@@ -13,7 +13,7 @@ license = "Apache-2.0"
[features]
default = ["gpu"]
gpu = ["editor/gpu"]
tauri = ["editor/tauri"]
native = []
[lib]
crate-type = ["cdylib", "rlib"]
@@ -31,6 +31,7 @@ graphene-std = { workspace = true }
graph-craft = { workspace = true }
log = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
wasm-bindgen = { workspace = true }
serde-wasm-bindgen = { workspace = true }
js-sys = { workspace = true }

View File

@@ -154,6 +154,7 @@ impl EditorHandle {
}
// Sends a message to the dispatcher in the Editor Backend
#[cfg(not(feature = "native"))]
fn dispatch<T: Into<Message>>(&self, message: T) {
// Process no further messages after a crash to avoid spamming the console
if EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {
@@ -169,6 +170,16 @@ impl EditorHandle {
}
}
#[cfg(feature = "native")]
fn dispatch<T: Into<Message>>(&self, message: T) {
let message: Message = message.into();
let Ok(serialized_message) = serde_json::to_string(&message) else {
log::error!("Failed to serialize message");
return;
};
crate::send_message_to_cef(serialized_message)
}
// Sends a FrontendMessage to JavaScript
fn send_frontend_message_to_js(&self, mut message: FrontendMessage) {
if let FrontendMessage::UpdateImageData { ref image_data } = message {
@@ -202,11 +213,17 @@ impl EditorHandle {
}
}
#[cfg(feature = "native")]
#[wasm_bindgen(js_name = initAfterFrontendReady)]
pub fn init_after_frontend_ready(&self, platform: String) {
log::debug!("Init after frontend ready from rust");
}
// ========================================================================
// Add additional JS -> Rust wrapper functions below as needed for calling
// the backend from the web frontend.
// ========================================================================
#[cfg(not(feature = "native"))]
#[wasm_bindgen(js_name = initAfterFrontendReady)]
pub fn init_after_frontend_ready(&self, platform: String) {
// Send initialization messages

View File

@@ -27,6 +27,7 @@ thread_local! {
#[wasm_bindgen(start)]
pub fn init_graphite() {
// Set up the panic hook
#[cfg(not(feature = "native"))]
panic::set_hook(Box::new(panic_hook));
// Set up the logger with a default level of debug
@@ -105,6 +106,24 @@ extern "C" {
fn trace(msg: &str, format: &str);
}
#[wasm_bindgen]
extern "C" {
fn sendMessageToCefFromWasm(message: String);
}
#[wasm_bindgen]
pub fn send_message_to_cef(message: String) {
let global = js_sys::global();
// Get the function by name
let func = js_sys::Reflect::get(&global, &JsValue::from_str("sendMessageToCef")).expect("Function not found");
let func = func.dyn_into::<js_sys::Function>().expect("Not a function");
// Call it with argument
func.call1(&JsValue::NULL, &JsValue::from_str(&message)).expect("Function call failed");
}
#[derive(Default)]
pub struct WasmLog;