diff --git a/crates/freya-winit/src/lib.rs b/crates/freya-winit/src/lib.rs index 32835f43a..b3d39a902 100644 --- a/crates/freya-winit/src/lib.rs +++ b/crates/freya-winit/src/lib.rs @@ -48,7 +48,32 @@ pub mod tray { /// /// If a custom event loop was provided via [`LaunchConfig::with_event_loop`], it will be used. /// Otherwise a default one is created. -pub fn launch(mut launch_config: LaunchConfig) { +pub fn launch(launch_config: LaunchConfig) { + #[cfg(all(not(debug_assertions), not(target_os = "android")))] + { + let run_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + launch_inner(launch_config); + })); + if let Err(panic_payload) = run_result { + let description = panic_payload + .downcast_ref::<&str>() + .map(|message| message.to_string()) + .or_else(|| panic_payload.downcast_ref::().cloned()) + .unwrap_or_else(|| "The application panicked.".to_string()); + rfd::MessageDialog::new() + .set_title("Fatal Error") + .set_description(&description) + .set_level(rfd::MessageLevel::Error) + .show(); + std::process::exit(1); + } + } + + #[cfg(any(debug_assertions, target_os = "android"))] + launch_inner(launch_config); +} + +fn launch_inner(mut launch_config: LaunchConfig) { use std::collections::HashMap; use freya_core::integration::*; @@ -59,20 +84,6 @@ pub fn launch(mut launch_config: LaunchConfig) { }; use winit::event_loop::EventLoop; - #[cfg(all(not(debug_assertions), not(target_os = "android")))] - { - let previous_hook = std::panic::take_hook(); - std::panic::set_hook(Box::new(move |panic_info| { - rfd::MessageDialog::new() - .set_title("Fatal Error") - .set_description(&panic_info.to_string()) - .set_level(rfd::MessageLevel::Error) - .show(); - previous_hook(panic_info); - std::process::exit(1); - })); - } - let event_loop = launch_config.event_loop.take().unwrap_or_else(|| { EventLoop::::with_user_event() .build()