From a227cb68632bc44aa5ad2013e55ddbe71e6ddd07 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 19:17:40 -0800 Subject: [PATCH] Keep Recolor Preset with the drawing that supplied its colors --- crates/ui-egui/src/dialogs/recolor_preset.rs | 46 +++++++++++++++++--- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/dialogs/recolor_preset.rs b/crates/ui-egui/src/dialogs/recolor_preset.rs index 61dfc60d6f..37e2d113d5 100644 --- a/crates/ui-egui/src/dialogs/recolor_preset.rs +++ b/crates/ui-egui/src/dialogs/recolor_preset.rs @@ -6,7 +6,7 @@ //! Fields: `limitTo` (a library id, `"document"` for the document's swatches, `""` for None), as //! Recolor Artwork's. -use craft::ui::patterns::form_dialogs::{DialogFields, Param, ParamKind, ParamResult}; +use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, Param, ParamKind, ParamResult}; use craft::ui::runtime::env::Label; use serde_json::{Value, json}; @@ -16,6 +16,7 @@ use crate::VectorcraftApp; /// The dialog kind of Recolor with Preset. pub const KIND: &str = "recolorPreset"; +const OPENING: &str = "__opening"; /// Open the dialog for the preset of `p` (`colors`: a count; `library`: the library it starts on, /// "" the first library) on the selected art. @@ -24,7 +25,7 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { if st.selection.is_empty() { return Err("select artwork to recolor".into()); } - let document = st.uid; + let (document, reverts) = (st.uid, st.reverts); if p.get("group").is_some_and(|g| !g.is_null()) { return Err("`group` does not apply to a preset".into()); } @@ -36,15 +37,20 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { let options = library_choices(app).into_iter().map(|(key, name)| (key, Label::from(name))).collect(); let mut fields = DialogFields::new(); fields.set("limitTo", limit.clone()); + fields.set(OPENING, FieldValue::Json(json!({"uid": document, "reverts": reverts, "colors": colors}))); let rows = vec![Param::new("limitTo", field_label(tl!("Limit to Library:")), ParamKind::choice(options, Some(limit)))]; let dialog = hosted(&ctx, KIND, tl!("Recolor with Preset"), fields, rows, false); // Recolor Artwork takes the dialog's place; when it cannot open, this dialog stays. - let ok = move |app: &mut VectorcraftApp, answer: &ParamResult| -> Result<(), String> { - if !app.session.active().is_some_and(|active| active.uid == document) { + let ok = |app: &mut VectorcraftApp, answer: &ParamResult| -> Result<(), String> { + let Some(FieldValue::Json(opening)) = answer.fields.get(OPENING) else { return Err("the Recolor with Preset target is missing".into()) }; + let Some(active) = app.session.active().filter(|active| Some(active.uid) == opening["uid"].as_u64()) else { return Err(tl!("Open the original drawing to continue with Recolor with Preset.").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 mut p = serde_json::Map::new(); - if let Some(n) = colors { + if let Some(n) = opening["colors"].as_u64() { p.insert("colors".into(), json!(n)); } if let Some(limit) = answer.fields.text("limitTo").filter(|l| !l.is_empty()) { @@ -101,4 +107,34 @@ mod tests { let limit = super::super::recolor::limit_to(&host, dialog_id(super::super::recolor::KIND)); assert_eq!(limit.as_deref(), Some("document")); } + + #[test] + fn an_old_recolor_preset_does_not_open_on_restored_artwork() { + let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), Default::default())); + app.run("file.new", json!({"width": 400, "height": 400})).unwrap(); + let shape = app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 40, "height": 40})).unwrap()["id"].as_u64().unwrap(); + let dir = vectorcraft_testkit::TempDir::new("recolor-preset-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("paint.setFill", json!({"ids": [shape], "color": "#ff0000"})).unwrap(); + app.run("select.set", json!({"ids": [shape]})).unwrap(); + app.run("ui.recolorDialog", json!({"preset": true, "colors": 1})).unwrap(); + let ctx = app.ui_ctx.clone().unwrap(); + let host = app.dialogs().unwrap(); + let id = dialog_id(KIND); + assert!(!host.with(|h| h.confirm_by_id(&ctx, id))); + assert_eq!(host.with(|h| h.held_state(id)), Some(HeldOk::Waiting)); + + app.run("file.revert", json!({"confirmed": true})).unwrap(); + app.run("select.set", json!({"ids": [shape]})).unwrap(); + super::super::host::pump(&mut app, &ctx); + assert!(app.color_dialogs.recolor.is_none()); + assert!(matches!(host.with(|h| h.held_state(id)), Some(HeldOk::Refused(_)))); + + host.with(|h| h.cancel_by_id(&ctx, id)); + app.run("ui.recolorDialog", json!({"preset": true, "colors": 1})).unwrap(); + assert!(super::super::tests::confirm(&mut app)); + assert!(app.color_dialogs.recolor.is_some()); + } }