Skip to content

Commit

Permalink
Make eframe::App::as_any_mut optional to implement (#2061)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk authored Sep 20, 2022
1 parent 311eb66 commit 2b0bf82
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 58 deletions.
2 changes: 2 additions & 0 deletions crates/eframe/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ NOTE: [`egui-winit`](../egui-winit/CHANGELOG.md), [`egui_glium`](../egui_glium/C
* Added `shader_version` to `NativeOptions` for cross compiling support on different target OpenGL | ES versions (on native `glow` renderer only) ([#1993](https://github.com/emilk/egui/pull/1993)).
* Fix: app state is now saved when user presses Cmd-Q on Mac ([#2013](https://github.com/emilk/egui/pull/2013)).
* Added `center` to `NativeOptions` and `monitor_size` to `WindowInfo` on desktop ([#2035](https://github.com/emilk/egui/pull/2035)).
* Web: you can access your application from JS using `AppRunner::app_mut`. See `crates/egui_demo_app/src/lib.rs`.


## 0.19.0 - 2022-08-20
* MSRV (Minimum Supported Rust Version) is now `1.61.0` ([#1846](https://github.com/emilk/egui/pull/1846)).
Expand Down
17 changes: 11 additions & 6 deletions crates/eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,25 @@ 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.
/// Get a 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.
/// Can be used from web to interact or other external context.
///
/// You need to implement this if you want to be able to access the application from JS using [`AppRunner::app_mut`].
///
/// This is needed because downcasting Box<dyn App> -> Box<dyn Any> to get &ConcreteApp is not simple in current rust.
///
/// Just copy-paste this as your implementation:
/// ```ignore
/// #[cfg(target_arch = "wasm32")]
/// fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
/// &mut *self
/// fn as_any_mut(&mut self) -> Option<&mut dyn std::any::Any> {
/// Some(&mut *self)
/// }
/// ```
#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> &mut dyn Any;
fn as_any_mut(&mut self) -> Option<&mut dyn Any> {
None
}

/// Called on shutdown, and perhaps at regular intervals. Allows you to save state.
///
Expand Down
8 changes: 7 additions & 1 deletion crates/eframe/src/web/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,14 @@ impl AppRunner {
}

/// Get mutable access to the concrete [`App`] we enclose.
///
/// This will panic if your app does not implement [`App::as_any_mut`].
pub fn app_mut<ConreteApp: 'static + crate::App>(&mut self) -> &mut ConreteApp {
self.app.as_any_mut().downcast_mut::<ConreteApp>().unwrap()
self.app
.as_any_mut()
.expect("Your app must implement `as_any_mut`, but it doesn't")
.downcast_mut::<ConreteApp>()
.unwrap()
}

pub fn auto_save(&mut self) {
Expand Down
8 changes: 0 additions & 8 deletions crates/egui_demo_app/src/apps/custom3d_glow.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
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 @@ -51,11 +48,6 @@ 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: 0 additions & 8 deletions crates/egui_demo_app/src/apps/custom3d_wgpu.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
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 @@ -120,11 +117,6 @@ 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: 0 additions & 8 deletions crates/egui_demo_app/src/apps/http_app.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
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 @@ -109,11 +106,6 @@ 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
24 changes: 2 additions & 22 deletions crates/egui_demo_app/src/wrap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ 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 @@ -35,11 +30,6 @@ 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 @@ -59,11 +49,6 @@ 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 @@ -88,11 +73,6 @@ impl eframe::App for ColorTestApp {
});
});
}

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

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -250,8 +230,8 @@ impl eframe::App for WrapApp {
}

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

Expand Down
5 changes: 0 additions & 5 deletions examples/custom_3d_three-d/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ 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`].
Expand Down

0 comments on commit 2b0bf82

Please sign in to comment.