From 50be08fb0b8bc6f163f2f1a0359c194ffba971d0 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 18:08:51 -0800 Subject: [PATCH] Require fresh Save for Web approval after Revert --- crates/ui-egui/src/dialogs/save_for_web.rs | 32 ++++++++++------- .../ui-egui/src/dialogs/save_for_web/tests.rs | 36 +++++++++++++++++++ 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/crates/ui-egui/src/dialogs/save_for_web.rs b/crates/ui-egui/src/dialogs/save_for_web.rs index e7afa2c556..bd1c548175 100644 --- a/crates/ui-egui/src/dialogs/save_for_web.rs +++ b/crates/ui-egui/src/dialogs/save_for_web.rs @@ -42,7 +42,7 @@ const WRONG_DOCUMENT: &str = "Save for Web belongs to the document that opened t /// What the open dialog's pump keeps between frames: data keyed by its inputs, never UI state. #[derive(Default)] pub(crate) struct WebExport { - document: Option, + document: Option<(u64, u64)>, opening: Arc<()>, /// The rendering of the art before optimisation, by [`render_key`]. render: Option<(String, Result, String>)>, @@ -186,7 +186,8 @@ fn settings_of(options: &SaveForWebOptions, table: &Value, kept: &Value, path: O /// Open on the document's active artboard, keeping the other remembered settings. pub fn open(app: &mut VectorcraftApp) -> Result { - let document = app.session.active().ok_or("no document")?.uid; + let active = app.session.active().ok_or("no document")?; + let document = (active.uid, active.reverts); let host = app.dialogs().ok_or("no window")?; let ctx = app.ui_ctx.clone().ok_or("no window")?; // Settle a completed opening while its settings and source are still available. @@ -461,8 +462,13 @@ fn on_dialog(host: &craft::ui::shell::WindowDialogs, f: impl FnOnce(&mut Host host.with(|h| h.get_mut::().map(f)) } -fn on_original_document(app: &VectorcraftApp) -> bool { - app.session.active().is_some_and(|active| app.web_export.document == Some(active.uid)) +fn document_problem(app: &VectorcraftApp) -> Option<&'static str> { + match (app.web_export.document, app.session.active()) { + (Some((uid, reverts)), Some(active)) if uid == active.uid => { + (reverts != active.reverts).then(|| tl!("The document was reverted. Reopen this dialog to continue.")) + } + _ => Some(tl!(WRONG_DOCUMENT)), + } } /// Answers the open dialog once per frame: its result when it closed, else its requests. @@ -479,7 +485,7 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { _ => {} } let Some(asked) = on_dialog(&host, Asked::take) else { return }; - if !on_original_document(app) { + if let Some(reason) = document_problem(app) { let restore = asked.tables.iter().any(|event| matches!(event.edit, ColorTableEdit::Restore)); if restore { color_table::restore(&mut app.web_export.table); @@ -494,7 +500,7 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { update_presets(app, &host, asked.presets, restore); if requested { on_dialog(&host, HostedSaveForWeb::cancel_requests); - app.status(tl!(WRONG_DOCUMENT).to_owned()); + app.status(reason); } return; } @@ -595,8 +601,8 @@ pub(crate) fn take_palette_pick(app: &mut VectorcraftApp, parent: DialogId, key: let Some(options) = host.with(|h| h.get_mut_by_id::(parent).map(|d| d.current().clone())) else { return false; }; - if !on_original_document(app) { - app.status(tl!(WRONG_DOCUMENT).to_owned()); + if let Some(reason) = document_problem(app) { + app.status(reason); return true; } let rgb = |c: Srgba| { @@ -736,11 +742,11 @@ fn ask_path(app: &mut VectorcraftApp, host: &craft::ui::shell::WindowDialogs, id if !Arc::ptr_eq(&opening, &app.web_export.opening) { return Ok(Value::Null); } - if !on_original_document(app) { + if let Some(reason) = document_problem(app) { if let Some(host) = app.dialogs() { on_dialog(&host, HostedSaveForWeb::cancel_requests); } - app.status(tl!(WRONG_DOCUMENT).to_owned()); + app.status(reason); return Ok(Value::Null); } match picked.first_path() { @@ -765,12 +771,12 @@ fn answer_path(app: &VectorcraftApp, id: DialogId, key: &str, path: PathBuf) { /// The dialog closed with Save… or Done: the settings are remembered, and with a path the files are /// written. Failures show in the status line; the dialog has closed. fn finish(app: &mut VectorcraftApp, result: SaveForWebResult) { - let wrong_document = result.path.is_some() && !on_original_document(app); + let problem = document_problem(app).filter(|_| result.path.is_some()); let table = std::mem::take(&mut app.web_export.table); let kept = std::mem::take(&mut app.web_export.kept); app.web_export = WebExport::default(); - if wrong_document { - return app.status(tl!(WRONG_DOCUMENT).to_owned()); + if let Some(reason) = problem { + return app.status(reason); } let params = settings_of(&result.options, &table, &kept, None); if let Err(e) = app.run("webExport.settings", params.clone()) { diff --git a/crates/ui-egui/src/dialogs/save_for_web/tests.rs b/crates/ui-egui/src/dialogs/save_for_web/tests.rs index b030f110db..6c79fb69da 100644 --- a/crates/ui-egui/src/dialogs/save_for_web/tests.rs +++ b/crates/ui-egui/src/dialogs/save_for_web/tests.rs @@ -355,6 +355,42 @@ fn save_for_web_does_not_write_another_drawing_when_the_picker_returns() { assert!(dir.join("Untitled-1.png").exists()); } +#[test] +fn save_for_web_requires_a_fresh_approval_after_file_revert() { + let dir = vectorcraft_testkit::TempDir::new("sfw-revert-output"); + let source = vectorcraft_testkit::TempDir::new("sfw-revert-drawing"); + let (mut app, _) = app(dir.as_ref(), false); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + let path = source.join("Source.vectorcraft"); + app.run("file.saveAs", json!({"path": path})).unwrap(); + app.run("shape.rectangle", json!({"x": 5, "y": 5, "width": 10, "height": 10})).unwrap(); + let original = app.session.active().unwrap(); + let (uid, reverts) = (original.uid, original.reverts); + app.run("file.saveForWeb", json!({})).unwrap(); + set(&mut app, "format", json!("png24")); + let ctx = app.ui_ctx.clone().unwrap(); + let dialog = id(&app); + host(&app).with(|h| h.confirm_by_id(&ctx, dialog)); + settle(&mut app); + + app.run("file.revert", json!({"confirmed": true})).unwrap(); + let current = app.session.active().unwrap(); + assert_eq!((current.uid, current.reverts), (uid, reverts + 1)); + answer_saves(&mut app); + settle(&mut app); + assert_eq!(std::fs::read_dir(dir.as_ref()).unwrap().count(), 0); + assert_eq!(app.ui.status, "The document was reverted. Reopen this dialog to continue."); + + close(&mut app); + settle(&mut app); + app.run("file.saveForWeb", json!({})).unwrap(); + set(&mut app, "format", json!("png24")); + save(&mut app); + let outputs: Vec<_> = std::fs::read_dir(dir.as_ref()).unwrap().map(|entry| entry.unwrap().path()).collect(); + assert_eq!(outputs.len(), 1); + assert!(std::fs::read(&outputs[0]).unwrap().starts_with(b"\x89PNG")); +} + /// The remembered Transparency Dither, which the dialog does not show, reaches the file Save writes /// and stays remembered after Done. #[test]