Fix panic with persistence without window (#3167)

A window may not always be available and may have already been closed by the time an eframe app is closing. An example of this is Android, where the main activity window may have been stopped or discarded because the app is no longer in the foreground, and then the user decides to close your app without resuming it using the multitasking view.

In this case, skip the window persistence step if it does not exist anymore by the time we are saving the persistence data. Currently eframe will panic with `winit window doesn't exist` instead.
This commit is contained in:
Stephen M. Coakley 2023-08-09 05:42:43 -05:00 committed by GitHub
parent 67ba4f2811
commit 486cff8ac3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 13 deletions

View File

@ -562,24 +562,26 @@ impl EpiIntegration {
pub fn maybe_autosave(&mut self, app: &mut dyn epi::App, window: &winit::window::Window) {
let now = std::time::Instant::now();
if now - self.last_auto_save > app.auto_save_interval() {
self.save(app, window);
self.save(app, Some(window));
self.last_auto_save = now;
}
}
#[allow(clippy::unused_self)]
pub fn save(&mut self, _app: &mut dyn epi::App, _window: &winit::window::Window) {
pub fn save(&mut self, _app: &mut dyn epi::App, _window: Option<&winit::window::Window>) {
#[cfg(feature = "persistence")]
if let Some(storage) = self.frame.storage_mut() {
crate::profile_function!();
if _app.persist_native_window() {
crate::profile_scope!("native_window");
epi::set_value(
storage,
STORAGE_WINDOW_KEY,
&WindowSettings::from_display(_window),
);
if let Some(window) = _window {
if _app.persist_native_window() {
crate::profile_scope!("native_window");
epi::set_value(
storage,
STORAGE_WINDOW_KEY,
&WindowSettings::from_display(window),
);
}
}
if _app.persist_egui_memory() {
crate::profile_scope!("egui_memory");

View File

@ -801,7 +801,7 @@ mod glow_integration {
if let Some(mut running) = self.running.take() {
running
.integration
.save(running.app.as_mut(), running.gl_window.window());
.save(running.app.as_mut(), running.gl_window.window.as_ref());
running.app.on_exit(Some(&running.gl));
running.painter.destroy();
}
@ -1260,9 +1260,9 @@ mod wgpu_integration {
fn save_and_destroy(&mut self) {
if let Some(mut running) = self.running.take() {
if let Some(window) = &self.window {
running.integration.save(running.app.as_mut(), window);
}
running
.integration
.save(running.app.as_mut(), self.window.as_ref());
#[cfg(feature = "glow")]
running.app.on_exit(None);