From e8f812659033f8924767ac77fc618cba71bf9a61 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 17:58:45 -0800 Subject: [PATCH 1/2] Reproduce Symbol Options applying to changed art and reverted definitions --- crates/ui-egui/src/dialogs/symbol_options.rs | 67 +++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/dialogs/symbol_options.rs b/crates/ui-egui/src/dialogs/symbol_options.rs index 470f78e55e..89e2e0135a 100644 --- a/crates/ui-egui/src/dialogs/symbol_options.rs +++ b/crates/ui-egui/src/dialogs/symbol_options.rs @@ -195,10 +195,12 @@ fn merge(into: &mut Value, extra: Value) { #[cfg(test)] mod document_tests { use craft::ui::dialog::dialog_id; + use vectorcraft_doc::NodeId; use vectorcraft_engine::Session; - use super::super::tests_warp::{accept, set, windowed}; + use super::super::tests_warp::{accept, dismiss, set, windowed}; use super::*; + use crate::tests::picks::answer_dialog; #[test] fn editing_symbol_options_does_not_rename_another_drawings_symbol() { @@ -225,4 +227,67 @@ mod document_tests { accept(&mut app, id); assert!(app.session.active().unwrap().doc.symbols.iter().any(|symbol| symbol.name == "Source badge")); } + + #[test] + fn new_symbol_does_not_replace_art_selected_after_opening() { + let mut app = windowed(VectorcraftApp::new(Session::new(), Default::default())); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + let original = app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 30, "height": 30})).unwrap()["id"].as_u64().unwrap(); + let other = app.run("shape.rectangle", json!({"x": 60, "y": 0, "width": 50, "height": 50})).unwrap()["id"].as_u64().unwrap(); + app.run("select.set", json!({"ids": [original]})).unwrap(); + open(&mut app, false).unwrap(); + let id = dialog_id(KIND); + set(&app, id, "name", json!("Badge")); + app.run("select.set", json!({"ids": [other]})).unwrap(); + let before = app.session.active().unwrap().doc.clone(); + let undo = app.session.active().unwrap().history.undo.len(); + + answer_dialog(&mut app, id, &[], true); + assert_eq!(app.session.active().unwrap().doc, before, "OK cannot turn the later selection into a symbol"); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + + app.run("select.set", json!({"ids": [original]})).unwrap(); + answer_dialog(&mut app, id, &[], true); + let symbols = app.run("symbol.list", json!({})).unwrap(); + assert_eq!(symbols["symbols"][0]["name"], "Badge"); + assert_eq!(symbols["symbols"][0]["size"], json!([30.0, 30.0]), "the original art becomes the symbol"); + assert!(app.session.active().unwrap().doc.node(NodeId(other)).is_some(), "the unrelated rectangle remains"); + } + + #[test] + fn editing_symbol_options_keeps_the_definition_restored_by_revert() { + let mut app = windowed(VectorcraftApp::new(Session::new(), Default::default())); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 30, "height": 30})).unwrap(); + app.run("symbol.new", json!({"name": "Badge"})).unwrap(); + let file = std::env::temp_dir().join(format!("vectorcraft-symbol-options-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("symbol.options", json!({"name": "Badge", "registration": 0})).unwrap(); + open_edit(&mut app, Some("Badge")).unwrap(); + let id = dialog_id(KIND); + set(&app, id, "name", json!("Edited badge")); + 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!(st.doc.symbol_registration("Badge").0, 4); + let restored = st.doc.clone(); + let undo = st.history.undo.len(); + + answer_dialog(&mut app, id, &[], true); + assert_eq!(app.session.active().unwrap().doc.symbols[0].name, "Badge", "the restored symbol keeps its name"); + assert_eq!(app.session.active().unwrap().doc, restored); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + + dismiss(&mut app, id); + open_edit(&mut app, Some("Badge")).unwrap(); + set(&app, id, "name", json!("New badge")); + accept(&mut app, id); + assert_eq!(app.session.active().unwrap().doc.symbols[0].name, "New badge"); + } } From 79d9e1e1ab6f73a5f7bba8273c19286a93ba50a4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 17:59:31 -0800 Subject: [PATCH 2/2] Guard Symbol Options against later selections and reverted drawings --- crates/ui-egui/src/dialogs/symbol_options.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/dialogs/symbol_options.rs b/crates/ui-egui/src/dialogs/symbol_options.rs index 89e2e0135a..e4282e51e6 100644 --- a/crates/ui-egui/src/dialogs/symbol_options.rs +++ b/crates/ui-egui/src/dialogs/symbol_options.rs @@ -12,6 +12,8 @@ //! Help: //! . +use std::collections::HashSet; + use craft::ui::contract::{Bind, Outcome as _, Show as _}; use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, HostedParamDialog, Param, ParamDialog, ParamKind, ParamResult}; use craft::ui::pickers::{Anchor9, ReferencePoint, ReferencePointLook}; @@ -100,13 +102,23 @@ fn rows(name: &str) -> Vec { /// edited), applies the fields to it; `keep_art` leaves the selection as ordinary art for a new one; /// `opened` are the guides the dialog opened with. fn open_dialog(app: &mut VectorcraftApp, fields: DialogFields, edit: Option, keep_art: bool, opened: [f64; 4]) -> Result { - let document = app.session.active().ok_or("no document open")?.uid; + let st = app.session.active().ok_or("no document open")?; + let (document, reverts) = (st.uid, st.reverts); + let selection = edit.is_none().then(|| st.selection.objects.iter().copied().collect::>()); let ctx = app.ui_ctx.clone().ok_or("no window")?; let name = fields.text("name").unwrap_or_default().to_string(); let dialog = HostedParamDialog::new(&ctx, KIND, fields, move |_| ParamDialog::new(KIND, tl!("Symbol Options"), rows(&name))); let ok = move |app: &mut VectorcraftApp, result: &ParamResult| -> Result<(), String> { - if !app.session.active().is_some_and(|active| active.uid == document) { - return Err(tl!("This symbol belongs to the document that opened the dialog.").to_owned()); + let Some(active) = app.session.active().filter(|st| st.uid == document) else { + return Err(tl!("This symbol belongs to the document that opened the dialog.").into()); + }; + if active.reverts != reverts { + return Err(tl!("The document was reverted. Reopen this dialog to continue.").into()); + } + if let Some(selection) = &selection + && (active.selection.objects.len() != selection.len() || active.selection.objects.iter().any(|id| !selection.contains(id))) + { + return Err(tl!("The selected artwork changed. Restore the selection to continue.").into()); } let f = &result.fields; let name = f.text("name").unwrap_or_default().trim().to_string(); @@ -243,7 +255,7 @@ mod document_tests { let undo = app.session.active().unwrap().history.undo.len(); answer_dialog(&mut app, id, &[], true); - assert_eq!(app.session.active().unwrap().doc, before, "OK cannot turn the later selection into a symbol"); + assert!(app.session.active().unwrap().doc == before, "OK cannot turn the later selection into a symbol"); assert_eq!(app.session.active().unwrap().history.undo.len(), undo); app.run("select.set", json!({"ids": [original]})).unwrap();