diff --git a/crates/ui-egui/src/dialogs/char_style_options.rs b/crates/ui-egui/src/dialogs/char_style_options.rs index 55c89cbd32..b22cef7e59 100644 --- a/crates/ui-egui/src/dialogs/char_style_options.rs +++ b/crates/ui-egui/src/dialogs/char_style_options.rs @@ -1165,7 +1165,8 @@ pub(crate) use style_dialog; pub(crate) struct CharStyleOptions { inner: HostedPropertiesDialog, opening: Arc, - document: u64, + /// The opening document's uid and revert count. + document: (u64, u64), } impl CharStyleOptions { @@ -1192,7 +1193,7 @@ pub(crate) struct Preview { pub fn open(app: &mut VectorcraftApp, name: &str) -> Result { crate::panels::character::end_typing(app); let state = app.session.active().ok_or("no document open")?; - let (document, doc) = (state.uid, &state.doc); + let (document, doc) = ((state.uid, state.reverts), &state.doc); let def = doc.char_styles.iter().find(|s| s.name == name).ok_or_else(|| format!("no character style `{name}`"))?; let opening = Arc::new(Opening::new(name, def.attrs.clone(), &doc.setup, swatch_choices(doc))); let dialog = CharStyleOptions { inner: hosted(KIND, tl!("Character Style Options"), &opening, name).previews(), opening, document }; @@ -1245,14 +1246,15 @@ pub(crate) struct Refusals { } /// Runs one frame of the style options dialog `D` that opened on one document: previews and commits `command` -/// (its undo label and id) only while that document is active and no other edit is active in it, refuses OK -/// elsewhere with `refusals`, and at Cancel rolls back only the preview the dialog still owns. `preview` is the -/// app's slot for the dialog's preview, `state` gives the opening document's uid and this frame's state, and -/// `hosted` gives the hosted dialog that takes OK's answer. +/// (its undo label and id) only while that document is active, has not been reverted since the dialog opened and +/// has no other edit active in it, refuses OK elsewhere with `refusals`, and at Cancel rolls back only the preview +/// the dialog still owns. `preview` is the app's slot for the dialog's preview, `state` gives the opening +/// document's uid and revert count and this frame's state, and `hosted` gives the hosted dialog that takes OK's +/// answer. pub(crate) fn pump_in_opening_document( app: &mut VectorcraftApp, preview: fn(&mut VectorcraftApp) -> &mut Preview, - state: fn(&mut D) -> (u64, Frame), + state: fn(&mut D) -> ((u64, u64), Frame), hosted: fn(&mut D) -> &mut HostedPropertiesDialog, (label, command): (&str, &str), refusals: &Refusals, @@ -1263,7 +1265,7 @@ pub(crate) fn pump_in_opening_document( let owned = preview(app); let owns_interaction = owned.activation == active && owned.interaction_selection.as_ref().zip(interaction_selection.as_ref()).is_some_and(|(owned, current)| Arc::ptr_eq(owned, current)); - let Some((document, frame)) = dialogs.with(|host| host.get_mut::().map(state)) else { + let Some(((document, reverts), frame)) = dialogs.with(|host| host.get_mut::().map(state)) else { if matches!(dialogs.with(|host| host.take::()), Some(DialogResult::Cancel)) { let owned = std::mem::take(preview(app)); if owns_interaction { @@ -1272,7 +1274,8 @@ pub(crate) fn pump_in_opening_document( } return; }; - let done = if let Some(activation) = active.filter(|(uid, _)| *uid == document) { + let in_opening = app.session.active().is_some_and(|doc| (doc.uid, doc.reverts) == (document, reverts)); + let done = if let Some(activation) = active.filter(|_| in_opening) { if interaction_selection.is_some() && !owns_interaction { frame.ok.then(|| Err(refusals.busy.into())) } else { @@ -1285,7 +1288,13 @@ pub(crate) fn pump_in_opening_document( answer } } else if frame.ok { - let why = if app.session.documents().iter().any(|doc| doc.uid == document) { refusals.elsewhere } else { refusals.closed }; + let why = if app.session.active().is_some_and(|doc| doc.uid == document) { + "The document was reverted. Reopen this dialog to continue." + } else if app.session.documents().iter().any(|doc| doc.uid == document) { + refusals.elsewhere + } else { + refusals.closed + }; Some(Err(why.into())) } else { None diff --git a/crates/ui-egui/src/dialogs/char_style_options/tests.rs b/crates/ui-egui/src/dialogs/char_style_options/tests.rs index af7ec9253e..4c54feadf6 100644 --- a/crates/ui-egui/src/dialogs/char_style_options/tests.rs +++ b/crates/ui-egui/src/dialogs/char_style_options/tests.rs @@ -157,6 +157,33 @@ fn the_style_preview_stays_in_its_opening_document() { assert_eq!(app.session.doc().unwrap().doc, opening); } +/// After File > Revert the draft neither previews on nor commits to the restored document, and OK is refused. +#[test] +fn style_options_refuse_their_draft_after_file_revert() { + let mut app = app(); + styled(&mut app, json!({"size": 24.0}), "Opening document"); + let dir = vectorcraft_testkit::TempDir::new("char-style-options-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(); + open(&mut app, "Body"); + set::(&app, "name", json!("Revised")); + pump(&mut app); + assert!(app.session.in_interaction()); + + app.run("file.revert", json!({"confirmed": true})).unwrap(); + let restored = app.session.doc().unwrap().doc.clone(); + let undo = app.session.doc().unwrap().history.undo.len(); + set::(&app, "size", json!(18)); + pump(&mut app); + assert_eq!(app.session.doc().unwrap().doc, restored, "Preview leaves the restored document's Body style unchanged"); + assert!(!ok::(&mut app), "the dialog stays open with the refusal"); + assert_eq!(app.session.doc().unwrap().doc, restored, "OK leaves the restored document's Body style unchanged"); + assert_eq!(app.session.doc().unwrap().history.undo.len(), undo); + let refusal = with_dialog::(&app, |host, id| host.held_state(id)); + assert!(matches!(refusal, Some(craft::ui::dialog::HeldOk::Refused(reason)) if reason.as_str().contains("document was reverted"))); +} + #[test] fn a_style_options_confirmation_stays_in_its_opening_document() { let mut app = options_in_the_first_of_two_documents(); diff --git a/crates/ui-egui/src/dialogs/new_text_style.rs b/crates/ui-egui/src/dialogs/new_text_style.rs index 3ef9024618..b94bf89708 100644 --- a/crates/ui-egui/src/dialogs/new_text_style.rs +++ b/crates/ui-egui/src/dialogs/new_text_style.rs @@ -29,7 +29,8 @@ pub(crate) struct NewTextStyle { opening: Opening, /// Whether the dialog makes a paragraph style. pub(crate) para: bool, - document: u64, + /// The opening document's uid and revert count. + document: (u64, u64), target: Value, } @@ -56,7 +57,7 @@ style_dialog!(NewTextStyle, KIND); /// the style, to make it from the text `target` names. pub fn open(app: &mut VectorcraftApp, para: bool, target: Value) -> Result { let state = app.session.active().ok_or("no document open")?; - let (document, doc) = (state.uid, &state.doc); + let (document, doc) = ((state.uid, state.reverts), &state.doc); let name = vectorcraft_engine::cmd::textstyles::new_style_name(doc, para); let mut target = if target.is_object() { target } else { json!({}) }; let selected_attrs = vectorcraft_engine::cmd::textstyles::target_style_attrs(&app.session, &target, para); @@ -80,7 +81,8 @@ pub fn open(app: &mut VectorcraftApp, para: bool, target: Value) -> Result().map(|d| (d.para, d.document, d.frame()))) else { + let Some((para, (document, reverts), frame)) = dialogs.with(|host| host.get_mut::().map(|d| (d.para, d.document, d.frame()))) + else { // The dialog closed: its answer or cancel has nothing left for the app to do. let _ = dialogs.with(|host| host.take::()); return; @@ -88,8 +90,10 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { if !frame.ok { return; } - let made = if app.session.active().is_some_and(|doc| doc.uid == document) { + let made = if app.session.active().is_some_and(|doc| (doc.uid, doc.reverts) == (document, reverts)) { frame.params.and_then(|params| app.run(if para { "paraStyle.new" } else { "charStyle.new" }, params)) + } else if app.session.active().is_some_and(|doc| doc.uid == document) { + Err("The document was reverted. Reopen this dialog to continue.".into()) } else if app.session.documents().iter().any(|doc| doc.uid == document) { Err("switch back to the document where this style was opened".into()) } else { @@ -151,6 +155,46 @@ mod tests { assert_eq!(app.ui.text_styles.made, [(opening, para, name.to_string())]); } + fn assert_style_is_refused_after_file_revert(para: bool) { + let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), Default::default())); + app.run("file.new", json!({"width": 200, "height": 100})).unwrap(); + app.run("text.create", json!({"x": 10, "y": 40, "text": "Opening document"})).unwrap(); + let dir = vectorcraft_testkit::TempDir::new("new-text-style-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(if para { "ui.newParaStyle" } else { "ui.newCharStyle" }, json!({})).unwrap(); + let ctx = app.ui_ctx.clone().unwrap(); + let host = app.dialogs().unwrap(); + let dialog = host.with(|h| h.get::().unwrap().id()); + let name = "Draft Style"; + host.with(|h| h.set_json_field(&ctx, dialog, "name", json!(name))).unwrap(); + app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap(); + + app.run("file.revert", json!({"confirmed": true})).unwrap(); + let undo = app.session.doc().unwrap().history.undo.len(); + host.with(|h| h.confirm_by_id(&ctx, dialog)); + pump(&mut app, &ctx); + + assert!(!style_exists(&app, para, name), "OK must not make the draft's style in the restored document"); + assert_eq!(app.session.doc().unwrap().history.undo.len(), undo); + assert!(app.ui.text_styles.made.is_empty(), "the restored document must not highlight a style"); + assert!(matches!( + host.with(|h| h.held_state(dialog)), + Some(HeldOk::Refused(reason)) if reason.as_str().contains("document was reverted") + )); + } + + #[test] + fn new_character_style_is_refused_after_file_revert() { + assert_style_is_refused_after_file_revert(false); + } + + #[test] + fn new_paragraph_style_is_refused_after_file_revert() { + assert_style_is_refused_after_file_revert(true); + } + #[test] fn new_character_style_confirmation_stays_in_its_document() { assert_style_stays_in_its_opening_document(false); diff --git a/crates/ui-egui/src/dialogs/para_style_options.rs b/crates/ui-egui/src/dialogs/para_style_options.rs index 1f89f6c70a..bcc5577806 100644 --- a/crates/ui-egui/src/dialogs/para_style_options.rs +++ b/crates/ui-egui/src/dialogs/para_style_options.rs @@ -764,7 +764,8 @@ pub(crate) fn hosted(key: &'static str, title: &'static str, opening: &Arc, - document: u64, + /// The opening document's uid and revert count. + document: (u64, u64), } impl ParaStyleOptions { @@ -784,7 +785,7 @@ pub fn open(app: &mut VectorcraftApp, name: &str) -> Result { crate::panels::character::end_typing(app); let unit = app.session.general_unit(); let state = app.session.active().ok_or("no document open")?; - let (document, doc) = (state.uid, &state.doc); + let (document, doc) = ((state.uid, state.reverts), &state.doc); let def = doc.para_styles.iter().find(|s| s.name == name).ok_or_else(|| format!("no style `{name}`"))?; let opening = Arc::new(Opening::new(name, def.attrs.clone(), unit, vertical_text(app))); let dialog = ParaStyleOptions { inner: hosted(KIND, tl!("Paragraph Style Options"), &opening, name).previews(), opening, document }; diff --git a/crates/ui-egui/src/dialogs/para_style_options/tests.rs b/crates/ui-egui/src/dialogs/para_style_options/tests.rs index d3f1f21672..22802313ce 100644 --- a/crates/ui-egui/src/dialogs/para_style_options/tests.rs +++ b/crates/ui-egui/src/dialogs/para_style_options/tests.rs @@ -358,6 +358,33 @@ fn a_style_options_confirmation_stays_in_its_opening_document() { assert!(app.session.doc().unwrap().doc.para_styles.iter().any(|style| style.name == "Revised")); } +/// After File > Revert the draft neither previews on nor commits to the restored document, and OK is refused. +#[test] +fn style_options_refuse_their_draft_after_file_revert() { + let mut app = app(); + styled(&mut app, 6.0, "Opening document"); + let dir = vectorcraft_testkit::TempDir::new("para-style-options-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(); + open(&mut app, "Body"); + set(&app, "name", json!("Revised")); + pump(&mut app); + assert!(app.session.in_interaction()); + + app.run("file.revert", json!({"confirmed": true})).unwrap(); + let restored = app.session.doc().unwrap().doc.clone(); + let undo = app.session.doc().unwrap().history.undo.len(); + set(&app, "left_indent", json!("12")); + pump(&mut app); + assert_eq!(app.session.doc().unwrap().doc, restored, "Preview leaves the restored document's Body style unchanged"); + assert!(!ok(&mut app), "the dialog stays open with the refusal"); + assert_eq!(app.session.doc().unwrap().doc, restored, "OK leaves the restored document's Body style unchanged"); + assert_eq!(app.session.doc().unwrap().history.undo.len(), undo); + let refusal = with_dialog(&app, |host, id| host.held_state(id)); + assert!(matches!(refusal, Some(craft::ui::dialog::HeldOk::Refused(reason)) if reason.as_str().contains("document was reverted"))); +} + /// OK after the opening document has closed is refused and leaves the remaining document as it is. #[test] fn a_style_options_confirmation_is_refused_after_its_document_closes() { diff --git a/crates/ui-egui/src/dialogs/transform.rs b/crates/ui-egui/src/dialogs/transform.rs index e8651efb49..7437183918 100644 --- a/crates/ui-egui/src/dialogs/transform.rs +++ b/crates/ui-egui/src/dialogs/transform.rs @@ -106,8 +106,8 @@ impl Kind { pub(crate) struct Open { /// The dialog's id on the window's host. pub id: DialogId, - /// The drawing whose selection and reference point the dialog acts on. - document: Option, + /// The drawing whose selection and reference point the dialog acts on: its uid and revert count. + document: Option<(u64, u64)>, /// Which transform it is. pub kind: Kind, /// The reference point of a tool click, sent with every command but Move's. @@ -118,7 +118,12 @@ pub(crate) struct Open { impl Open { fn in_source(&self, app: &VectorcraftApp) -> bool { - self.document == app.session.active().map(|doc| doc.uid) + self.document == app.session.active().map(|doc| (doc.uid, doc.reverts)) + } + + /// Whether the active drawing is the opening one, reverted since the dialog opened. + fn reverted(&self, app: &VectorcraftApp) -> bool { + self.document.is_some_and(|(uid, reverts)| app.session.active().is_some_and(|doc| doc.uid == uid && doc.reverts != reverts)) } fn owns_preview(&self, app: &VectorcraftApp) -> bool { @@ -142,7 +147,7 @@ pub(crate) fn open(app: &mut VectorcraftApp, kind: Kind, tool: &Value) { return; }; let Value::Object(opening) = opening(app, kind) else { return }; - let document = app.session.active().map(|doc| doc.uid); + let document = app.session.active().map(|doc| (doc.uid, doc.reverts)); let rows = rows(app, kind, &opening); let fields = fields_of(&rows, &opening); let dialog = HostedParamDialog::new(&ctx, kind.command(), fields, move |f| { @@ -204,8 +209,13 @@ fn ok(app: &mut VectorcraftApp, kind: Kind, ctx: &egui::Context, result: &ParamR return Err("the Transform dialog was replaced".into()); } if !open.in_source(app) { + let why = if open.reverted(app) { + tl!("The document was reverted. Reopen this dialog to continue.") + } else { + tl!("Return to the original drawing to apply the transform") + }; app.object_dialogs.transform = Some(open); - return Err(tl!("Return to the original drawing to apply the transform").into()); + return Err(why.into()); } open.cancel_preview(app); let fields = &result.fields; @@ -546,6 +556,33 @@ mod tests { assert_eq!(rig.bounds().x0, 10.0); } + #[test] + fn transform_refuses_its_draft_after_file_revert() { + let mut rig = Rig::new(); + let dir = vectorcraft_testkit::TempDir::new("transform-revert"); + rig.app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + rig.app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + rig.app.run("file.saveAs", json!({"path": dir.join("Art.vectorcraft")})).unwrap(); + rig.open("object.move"); + rig.set("dx", json!(35)); + assert_eq!(rig.bounds().x0, 135.0); + + rig.app.run("file.revert", json!({"confirmed": true})).unwrap(); + rig.app.run("select.all", json!({})).unwrap(); + rig.frame(); + assert_eq!(rig.bounds().x0, 100.0, "the draft must not preview on the restored drawing"); + let steps = rig.undo_steps(); + let id = rig.id(); + let dialogs = rig.app.dialogs().unwrap(); + rig.confirm(); + assert_eq!(rig.bounds().x0, 100.0, "OK must not move the restored drawing's art"); + assert_eq!(rig.undo_steps(), steps); + assert!(matches!( + dialogs.with(|h| h.held_state(id)), + Some(craft::ui::dialog::HeldOk::Refused(reason)) if reason.as_str().contains("document was reverted") + )); + } + fn two_drawings() -> Rig { let mut rig = Rig::new(); rig.app.run("file.new", json!({"width": 300, "height": 300})).unwrap();