eframe web: access app from WebHandle (#1886)

Co-authored-by: Stanislav <enomado@users.noreply.github.com>
Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
Stanislav 2022-09-09 09:22:46 +03:00 committed by GitHub
parent e0c7533ede
commit ebc4fc866d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 80 additions and 1 deletions

View file

@ -6,6 +6,9 @@
#![warn(missing_docs)] // Let's keep `epi` well-documented. #![warn(missing_docs)] // Let's keep `epi` well-documented.
#[cfg(target_arch = "wasm32")]
use std::any::Any;
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
pub use crate::native::run::RequestRepaintEvent; pub use crate::native::run::RequestRepaintEvent;
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
@ -66,6 +69,15 @@ pub trait App {
/// To force a repaint, call [`egui::Context::request_repaint`] at any time (e.g. from another thread). /// To force a repaint, call [`egui::Context::request_repaint`] at any time (e.g. from another thread).
fn update(&mut self, ctx: &egui::Context, frame: &mut Frame); fn update(&mut self, ctx: &egui::Context, frame: &mut Frame);
/// Handle to the app.
///
/// Can be used from web to interact or other external context
/// Implementation is needed, because downcasting Box<dyn App> -> Box<dyn Any> to get &ConcreteApp is not simple in current rust.
///
/// Just return &mut *self
#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any;
/// Called on shutdown, and perhaps at regular intervals. Allows you to save state. /// Called on shutdown, and perhaps at regular intervals. Allows you to save state.
/// ///
/// Only called when the "persistence" feature is enabled. /// Only called when the "persistence" feature is enabled.

View file

@ -265,6 +265,11 @@ impl AppRunner {
&self.egui_ctx &self.egui_ctx
} }
/// Get mutable access to the concrete [`App`] we enclose.
pub fn app_mut<ConreteApp: 'static + crate::App>(&mut self) -> &mut ConreteApp {
self.app.as_any_mut().downcast_mut::<ConreteApp>().unwrap()
}
pub fn auto_save(&mut self) { pub fn auto_save(&mut self) {
let now = now_sec(); let now = now_sec();
let time_since_last_save = now - self.last_save_time; let time_since_last_save = now - self.last_save_time;

View file

@ -1,5 +1,8 @@
use std::sync::Arc; use std::sync::Arc;
#[cfg(target_arch = "wasm32")]
use core::any::Any;
use eframe::egui_glow; use eframe::egui_glow;
use egui::mutex::Mutex; use egui::mutex::Mutex;
use egui_glow::glow; use egui_glow::glow;
@ -48,6 +51,11 @@ impl eframe::App for Custom3d {
self.rotating_triangle.lock().destroy(gl); self.rotating_triangle.lock().destroy(gl);
} }
} }
#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
} }
impl Custom3d { impl Custom3d {

View file

@ -1,5 +1,8 @@
use std::sync::Arc; use std::sync::Arc;
#[cfg(target_arch = "wasm32")]
use core::any::Any;
use eframe::{ use eframe::{
egui_wgpu::{self, wgpu}, egui_wgpu::{self, wgpu},
wgpu::util::DeviceExt, wgpu::util::DeviceExt,
@ -117,6 +120,11 @@ impl eframe::App for Custom3d {
}); });
}); });
} }
#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
} }
impl Custom3d { impl Custom3d {

View file

@ -1,6 +1,9 @@
use egui_extras::RetainedImage; use egui_extras::RetainedImage;
use poll_promise::Promise; use poll_promise::Promise;
#[cfg(target_arch = "wasm32")]
use core::any::Any;
struct Resource { struct Resource {
/// HTTP response /// HTTP response
response: ehttp::Response, response: ehttp::Response,
@ -106,6 +109,11 @@ impl eframe::App for HttpApp {
} }
}); });
} }
#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
} }
fn ui_url(ui: &mut egui::Ui, frame: &mut eframe::Frame, url: &mut String) -> bool { fn ui_url(ui: &mut egui::Ui, frame: &mut eframe::Frame, url: &mut String) -> bool {

View file

@ -32,7 +32,6 @@ pub struct WebHandle {
#[wasm_bindgen] #[wasm_bindgen]
impl WebHandle { impl WebHandle {
#[wasm_bindgen] #[wasm_bindgen]
#[cfg(target_arch = "wasm32")]
pub fn stop_web(&self) -> Result<(), wasm_bindgen::JsValue> { pub fn stop_web(&self) -> Result<(), wasm_bindgen::JsValue> {
let mut app = self.handle.lock(); let mut app = self.handle.lock();
let res = app.destroy(); let res = app.destroy();
@ -43,6 +42,12 @@ impl WebHandle {
res res
} }
#[wasm_bindgen]
pub fn set_some_content_from_javasript(&mut self, _some_data: &str) {
let _app = self.handle.lock().app_mut::<WrapApp>();
// _app.data = some_data;
}
} }
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]

View file

@ -3,6 +3,9 @@ use egui_demo_lib::is_mobile;
#[cfg(feature = "glow")] #[cfg(feature = "glow")]
use eframe::glow; use eframe::glow;
#[cfg(target_arch = "wasm32")]
use core::any::Any;
#[derive(Default)] #[derive(Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
struct EasyMarkApp { struct EasyMarkApp {
@ -13,6 +16,11 @@ impl eframe::App for EasyMarkApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
self.editor.panels(ctx); self.editor.panels(ctx);
} }
#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -27,6 +35,11 @@ impl eframe::App for DemoApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
self.demo_windows.ui(ctx); self.demo_windows.ui(ctx);
} }
#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -46,6 +59,11 @@ impl eframe::App for FractalClockApp {
.ui(ui, Some(crate::seconds_since_midnight())); .ui(ui, Some(crate::seconds_since_midnight()));
}); });
} }
#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -70,6 +88,11 @@ impl eframe::App for ColorTestApp {
}); });
}); });
} }
#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -225,6 +248,11 @@ impl eframe::App for WrapApp {
custom3d.on_exit(gl); custom3d.on_exit(gl);
} }
} }
#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
} }
impl WrapApp { impl WrapApp {

View file

@ -70,6 +70,11 @@ impl eframe::App for MyApp {
}); });
}); });
} }
#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
} }
/// We get a [`glow::Context`] from `eframe` and we want to construct a [`ThreeDApp`]. /// We get a [`glow::Context`] from `eframe` and we want to construct a [`ThreeDApp`].