From c02842e8f57a0101cfbf6ac86cee8bf741455c13 Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 2 Nov 2026 09:19:45 +0100 Subject: [PATCH] fix(ui): keep a Color Picker's target fixed while it is open (#9979 review) - ui.dialog.set refuses `__dialog` and `__field` on an open Color Picker. Otherwise automation could point the picker's OK at another dialog than the one its live preview showed the colour in. - Cancelling a dialog closes only the field pickers the live preview records for it. A `__dialog` match counts only for a picker without a live record, so a rewritten `__dialog` can no longer close another dialog's picker and put that dialog back as it was. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/control/dispatch.rs | 2 ++ crates/ui-egui/src/dialogs.rs | 9 ++++++- .../dialogs/color_picker_ui/live_preview.rs | 6 +++++ .../color_picker_ui/live_preview/tests.rs | 25 +++++++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/control/dispatch.rs b/crates/ui-egui/src/control/dispatch.rs index 89ac0d08256..cd81f105865 100644 --- a/crates/ui-egui/src/control/dispatch.rs +++ b/crates/ui-egui/src/control/dispatch.rs @@ -1027,6 +1027,8 @@ fn dialog_set(app: &mut PhotocraftApp, p: &Value) -> Outcome { // action has a valid token. Replacing it would resume or cancel that action. Some(_) if field == crate::actions::RESUME => err("the paused action token is read-only"), Some(_) if previewed => err("confirm or cancel the color picker first"), + // A Color Picker's target is fixed when it opens: OK must set the field it previewed in. + Some(d) if matches!(field, "__dialog" | "__field") && crate::color_picker_ui::owns(&d.fields) => err("a Color Picker's target is fixed when it opens"), // A marker picks the handler OK runs: a dialog keeps the ones it was opened with. Some(d) if !d.fields.contains_key(field) && crate::dialogs::is_route_marker(field) => err(format!("`{field}` is set only by the dialog it belongs to")), Some(d) => { diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index c85ab8dd61a..7716a408edb 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -1072,9 +1072,16 @@ pub fn cancel(app: &mut PhotocraftApp, id: u64) -> Result { // A Color Picker puts back what its live preview showed in its target. crate::color_picker_ui::live_preview::end(app, id); // A field Color Picker of this dialog has no target left: it closes with the dialog. + // A picker with a live target goes by the parent recorded when it opened; `__dialog` only + // names the parent of one without (e.g. its parent was closed before it could preview). let mut pickers = crate::color_picker_ui::live_preview::pickers_of(app, id); pickers.extend( - app.ui.dialogs.iter().filter(|d| crate::color_picker_ui::owns(&d.fields) && d.fields.get("__dialog").and_then(Value::as_u64) == Some(id)).map(|d| d.id), + app.ui + .dialogs + .iter() + .filter(|d| crate::color_picker_ui::owns(&d.fields) && d.fields.get("__dialog").and_then(Value::as_u64) == Some(id)) + .filter(|d| !crate::color_picker_ui::live_preview::previews(app, d.id)) + .map(|d| d.id), ); for picker in &pickers { crate::color_picker_ui::live_preview::end(app, *picker); diff --git a/crates/ui-egui/src/dialogs/color_picker_ui/live_preview.rs b/crates/ui-egui/src/dialogs/color_picker_ui/live_preview.rs index ae2035ed0ff..dbf4455fc1a 100644 --- a/crates/ui-egui/src/dialogs/color_picker_ui/live_preview.rs +++ b/crates/ui-egui/src/dialogs/color_picker_ui/live_preview.rs @@ -65,6 +65,12 @@ pub(crate) fn pickers_of(app: &PhotocraftApp, parent: u64) -> Vec { app.color_picker_live.iter().filter(|l| matches!(l.before, Before::Field { parent: p, .. } if p == parent)).map(|l| l.picker).collect() } +/// Whether picker `picker` has a live target: then its parent is the one recorded here, not its +/// `__dialog` field, which automation could have rewritten. +pub(crate) fn previews(app: &PhotocraftApp, picker: u64) -> bool { + app.color_picker_live.iter().any(|l| l.picker == picker) +} + /// The colour picker `picker` shows: `Some(None)` while it is the one it opened on (or not /// a colour), `None` once the picker is closed. fn picked(app: &PhotocraftApp, picker: u64) -> Option> { diff --git a/crates/ui-egui/src/dialogs/color_picker_ui/live_preview/tests.rs b/crates/ui-egui/src/dialogs/color_picker_ui/live_preview/tests.rs index f1d277fb8ee..1a3b852f9fa 100644 --- a/crates/ui-egui/src/dialogs/color_picker_ui/live_preview/tests.rs +++ b/crates/ui-egui/src/dialogs/color_picker_ui/live_preview/tests.rs @@ -299,3 +299,28 @@ fn the_dialog_shows_the_colour_while_the_picker_is_open() { assert_eq!(crate::color_picker_ui::top(h.state()), None); assert_eq!(fields(h.state(), fill)["color"], json!("#20a040")); } + +/// A picker's target is fixed when it opens: automation can't point its OK at another dialog, and +/// cancelling a dialog its `__dialog` was rewritten to never closes it. +#[test] +fn a_pickers_target_stays_the_dialog_it_opened_on() { + let ctx = egui::Context::default(); + let mut app = app(); + let (fill, picker) = fill_with_picker(&mut app); + let other = app.ui.open_dialog(DialogKind::Command, Map::new()); + let set = |app: &mut PhotocraftApp, field: &str, value: Value| { + let (req, _rx) = crate::control::ControlRequest::new("ui.dialog.set", json!({"dialog": picker, "field": field, "value": value})); + let crate::control::Outcome::Done(r) = crate::control::handle(app, &ctx, &req) else { panic!("an immediate reply") }; + r["ok"].as_bool() + }; + assert_eq!(set(&mut app, "__dialog", json!(other)), Some(false)); + assert_eq!(set(&mut app, "__field", json!("opacity")), Some(false)); + assert_eq!(set(&mut app, "color", json!("#336699")), Some(true), "the colour itself stays drivable"); + assert_eq!(fields(&app, fill)["color"], json!("#336699")); + + // Rewritten another way, `__dialog` still doesn't make it another dialog's picker. + app.ui.dialog_mut(picker).unwrap().fields.insert("__dialog".into(), json!(other)); + crate::dialogs::cancel(&mut app, other).unwrap(); + assert!(app.ui.dialogs.iter().any(|d| d.id == picker), "the picker stays with its own dialog"); + assert_eq!(fields(&app, fill)["color"], json!("#336699"), "and keeps previewing there"); +}