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

Support returning errors when creating the app (#4565)

The closure passed to `eframe::run_native` now returns a `Result`,
allowing you to return an error during app creation, which will be
returned to the caller of `run_native`.

This means you need to wrap your `Box::new(MyApp::new(…))` in an
`Ok(…)`.

* Closes https://github.com/emilk/egui/issues/4474
This commit is contained in:
Emil Ernerfeldt
2024-05-28 21:59:19 +02:00
committed by GitHub
parent 54429e0549
commit 942fe4ab31
28 changed files with 44 additions and 36 deletions

View File

@@ -41,10 +41,12 @@ pub type EventLoopBuilderHook = Box<dyn FnOnce(&mut EventLoopBuilder<UserEvent>)
#[cfg(any(feature = "glow", feature = "wgpu"))]
pub type WindowBuilderHook = Box<dyn FnOnce(egui::ViewportBuilder) -> egui::ViewportBuilder>;
type DynError = Box<dyn std::error::Error>;
/// This is how your app is created.
///
/// You can use the [`CreationContext`] to setup egui, restore state, setup OpenGL things, etc.
pub type AppCreator = Box<dyn FnOnce(&CreationContext<'_>) -> Box<dyn App>>;
pub type AppCreator = Box<dyn FnOnce(&CreationContext<'_>) -> Result<Box<dyn App>, DynError>>;
/// Data that is passed to [`AppCreator`] that can be used to setup and initialize your app.
pub struct CreationContext<'s> {

View File

@@ -30,7 +30,7 @@
//!
//! fn main() {
//! let native_options = eframe::NativeOptions::default();
//! eframe::run_native("My egui App", native_options, Box::new(|cc| Box::new(MyEguiApp::new(cc))));
//! eframe::run_native("My egui App", native_options, Box::new(|cc| Ok(Box::new(MyEguiApp::new(cc)))));
//! }
//!
//! #[derive(Default)]
@@ -90,7 +90,7 @@
//! .start(
//! canvas_id,
//! eframe::WebOptions::default(),
//! Box::new(|cc| Box::new(MyEguiApp::new(cc))),
//! Box::new(|cc| Ok(Box::new(MyEguiApp::new(cc))),)
//! )
//! .await
//! }
@@ -199,7 +199,7 @@ pub mod icon_data;
///
/// fn main() -> eframe::Result<()> {
/// let native_options = eframe::NativeOptions::default();
/// eframe::run_native("MyApp", native_options, Box::new(|cc| Box::new(MyEguiApp::new(cc))))
/// eframe::run_native("MyApp", native_options, Box::new(|cc| Ok(Box::new(MyEguiApp::new(cc)))))
/// }
///
/// #[derive(Default)]
@@ -324,7 +324,7 @@ pub fn run_simple_native(
run_native(
app_name,
native_options,
Box::new(|_cc| Box::new(SimpleApp { update_fun })),
Box::new(|_cc| Ok(Box::new(SimpleApp { update_fun }))),
)
}
@@ -333,6 +333,9 @@ pub fn run_simple_native(
/// The different problems that can occur when trying to run `eframe`.
#[derive(Debug)]
pub enum Error {
/// Something went wrong in user code when creating the app.
AppCreation(Box<dyn std::error::Error>),
/// An error from [`winit`].
#[cfg(not(target_arch = "wasm32"))]
Winit(winit::error::OsError),
@@ -403,6 +406,8 @@ impl From<egui_wgpu::WgpuError> for Error {
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::AppCreation(err) => write!(f, "app creation error: {err}"),
#[cfg(not(target_arch = "wasm32"))]
Self::Winit(err) => {
write!(f, "winit error: {err}")

View File

@@ -313,7 +313,7 @@ impl GlowWinitApp {
raw_window_handle: window.window_handle().map(|h| h.as_raw()),
};
crate::profile_scope!("app_creator");
app_creator(&cc)
app_creator(&cc).map_err(crate::Error::AppCreation)?
};
let glutin = Rc::new(RefCell::new(glutin));

View File

@@ -182,7 +182,7 @@ impl WgpuWinitApp {
storage: Option<Box<dyn Storage>>,
window: Window,
builder: ViewportBuilder,
) -> Result<&mut WgpuWinitRunning, egui_wgpu::WgpuError> {
) -> crate::Result<&mut WgpuWinitRunning> {
crate::profile_function!();
#[allow(unsafe_code, unused_mut, unused_unsafe)]
@@ -272,7 +272,7 @@ impl WgpuWinitApp {
};
let app = {
crate::profile_scope!("user_app_creator");
app_creator(&cc)
app_creator(&cc).map_err(crate::Error::AppCreation)?
};
let mut viewport_from_window = HashMap::default();

View File

@@ -30,7 +30,7 @@ impl Drop for AppRunner {
impl AppRunner {
/// # Errors
/// Failure to initialize WebGL renderer.
/// Failure to initialize WebGL renderer, or failure to create app.
pub async fn new(
canvas_id: &str,
web_options: crate::WebOptions,
@@ -71,7 +71,7 @@ impl AppRunner {
let theme = system_theme.unwrap_or(web_options.default_theme);
egui_ctx.set_visuals(theme.egui_visuals());
let app = app_creator(&epi::CreationContext {
let cc = epi::CreationContext {
egui_ctx: egui_ctx.clone(),
integration_info: info.clone(),
storage: Some(&storage),
@@ -86,7 +86,8 @@ impl AppRunner {
wgpu_render_state: painter.render_state(),
#[cfg(all(feature = "wgpu", feature = "glow"))]
wgpu_render_state: None,
});
};
let app = app_creator(&cc).map_err(|err| err.to_string())?;
let frame = epi::Frame {
info,

View File

@@ -54,7 +54,7 @@ impl WebRunner {
/// Create the application, install callbacks, and start running the app.
///
/// # Errors
/// Failing to initialize graphics.
/// Failing to initialize graphics, or failure to create app.
pub async fn start(
&self,
canvas_id: &str,