Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Lost window device: quitting goes through while recovery can't be saved (#8620)

Mathieu Rancourt5004d96acb28
diff --git a/crates/ui-egui/src/discard_ui.rs b/crates/ui-egui/src/discard_ui.rs
index 263f3652b..a6a65f29f 100644
--- a/crates/ui-egui/src/discard_ui.rs
+++ b/crates/ui-egui/src/discard_ui.rs
@@ -71,6 +71,14 @@ pub fn intercept(app: &mut PhotocraftApp, id: &str, params: &Value) -> bool {
if docs.is_empty() {
return false;
}
+ // Once the window's GPU device is lost nothing can be drawn (`gpu_status::check_window`), so
+ // the prompt would never show. While saving for recovery keeps failing, quitting (the close
+ // button, File › Exit) must still go through rather than leave a blank window that can't be
+ // closed.
+ if id == EXIT && app.window_lost {
+ log::warn!("quitting a window that lost its GPU device before its unsaved work could be saved for recovery");
+ return false;
+ }
let prompt = Prompt { id: id.to_string(), params: params.clone(), target, docs, focused: None };
match &app.discard {
None => ask(app, prompt),
@@ -1042,6 +1050,41 @@ mod tests {
assert!(app.discard.is_some());
}
+ /// After the window's GPU device is lost nothing can be drawn, so a prompt could never be seen
+ /// or answered. While unsaved work can't be written for recovery (the save keeps retrying), the
+ /// close button must still quit, instead of opening a prompt that never shows.
+ #[test]
+ fn window_close_goes_through_once_the_window_device_is_lost() {
+ let mut app = app_with_docs(1);
+ make_dirty(&mut app, 0);
+ app.window_lost = true;
+ assert!(!press_window_close(&mut app), "the close isn't cancelled");
+ assert!(app.discard.is_none(), "no prompt opened");
+ }
+
+ /// The title bar's close button and File › Exit run `file.exit` through `menus::invoke`, which
+ /// parks it behind the prompt before any close request is made: once the window's device is
+ /// lost they must close instead.
+ #[test]
+ fn file_exit_quits_without_a_prompt_once_the_window_device_is_lost() {
+ let exit = |app: &mut PhotocraftApp| {
+ let mut out = egui::Context::default().run_ui(egui::RawInput::default(), |ui| {
+ let _ = crate::menus::invoke(app, ui.ctx(), EXIT, json!({}));
+ });
+ out.textures_delta.clear();
+ let commands = out.viewport_output.remove(&egui::ViewportId::ROOT).map(|o| o.commands).unwrap_or_default();
+ commands.iter().any(|c| matches!(c, egui::ViewportCommand::Close))
+ };
+ let mut app = app_with_docs(1);
+ make_dirty(&mut app, 0);
+ assert!(!exit(&mut app), "a window that can draw asks first");
+ assert!(app.discard.is_some());
+ app.discard = None;
+ app.window_lost = true;
+ assert!(exit(&mut app), "the window is asked to close");
+ assert!(app.discard.is_none(), "no prompt opened");
+ }
+
#[test]
fn unsaved_work_is_any_dirty_document_until_closing_is_confirmed() {
let mut app = app_with_docs(2);
diff --git a/crates/ui-egui/src/titlebar.rs b/crates/ui-egui/src/titlebar.rs
index dc659840b..e81adb9f1 100644
--- a/crates/ui-egui/src/titlebar.rs
+++ b/crates/ui-egui/src/titlebar.rs
@@ -729,6 +729,25 @@ mod tests {
assert!(cmds.iter().any(|c| matches!(c, ViewportCommand::Close)), "{cmds:?}");
}
+ /// After the window's GPU device is lost, the prompt asking about unsaved work could never be
+ /// drawn: the close button quits instead of opening it (see `discard_ui::intercept`).
+ #[test]
+ fn close_quits_without_a_prompt_once_the_window_device_is_lost() {
+ let mut w = Win::new(1100.0);
+ w.app.run("file.new", json!({"width": 8, "height": 8})).unwrap();
+ w.app.run("layer.new.layer", json!({})).unwrap();
+ w.frame(vec![]);
+ let close = w.caption(Caption::Close).center();
+ let asks = w.click(close);
+ assert!(!asks.iter().any(|c| matches!(c, ViewportCommand::Close)), "a window that can draw asks first: {asks:?}");
+ assert!(w.app.discard.is_some());
+ w.app.discard = None;
+ w.app.window_lost = true;
+ let quits = w.click(close);
+ assert!(quits.iter().any(|c| matches!(c, ViewportCommand::Close)), "{quits:?}");
+ assert!(w.app.discard.is_none(), "no prompt opened");
+ }
+
#[test]
fn minimize_and_maximize_follow_the_window_state() {
let mut w = Win::new(1100.0);