Updates: Update and Restart asks about unsaved drawings instead of refusing (#3698)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
cdwilson127
2026-12-01 13:58:11 +13:00
co-authored by Claude Opus 5.5
parent 23f60b50aa
commit fc1bf5e2ef
2 changed files with 103 additions and 7 deletions
+2
View File
@@ -39,6 +39,8 @@ web-time = { workspace = true }
craft-update = { workspace = true }
[dev-dependencies]
# `Updater::ready_for_tests`.
craft-update = { workspace = true, features = ["screen-testing"] }
# The workspace `egui` dependency enables `default_fonts` and `monochrome_emoji_fonts` in
# production too; the tests exercise the same installed chain as the app.
egui_kittest = { workspace = true, features = ["wgpu", "snapshot"] }
+101 -7
View File
@@ -3,7 +3,7 @@
//! `craft_update::updater::Updater`; its dialogs run on [`CadApp::framework_dialogs`].
use craft_ui::dialog::DialogResult;
use craft_update::stack_dialogs::{CheckChoice, UpdateCheckDialog};
use craft_update::stack_dialogs::{CheckChoice, UpdateCheckDialog, UpdatePromptDialog};
use craft_update::updater::{Action, Status, Updater};
use crate::CadApp;
@@ -80,33 +80,44 @@ fn result_window_shows(status: &Status, prompt_open: bool) -> bool {
/// check result dialog closed in the last pass, drives the startup prompt, and keeps the result dialog open and
/// current while the updater has something to report.
pub fn show(app: &mut CadApp, ctx: &egui::Context) {
// The close for a ready update waits until no drawing holds unsaved changes.
// An update the user is asking for closes the app with drawings unsaved: the close path asks Save / Don't Save /
// Cancel about each. With the prompt dismissed (Later), a finished installation waits until nothing is unsaved.
let clean = !app.session.docs.iter().any(|d| d.is_dirty());
let mut may_close = clean || app.updates_rt.updater.prompt_open();
// Answered before the dialog is kept open below, so a dismissed result does not open again at once.
match app.framework_dialogs.take::<UpdateCheckDialog>() {
Some(DialogResult::Ok(CheckChoice::CheckNow | CheckChoice::CheckAgain)) => {
app.updates_rt.result_echoed = false;
app.updates_rt.check_requested = true;
}
// The next `run` asks the window to close once nothing is unsaved.
Some(DialogResult::Ok(CheckChoice::RestartToUpdate)) => app.updates_rt.updater.restart_to_update(),
// The `run` below asks the window to close.
Some(DialogResult::Ok(CheckChoice::RestartToUpdate)) => {
app.updates_rt.updater.restart_to_update();
may_close = true;
}
Some(DialogResult::Ok(CheckChoice::Update)) => {
app.updates_rt.updater.request_install(clean);
app.updates_rt.updater.request_install(true);
}
Some(DialogResult::Ok(CheckChoice::Download)) => ctx.open_url(egui::OpenUrl::new_tab(crate::UPDATE_APP.releases_url())),
// Close, Esc and the close box dismiss the outcome it showed.
Some(_) => app.updates_rt.updater.dismiss_status(),
None => {}
}
match app.updates_rt.updater.run(&mut app.framework_dialogs, clean) {
match app.updates_rt.updater.run(&mut app.framework_dialogs, may_close) {
Action::OpenUrl(url) => ctx.open_url(egui::OpenUrl::new_tab(url)),
// The guarded close path asks about unsaved changes. The install helper starts when the
// app exits and drops the updater.
Action::Close => ctx.send_viewport_cmd(egui::ViewportCommand::Close),
_ => {}
}
// Save Changes is drawn below the framework dialogs, so the update windows close for it. If it is cancelled, the
// update stays ready and installs at the next exit.
let closing = app.closing.is_some();
if closing {
app.framework_dialogs.cancel::<UpdatePromptDialog>(ctx);
}
let rt = &app.updates_rt;
let shows = result_window_shows(rt.updater.status(), rt.updater.prompt_open());
let shows = !closing && result_window_shows(rt.updater.status(), rt.updater.prompt_open());
if shows && !app.framework_dialogs.is_open::<UpdateCheckDialog>() {
app.framework_dialogs.open(UpdateCheckDialog::new(VERSION));
}
@@ -146,6 +157,89 @@ mod tests {
assert!(!result_window_shows(&Status::Idle, false));
}
/// One pass of the update flow and the framework dialogs. Returns whether the app asked the window to close.
fn frame(app: &mut CadApp, ctx: &egui::Context) -> bool {
let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {
show(app, ui.ctx());
app.run_framework_dialogs(ui);
});
out.textures_delta.clear();
out.viewport_output.get(&egui::ViewportId::ROOT).is_some_and(|v| v.commands.contains(&egui::ViewportCommand::Close))
}
/// An app with one unsaved drawing and an update that installs at the exit.
fn unsaved_with_update_ready(ctx: &egui::Context) -> CadApp {
crate::framework::install_headless(ctx);
let mut app = CadApp::new(cadcraft_engine::Session::new(), crate::Services::default());
app.session.execute("circle", &serde_json::json!({ "center": [0, 0], "radius": 1 })).unwrap();
assert!(app.session.docs.iter().any(|d| d.is_dirty()));
let release = craft_update::Release {
tag: "v99.0.0".into(),
version: None,
name: "CADCraft v99.0.0".into(),
published_at: None,
html_url: String::new(),
notes: String::new(),
prerelease: false,
assets: Vec::new(),
repo: String::new(),
};
app.updates_rt.updater = Updater::ready_for_tests(&crate::UPDATE_APP, VERSION, release, || Ok(()));
app
}
/// Choose Restart to Update in the check's result; the frame that takes it asks the window to close.
fn restart_to_update(app: &mut CadApp, ctx: &egui::Context) {
check_now(app);
tick(app, ctx);
assert!(matches!(app.updates_rt.updater.status(), Status::Ready));
assert!(!frame(app, ctx), "a ready update closes nothing by itself");
app.framework_dialogs.get_mut::<UpdateCheckDialog>().unwrap().set_choice(CheckChoice::RestartToUpdate);
assert!(app.framework_dialogs.confirm::<UpdateCheckDialog>(ctx), "Restart to Update");
assert!(frame(app, ctx), "the window is asked to close");
assert!(app.request_quit(), "the close waits for an answer");
assert!(app.close_prompt().is_some(), "Save Changes asks about the drawing");
}
/// Restart to Update with a drawing unsaved closes through Save Changes. Cancel keeps the drawing and the update.
#[test]
fn restart_to_update_with_unsaved_work_asks_to_save() {
let ctx = egui::Context::default();
let mut app = unsaved_with_update_ready(&ctx);
restart_to_update(&mut app, &ctx);
for _ in 0..2 {
frame(&mut app, &ctx);
assert!(!app.framework_dialogs.shows_modal(), "no update window over Save Changes");
}
app.answer_close(crate::closing::Choice::Cancel);
assert!(app.session.docs.iter().any(|d| d.is_dirty()), "the drawing is kept");
assert!(!frame(&mut app, &ctx), "a cancelled close is not asked again");
check_now(&mut app);
tick(&mut app, &ctx);
assert!(matches!(app.updates_rt.updater.status(), Status::Ready), "the update is still ready");
}
/// Don't Save goes on to the exit, where the update installs.
#[test]
fn restart_to_update_without_saving_quits() {
let ctx = egui::Context::default();
let mut app = unsaved_with_update_ready(&ctx);
restart_to_update(&mut app, &ctx);
app.answer_close(crate::closing::Choice::Discard);
assert!(app.quit_requested && app.quit_confirmed);
}
/// With no window of the updater open, a finished installation does not interrupt unsaved work.
#[test]
fn a_dismissed_update_waits_for_unsaved_work() {
let ctx = egui::Context::default();
let mut app = unsaved_with_update_ready(&ctx);
app.updates_rt.updater.restart_to_update();
assert!(!frame(&mut app, &ctx), "the drawing is unsaved");
app.session.docs.clear();
assert!(frame(&mut app, &ctx), "nothing is unsaved any more");
}
/// The startup check runs only where the host enabled it and the user did not turn it off;
/// the choice is kept in the profile and set through the OPTIONS JSON form.
#[test]