diff --git a/crates/automation/tests/it/dialogs.rs b/crates/automation/tests/it/dialogs.rs index d1ccd0e21..f05f2563e 100644 --- a/crates/automation/tests/it/dialogs.rs +++ b/crates/automation/tests/it/dialogs.rs @@ -34,7 +34,9 @@ fn dialog_methods_read_set_and_answer_typed_dialogs() { ); assert_eq!(inspect(&host)[0]["fields"]["radius"], 4.5); assert_eq!(call(&mut host, &ctx, "ui.dialog.confirm", json!({"dialog": blur})), Ok(Value::Null)); - let Some(DialogResult::Ok(result)) = host.take::() else { panic!("the blur dialog answers OK") }; + let Some(DialogResult::Ok(result)) = host.take_keyed::().map(|(result, _)| result) else { + panic!("the blur dialog answers OK") + }; assert_eq!((result.fields.number("radius"), result.fields.text("method")), (Some(4.5), Some("spin"))); assert_eq!(inspect(&host), json!([])); @@ -90,7 +92,7 @@ fn param_dialog_automation_follows_its_rows() { host.open(blur(false)); let id = inspect(&host)[0]["id"].clone(); assert!(call(&mut host, &ctx, "ui.dialog.confirm", json!({"dialog": id})).is_err()); - assert!(host.take::().is_none()); + assert!(host.take_keyed::().map(|(result, _)| result).is_none()); assert!(host.cancel::(&ctx)); host.open(blur(true)); @@ -104,7 +106,9 @@ fn param_dialog_automation_follows_its_rows() { assert_eq!(set(&mut host, "radius", json!(7)), Ok(Value::Null)); assert_eq!(inspect(&host)[0]["fields"]["cells"], json!([1, 2, 3])); assert_eq!(call(&mut host, &ctx, "ui.dialog.confirm", json!({"dialog": id})), Ok(Value::Null)); - let Some(DialogResult::Ok(result)) = host.take::() else { panic!("the blur dialog answers OK") }; + let Some(DialogResult::Ok(result)) = host.take_keyed::().map(|(result, _)| result) else { + panic!("the blur dialog answers OK") + }; assert_eq!(result.fields.grid("cells"), Some(&[1, 2, 3][..])); result.remember(&ctx); @@ -114,7 +118,9 @@ fn param_dialog_automation_follows_its_rows() { let fields = &listed[0]["fields"]; assert_eq!((fields["radius"].as_f64(), &fields["method"], &fields["cells"]), (Some(7.0), &json!("zoom"), &json!([0, 1, 0])), "{fields}"); assert_eq!(call(&mut host, &ctx, "ui.dialog.confirm", json!({"dialog": id})), Ok(Value::Null)); - let Some(DialogResult::Ok(reopened)) = host.take::() else { panic!("the reopened dialog answers OK") }; + let Some(DialogResult::Ok(reopened)) = host.take_keyed::().map(|(result, _)| result) else { + panic!("the reopened dialog answers OK") + }; assert_eq!((reopened.fields.number("radius"), reopened.fields.text("method")), (Some(7.0), Some("zoom"))); } diff --git a/crates/imaging/src/ui/adjust/hosted.rs b/crates/imaging/src/ui/adjust/hosted.rs index 3df34fa14..2e4602471 100644 --- a/crates/imaging/src/ui/adjust/hosted.rs +++ b/crates/imaging/src/ui/adjust/hosted.rs @@ -1,11 +1,16 @@ //! [`HostedAdjustDialog`]: an Image › Adjustments dialog on the dialog host, around any adjustment editor. use craft_behavior::{GestureId, Value}; -use craft_ui::contract::{Bind, Sampler as _}; -use craft_ui::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, StackDialog, dialog_id, serde_fields, set_serde_field}; +use craft_theme::variant::Tone; +use craft_ui::contract::{Bind, Sampler as _, Show as _}; +use craft_ui::dialog::{ + DialogId, DialogResult, DialogStack, FieldRefusal, HeldAnswer, HeldOk, StackDialog, dialog_id, serde_fields, set_serde_field, +}; +use craft_ui::notices::{Notice, NoticeLayout}; use craft_ui::patterns::form_dialogs::FormDialog; use craft_ui::runtime::env::Label; use craft_ui::runtime::memory::SaveKey; +use craft_ui::runtime::overlay::CloseReason; use egui::{Context, Ui}; use serde::Serialize; use serde::de::DeserializeOwned; @@ -99,6 +104,10 @@ pub struct HostedAdjustDialog { select_point: Option, editor: Box>, asks: Vec, + /// Whether OK files its value for the app instead of closing ([`held_ok`](HostedAdjustDialog::held_ok)). + hold: bool, + /// The OK the app holds back until it answers `answer_ok`, and its refusal, shown until the value changes. + held: HeldAnswer, #[cfg(feature = "screen-testing")] facts: craft_ui::testing::StoryOutput, } @@ -133,11 +142,33 @@ impl Hoste select_point: None, editor: Box::new(editor), asks: Vec::new(), + hold: false, + held: HeldAnswer::new(), #[cfg(feature = "screen-testing")] facts: craft_ui::testing::StoryOutput::default(), } } + /// Makes OK file its value for the app instead of closing: the app runs the adjustment and answers with + /// [`answer_ok`](HostedAdjustDialog::answer_ok); a refusal keeps the dialog open and shows why. OK stays disabled + /// until the answer. + #[must_use] + pub fn held_ok(mut self) -> Self { + self.hold = true; + self + } + + /// The value a held OK filed, once. + pub fn take_ok(&mut self) -> Option { + self.held.take() + } + + /// The app's answer to the held OK: `Ok` closes the dialog with the held value in the next pass; `Err` enables OK + /// again and shows the reason under the editor until the value changes. Without a held OK it does nothing. + pub fn answer_ok(&mut self, answer: Result<(), Label>) { + self.held.answer(answer); + } + /// The editor's context: Dynamic Color Sliders, a grey document. Always a dialog context. #[must_use] pub fn context(mut self, dynamic_color_sliders: bool, gray: bool) -> Self { @@ -261,20 +292,28 @@ impl Stack clipping, select_point, reset_with, + hold, + held, .. } = self; + // The app accepted the held OK: this pass closes the dialog with it. A measuring run closes nothing. + let finish = held.is_accepted() && !craft_ui::runtime::measure::active(ui.ctx()); + let refusal = held.refusal().map(|(reason, refused)| (reason.clone(), refused.clone())); // 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()); - let form = FormDialog::new(title.clone(), Bind::bound(value)).opened(Bind::bound(open)).preview(Some(Bind::bound(&mut *preview))).shaped( - move |dialog| { + let form = FormDialog::new(title.clone(), Bind::bound(value)) + .opened(Bind::bound(open)) + .preview(Some(Bind::bound(&mut *preview))) + .accept_stays_open(*hold) + .ok_enabled(!held.is_waiting()) + .shaped(move |dialog| { let dialog = dialog.resizable(resizable); match remember { Some(key) => dialog.remember(key), None => dialog, } - }, - ); + }); let form = match reset_with.as_deref() { Some(target) => form.reset_with(target), None => form, @@ -286,14 +325,30 @@ impl Stack let mut cx = AdjustDialogCx { context, canvas, picker: &mut *picker, channel: &mut *channel, select_point, clipping: &mut *clipping, edit: None }; asks.extend(editor(ui, value, &mut cx)); + if let Some((reason, _)) = refusal.as_ref().filter(|(_, refused)| refused == value) { + Notice::new(Tone::Bad, reason.clone()).layout(NoticeLayout::Inline).show(ui); + } + if finish { + form.request_close_as(CloseReason::Activated); + } true }); *comparing = shown.comparing; + if held.refusal().is_some_and(|(_, refused)| refused != value) { + held.clear_refusal(); + } + // The pass reports a press only in the pass that took the input, so a second pass of the frame files nothing. + if *hold && shown.picked == Some(0) && !held.is_waiting() { + held.file(value.clone()); + } + if finish && shown.closed == Some(CloseReason::Activated) { + return Some(held.finish().map_or(DialogResult::Cancel, DialogResult::Ok)); + } #[cfg(feature = "screen-testing")] { self.facts = craft_ui::contract::Outcome::story_facts(&shown.response); } - let accepted = shown.accepted.map(DialogResult::Ok); + let accepted = shown.accepted.filter(|_| !*hold).map(DialogResult::Ok); accepted.or(shown.closed.map(|_| DialogResult::Cancel)) } @@ -301,10 +356,26 @@ impl Stack Some(&self.key) } + /// OK as the button: a held dialog files the value and answers none, and answers the held value once the app + /// accepted it. fn confirm(&mut self, _: &Context) -> Option { + if self.held.is_accepted() { + return self.held.finish(); + } + if self.held.is_waiting() { + return None; + } + if self.hold { + self.held.file(self.value.clone()); + return None; + } Some(self.value.clone()) } + fn held_state(&self) -> Option { + self.held.state() + } + fn json_fields(&self) -> Option { let mut fields = serde_fields(&self.value)?; let object = fields.as_object_mut()?; diff --git a/crates/imaging/tests/it/hosted_dialogs.rs b/crates/imaging/tests/it/hosted_dialogs.rs index e5ac507c5..aa312782f 100644 --- a/crates/imaging/tests/it/hosted_dialogs.rs +++ b/crates/imaging/tests/it/hosted_dialogs.rs @@ -7,7 +7,8 @@ use std::rc::Rc; use craft_imaging::layer_style::{EffectId, EffectInstance, EffectKind, LayerStyle, LayerStyleDraft, LayerStylePage, StyleParams}; use craft_imaging::ui::adjust::{BrightnessContrast, BrightnessContrastEditor, HostedAdjustDialog}; use craft_imaging::ui::layer_style::{HostedLayerStyle, LayerStyleAsk}; -use craft_ui::dialog::{DialogHost, DialogResult, StackDialog, dialog_id}; +use craft_ui::dialog::{DialogHost, DialogResult, HeldOk, StackDialog, dialog_id}; +use craft_ui::runtime::env::Label; use craft_ui::testing::{Fixture, trial}; use egui::{Key, Modifiers, Vec2}; @@ -98,3 +99,46 @@ fn a_hosted_adjustment_dialog_previews_and_answers_with_its_value() { fixture.idle(10).key(Key::Escape, Modifiers::NONE).idle(4); assert_eq!(host.borrow_mut().take::(), Some(DialogResult::Cancel), "Escape cancels a reopened dialog"); } + +/// Why the app refuses the held OK. +const REFUSED: &str = "The layer is locked."; + +// OK of a held adjustment dialog files its value and waits with OK disabled; a refusal keeps the dialog open with OK +// enabled and the reason shown until the value changes; an accepted OK closes it with the value. +#[trial] +fn a_held_adjustment_ok_refused_stays_open_and_an_accepted_one_closes() { + let host = Rc::new(RefCell::new(DialogHost::new())); + let mut fixture = fixture(&host); + let id = host.borrow_mut().open(brightness_contrast().held_ok()); + fixture.idle(10); + let on_dialog = |act: &mut dyn FnMut(&mut Adjust)| host.borrow_mut().get_mut::().map(act); + + let ok = fixture.by_label("OK").expect("the OK button"); + fixture.click(&ok).idle(4); + let mut filed = None; + on_dialog(&mut |dialog| filed = dialog.take_ok()); + assert_eq!(filed.map(|value| value.brightness), Some(20.0), "OK files the value"); + assert_eq!(host.borrow().held_state(id), Some(HeldOk::Waiting), "the filed OK waits for the app"); + let ok = fixture.by_label("OK").expect("the OK button"); + assert!(fixture.tree().is_disabled_key(ok.node), "OK waits disabled for the app's answer"); + + on_dialog(&mut |dialog| dialog.answer_ok(Err(Label::from(REFUSED)))); + fixture.idle(4); + assert!(host.borrow().is_open::(), "a refused OK keeps the dialog open"); + assert_eq!(host.borrow().held_state(id), Some(HeldOk::Refused(Label::from(REFUSED)))); + assert!(fixture.by_label(REFUSED).is_some(), "the refusal shows in the dialog"); + let ok = fixture.by_label("OK").expect("the OK button"); + assert!(!fixture.tree().is_disabled_key(ok.node), "a refusal enables OK again"); + on_dialog(&mut |dialog| dialog.value_mut().brightness = 5.0); + fixture.idle(4); + assert!(fixture.by_label(REFUSED).is_none(), "a changed value clears the refusal"); + + let ctx = fixture.ctx().clone(); + assert!(!host.borrow_mut().confirm_by_id(&ctx, id), "confirm on a held dialog files the value and answers nothing yet"); + on_dialog(&mut |dialog| filed = dialog.take_ok()); + assert_eq!(filed.map(|value| value.brightness), Some(5.0), "confirm files the value as the button does"); + on_dialog(&mut |dialog| dialog.answer_ok(Ok(()))); + fixture.idle(4); + let answered = host.borrow_mut().take_keyed::().and_then(|(result, _)| result.ok()).map(|value| value.brightness); + assert_eq!(answered, Some(5.0), "the accepted OK closes the dialog with the held value"); +} diff --git a/crates/ui-canvas/src/canvas/full_window/hosted.rs b/crates/ui-canvas/src/canvas/full_window/hosted.rs index 0faae5bb9..4841773b3 100644 --- a/crates/ui-canvas/src/canvas/full_window/hosted.rs +++ b/crates/ui-canvas/src/canvas/full_window/hosted.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use egui::{Context, Response, Ui}; use crate::contract::Bind; -use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, HeldOk, StackDialog, dialog_id}; +use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, HeldAnswer, HeldOk, StackDialog, dialog_id}; use crate::runtime::env::Label; /// A full-window workspace (Liquify, Filter Gallery, Select and Mask) that a [`HostedWorkspace`] shows on the app's @@ -159,16 +159,9 @@ pub struct HostedWorkspace { open: bool, revision: u64, hold_ok: bool, - /// From the filed OK until the app's answer: the workspace disables its OK. - ok_waits: bool, - /// The held OK's answer, kept until the app answers it. - filed: Option, - /// `take_ok` has not handed out `filed` yet. - untaken: bool, - /// The app accepted the held OK: the next pass finishes with `filed`. - finish: bool, - /// Why the app refused the last held OK, until OK files again. - refused: Option