From 23147bf20e1675fcedda6a57449526b3112671e0 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 19:23:27 -0800 Subject: [PATCH 1/2] Keep Graphic Style Options off definitions restored by File Revert --- .../src/dialogs/graphic_style_options.rs | 61 +++++++++++++++++-- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/dialogs/graphic_style_options.rs b/crates/ui-egui/src/dialogs/graphic_style_options.rs index 6081982fb2..ee66103157 100644 --- a/crates/ui-egui/src/dialogs/graphic_style_options.rs +++ b/crates/ui-egui/src/dialogs/graphic_style_options.rs @@ -7,7 +7,8 @@ //! OK, not by a field, so a name another style has refuses the OK and keeps the dialog open. use craft::ui::contract::Bind; -use craft::ui::patterns::form_dialogs::{DialogFields, HostedParamDialog, Param, ParamDialog, ParamKind, ParamResult}; +use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, HostedParamDialog, Param, ParamDialog, ParamKind, ParamResult}; +use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; use super::host; @@ -15,8 +16,10 @@ use crate::VectorcraftApp; /// The dialog kind of Graphic Style Options. pub const KIND: &str = "graphicStyleOptions"; +const OPENING: &str = "__opening"; /// What OK does with the name. +#[derive(Deserialize, Serialize)] enum Target { /// Name the style made from the selection. New, @@ -40,17 +43,24 @@ impl Target { /// Open the dialog with `name` in its field; OK runs `target`. fn open_with(app: &mut VectorcraftApp, name: String, target: Target) -> Result { - let document = app.session.active().ok_or("no document open")?.uid; + let active = app.session.active().ok_or("no document open")?; + let mut fields = DialogFields::from_pairs([("name", name.clone())]); + fields.set(OPENING, FieldValue::Json(json!({"uid": active.uid, "reverts": active.reverts, "target": target}))); let params = vec![Param::new("name", tl!("Style Name:").to_string(), ParamKind::text(name.clone()))]; let ctx = app.ui_ctx.clone().ok_or("no window")?; - let dialog = HostedParamDialog::command(&ctx, KIND, DialogFields::from_pairs([("name", name)]), move |fields| { + let dialog = HostedParamDialog::command(&ctx, KIND, fields, move |fields| { ParamDialog::command(KIND, tl!("Graphic Style Options").to_string(), params.clone()).values(Bind::bound(fields)) }); // An `Err` (a name another style has) keeps the dialog open with the reason. - let ok = move |app: &mut VectorcraftApp, result: &ParamResult| { - if !app.session.active().is_some_and(|active| active.uid == document) { + let ok = |app: &mut VectorcraftApp, result: &ParamResult| { + let Some(FieldValue::Json(opening)) = result.fields.get(OPENING) else { return Err("the graphic style target is missing".into()) }; + let Some(active) = app.session.active().filter(|st| Some(st.uid) == opening["uid"].as_u64()) else { return Err(tl!("This graphic style belongs to the document that opened the dialog.").to_owned()); + }; + if Some(active.reverts) != opening["reverts"].as_u64() { + return Err(tl!("The document was reverted. Reopen this dialog to continue.").to_owned()); } + let target = Target::deserialize(&opening["target"]).map_err(|e| e.to_string())?; target.run(app, result.fields.text("name").unwrap_or_default()).map(|_| ()) }; host::open_held(app, dialog, ok).ok_or("no window")?; @@ -164,4 +174,45 @@ mod tests { accept(&mut app, id); assert!(names(&app).iter().any(|name| name == "Source style")); } + + #[test] + fn graphic_style_options_leave_restored_definitions_alone() { + for action in ["rename", "merge", "new"] { + let mut app = app(); + let art = app.session.active().unwrap().selection.objects[0]; + app.run("graphicStyle.new", json!({"name": "First"})).unwrap(); + app.run("paint.setFill", json!({"ids": [art.0], "color": "#abcdef"})).unwrap(); + app.run("graphicStyle.new", json!({"name": "Second"})).unwrap(); + let saved = names(&app); + let dir = vectorcraft_testkit::TempDir::new("graphic-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("object.move", json!({"ids": [art.0], "dx": 10, "dy": 0})).unwrap(); + let open_action = |app: &mut VectorcraftApp| match action { + "rename" => open(app, Some("First")), + "merge" => open_merge(app, vec!["First".into(), "Second".into()]), + _ => open(app, None), + }; + open_action(&mut app).unwrap(); + let id = options(&app); + set(&app, id, "name", json!("Draft Style")); + + app.run("file.revert", json!({"confirmed": true})).unwrap(); + app.run("select.set", json!({"ids": [art.0]})).unwrap(); + let steps = app.session.active().unwrap().history.undo.len(); + accept(&mut app, id); + assert_eq!(names(&app), saved, "{action}"); + assert_eq!(app.session.active().unwrap().history.undo.len(), steps); + assert_eq!(options(&app), id); + + let ctx = app.ui_ctx.clone().unwrap(); + app.dialogs().unwrap().with(|h| h.cancel_by_id(&ctx, id)); + open_action(&mut app).unwrap(); + let id = options(&app); + set(&app, id, "name", json!("Current Style")); + accept(&mut app, id); + assert!(names(&app).iter().any(|name| name == "Current Style"), "{action}"); + } + } } From 37b6e160c1dc9a40923f2b4d1b354fa1d2f0eaba Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 19:51:57 -0800 Subject: [PATCH 2/2] Keep the Revert guard compatible with localized refusals --- crates/ui-egui/src/dialogs/graphic_style_options.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/dialogs/graphic_style_options.rs b/crates/ui-egui/src/dialogs/graphic_style_options.rs index ee66103157..bf1bbcd0ca 100644 --- a/crates/ui-egui/src/dialogs/graphic_style_options.rs +++ b/crates/ui-egui/src/dialogs/graphic_style_options.rs @@ -54,13 +54,15 @@ fn open_with(app: &mut VectorcraftApp, name: String, target: Target) -> Result