69 lines
1.8 KiB
Rust
69 lines
1.8 KiB
Rust
#![allow(clippy::mem_forget)] // False positives from #[wasm_bindgen] macro
|
|
|
|
use eframe::wasm_bindgen::{self, prelude::*};
|
|
|
|
use crate::WrapApp;
|
|
|
|
/// Our handle to the web app from JavaScript.
|
|
#[derive(Clone)]
|
|
#[wasm_bindgen]
|
|
pub struct WebHandle {
|
|
runner: eframe::WebRunner,
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
impl WebHandle {
|
|
/// Installs a panic hook, then returns.
|
|
#[allow(clippy::new_without_default)]
|
|
#[wasm_bindgen(constructor)]
|
|
pub fn new() -> Self {
|
|
// Redirect [`log`] message to `console.log` and friends:
|
|
eframe::WebLogger::init(log::LevelFilter::Debug).ok();
|
|
|
|
Self {
|
|
runner: eframe::WebRunner::new(),
|
|
}
|
|
}
|
|
|
|
/// Call this once from JavaScript to start your app.
|
|
#[wasm_bindgen]
|
|
pub async fn start(&self, canvas_id: &str) -> Result<(), wasm_bindgen::JsValue> {
|
|
self.runner
|
|
.start(
|
|
canvas_id,
|
|
eframe::WebOptions::default(),
|
|
Box::new(|cc| Box::new(WrapApp::new(cc))),
|
|
)
|
|
.await
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn destroy(&self) {
|
|
self.runner.destroy();
|
|
}
|
|
|
|
/// Example on how to call into your app from JavaScript.
|
|
#[wasm_bindgen]
|
|
pub fn example(&self) {
|
|
if let Some(_app) = self.runner.app_mut::<WrapApp>() {
|
|
// _app.example();
|
|
}
|
|
}
|
|
|
|
/// The JavaScript can check whether or not your app has crashed:
|
|
#[wasm_bindgen]
|
|
pub fn has_panicked(&self) -> bool {
|
|
self.runner.has_panicked()
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn panic_message(&self) -> Option<String> {
|
|
self.runner.panic_summary().map(|s| s.message())
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn panic_callstack(&self) -> Option<String> {
|
|
self.runner.panic_summary().map(|s| s.callstack())
|
|
}
|
|
}
|