diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index c0c6bd0bc6..ff64bdd2c1 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -1463,6 +1463,12 @@ fn dispatch(app: &mut VectorcraftApp, ctx: &egui::Context, method: &str, p: &Val }; let pdf = app.save_pdf.as_ref().map(|open| open.id).filter(|_| method == "ui.dialog.set"); let reply = dialogs.with(|host| { + // A Color Picker whose Recolor Artwork row changed while it was open refuses OK. + if method == "ui.dialog.confirm" + && let Err(reason) = crate::dialogs::color_picker::check_confirm(app, host, ¶ms) + { + return Some(Err(reason)); + } // The answer goes to the dialog the fields went to, even when setting them opens another over it. if answer.is_some() && params["dialog"] == "top" @@ -1475,6 +1481,12 @@ fn dispatch(app: &mut VectorcraftApp, ctx: &egui::Context, method: &str, p: &Val { crate::dialogs::effect::normalize_set_mode(app, id, &mut params); } + // A set with `action: confirm` on such a picker changes none of its fields. + if answer == Some("ui.dialog.confirm") + && let Err(reason) = crate::dialogs::color_picker::check_confirm(app, host, ¶ms) + { + return Some(Err(reason)); + } let sets = params.as_object().is_some_and(|o| o.keys().any(|k| k != "dialog")); let set = if answer.is_none() || sets { // A Save PDF settings object keeps the options it leaves out. diff --git a/crates/ui-egui/src/dialogs/color_picker.rs b/crates/ui-egui/src/dialogs/color_picker.rs index 1a26995add..9209c854e0 100644 --- a/crates/ui-egui/src/dialogs/color_picker.rs +++ b/crates/ui-egui/src/dialogs/color_picker.rs @@ -85,6 +85,9 @@ pub(crate) struct Open { pub preview: LivePreview, /// What its Color Swatches view listed when it opened, over Recolor Artwork; empty otherwise. pub view: ViewSwatches, + /// The Recolor Artwork row it opened for, as its `from` colors and its `to` color; `None` + /// otherwise. OK answers nothing when the row at its key no longer matches. + pub row: Option, } /// The records of the colour area's open dialogs: one holder, so the app has one field for them. @@ -300,10 +303,28 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { }; // OK with the artboard's color (or the white that stands in for none) unchanged keeps the artboard as it is. let keep = artboard.is_some() && p.get("color").is_none(); - app.color_dialogs.pickers.push(Open { id, target, start, keep, preview: LivePreview::default(), view: Vec::new() }); + app.color_dialogs.pickers.push(Open { id, target, start, keep, preview: LivePreview::default(), view: Vec::new(), row: None }); Ok(Value::Null) } +/// The reason `ui.dialog.confirm` with `params` fails, when it would confirm a picker whose Recolor +/// Artwork row changed since the picker opened (OK would then answer nothing). The picker stays open. +pub(crate) fn check_confirm(app: &VectorcraftApp, host: &craft::ui::dialog::DialogHost, params: &Value) -> Result<(), String> { + let Ok(id) = craft::automation::dialogs::dialog_id(host, params) else { return Ok(()) }; + let Some(open) = app.color_dialogs.pickers.iter().find(|open| open.id == id) else { return Ok(()) }; + if let (Target::Answer { parent, key }, Some(row)) = (&open.target, &open.row) + && super::recolor::row_identity_in(host, *parent, key).as_ref() != Some(row) + { + return Err(rows_changed()); + } + Ok(()) +} + +/// Why a picker's OK answers nothing over Recolor Artwork. +fn rows_changed() -> String { + tl!("The Recolor Artwork rows changed while the Color Picker was open").to_string() +} + /// Once per frame (after the shell pass): opens a picker for each dialog's colour request, previews /// each open picker's colour on the art, hands it the screen picks, and settles the ones that /// closed. @@ -326,6 +347,7 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { .gamut(gamut(app)) .working_rgb(WorkingRgb) .swatch_view(swatches); + let row = super::recolor::row_identity(&dialogs, parent, &key); let id = dialogs.with(|h| h.open_child(child, parent)); // Over Recolor Artwork, the swatches view shows the library that Limit to Library limits to. if let Some(i) = library @@ -346,6 +368,7 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { keep: false, preview: LivePreview::default(), view, + row, }); } let busy = app.screen_pick.busy(); @@ -402,6 +425,11 @@ fn finish( let preview = std::mem::take(&mut open.preview); match (result, &open.target) { (Some(DialogResult::Ok(r)), Target::Answer { parent, key }) => { + // A row that the dialog's rows changed under (`ui.dialog.set`) takes no answer. + if open.row.is_some() && super::recolor::row_identity(dialogs, *parent, key) != open.row { + app.status(rows_changed()); + return; + } // Over Recolor Artwork, a colour picked from a swatch carries the swatch to the row. if let Some(swatch) = r.swatch.and_then(|p| picked_row_swatch(app, &open.view, p)) && super::recolor::is_recolor(dialogs, *parent) diff --git a/crates/ui-egui/src/dialogs/recolor.rs b/crates/ui-egui/src/dialogs/recolor.rs index 1bd96add15..8c0ba7c440 100644 --- a/crates/ui-egui/src/dialogs/recolor.rs +++ b/crates/ui-egui/src/dialogs/recolor.rs @@ -1863,6 +1863,19 @@ pub(super) fn answer_swatch(dialogs: &craft::ui::shell::WindowDialogs, parent: D }); } +/// Row `key` of Recolor Artwork `parent`, every field of it, or `None` when `parent` is not Recolor +/// Artwork or has no such row. A Color Picker opened for the row answers it only while this is unchanged. +pub(super) fn row_identity(dialogs: &craft::ui::shell::WindowDialogs, parent: DialogId, key: &str) -> Option { + dialogs.with(|host| row_identity_in(host, parent, key)) +} + +/// [`row_identity`] on the dialog host itself. +pub(super) fn row_identity_in(host: &craft::ui::dialog::DialogHost, parent: DialogId, key: &str) -> Option { + let row: usize = key.parse().ok()?; + let d = host.get_by_id::(parent)?; + rows(&d.fields).into_iter().nth(row) +} + /// Whether `parent` is the Recolor Artwork dialog. pub(super) fn is_recolor(dialogs: &craft::ui::shell::WindowDialogs, parent: DialogId) -> bool { dialogs.with(|host| host.get_by_id::(parent).is_some()) diff --git a/crates/ui-egui/src/tests/recolor.rs b/crates/ui-egui/src/tests/recolor.rs index b32a5d0769..96d6129364 100644 --- a/crates/ui-egui/src/tests/recolor.rs +++ b/crates/ui-egui/src/tests/recolor.rs @@ -1484,6 +1484,32 @@ fn row_context_menu_pick_from_swatches_and_limit_to_library() { } } +/// A Color Picker opened for a row answers nothing when `ui.dialog.set` changed the rows while it +/// was open: OK leaves the row now at its index, and that row's swatch, as they are. +#[test] +fn a_picker_answer_skips_a_row_that_changed_while_it_was_open() { + let mut r = edit(&["#ff0000", "#0000ff"]); + let at = marker(&r, 0); + right_click(&mut r, at); + tap(&mut r, "Color Picker"); + let (id, key, _) = picker(&r).expect("the marker's Color Picker opens the picker"); + assert_eq!(key, "0"); + r.host(|host, ctx| host.set_json_field(ctx, id, "color", json!("#00ff00"))).unwrap().unwrap(); + let params = json!({"dialog": id.value().to_string()}); + let check = |r: &Recolor| r.host(|host, _| dialogs::color_picker::check_confirm(&r.app(), host, ¶ms)).unwrap(); + assert_eq!(check(&r), Ok(()), "an unchanged row takes the answer"); + let mut swapped = field(&r, "rows"); + swapped.as_array_mut().unwrap().swap(0, 1); + put(&mut r, "rows", swapped.clone()); + let refused = check(&r).expect_err("ui.dialog.confirm refuses the stale answer"); + assert!(refused.contains("rows changed"), "{refused}"); + r.host(|host, ctx| host.confirm_by_id(ctx, id)); + r.settled(); + assert!(picker(&r).is_none() && r.is_open(), "back on Recolor Artwork"); + assert_eq!(field(&r, "rows"), swapped, "neither row takes the answer"); + assert!(r.app().ui.status.contains("rows changed"), "{}", r.app().ui.status); +} + /// A Color Picker opened over the dialog, on its color models, has the Color Swatches button that /// switches it to the swatches. #[test]