diff --git a/crates/imaging/src/ui/adjust/hosted.rs b/crates/imaging/src/ui/adjust/hosted.rs index 2e4602471..328bceaa9 100644 --- a/crates/imaging/src/ui/adjust/hosted.rs +++ b/crates/imaging/src/ui/adjust/hosted.rs @@ -91,6 +91,9 @@ pub struct HostedAdjustDialog { title: Label, open: bool, value: V, + /// Whether the value was edited since the dialog opened: by the editor, Reset, a field set or + /// [`value_mut`](HostedAdjustDialog::value_mut). A [`seed`](HostedAdjustDialog::seed) is not an edit. + edited: bool, preview: bool, comparing: bool, context: AdjustContext, @@ -129,6 +132,7 @@ impl Hoste title: title.into(), open: true, value, + edited: false, preview: true, comparing: false, context: AdjustContext::dialog(true), @@ -246,11 +250,26 @@ impl Hoste &self.value } - /// The value, for the app's answers to the editor's asks (Auto's levels). + /// The value, for the app's edits of it (Auto's levels, a picked colour, an automation set): the dialog counts + /// as [`edited`](HostedAdjustDialog::edited) from then on. pub fn value_mut(&mut self) -> &mut V { + self.edited = true; &mut self.value } + /// Replaces the value with the one the dialog opens at (the app seeds it after opening, or again once its target + /// resolves or changes), without counting as an edit. + pub fn seed(&mut self, value: V) { + self.value = value; + } + + /// Whether the value was edited since the dialog opened: by the editor, Reset, a field set or + /// [`value_mut`](HostedAdjustDialog::value_mut). Preview, the picker, the channel and the clipping display are not + /// the value; seeds are not edits. + pub fn edited(&self) -> bool { + self.edited + } + /// Whether the Preview box is ticked. pub fn is_previewing(&self) -> bool { self.preview @@ -279,6 +298,7 @@ impl Stack title, open, value, + edited, preview, comparing, context, @@ -302,6 +322,8 @@ impl Stack // The request to select a point is this pass's alone: a rerun of the pass in the frame finds none. let (canvas, select_point) = (*canvas, select_point.take()); let (context, resizable, remember) = (*context, *resizable, remember.clone()); + // What the editor, Reset and the form's undo change in this pass is the dialog's edit. + let before = value.clone(); let form = FormDialog::new(title.clone(), Bind::bound(value)) .opened(Bind::bound(open)) .preview(Some(Bind::bound(&mut *preview))) @@ -334,6 +356,7 @@ impl Stack true }); *comparing = shown.comparing; + *edited |= *value != before; if held.refusal().is_some_and(|(_, refused)| refused != value) { held.clear_refusal(); } @@ -403,7 +426,10 @@ impl Stack let clip = clipping_from_json(&value).ok_or_else(|| rejected(&value))?; self.set_clipping(clip); } - _ => return set_serde_field(&mut self.value, field, value), + _ => { + set_serde_field(&mut self.value, field, value)?; + self.edited = true; + } } Ok(()) } diff --git a/crates/imaging/tests/it/hosted_dialogs.rs b/crates/imaging/tests/it/hosted_dialogs.rs index aa312782f..eb57caa4b 100644 --- a/crates/imaging/tests/it/hosted_dialogs.rs +++ b/crates/imaging/tests/it/hosted_dialogs.rs @@ -100,6 +100,36 @@ fn a_hosted_adjustment_dialog_previews_and_answers_with_its_value() { assert_eq!(host.borrow_mut().take::(), Some(DialogResult::Cancel), "Escape cancels a reopened dialog"); } +// The dialog's own edited signal: false at open, after a preview toggle and after a seed; true after an editor change, +// and after a field set of the value. +#[trial] +fn an_adjustment_dialog_counts_editor_changes_and_sets_as_edits_but_not_seeds_or_preview() { + let host = Rc::new(RefCell::new(DialogHost::new())); + let mut fixture = fixture(&host); + let id = host.borrow_mut().open(brightness_contrast()); + fixture.idle(10); + let edited = |host: &Rc>| host.borrow().get::().map(Adjust::edited); + assert_eq!(edited(&host), Some(false), "a dialog opens unedited"); + let preview = fixture.by_label("Preview").expect("the Preview box"); + fixture.click(&preview).idle(4); + if let Some(dialog) = host.borrow_mut().get_mut::() { + dialog.seed(BrightnessContrast { brightness: 0.0, contrast: 0.0, legacy: false }); + } + fixture.idle(4); + assert_eq!(edited(&host), Some(false), "a preview toggle and a seed are not edits"); + let slider = fixture.by_role(craft_ui::contract::Role::SLIDER).first().cloned().expect("the Brightness slider"); + fixture.drag(&slider, Vec2::new(slider.rect.width() / 4.0, 0.0)).settle(); + assert_eq!(edited(&host), Some(true), "an editor change is an edit"); + + let host = Rc::new(RefCell::new(DialogHost::new())); + let mut again = self::fixture(&host); + host.borrow_mut().open(brightness_contrast()); + again.idle(10); + let ctx = again.ctx().clone(); + assert_eq!(host.borrow_mut().set_json_field(&ctx, id, "contrast", serde_json::json!(0.0)), Ok(())); + assert_eq!(edited(&host), Some(true), "a field set of the value is an edit"); +} + /// Why the app refuses the held OK. const REFUSED: &str = "The layer is locked.";