1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Make eframe::App::as_any_mut optional to implement (#2061)

This commit is contained in:
Emil Ernerfeldt
2022-09-20 13:58:55 +02:00
committed by GitHub
parent 311eb66cae
commit 2b0bf82b51
8 changed files with 22 additions and 58 deletions

View File

@@ -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)).

View File

@@ -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.
///

View File

@@ -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) {