diff --git a/crates/ui-egui/src/lifecycle_tests.rs b/crates/ui-egui/src/lifecycle_tests.rs index 6c9eb85d2..ce230f54c 100644 --- a/crates/ui-egui/src/lifecycle_tests.rs +++ b/crates/ui-egui/src/lifecycle_tests.rs @@ -224,6 +224,60 @@ fn workspace_layout_autosave_writes_on_a_hidden_tick() { assert_eq!(app.ui_passes(), 0); } +/// Whether a frame running `f` asked the root window to close. +#[cfg(not(target_arch = "wasm32"))] +fn asks_to_close(ctx: &egui::Context, mut f: impl FnMut(&egui::Context)) -> bool { + let mut out = ctx.run_ui(raw(egui::vec2(800.0, 480.0)), |ui| f(ui.ctx())); + out.textures_delta.clear(); + out.viewport_output.get(&egui::ViewportId::ROOT).is_some_and(|v| v.commands.contains(&egui::ViewportCommand::Close)) +} + +#[test] +#[cfg(not(target_arch = "wasm32"))] +fn restart_to_update_with_unsaved_changes_opens_the_unsaved_changes_prompt() { + let ctx = egui::Context::default(); + let frame = eframe::Frame::_new_kittest(); + let mut app = app_with_comp(); + assert!(app.session.is_dirty(), "creating a comp and a layer leaves unsaved changes"); + app.updates_rt.manual_window_open = true; + assert!(asks_to_close(&ctx, |ctx| updates_ui::restart_to_update(&mut app.updates_rt, ctx, false)), "the window is asked to close"); + assert!(!app.updates_rt.manual_window_open, "the modal result dialog closes"); + let mut raw = raw(egui::vec2(800.0, 480.0)); + raw.viewports.insert(egui::ViewportId::ROOT, egui::ViewportInfo { events: vec![egui::ViewportEvent::Close], ..Default::default() }); + let out = tick_with(&mut app, &ctx, &frame, &raw); + assert!(app.dialog.is_some(), "the close opens the unsaved-changes prompt"); + assert!(out.viewport_commands.values().flatten().any(|c| matches!(c, egui::ViewportCommand::CancelClose))); +} + +#[test] +#[cfg(not(target_arch = "wasm32"))] +fn restart_to_update_with_nothing_unsaved_leaves_the_close_to_the_updater() { + let ctx = egui::Context::default(); + let mut app = app_with_comp(); + app.updates_rt.manual_window_open = true; + assert!(!asks_to_close(&ctx, |ctx| updates_ui::restart_to_update(&mut app.updates_rt, ctx, true))); + assert!(app.updates_rt.manual_window_open, "the result dialog stays until the updater closes the app"); +} + +#[test] +#[cfg(not(target_arch = "wasm32"))] +fn restart_to_update_with_the_startup_prompt_open_leaves_the_window_open() { + let ctx = egui::Context::default(); + let mut app = app_with_comp(); + app.updates_rt.updater.offer(craft_update::Release { + tag: "v99.0.0".into(), + version: craft_update::parse_version("99.0.0"), + name: "EffectCraft 99.0.0".into(), + html_url: "https://github.com/storytold/effectcraft/releases/tag/v99.0.0".into(), + ..Default::default() + }); + assert!(app.updates_rt.updater.prompt_open()); + app.updates_rt.manual_window_open = true; + assert!(!asks_to_close(&ctx, |ctx| updates_ui::restart_to_update(&mut app.updates_rt, ctx, false)), "the prompt carries the restart"); + assert!(!app.updates_rt.manual_window_open, "the result dialog closes"); + assert!(app.updates_rt.updater.prompt_open(), "the prompt stays"); +} + #[test] fn the_close_guard_answers_a_close_request_without_a_pass() { let ctx = egui::Context::default(); diff --git a/crates/ui-egui/src/updates_ui.rs b/crates/ui-egui/src/updates_ui.rs index 316e34ac4..a4dba938a 100644 --- a/crates/ui-egui/src/updates_ui.rs +++ b/crates/ui-egui/src/updates_ui.rs @@ -9,7 +9,7 @@ use craft_update::updater::{Action, Status, Updater}; pub(crate) struct Runtime { #[cfg(not(target_arch = "wasm32"))] - updater: Updater, + pub(crate) updater: Updater, /// The desktop app turns the check at start on; tests and the web build leave it off. #[cfg(not(target_arch = "wasm32"))] startup_check: bool, @@ -44,6 +44,26 @@ impl EffectcraftApp { } } +/// Restart to Update. With unsaved changes the result dialog closes and the window is asked to +/// close, so the unsaved-changes prompt opens. Cancel there leaves the update ready. An open +/// startup prompt keeps its own Restart to Update and save-first note, so the window stays open. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn restart_to_update(rt: &mut Runtime, ctx: &egui::Context, clean: bool) { + if clean { + rt.updater.restart_to_update(); + return; + } + rt.manual_window_open = false; + rt.updater.dismiss_status(); + if rt.updater.prompt_open() { + return; + } + // Without a prompt this only takes the close a finished installation queued, so a save after + // Cancel does not close the app. + let _ = rt.updater.show_if(ctx, true); + ctx.send_viewport_cmd(egui::ViewportCommand::Close); +} + pub(crate) fn check(app: &mut EffectcraftApp) { #[cfg(not(target_arch = "wasm32"))] { @@ -91,7 +111,7 @@ pub(crate) fn show(app: &mut EffectcraftApp, ctx: &egui::Context) { rt.updater.dismiss_status(); } CheckResultResponse::CheckAgain => rt.updater.check_now(ctx), - CheckResultResponse::RestartToUpdate => rt.updater.restart_to_update(), + CheckResultResponse::RestartToUpdate => restart_to_update(rt, ctx, clean), } // A check that found a release leaves the status idle and the startup prompt takes over. if matches!(rt.updater.status(), Status::Idle) && !rt.check_requested {