Skip to content

Commit

Permalink
handle for app from WehHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
enomado committed Aug 3, 2022
1 parent a827c3e commit 6e36ca7
Show file tree
Hide file tree
Showing 10 changed files with 594 additions and 384 deletions.
884 changes: 501 additions & 383 deletions docs/egui_demo_app.js

Large diffs are not rendered by default.

Binary file modified docs/egui_demo_app_bg.wasm
Binary file not shown.
12 changes: 12 additions & 0 deletions eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#![warn(missing_docs)] // Let's keep `epi` well-documented.

#[cfg(target_arch = "wasm32")]
use std::any::Any;

/// This is how your app is created.
///
/// You can use the [`CreationContext`] to setup egui, restore state, setup OpenGL things, etc.
Expand Down Expand Up @@ -49,6 +52,15 @@ pub trait App {
/// 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);

// 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.
///
/// Only called when the "persistence" feature is enabled.
Expand Down
6 changes: 5 additions & 1 deletion eframe/src/web/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl WebInput {

// ----------------------------------------------------------------------------

use std::sync::atomic::Ordering::SeqCst;
use std::{any::Any, sync::atomic::Ordering::SeqCst};

/// Stores when to do the next repaint.
pub struct NeedRepaint(Mutex<f64>);
Expand Down Expand Up @@ -265,6 +265,10 @@ impl AppRunner {
&self.egui_ctx
}

pub fn get_app_mut(&mut self) -> &mut dyn Any {
self.app.as_any_mut()
}

pub fn auto_save(&mut self) {
let now = now_sec();
let time_since_last_save = now - self.last_save_time;
Expand Down
8 changes: 8 additions & 0 deletions egui_demo_app/src/apps/custom3d_glow.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::sync::Arc;

#[cfg(target_arch = "wasm32")]
use core::any::Any;

use eframe::egui_glow;
use egui::mutex::Mutex;
use egui_glow::glow;
Expand Down Expand Up @@ -49,6 +52,11 @@ impl eframe::App for Custom3d {
self.rotating_triangle.lock().destroy(gl);
}
}

#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
}

impl Custom3d {
Expand Down
8 changes: 8 additions & 0 deletions egui_demo_app/src/apps/custom3d_wgpu.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::sync::Arc;

#[cfg(target_arch = "wasm32")]
use core::any::Any;

use eframe::{
egui_wgpu::{self, wgpu},
wgpu::util::DeviceExt,
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions egui_demo_app/src/apps/http_app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use egui_extras::RetainedImage;
use poll_promise::Promise;

#[cfg(target_arch = "wasm32")]
use core::any::Any;

struct Resource {
/// HTTP response
response: ehttp::Response,
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions egui_demo_app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ impl WebHandle {

res
}

// helper for mutating original app from javascript
fn with_app<F>(&mut self, func: F) -> ()
where
F: Fn(&mut WrapApp) -> (),
{
let mut runner_ref = self.handle.lock();
let app_ref = runner_ref.get_app_mut();
let app = app_ref.downcast_mut::<WrapApp>().unwrap();
func(app);
}

#[wasm_bindgen]
#[cfg(target_arch = "wasm32")]
pub fn set_some_content_from_javasript(&mut self, _some_data: &str) {
self.with_app(|_app| {
// app.data = some_data;
});
}
}

#[cfg(target_arch = "wasm32")]
Expand Down
28 changes: 28 additions & 0 deletions egui_demo_app/src/wrap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use egui_demo_lib::is_mobile;
#[cfg(feature = "glow")]
use eframe::glow;

#[cfg(target_arch = "wasm32")]
use core::any::Any;

#[derive(Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
struct EasyMarkApp {
Expand All @@ -13,6 +16,11 @@ impl eframe::App for EasyMarkApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
self.editor.panels(ctx);
}

#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
}

// ----------------------------------------------------------------------------
Expand All @@ -27,6 +35,11 @@ impl eframe::App for DemoApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
self.demo_windows.ui(ctx);
}

#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
}

// ----------------------------------------------------------------------------
Expand All @@ -46,6 +59,11 @@ impl eframe::App for FractalClockApp {
.ui(ui, Some(crate::seconds_since_midnight()));
});
}

#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
}

// ----------------------------------------------------------------------------
Expand All @@ -70,6 +88,11 @@ impl eframe::App for ColorTestApp {
});
});
}

#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -214,6 +237,11 @@ impl eframe::App for WrapApp {
fn on_exit(&mut self, gl: Option<&glow::Context>) {
self.custom3d.on_exit(gl);
}

#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
}

impl WrapApp {
Expand Down
5 changes: 5 additions & 0 deletions examples/custom_3d_three-d/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ impl eframe::App for MyApp {
});
});
}

#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any {
&mut *self
}
}

impl MyApp {
Expand Down

0 comments on commit 6e36ca7

Please sign in to comment.