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 <[email protected]>
This commit is contained in:
ljkxandClaude Opus 5.5 committed 2026-11-02 09:19:45 +01:00
1 parent ead06c032d
commit c02842e8f5
4 files changed
+41 -1

No files matched your search

+2
View File
@@ -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) => {
+8 -1
View File
@@ -1072,9 +1072,16 @@ pub fn cancel(app: &mut PhotocraftApp, id: u64) -> Result<Value, String> {
// 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);
@@ -65,6 +65,12 @@ pub(crate) fn pickers_of(app: &PhotocraftApp, parent: u64) -> Vec<u64> {
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<Option<String>> {
@@ -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");
}