diff --git a/docs/index.html b/docs/index.html index dae8cc64..e228ed34 100644 --- a/docs/index.html +++ b/docs/index.html @@ -133,7 +133,11 @@ console.debug("wasm loaded. starting app…"); // This call installs a bunch of callbacks and then returns: - wasm_bindgen.start("the_canvas_id"); + const handle = wasm_bindgen.start("the_canvas_id"); + + // call `handle.stop_web()` to stop + // uncomment to quick result + // setTimeout(() => {handle.stop_web(); handle.free())}, 2000) console.debug("app started."); document.getElementById("center_text").remove(); diff --git a/docs/multiple_apps.html b/docs/multiple_apps.html new file mode 100644 index 00000000..e08659c0 --- /dev/null +++ b/docs/multiple_apps.html @@ -0,0 +1,200 @@ + + + + + + + + + egui – An immediate mode GUI written in Rust + + + + + + +
controls
+ + + +
+ +
+ +
+ +
+ + + +
+

+ Loading… +

+
+
+ + + + + + + + + + + + diff --git a/eframe/CHANGELOG.md b/eframe/CHANGELOG.md index b61a4a25..c7b5fb21 100644 --- a/eframe/CHANGELOG.md +++ b/eframe/CHANGELOG.md @@ -28,6 +28,8 @@ NOTE: [`egui-winit`](../egui-winit/CHANGELOG.md), [`egui_glium`](../egui_glium/C #### Web: * Added option to select WebGL version ([#1803](https://github.com/emilk/egui/pull/1803)). +* Added ability to stop/re-run web app from JavaScript ([#1803](https://github.com/emilk/egui/pull/1650)). This change is also finds parent html container and resizes canvas to it's size. So settings `html, body: [height: 100%; width: 100%;]` for older demos is required. + ## 0.18.0 - 2022-04-30 diff --git a/egui_demo_app/src/lib.rs b/egui_demo_app/src/lib.rs index 61a6525a..a0ebb030 100644 --- a/egui_demo_app/src/lib.rs +++ b/egui_demo_app/src/lib.rs @@ -40,16 +40,46 @@ impl WebHandle { #[wasm_bindgen] #[cfg(target_arch = "wasm32")] pub fn stop_web(&self) -> Result<(), wasm_bindgen::JsValue> { - let res = self.handle.lock().destroy(); + let mut app = self.handle.lock(); + let res = app.destroy(); - // let numw = Arc::weak_count(&uu); - // let nums = Arc::strong_count(&uu); + // let numw = Arc::weak_count(&app); + // let nums = Arc::strong_count(&app); // tracing::debug!("runner ref {:?}, {:?}", numw, nums); res } } + + + +#[cfg(target_arch = "wasm32")] +#[wasm_bindgen] +pub fn init_wasm_hooks(){ + // Make sure panics are logged using `console.error`. + console_error_panic_hook::set_once(); + + // Redirect tracing to console.log and friends: + tracing_wasm::set_as_global_default(); +} + + +#[cfg(target_arch = "wasm32")] +#[wasm_bindgen] +pub fn start_separate(canvas_id: &str) -> Result { + let web_options = eframe::WebOptions::default(); + let handle = eframe::start_web( + canvas_id, + web_options, + Box::new(|cc| Box::new(WrapApp::new(cc))), + ) + .map(|handle| WebHandle { handle }); + + handle +} + + /// This is the entry-point for all the web-assembly. /// This is called once from the HTML. /// It loads the app, installs some callbacks, then returns. @@ -57,19 +87,7 @@ impl WebHandle { #[cfg(target_arch = "wasm32")] #[wasm_bindgen] pub fn start(canvas_id: &str) -> Result { - // Make sure panics are logged using `console.error`. - console_error_panic_hook::set_once(); - // Redirect tracing to console.log and friends: - tracing_wasm::set_as_global_default(); - - let web_options = eframe::WebOptions::default(); - let handle = eframe::start_web( - canvas_id, - web_options, - Box::new(|cc| Box::new(WrapApp::new(cc))), - ) - .map(|handle| WebHandle { handle }); - - handle + init_wasm_hooks(); + start_separate(canvas_id) }