Skip to content

Commit ebc4fc8

Browse files
enomadoemilk
andauthored
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>
1 parent e0c7533 commit ebc4fc8

File tree

8 files changed

+80
-1
lines changed

8 files changed

+80
-1
lines changed

crates/eframe/src/epi.rs

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
77
#![warn(missing_docs)] // Let's keep `epi` well-documented.
88

9+
#[cfg(target_arch = "wasm32")]
10+
use std::any::Any;
11+
912
#[cfg(not(target_arch = "wasm32"))]
1013
pub use crate::native::run::RequestRepaintEvent;
1114
#[cfg(not(target_arch = "wasm32"))]
@@ -66,6 +69,15 @@ pub trait App {
6669
/// To force a repaint, call [`egui::Context::request_repaint`] at any time (e.g. from another thread).
6770
fn update(&mut self, ctx: &egui::Context, frame: &mut Frame);
6871

72+
/// Handle to the app.
73+
///
74+
/// Can be used from web to interact or other external context
75+
/// Implementation is needed, because downcasting Box<dyn App> -> Box<dyn Any> to get &ConcreteApp is not simple in current rust.
76+
///
77+
/// Just return &mut *self
78+
#[cfg(target_arch = "wasm32")]
79+
fn as_any_mut(&mut self) -> &mut dyn Any;
80+
6981
/// Called on shutdown, and perhaps at regular intervals. Allows you to save state.
7082
///
7183
/// Only called when the "persistence" feature is enabled.

crates/eframe/src/web/backend.rs

+5
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ impl AppRunner {
265265
&self.egui_ctx
266266
}
267267

268+
/// Get mutable access to the concrete [`App`] we enclose.
269+
pub fn app_mut<ConreteApp: 'static + crate::App>(&mut self) -> &mut ConreteApp {
270+
self.app.as_any_mut().downcast_mut::<ConreteApp>().unwrap()
271+
}
272+
268273
pub fn auto_save(&mut self) {
269274
let now = now_sec();
270275
let time_since_last_save = now - self.last_save_time;

crates/egui_demo_app/src/apps/custom3d_glow.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use std::sync::Arc;
22

3+
#[cfg(target_arch = "wasm32")]
4+
use core::any::Any;
5+
36
use eframe::egui_glow;
47
use egui::mutex::Mutex;
58
use egui_glow::glow;
@@ -48,6 +51,11 @@ impl eframe::App for Custom3d {
4851
self.rotating_triangle.lock().destroy(gl);
4952
}
5053
}
54+
55+
#[cfg(target_arch = "wasm32")]
56+
fn as_any_mut(&mut self) -> &mut dyn Any {
57+
&mut *self
58+
}
5159
}
5260

5361
impl Custom3d {

crates/egui_demo_app/src/apps/custom3d_wgpu.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use std::sync::Arc;
22

3+
#[cfg(target_arch = "wasm32")]
4+
use core::any::Any;
5+
36
use eframe::{
47
egui_wgpu::{self, wgpu},
58
wgpu::util::DeviceExt,
@@ -117,6 +120,11 @@ impl eframe::App for Custom3d {
117120
});
118121
});
119122
}
123+
124+
#[cfg(target_arch = "wasm32")]
125+
fn as_any_mut(&mut self) -> &mut dyn Any {
126+
&mut *self
127+
}
120128
}
121129

122130
impl Custom3d {

crates/egui_demo_app/src/apps/http_app.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use egui_extras::RetainedImage;
22
use poll_promise::Promise;
33

4+
#[cfg(target_arch = "wasm32")]
5+
use core::any::Any;
6+
47
struct Resource {
58
/// HTTP response
69
response: ehttp::Response,
@@ -106,6 +109,11 @@ impl eframe::App for HttpApp {
106109
}
107110
});
108111
}
112+
113+
#[cfg(target_arch = "wasm32")]
114+
fn as_any_mut(&mut self) -> &mut dyn Any {
115+
&mut *self
116+
}
109117
}
110118

111119
fn ui_url(ui: &mut egui::Ui, frame: &mut eframe::Frame, url: &mut String) -> bool {

crates/egui_demo_app/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ pub struct WebHandle {
3232
#[wasm_bindgen]
3333
impl WebHandle {
3434
#[wasm_bindgen]
35-
#[cfg(target_arch = "wasm32")]
3635
pub fn stop_web(&self) -> Result<(), wasm_bindgen::JsValue> {
3736
let mut app = self.handle.lock();
3837
let res = app.destroy();
@@ -43,6 +42,12 @@ impl WebHandle {
4342

4443
res
4544
}
45+
46+
#[wasm_bindgen]
47+
pub fn set_some_content_from_javasript(&mut self, _some_data: &str) {
48+
let _app = self.handle.lock().app_mut::<WrapApp>();
49+
// _app.data = some_data;
50+
}
4651
}
4752

4853
#[cfg(target_arch = "wasm32")]

crates/egui_demo_app/src/wrap_app.rs

+28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ use egui_demo_lib::is_mobile;
33
#[cfg(feature = "glow")]
44
use eframe::glow;
55

6+
#[cfg(target_arch = "wasm32")]
7+
use core::any::Any;
8+
69
#[derive(Default)]
710
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
811
struct EasyMarkApp {
@@ -13,6 +16,11 @@ impl eframe::App for EasyMarkApp {
1316
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
1417
self.editor.panels(ctx);
1518
}
19+
20+
#[cfg(target_arch = "wasm32")]
21+
fn as_any_mut(&mut self) -> &mut dyn Any {
22+
&mut *self
23+
}
1624
}
1725

1826
// ----------------------------------------------------------------------------
@@ -27,6 +35,11 @@ impl eframe::App for DemoApp {
2735
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
2836
self.demo_windows.ui(ctx);
2937
}
38+
39+
#[cfg(target_arch = "wasm32")]
40+
fn as_any_mut(&mut self) -> &mut dyn Any {
41+
&mut *self
42+
}
3043
}
3144

3245
// ----------------------------------------------------------------------------
@@ -46,6 +59,11 @@ impl eframe::App for FractalClockApp {
4659
.ui(ui, Some(crate::seconds_since_midnight()));
4760
});
4861
}
62+
63+
#[cfg(target_arch = "wasm32")]
64+
fn as_any_mut(&mut self) -> &mut dyn Any {
65+
&mut *self
66+
}
4967
}
5068

5169
// ----------------------------------------------------------------------------
@@ -70,6 +88,11 @@ impl eframe::App for ColorTestApp {
7088
});
7189
});
7290
}
91+
92+
#[cfg(target_arch = "wasm32")]
93+
fn as_any_mut(&mut self) -> &mut dyn Any {
94+
&mut *self
95+
}
7396
}
7497

7598
// ----------------------------------------------------------------------------
@@ -225,6 +248,11 @@ impl eframe::App for WrapApp {
225248
custom3d.on_exit(gl);
226249
}
227250
}
251+
252+
#[cfg(target_arch = "wasm32")]
253+
fn as_any_mut(&mut self) -> &mut dyn Any {
254+
&mut *self
255+
}
228256
}
229257

230258
impl WrapApp {

examples/custom_3d_three-d/src/main.rs

+5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ impl eframe::App for MyApp {
7070
});
7171
});
7272
}
73+
74+
#[cfg(target_arch = "wasm32")]
75+
fn as_any_mut(&mut self) -> &mut dyn Any {
76+
&mut *self
77+
}
7378
}
7479

7580
/// We get a [`glow::Context`] from `eframe` and we want to construct a [`ThreeDApp`].

0 commit comments

Comments
 (0)