diff --git a/crates/ui-egui/src/dialogs/kinsoku_sets.rs b/crates/ui-egui/src/dialogs/kinsoku_sets.rs index 6eb690989d..8c9c5075f5 100644 --- a/crates/ui-egui/src/dialogs/kinsoku_sets.rs +++ b/crates/ui-egui/src/dialogs/kinsoku_sets.rs @@ -55,7 +55,8 @@ fn categories() -> [(&'static str, &'static str); 4] { /// What OK files for the pump: the document the dialog opened on, the names of its sets then, and the sets. #[derive(Clone)] struct Request { - doc: Option, + doc: u64, + reverts: u64, opened: Vec, sets: Value, } @@ -63,8 +64,9 @@ struct Request { /// The Kinsoku Shori Settings dialog on the host. pub(crate) struct KinsokuDialog { id: DialogId, - /// The uid of the document the dialog opened on, which OK sets the sets of. - doc: Option, + /// The drawing and its saved-content generation when the dialog opened. + doc: u64, + reverts: u64, /// The names of that document's sets when the dialog opened: OK keeps the sets the document gained /// since, after the dialog's own, and New's suggestion skips these names. opened: Vec, @@ -84,7 +86,7 @@ pub(crate) struct KinsokuDialog { /// Open Kinsoku Shori Settings on the active document's custom sets, the first one picked. pub fn open(app: &mut VectorcraftApp) -> Result { let st = app.session.active().ok_or("no document open")?; - let dialog = KinsokuDialog::new(st.uid, st.doc.kinsoku_sets.iter()); + let dialog = KinsokuDialog::new(st.uid, st.reverts, st.doc.kinsoku_sets.iter()); app.dialogs().ok_or("no window")?.with(|host| host.open(dialog)); Ok(Value::Null) } @@ -176,10 +178,11 @@ fn text_of(field: &str, value: &Value) -> Result { } impl KinsokuDialog { - fn new<'a>(doc: u64, document_sets: impl Iterator + Clone) -> Self { + fn new<'a>(doc: u64, reverts: u64, document_sets: impl Iterator + Clone) -> Self { let mut d = KinsokuDialog { id: dialog_id(COMMAND), - doc: Some(doc), + doc, + reverts, opened: document_sets.clone().map(|k| k.name.clone()).collect(), sets: Value::Array(document_sets.map(listed).collect()), selected: 0, @@ -340,7 +343,7 @@ impl KinsokuDialog { } self.commit(); self.problem = None; - self.held.file(Request { doc: self.doc, opened: self.opened.clone(), sets: self.sets.clone() }); + self.held.file(Request { doc: self.doc, reverts: self.reverts, opened: self.opened.clone(), sets: self.sets.clone() }); } /// The answer OK filed, once. @@ -517,7 +520,7 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &Context) { let Some(host) = app.dialogs() else { return }; let Some(request) = host.with(|h| h.get_mut::().and_then(KinsokuDialog::take_ok)) else { return }; let answer = apply(app, &request); - let gone = answer.is_err() && !app.session.documents().iter().any(|d| Some(d.uid) == request.doc); + let gone = answer.is_err() && !app.session.documents().iter().any(|d| d.uid == request.doc); // The command ran outside the host's lock, which a dialog's own methods take. host.with(|h| { if let Some(d) = h.get_mut::() { @@ -530,8 +533,12 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &Context) { } fn apply(app: &mut VectorcraftApp, request: &Request) -> Result<(), String> { - if app.session.active().map(|st| st.uid) != request.doc { - let index = app.session.documents().iter().position(|d| Some(d.uid) == request.doc).ok_or("that document is no longer open")?; + let (index, document) = + app.session.documents().iter().enumerate().find(|(_, st)| st.uid == request.doc).ok_or("that document is no longer open")?; + if document.reverts != request.reverts { + return Err(tl!("The document was reverted. Reopen this dialog to continue.").into()); + } + if app.session.active().is_none_or(|st| st.uid != request.doc) { app.run("document.activate", json!({ "index": index }))?; } // A `sets` that is not a list of sets goes to the command as it is, which refuses it. diff --git a/crates/ui-egui/src/dialogs/kinsoku_sets/tests.rs b/crates/ui-egui/src/dialogs/kinsoku_sets/tests.rs index 887dbbc202..7d1755f51c 100644 --- a/crates/ui-egui/src/dialogs/kinsoku_sets/tests.rs +++ b/crates/ui-egui/src/dialogs/kinsoku_sets/tests.rs @@ -226,6 +226,47 @@ fn ok_sets_the_document_the_dialog_opened_on() { assert!(app.session.documents().iter().all(|d| d.doc.kinsoku_sets.is_empty())); } +#[test] +fn old_ok_keeps_restored_kinsoku_rules_and_the_current_tab() { + let mut app = app(); + app.run("type.kinsokuSets.set", json!({"sets": [{"name": "Custom", "cannotStart": "ゝ"}]})).unwrap(); + let file = std::env::temp_dir().join(format!("vectorcraft-kinsoku-revert-{}.vectorcraft", std::process::id())); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|err| err.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|err| err.to_string()))); + app.run("file.saveAs", json!({"path": file.to_string_lossy()})).unwrap(); + let st = app.session.active().unwrap(); + let (uid, reverts) = (st.uid, st.reverts); + + app.run("type.kinsokuSets.set", json!({"sets": [{"name": "Custom", "cannotStart": "ゞ"}]})).unwrap(); + open(&mut app).unwrap(); + set(&app, "cannotStart", json!("ー")); + app.run("file.revert", json!({"confirmed": true})).unwrap(); + let _ = std::fs::remove_file(file); + let st = app.session.active().unwrap(); + assert_eq!((st.uid, st.reverts), (uid, reverts + 1)); + assert_eq!(doc_sets(&app)[0].cannot_start, ['ゝ']); + let undo = steps(&app); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + let other = app.session.active().unwrap().uid; + assert_ne!(uid, other); + + pump(&mut app); + let restored = app.session.documents().iter().find(|st| st.uid == uid).unwrap(); + assert_eq!(restored.doc.kinsoku_sets[0].cannot_start, ['ゝ'], "an old OK must keep the saved line-breaking rule"); + assert_eq!(restored.history.undo.len(), undo); + assert_eq!(app.session.active().unwrap().uid, other, "a stale OK does not activate the original tab"); + assert!(is_open(&app)); + dialog(&app, |d| assert_eq!(d.problem.as_deref(), Some("The document was reverted. Reopen this dialog to continue."))); + cancel(&app); + + assert!(app.session.set_active(0)); + open(&mut app).unwrap(); + set(&app, "cannotStart", json!("ー")); + pump(&mut app); + assert!(!is_open(&app)); + assert_eq!(doc_sets(&app)[0].cannot_start, ['ー'], "a fresh draft can edit the restored rule"); +} + /// OK keeps a set the document gained while the dialog was open, after the dialog's sets, and a /// set deleted in the dialog stays deleted. A `sets` field that is not a list of sets keeps the /// dialog open and leaves the document's sets as they are, and New refuses to start from it.