diff --git a/crates/ui-egui/src/dialogs/command.rs b/crates/ui-egui/src/dialogs/command.rs index 81c1d6d1a0..42e7d7e5ff 100644 --- a/crates/ui-egui/src/dialogs/command.rs +++ b/crates/ui-egui/src/dialogs/command.rs @@ -44,8 +44,12 @@ struct Open { preview: LivePreview, /// Set Role Swatch: the role the Swatch row was last filled for. filled_for: Option, - /// The uid of the document whose objects the dialog opened on (`__doc`), for Set Fold Angle. - doc: Option, + /// The uid of the document whose objects the dialog opened on (`__doc`), for Set Fold Angle, and its revert count + /// then. + doc: Option<(u64, u64)>, + /// The document checked for File > Revert, with its revert count when the dialog opened: `doc`, or the active + /// document for a dialog that previews. + reverts_of: Option<(u64, u64)>, /// Names this dialog's own open among reopenings of the same command, for its OK and Cancel. token: Arc<()>, } @@ -72,7 +76,9 @@ impl Setting { /// whose objects the dialog edits). pub(crate) fn open(app: &mut VectorcraftApp, command: &str, label: &str, mut params: Map) -> Result { let ctx = app.ui_ctx.clone().ok_or("no window")?; - if let Some(old) = app.command_dialog.open.take() { + if let Some(old) = app.command_dialog.open.take() + && !reverted(app, &old) + { old.preview.cancel(app); } let fixed = match params.remove("__params") { @@ -80,9 +86,13 @@ pub(crate) fn open(app: &mut VectorcraftApp, command: &str, label: &str, mut par _ => Map::new(), }; let linked = params.remove("__rowGutterLinked").and_then(|v| v.as_f64()); - let doc = params.remove("__doc").and_then(|v| v.as_u64()); + let doc = params.remove("__doc").and_then(|v| v.as_u64()).map(|uid| { + let reverts = app.session.documents().iter().find(|st| st.uid == uid).map_or(0, |st| st.reverts); + (uid, reverts) + }); params.retain(|k, _| !k.starts_with("__")); let previews = previews(command, ¶ms); + let reverts_of = doc.or_else(|| app.session.active().filter(|_| previews).map(|st| (st.uid, st.reverts))); let setting = Setting::of(app); let fields = form::fields_of(&command_params(command, ¶ms, setting), ¶ms); let token = Arc::new(()); @@ -111,6 +121,7 @@ pub(crate) fn open(app: &mut VectorcraftApp, command: &str, label: &str, mut par preview: LivePreview::default(), filled_for: None, doc, + reverts_of, token, }); Ok(Value::Null) @@ -161,7 +172,7 @@ fn step(app: &mut VectorcraftApp, open: &mut Open) { } }); } - if open.previews { + if open.previews && !reverted(app, open) { let value = call(&open.command, &open.fixed, open.linked, json); // The preview reports a refusal in the status bar itself. let _ = open.preview.run(app, previewing, &open.label, &open.command, &value); @@ -170,10 +181,15 @@ fn step(app: &mut VectorcraftApp, open: &mut Open) { /// OK: runs the command on the dialog's values (keeping a preview as the one undo step) and closes the dialog; an /// error keeps it open with the reason under its rows. A dialog opened on one document's objects (`__doc`, its uid) -/// closes as Cancel does and runs nothing once another document is the active one. +/// closes as Cancel does and runs nothing once another document is the active one. That dialog, and a dialog that +/// previews, stays open and runs nothing once File > Revert replaced the content of the document it opened on. fn confirm(app: &mut VectorcraftApp, token: &Arc<()>, fields: &DialogFields) -> Result { let open = app.command_dialog.open.take_if(|o| Arc::ptr_eq(&o.token, token)).ok_or("the dialog is gone")?; - if open.doc.is_some() && open.doc != app.session.active().map(|st| st.uid) { + if reverted(app, &open) { + app.command_dialog.open = Some(open); + return Err(tl!("The document was reverted. Reopen this dialog to continue.").into()); + } + if open.doc.is_some_and(|(uid, _)| app.session.active().is_none_or(|st| st.uid != uid)) { open.preview.cancel(app); return Ok(Value::Null); } @@ -197,11 +213,20 @@ fn confirm(app: &mut VectorcraftApp, token: &Arc<()>, fields: &DialogFields) -> /// Cancel: rolls back the preview of the dialog `token` names. fn cancel(app: &mut VectorcraftApp, token: &Arc<()>) { - if let Some(open) = app.command_dialog.open.take_if(|o| Arc::ptr_eq(&o.token, token)) { + if let Some(open) = app.command_dialog.open.take_if(|o| Arc::ptr_eq(&o.token, token)) + && !reverted(app, &open) + { open.preview.cancel(app); } } +/// Whether the active document is the one the dialog opened on (`__doc`, or the active one for a dialog that +/// previews), with its content replaced by File > Revert since: a preview shown before belonged to the content +/// thrown away. +fn reverted(app: &VectorcraftApp, open: &Open) -> bool { + open.reverts_of.is_some_and(|(uid, reverts)| app.session.active().is_some_and(|st| st.uid == uid && st.reverts != reverts)) +} + /// The rows of `command`'s dialog: one per value it opens with that the command takes, in `order(command)` first and /// the rest by name, Area Type Options' Text Flow last. fn command_params(command: &str, opening: &Map, setting: Setting) -> Vec { @@ -800,6 +825,46 @@ mod tests { assert_eq!(cols(&app), 2, "one Undo takes the whole change back"); } + /// Once File > Revert replaced the document's content, Grid Repeat Options neither previews its values on the + /// restored repeat nor applies them on OK, which stays open with the reason; a dialog opened after the revert + /// applies its values. + #[test] + fn grid_repeat_options_refuse_values_from_before_file_revert() { + let (mut app, id) = prepared("object.repeat.options"); + let dir = vectorcraft_testkit::TempDir::new("grid-repeat-revert"); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + app.run("file.saveAs", json!({"path": dir.join("Art.vectorcraft")})).unwrap(); + crate::menus::invoke(&mut app, "object.repeat.options", json!({})); + set(&mut app, "preview", json!(true)); + set(&mut app, "cols", json!(3)); + assert_eq!(cols(&app), 3); + + app.run("file.revert", json!({"confirmed": true})).unwrap(); + app.run("select.set", json!({"ids": [id]})).unwrap(); + assert_eq!(cols(&app), 2, "the restored repeat"); + set(&mut app, "cols", json!(4)); + assert_eq!(cols(&app), 2, "no preview on the restored repeat"); + assert!(!app.session.in_interaction()); + let undo = app.session.active().unwrap().history.undo.len(); + let dialog = id_of(&app); + answer_ok(&mut app); + assert_eq!(cols(&app), 2, "OK leaves the restored repeat as it was"); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + assert!(matches!( + app.dialogs().unwrap().with(|h| h.held_state(dialog)), + Some(craft::ui::dialog::HeldOk::Refused(reason)) if reason.as_str().contains("document was reverted") + )); + + answer_cancel(&mut app); + assert_eq!(cols(&app), 2, "Cancel rolls nothing back on the restored repeat"); + crate::menus::invoke(&mut app, "object.repeat.options", json!({})); + set(&mut app, "preview", json!(true)); + set(&mut app, "cols", json!(5)); + answer_ok(&mut app); + assert_eq!(cols(&app), 5, "a dialog opened after the revert applies its values"); + } + /// Radial and Mirror Repeat Options have no Preview (their public pages list none): their fields apply on OK /// only. #[test] @@ -990,6 +1055,40 @@ mod tests { assert_eq!(fold_angle(&mut app, a), Some(90.0), "the first document's line is left as it was"); } + /// Set Fold Angle…'s OK changes nothing once File > Revert replaced the document's content, and the dialog stays + /// open with the reason: the line it opened on is gone, even when the restored file has a line with its id. + #[test] + fn set_fold_angle_s_ok_refuses_the_lines_file_revert_restored() { + let (mut app, a, _) = fold_lines(); + let dir = vectorcraft_testkit::TempDir::new("fold-angle-revert"); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + app.run("file.saveAs", json!({"path": dir.join("Art.vectorcraft")})).unwrap(); + app.run("select.set", json!({"ids": [a]})).unwrap(); + crate::menus::invoke(&mut app, SET_FOLD_ANGLE, json!({})); + pump(&mut app); + set(&mut app, "angle", json!(30.0)); + + app.run("shape.rectangle", json!({"x": 200, "y": 120, "width": 20, "height": 20})).unwrap(); + app.run("file.revert", json!({"confirmed": true})).unwrap(); + let undo = app.session.active().unwrap().history.undo.len(); + let id = id_of(&app); + answer_ok(&mut app); + assert_eq!(fold_angle(&mut app, a), Some(90.0), "the restored line keeps its role's angle"); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + assert!(matches!( + app.dialogs().unwrap().with(|h| h.held_state(id)), + Some(craft::ui::dialog::HeldOk::Refused(reason)) if reason.as_str().contains("document was reverted") + )); + + answer_cancel(&mut app); + crate::menus::invoke(&mut app, SET_FOLD_ANGLE, json!({})); + pump(&mut app); + set(&mut app, "angle", json!(30.0)); + answer_ok(&mut app); + assert_eq!(fold_angle(&mut app, a), Some(30.0), "a dialog opened after the revert sets the angle"); + } + /// Set Fold Angle…'s Angle row takes -180° to 180°, the range the command takes, and shows every place the angle /// it opens on has. #[test] diff --git a/crates/ui-egui/src/dialogs/find_links.rs b/crates/ui-egui/src/dialogs/find_links.rs index 5fd1426068..4fb46a021c 100644 --- a/crates/ui-egui/src/dialogs/find_links.rs +++ b/crates/ui-egui/src/dialogs/find_links.rs @@ -10,7 +10,7 @@ //! choice and those that fail to relink go back to that dialog; opened from the Links panel's Find //! in Folder… (`ui.findLinksInFolder`), they stay as they are. //! -//! Fields: `document` (the document's uid), `missing` (`[{name, path, ids}]`), `modified` (the ids +//! Fields: `document` (the document's uid), `reverts` (its revert count when the dialog opened), `missing` (`[{name, path, ids}]`), `modified` (the ids //! of the images whose file was modified, offered for update afterwards), `notes` (from //! `missingLinks`: the open's notes, shown with the line about the modified files), `back` (the //! files left without a choice and those that fail to relink go back to dialog `missingLinks`), `state` @@ -282,10 +282,19 @@ impl FindLinksDialog { /// The fields of a Find Linked Files dialog for the `missing` files of the active document. fn dialog(app: &VectorcraftApp, missing: Vec, modified: Vec, back: bool) -> Result { - let uid = app.session.active().map(|d| d.uid).ok_or("no document open")?; + let (uid, reverts) = app.session.active().map(|d| (d.uid, d.reverts)).ok_or("no document open")?; let picks = vec![Value::Null; missing.len()]; - let fields = - json!({ "document": uid, "missing": missing, "modified": modified, "back": back, "state": "", "picks": picks, "touched": [], "copy": false }); + let fields = json!({ + "document": uid, + "reverts": reverts, + "missing": missing, + "modified": modified, + "back": back, + "state": "", + "picks": picks, + "touched": [], + "copy": false, + }); match fields { Value::Object(fields) => Ok(fields), _ => Err("the dialog has no fields".into()), @@ -312,10 +321,11 @@ pub(super) fn from_missing_links( ) -> Result<(), String> { let mut find = dialog(app, missing.clone(), modified.clone(), true)?; let document = find["document"].as_u64().ok_or("no document open")?; + let reverts = find["reverts"].as_u64().ok_or("no document open")?; find.insert("notes".into(), json!(notes)); start_search(app, &mut find, folder)?; open(app, find)?; - app.link_asks = Some(LinkAsk { document, missing, modified, notes, back: true }); + app.link_asks = Some(LinkAsk { document, reverts, missing, modified, notes, back: true }); Ok(()) } @@ -384,13 +394,17 @@ fn is_image(app: &VectorcraftApp, id: &Value) -> bool { st.and_then(|st| st.doc.stored_node(vectorcraft_doc::NodeId(id))).is_some_and(|(n, _)| matches!(n.kind, vectorcraft_doc::NodeKind::Image(_))) } -/// Make the dialog's document the active one: it may be another tab's. +/// Make the dialog's document the active one: it may be another tab's. A document whose content File > Revert +/// replaced since the dialog opened is refused: the rows' ids named objects of the content thrown away. fn activate(app: &mut VectorcraftApp, d: &Fields) -> Result<(), String> { let uid = d.get("document").and_then(Value::as_u64); + let index = app.session.documents().iter().position(|doc| Some(doc.uid) == uid).ok_or("that document is no longer open")?; + if d.get("reverts").and_then(Value::as_u64) != app.session.documents().get(index).map(|doc| doc.reverts) { + return Err(tl!("The document was reverted. Reopen this dialog to continue.").into()); + } if app.session.active().map(|st| st.uid) == uid { return Ok(()); } - let index = app.session.documents().iter().position(|doc| Some(doc.uid) == uid).ok_or("that document is no longer open")?; app.run("document.activate", json!({ "index": index })).map(|_| ()) } diff --git a/crates/ui-egui/src/dialogs/gradient_stop.rs b/crates/ui-egui/src/dialogs/gradient_stop.rs index 8a0f587513..5b2ee66107 100644 --- a/crates/ui-egui/src/dialogs/gradient_stop.rs +++ b/crates/ui-egui/src/dialogs/gradient_stop.rs @@ -69,7 +69,8 @@ enum Target { /// The paint target and selected stop or point shown when the popover opened. #[derive(Clone, PartialEq, Eq)] struct TargetKey { - document: u64, + /// The document's uid and revert count: File > Revert keeps the uid and replaces the content. + document: (u64, u64), objects: Vec, item: Option, fill: bool, @@ -87,7 +88,7 @@ impl TargetKey { Target::Point(_, _, index) => (true, *index), }; Some(Self { - document: doc.uid, + document: (doc.uid, doc.reverts), objects: doc.selection.objects.clone(), item: app.session.appearance_item(), fill, @@ -891,6 +892,32 @@ mod tests { assert_eq!(stops(&app)[1].opacity, 0.25); } + #[test] + fn the_popover_discards_a_pending_edit_after_file_revert() { + let mut app = app(); + let dir = vectorcraft_testkit::TempDir::new("gradient-stop-revert"); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + let art = app.session.doc().unwrap().selection.objects[0]; + app.run("file.saveAs", json!({"path": dir.join("Art.vectorcraft")})).unwrap(); + open_popover(&mut app); + set(&app, "opacity", json!(25)).unwrap(); + + app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap(); + app.run("file.revert", json!({"confirmed": true})).unwrap(); + app.run("select.set", json!({"ids": [art.0]})).unwrap(); + app.run("gradient.selectStop", json!({"index": 1})).unwrap(); + let before = app.session.doc().unwrap().doc.clone(); + confirm(&mut app); + assert_eq!(app.session.doc().unwrap().doc, before, "OK must leave the restored gradient as it is"); + assert!(!is_open(&app)); + + open_popover(&mut app); + set(&app, "opacity", json!(25)).unwrap(); + confirm(&mut app); + assert_eq!(stops(&app)[1].opacity, 0.25); + } + #[test] fn the_popover_does_not_send_live_edits_to_another_stop() { let mut app = app(); diff --git a/crates/ui-egui/src/dialogs/missing_links.rs b/crates/ui-egui/src/dialogs/missing_links.rs index 60e7df2fb1..71ed198720 100644 --- a/crates/ui-egui/src/dialogs/missing_links.rs +++ b/crates/ui-egui/src/dialogs/missing_links.rs @@ -109,6 +109,8 @@ impl Asked { pub(crate) struct LinkAsk { /// The document that owns the linked-file IDs. pub(crate) document: u64, + /// That document's revert count when it was asked: File > Revert keeps the uid and replaces the content. + pub(crate) reverts: u64, /// The missing files still asked about (`[{name, path, ids}]`), the next one first. pub(crate) missing: Vec, /// The ids of the images whose file was modified. @@ -355,8 +357,8 @@ fn questions_for(app: &VectorcraftApp, r: &Value, web: bool) -> (Vec, Ve /// Opens the dialog about the first of `missing`, keeping what the questions after it need /// ([`LinkAsk`]). A window that isn't up has nothing to show a question in. fn open(app: &mut VectorcraftApp, missing: Vec, modified: Vec, notes: &[String]) { - let Some(document) = app.session.active().map(|st| st.uid) else { return }; - open_ask(app, LinkAsk { document, missing, modified, notes: notes.to_vec(), back: false }); + let Some((document, reverts)) = app.session.active().map(|st| (st.uid, st.reverts)) else { return }; + open_ask(app, LinkAsk { document, reverts, missing, modified, notes: notes.to_vec(), back: false }); } fn open_ask(app: &mut VectorcraftApp, ask: LinkAsk) { @@ -451,7 +453,12 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { } return; } - let LinkAsk { document, missing, modified, notes, .. } = ask; + // The ids asked about named objects of the content File > Revert threw away. + if app.session.active().is_some_and(|st| st.reverts != ask.reverts) { + app.status(tl!("The document was reverted. Reopen this dialog to continue.")); + return; + } + let LinkAsk { document, reverts, missing, modified, notes, .. } = ask; let done = match answer { MissingLinksAnswer::FindInFolder(folder) => { super::find_links::from_missing_links(app, missing.clone(), modified.clone(), notes.clone(), &folder).map(|()| None) @@ -463,7 +470,7 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { Ok(None) => {} Err(e) => { app.status(e); - open_ask(app, LinkAsk { document, missing, modified, notes, back: false }); + open_ask(app, LinkAsk { document, reverts, missing, modified, notes, back: false }); } } } @@ -678,6 +685,43 @@ mod tests { let _ = std::fs::remove_dir_all(dir); } + /// After File > Revert, Replace leaves the restored document's links as they are: the ids it asked about named + /// objects of the content thrown away. + #[test] + fn a_missing_file_replacement_is_refused_after_file_revert() { + let (mut app, dir, _) = opened_without_links("revert-replace"); + let replacement = dir.join("replacement.png"); + std::fs::write(&replacement, png([0, 0, 200])).unwrap(); + app.run("shape.rectangle", json!({"x": 200, "y": 120, "width": 20, "height": 20})).unwrap(); + app.run("file.revert", json!({"confirmed": true})).unwrap(); + + drive::answer::(&mut app, &[("path", json!(replacement.to_string_lossy()))], true); + assert_eq!(app.run("links.check", json!({})).unwrap()["missing"], 2, "the restored links stay as they are"); + assert_eq!(app.ui.status, "The document was reverted. Reopen this dialog to continue."); + assert!(!drive::is_open::(&app)); + let _ = std::fs::remove_dir_all(dir); + } + + /// After File > Revert, Find Linked Files opened from Find in Folder… relinks nothing in the restored document. + #[test] + fn find_linked_files_relinks_nothing_after_file_revert() { + use crate::dialogs::find_links::FindLinksDialog; + + let (mut app, dir, _) = opened_without_links("revert-find"); + drive::answer::(&mut app, &[("searchFolder", json!(dir.to_string_lossy()))], true); + assert!(drive::is_open::(&app)); + let replacement = dir.join("replacement.png"); + std::fs::write(&replacement, png([0, 0, 200])).unwrap(); + app.run("shape.rectangle", json!({"x": 200, "y": 120, "width": 20, "height": 20})).unwrap(); + app.run("file.revert", json!({"confirmed": true})).unwrap(); + + let picks = json!([replacement.to_string_lossy(), replacement.to_string_lossy()]); + drive::answer::(&mut app, &[("picks", picks)], true); + assert_eq!(app.run("links.check", json!({})).unwrap()["missing"], 2, "the restored links stay as they are"); + assert_eq!(app.ui.status, "The document was reverted. Reopen this dialog to continue."); + let _ = std::fs::remove_dir_all(dir); + } + /// Replace relinks the first file and asks about the next; Ignore keeps its preview. #[test] fn opening_without_linked_files_asks_to_replace_or_ignore_each() { diff --git a/crates/ui-egui/src/dialogs/shapes.rs b/crates/ui-egui/src/dialogs/shapes.rs index ccdf07a76e..4728e52bcc 100644 --- a/crates/ui-egui/src/dialogs/shapes.rs +++ b/crates/ui-egui/src/dialogs/shapes.rs @@ -36,6 +36,8 @@ pub(crate) const OPTION_TOOLS: [&str; 4] = ["rectangularGrid", "polarGrid", "arc const SIZE_KINDS: [&str; 3] = ["rectangle", "roundedRectangle", "ellipse"]; const CLICK_DOCUMENT: &str = "__clickDocument"; +/// The clicked document's revert count: File > Revert keeps its uid and replaces the content clicked on. +const CLICK_REVERTS: &str = "__clickReverts"; const CLICK_ALT: &str = "__clickAlt"; /// Where a shape tool was clicked: the dialog's shape is drawn there, and with `alt` held a box shape @@ -92,13 +94,14 @@ pub(crate) fn open(app: &mut VectorcraftApp, kind: &str, click: Option) { } // Keep the placement with the draft whose fields OK receives. if let Some(click) = click { - let Some(document) = app.session.active().map(|doc| doc.uid) else { + let Some((document, reverts)) = app.session.active().map(|doc| (doc.uid, doc.reverts)) else { app.status(tl!("Open a document before drawing a shape").to_string()); return; }; values.insert("x".into(), json!(click.at.x)); values.insert("y".into(), json!(click.at.y)); values.insert(CLICK_DOCUMENT.into(), json!(document.to_string())); + values.insert(CLICK_REVERTS.into(), json!(reverts.to_string())); values.insert(CLICK_ALT.into(), json!(click.alt)); } let ink = (kind == "arc" && click.is_none()).then(|| ArcInk::of(&app.session)); @@ -488,13 +491,16 @@ fn confirm(app: &mut VectorcraftApp, kind: &str, fields: &DialogFields) -> Resul }; }; let document = document.parse::().map_err(|_| tl!("The shape click document is missing").to_string())?; - if app.session.active().is_none_or(|doc| doc.uid != document) { + let Some(active) = app.session.active().filter(|doc| doc.uid == document) else { let reason = if app.session.documents().iter().any(|doc| doc.uid == document) { tl!("Switch back to the document where you clicked to draw the shape") } else { tl!("The document where you clicked to draw the shape has closed") }; return Err(reason.into()); + }; + if fields.text(CLICK_REVERTS).and_then(|r| r.parse::().ok()) != Some(active.reverts) { + return Err(tl!("The document was reverted. Reopen this dialog to continue.").into()); } let (Some(x), Some(y)) = (fields.number("x"), fields.number("y")) else { return Err(tl!("The shape click position is missing").into()); @@ -699,6 +705,33 @@ mod tests { assert!((bounds.width() - 32.0).abs() < 1e-6); } + #[test] + fn rectangle_click_draws_nothing_after_file_revert() { + let (mut app, mut fx) = shown(); + let dir = vectorcraft_testkit::TempDir::new("shape-click-revert"); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + app.run("file.saveAs", json!({"path": dir.join("Art.vectorcraft")})).unwrap(); + super::super::open_tool_dialog(&mut app, "rectangle", json!({"x": 40.0, "y": 60.0})); + set(&app, &fx, "rectangle", "width", json!(32.0)).unwrap(); + + app.run("shape.rectangle", json!({"x": 200, "y": 200, "width": 20, "height": 20})).unwrap(); + app.run("file.revert", json!({"confirmed": true})).unwrap(); + let undo = app.session.doc().unwrap().history.undo.len(); + press_ok(&mut app, &mut fx, "rectangle"); + assert_eq!(drawn(&app), 0, "OK must not draw on the restored drawing"); + assert_eq!(app.session.doc().unwrap().history.undo.len(), undo); + assert!(matches!( + app.dialogs().unwrap().with(|h| h.held_state(dialog_id("rectangle"))), + Some(craft::ui::dialog::HeldOk::Refused(reason)) if reason.as_str().contains("document was reverted") + )); + + cancel(&mut app, &mut fx, "rectangle"); + super::super::open_tool_dialog(&mut app, "rectangle", json!({"x": 40.0, "y": 60.0})); + press_ok(&mut app, &mut fx, "rectangle"); + assert_eq!(drawn(&app), 1, "a click after the revert draws"); + } + #[test] fn a_reopened_rectangle_uses_the_replacement_click_and_modifier() { let (mut app, mut fx) = shown(); diff --git a/crates/ui-egui/src/dialogs/tools.rs b/crates/ui-egui/src/dialogs/tools.rs index 3d798cb1e3..e0534d4d7a 100644 --- a/crates/ui-egui/src/dialogs/tools.rs +++ b/crates/ui-egui/src/dialogs/tools.rs @@ -156,4 +156,30 @@ mod tests { let doc = &app.session.doc().unwrap().doc; assert_eq!(doc.layers[0].children().unwrap().len(), 1); } + + #[test] + fn graph_size_does_not_draw_after_file_revert() { + let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), Default::default())); + let dir = vectorcraft_testkit::TempDir::new("graph-click-revert"); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + app.run("file.saveAs", json!({"path": dir.join("Art.vectorcraft")})).unwrap(); + let click = json!({"x": 40.0, "y": 30.0, "type": "column"}); + open_tool_dialog(&mut app, "graph", click.clone()); + + app.run("shape.rectangle", json!({"x": 200, "y": 120, "width": 20, "height": 20})).unwrap(); + app.run("file.revert", json!({"confirmed": true})).unwrap(); + let restored = app.session.doc().unwrap().doc.clone(); + confirm_graph(&mut app); + assert_eq!(app.session.doc().unwrap().doc, restored, "OK must not draw on the restored drawing"); + assert!(app.any_dialog_open(), "the refused dialog stays open"); + + let ctx = app.ui_ctx.clone().unwrap(); + app.dialogs().unwrap().with(|h| h.cancel_by_id(&ctx, dialog_id("graph.create"))); + super::super::host::pump(&mut app, &ctx); + open_tool_dialog(&mut app, "graph", click); + confirm_graph(&mut app); + assert_eq!(app.session.doc().unwrap().doc.layers[0].children().unwrap().len(), 1, "a click after the revert draws"); + } }