From 41fe00e938f46648ece86d27abb3a5bd96d82582 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 16:07:46 -0800 Subject: [PATCH] Keep Recolor with Preset in the opening drawing --- crates/ui-egui/src/dialogs/recolor_preset.rs | 43 ++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/crates/ui-egui/src/dialogs/recolor_preset.rs b/crates/ui-egui/src/dialogs/recolor_preset.rs index 0d3020abb5..61dfc60d6f 100644 --- a/crates/ui-egui/src/dialogs/recolor_preset.rs +++ b/crates/ui-egui/src/dialogs/recolor_preset.rs @@ -24,6 +24,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; if p.get("group").is_some_and(|g| !g.is_null()) { return Err("`group` does not apply to a preset".into()); } @@ -39,6 +40,9 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { 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) { + return Err(tl!("Open the original drawing to continue with Recolor with Preset.").to_owned()); + } let mut p = serde_json::Map::new(); if let Some(n) = colors { p.insert("colors".into(), json!(n)); @@ -59,3 +63,42 @@ fn count(v: Option<&Value>) -> Result, String> { Some(n) => n.as_u64().filter(|n| *n >= 1).map(Some).ok_or_else(|| "`colors` must be a count of at least 1".into()), } } + +#[cfg(test)] +mod tests { + use craft::ui::dialog::{HeldOk, dialog_id}; + use vectorcraft_engine::Session; + + use super::*; + + #[test] + fn recolor_preset_does_not_open_artwork_in_another_drawing() { + let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), Default::default())); + for color in ["#ff0000", "#0000ff"] { + 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(); + app.run("paint.setFill", json!({"ids": [shape], "color": color})).unwrap(); + app.run("select.set", json!({"ids": [shape]})).unwrap(); + } + app.run("document.activate", json!({"index": 0})).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); + host.with(|h| h.set_json_field(&ctx, id, "limitTo", json!("document"))).unwrap(); + assert!(!host.with(|h| h.confirm_by_id(&ctx, id)), "the preset waits for the app"); + + app.run("document.activate", json!({"index": 1})).unwrap(); + let target_undo = app.session.active().unwrap().history.undo.len(); + super::super::host::pump(&mut app, &ctx); + assert!(app.color_dialogs.recolor.is_none(), "the held answer must not open Recolor Artwork on the other drawing"); + assert_eq!(app.session.active().unwrap().history.undo.len(), target_undo); + assert!(matches!(host.with(|h| h.held_state(id)), Some(HeldOk::Refused(_)))); + + app.run("document.activate", json!({"index": 0})).unwrap(); + assert!(super::super::tests::confirm(&mut app), "the original preset draft can open Recolor Artwork"); + assert!(app.color_dialogs.recolor.is_some()); + let limit = super::super::recolor::limit_to(&host, dialog_id(super::super::recolor::KIND)); + assert_eq!(limit.as_deref(), Some("document")); + } +}