fix(ui): close field Color Pickers with their dialog, guard previewed dialogs (#9979 review)

Follow-up to the CodeRabbit review of the live Color Picker preview:

- Cancelling a dialog now also closes its field Color Pickers (found
  by `__dialog` or by the live preview's record of the parent). Before,
  the picker stayed open and its OK failed with "the dialog this colour
  was for is closed".
- confirm_preflight also refuses a dialog that a live preview records
  as its parent, so rewriting a picker's `__dialog` through automation
  no longer lets the parent commit a colour the picker can still cancel.
- ui.dialog.set on a dialog under a previewing picker is refused until
  the picker's OK or Cancel, since closing the picker puts the whole
  dialog back as it was when the picker opened.
- The Crop shield preview adopts a shield colour set elsewhere
  (ui.set) while its picker is open, so Cancel no longer reverts it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ljkxandClaude Opus 5.5 committed 2026-11-02 08:28:48 +01:00
1 parent 2d3b27f00a
commit ead06c032d
4 files changed
+99 -20

No files matched your search

+4
View File
@@ -1019,10 +1019,14 @@ fn discard_dialog_confirm(app: &mut PhotocraftApp, ctx: &egui::Context, choice:
fn dialog_set(app: &mut PhotocraftApp, p: &Value) -> Outcome {
let (Some(id), Some(field)) = (u64_param(p, "dialog"), str_param(p, "field")) else { return err("need `dialog` and `field`") };
let value = p.get("value").cloned().unwrap_or(Value::Null);
// A Color Picker previewing in this dialog owns its fields until its OK or Cancel: an edit now
// would be undone by the picker putting the dialog back as it was.
let previewed = crate::color_picker_ui::live_preview::previewed_dialogs(app).contains(&id);
match app.ui.dialog_mut(id) {
// A continuation belongs to the dialog that paused it, even when another
// 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 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) => {
+13 -1
View File
@@ -981,7 +981,10 @@ fn confirm_preflight(app: &mut PhotocraftApp, id: u64) -> Result<(), String> {
}
// A colour field shows the open picker's colour before its OK (`live_preview`): the dialog
// would otherwise commit a colour the picker may still cancel.
if app.ui.dialogs.iter().any(|child| crate::color_picker_ui::owns(&child.fields) && child.fields.get("__dialog").and_then(Value::as_u64) == Some(id)) {
// The live preview's own record of the parent counts too: automation may rewrite `__dialog`.
if app.ui.dialogs.iter().any(|child| crate::color_picker_ui::owns(&child.fields) && child.fields.get("__dialog").and_then(Value::as_u64) == Some(id))
|| crate::color_picker_ui::live_preview::previewed_dialogs(app).contains(&id)
{
return Err("confirm or cancel the color picker first".into());
}
if let Some(d) = app.ui.dialogs.iter().find(|d| d.id == id).filter(|d| crate::guide_layout_ui::owns(&d.fields)) {
@@ -1068,6 +1071,15 @@ pub fn cancel(app: &mut PhotocraftApp, id: u64) -> Result<Value, String> {
let closed = app.ui.close_dialog(id).ok_or_else(|| format!("no dialog {id}"))?;
// 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.
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),
);
for picker in &pickers {
crate::color_picker_ui::live_preview::end(app, *picker);
}
app.ui.dialogs.retain(|d| !pickers.contains(&d.id));
crate::actions::stopped(app, &closed.fields);
if crate::guide_layout_ui::owns(&closed.fields) {
app.ui.dialogs.retain(|child| child.fields.get("__dialog").and_then(Value::as_u64) != Some(id));
@@ -60,7 +60,12 @@ pub(crate) fn previewed_dialogs(app: &PhotocraftApp) -> Vec<u64> {
.collect()
}
/// The colour to show for picker `picker`: `Some(None)` while it is the one it opened on (or not
/// The field pickers previewing in dialog `parent`: they close with it.
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()
}
/// 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>> {
let d = app.ui.dialogs.iter().find(|d| d.id == picker)?;
@@ -69,23 +74,41 @@ fn picked(app: &PhotocraftApp, picker: u64) -> Option<Option<String>> {
Some(color.filter(|c| orig.as_ref() != Some(c)))
}
/// Show `color` in the target, or what it was (`None`). `Err` when the target is gone.
fn show(app: &mut PhotocraftApp, before: &Before, color: Option<&str>) -> Result<(), String> {
/// `"#rrggbb"` as the Crop shield stores it.
fn shield_rgb(color: &str) -> Option<[u8; 3]> {
parse_hex(color).map(|c| c.map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8))
}
/// Show `color` in the target, or what it was (`None`), where it shows `shown` now. `Err` when
/// the target is gone.
fn show(app: &mut PhotocraftApp, before: &mut Before, shown: Option<&str>, color: Option<&str>) -> Result<(), String> {
match before {
Before::Field { parent, field, fields } => {
let mut shown = fields.clone();
let mut next = fields.clone();
if let Some(c) = color {
set_field(&mut shown, field, json!(c))?;
set_field(&mut next, field, json!(c))?;
}
app.ui.dialog_mut(*parent).ok_or("the dialog this colour was for is closed")?.fields = shown;
app.ui.dialog_mut(*parent).ok_or("the dialog this colour was for is closed")?.fields = next;
}
Before::CropShield { color: was, custom } => match color {
Some(c) => crate::crop_shield::set_custom_color(app, c)?,
None => {
let s = &mut app.ui.tool_options.crop_shield;
(s.color, s.custom_color) = (*was, *custom);
Before::CropShield { color: was, custom } => {
// A shield colour set elsewhere while the picker is open (`ui.set`) is what Cancel
// puts back, not the one the picker opened on.
let s = &app.ui.tool_options.crop_shield;
let expected = match shown {
Some(c) => (ShieldColor::Custom, shield_rgb(c)),
None => (*was, Some(*custom)),
};
if (s.color, Some(s.custom_color)) != expected {
(*was, *custom) = (s.color, s.custom_color);
}
},
match color {
Some(c) => crate::crop_shield::set_custom_color(app, c)?,
None => {
let s = &mut app.ui.tool_options.crop_shield;
(s.color, s.custom_color) = (*was, *custom);
}
}
}
}
Ok(())
}
@@ -101,7 +124,7 @@ pub(crate) fn sync(app: &mut PhotocraftApp) -> bool {
live.retain_mut(|l| {
let Some(color) = picked(app, l.picker) else {
if l.shown.is_some() {
changed |= show(app, &l.before, None).is_ok();
changed |= show(app, &mut l.before, l.shown.as_deref(), None).is_ok();
}
return false;
};
@@ -109,7 +132,7 @@ pub(crate) fn sync(app: &mut PhotocraftApp) -> bool {
return true;
}
// A target that is gone (its dialog closed) stops the preview.
if show(app, &l.before, color.as_deref()).is_err() {
if show(app, &mut l.before, l.shown.as_deref(), color.as_deref()).is_err() {
return false;
}
l.shown = color;
@@ -125,9 +148,9 @@ pub(crate) fn sync(app: &mut PhotocraftApp) -> bool {
/// stop previewing. OK then sets the target as it always has.
pub(crate) fn end(app: &mut PhotocraftApp, picker: u64) {
let Some(i) = app.color_picker_live.iter().position(|l| l.picker == picker) else { return };
let l = app.color_picker_live.remove(i);
let mut l = app.color_picker_live.remove(i);
if l.shown.is_some() {
let _ = show(app, &l.before, None);
let _ = show(app, &mut l.before, l.shown.as_deref(), None);
}
}
@@ -141,27 +141,67 @@ fn a_picker_or_parent_closed_any_other_way_ends_the_preview() {
assert_eq!(fields(&app, fill), &before);
assert!(app.color_picker_live.is_empty());
// A cancelled dialog takes its field picker with it, leaving no picker whose OK has no target.
let (fill, picker) = fill_with_picker(&mut app);
let other = open_for_field(&mut app, "Color Picker (Other)", [0.0; 3], 999_999, "color");
pick(&mut app, picker, "#336699");
crate::dialogs::cancel(&mut app, fill).unwrap();
pick(&mut app, picker, "#ff0000");
assert!(app.ui.dialogs.iter().all(|d| d.id != picker), "the picker closes with its dialog");
assert!(app.ui.dialogs.iter().any(|d| d.id == other), "another dialog's picker stays");
assert!(app.color_picker_live.is_empty(), "the preview ends with its dialog");
assert!(crate::dialogs::confirm(&mut app, picker).is_err(), "OK reports the closed dialog");
assert_eq!(past(&app), 0);
}
/// The dialog under a picker can't be confirmed (by automation) with a colour the picker still
/// only shows.
/// only shows, even when the picker's `__dialog` was rewritten to point elsewhere.
#[test]
fn the_dialog_waits_for_its_picker() {
let mut app = app();
let (fill, picker) = fill_with_picker(&mut app);
pick(&mut app, picker, "#336699");
assert!(crate::dialogs::confirm(&mut app, fill).is_err());
app.ui.dialog_mut(picker).unwrap().fields.insert("__dialog".into(), json!(999_999));
assert!(crate::dialogs::confirm(&mut app, fill).is_err(), "the live preview's record of the dialog counts");
assert_eq!(past(&app), 0);
assert!(app.ui.dialogs.iter().any(|d| d.id == fill), "the dialog stays open");
}
/// Automation can't edit a dialog under a previewing picker: Cancel would put the edit back.
#[test]
fn the_dialog_under_a_picker_refuses_control_edits() {
let ctx = egui::Context::default();
let mut app = app();
let (fill, picker) = fill_with_picker(&mut app);
let (req, _rx) = crate::control::ControlRequest::new("ui.dialog.set", json!({"dialog": fill, "field": "opacity", "value": 50}));
let crate::control::Outcome::Done(r) = crate::control::handle(&mut app, &ctx, &req) else { panic!("an immediate reply") };
assert_eq!(r["ok"], false, "{r}");
assert_eq!(fields(&app, fill)["opacity"], json!(100.0));
crate::dialogs::cancel(&mut app, picker).unwrap();
let (req, _rx) = crate::control::ControlRequest::new("ui.dialog.set", json!({"dialog": fill, "field": "opacity", "value": 50}));
let crate::control::Outcome::Done(r) = crate::control::handle(&mut app, &ctx, &req) else { panic!("an immediate reply") };
assert_eq!(r["ok"], true, "{r}");
}
/// A shield colour set elsewhere (`ui.set cropShield`) while its picker is open survives the
/// picker's Cancel.
#[test]
fn the_crop_shield_keeps_a_colour_set_while_its_picker_is_open() {
let mut app = app();
let picker = crate::crop_shield::pick_custom_color(&mut app);
pick(&mut app, picker, "#336699");
app.ui.tool_options.crop_shield.custom_color = [1, 2, 3];
crate::dialogs::cancel(&mut app, picker).unwrap();
let s = &app.ui.tool_options.crop_shield;
assert_eq!((s.color, s.custom_color), (ShieldColor::Custom, [1, 2, 3]));
let picker = crate::crop_shield::pick_custom_color(&mut app);
app.ui.tool_options.crop_shield.color = ShieldColor::MatchCanvas;
pick(&mut app, picker, "#ff0000");
crate::dialogs::cancel(&mut app, picker).unwrap();
let s = &app.ui.tool_options.crop_shield;
assert_eq!((s.color, s.custom_color), (ShieldColor::MatchCanvas, [1, 2, 3]), "Cancel puts back the choice made while it was open");
}
/// The dialog's own ⌘Z history records no step for a previewed colour: none after Cancel, one
/// after OK.
#[test]