From 5ca05d0f44d625c10d5670e75dc30dd0feb39665 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sun, 15 Nov 2026 16:54:50 +0000 Subject: [PATCH 1/5] tfix3-f3: trials: a press without a physical key fires its command once (separate passes and one pass); an app catalog's alternate key for a registry command runs it --- .../ui-shell/tests/it/commands/app_catalog.rs | 62 ++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/crates/ui-shell/tests/it/commands/app_catalog.rs b/crates/ui-shell/tests/it/commands/app_catalog.rs index 3ee50b9b9..d67e8ca47 100644 --- a/crates/ui-shell/tests/it/commands/app_catalog.rs +++ b/crates/ui-shell/tests/it/commands/app_catalog.rs @@ -3,7 +3,9 @@ // The catalog is given through `Frames::commands`, which keeps it for every later frame. use craft_ui::contract::{Bind, Props as _, Show as _}; -use craft_ui::runtime::commands::{AppCatalog, Binding, CommandDef, CommandInput, CommandKind, FirePhase, Scope, keymap_builds}; +use craft_ui::runtime::commands::{ + AppCatalog, Binding, CommandDef, CommandInput, CommandKind, CommandRef, CommandState, EditCommands, FirePhase, Scope, keymap_builds, +}; use craft_ui::runtime::env::WindowKey; use craft_ui::runtime::frame::take_output; use craft_ui::shell::AppShell; @@ -112,3 +114,61 @@ fn an_unchanged_catalog_builds_no_keymap() { frames.frame(vec![key_release(Key::P, chord)], &mut show); assert_eq!(fired(&frames), [hit(RENAME, FirePhase::Press)], "the new generation's command fires by its chord"); } + +/// A key event with no physical key, as egui_kittest and some platforms send it. +fn logical(key: Key, pressed: bool, modifiers: Modifiers) -> egui::Event { + egui::Event::Key { key, physical_key: None, pressed, repeat: false, modifiers } +} + +/// A press with no physical key runs its command by the logical key, once, whether its release comes in a later pass or +/// in the same one (a quick tap during a slow frame), for a plain letter and a Cmd chord alike. +#[trial] +fn a_press_without_a_physical_key_fires_its_command_once() { + let mut show = |ui: &mut Ui| { + AppShell::new(WindowKey::main()).show(ui, &mut ()); + }; + let mut frames = Frames::new(); + frames.commands(catalog(1)); + frames.idle(3, &mut show); + let chord = Modifiers::COMMAND | Modifiers::ALT; + for (key, mods, id) in [(Key::Q, Modifiers::NONE, QUICK), (Key::U, chord, EXPORT)] { + frames.frame(vec![logical(key, true, mods)], &mut show); + frames.frame(vec![logical(key, false, mods)], &mut show); + assert_eq!(fired(&frames), [hit(id, FirePhase::Press)], "{id}: press and release on separate passes"); + let one_pass = vec![ + egui::Event::ModifiersChanged(mods), + logical(key, true, mods), + logical(key, false, mods), + egui::Event::ModifiersChanged(Modifiers::NONE), + ]; + frames.frame(one_pass, &mut show); + frames.idle(2, &mut show); + assert_eq!(fired(&frames), [hit(id, FirePhase::Press)], "{id}: press and release in one pass"); + } +} + +/// An app catalog's def of a registry command adds its keys beside the registry's: F2 does what Cmd+X does (Photoshop's +/// second keys for Cut, Copy and Paste). +#[trial] +fn an_app_catalogs_alternate_key_for_a_registry_command_runs_it() { + let cut = CommandRef::from(EditCommands::Cut); + let alternate = CommandDef::new(cut.clone(), "Cut", Scope::app()).with_default(Binding::parse("F2").expect("the trial's chord parses")); + let input = || { + CommandInput::new() + .catalog(AppCatalog::new(vec![alternate.clone()], 1).expect("the trial's catalog is valid")) + .state(cut.clone(), CommandState::new()) + }; + let mut show = |ui: &mut Ui| { + AppShell::new(WindowKey::main()).show(ui, &mut ()); + }; + let mut frames = Frames::new(); + frames.commands(input()); + frames.idle(3, &mut show); + frames.frame(vec![key_press(Key::X, Modifiers::COMMAND)], &mut show); + frames.frame(vec![key_release(Key::X, Modifiers::COMMAND)], &mut show); + let by_registry_key = fired(&frames); + assert_eq!(by_registry_key, [(cut.id.clone(), None, FirePhase::Press)], "the registry's key runs Cut"); + frames.frame(vec![key_press(Key::F2, Modifiers::NONE)], &mut show); + frames.frame(vec![key_release(Key::F2, Modifiers::NONE)], &mut show); + assert_eq!(fired(&frames), by_registry_key, "the catalog's alternate key does what the registry's key does"); +} From ef51284928889633dd57d87592203f560ab2844b Mon Sep 17 00:00:00 2001 From: aiongg Date: Sun, 15 Nov 2026 16:56:45 +0000 Subject: [PATCH 2/5] tfix3-f3: a hosted param dialog is instanced by its command key; take_keyed takes results of any instance (take keeps to uninstanced ones) --- crates/automation/tests/it/dialogs.rs | 8 ++--- crates/ui-gallery/src/mock/editor.rs | 2 +- crates/ui-panels/src/dialog/host.rs | 14 ++++---- .../src/patterns/form_dialogs/dialog.rs | 8 +++-- .../tests/it/form_dialogs/dialog.rs | 36 +++++++++++++++++-- 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/crates/automation/tests/it/dialogs.rs b/crates/automation/tests/it/dialogs.rs index d1ccd0e21..681c9e96e 100644 --- a/crates/automation/tests/it/dialogs.rs +++ b/crates/automation/tests/it/dialogs.rs @@ -34,7 +34,7 @@ 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 +90,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 +104,7 @@ 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 +114,7 @@ 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/ui-gallery/src/mock/editor.rs b/crates/ui-gallery/src/mock/editor.rs index e8e7cb78c..2fc36b2e5 100644 --- a/crates/ui-gallery/src/mock/editor.rs +++ b/crates/ui-gallery/src/mock/editor.rs @@ -163,7 +163,7 @@ fn body(ui: &mut Ui, cx: &mut BodyCx<'_>, state: &mut MockState) { _ => {} } } - if let Some(result) = host.take::().and_then(DialogResult::::ok) { + if let Some(result) = host.take_keyed::().map(|(result, _)| result).and_then(DialogResult::::ok) { result.remember(ui); state.document.dirty = true; } diff --git a/crates/ui-panels/src/dialog/host.rs b/crates/ui-panels/src/dialog/host.rs index b5df26fec..8918402df 100644 --- a/crates/ui-panels/src/dialog/host.rs +++ b/crates/ui-panels/src/dialog/host.rs @@ -421,16 +421,18 @@ impl DialogHost { /// of the type; if that `open` replaced a `D` that was still open, the replaced one's /// `Cancel` waits instead. Results are per type, so the app tells two `D` apart by what the /// result carries (a key, a tag). A `D` opened with an instance ([`StackDialog::instance`]) - /// keeps its result apart, for [`take_by_id`](DialogHost::take_by_id) only. + /// keeps its result apart, for [`take_keyed`](DialogHost::take_keyed) and + /// [`take_by_id`](DialogHost::take_by_id). pub fn take(&mut self) -> Option> { - self.take_keyed::().map(|(result, _)| result) + let at = self.results.iter().rposition(|stored| stored.type_id == TypeId::of::() && stored.instance.is_none())?; + self.take_at::(at).map(|(result, _)| result) } - /// [`take`](DialogHost::take) with the key the dialog had ([`StackDialog::key`]): what a - /// `Cancel` after a replace-by-open needs to tell which command it was. `None` key for a - /// dialog without one. + /// The result of the last `D` that finished or was cancelled, whatever its instance, with the + /// key the dialog had ([`StackDialog::key`]), once: the key tells which command it was, a + /// `Cancel` after a replace-by-open included. `None` key for a dialog without one. pub fn take_keyed(&mut self) -> Option<(DialogResult, Option)> { - let at = self.results.iter().rposition(|stored| stored.type_id == TypeId::of::() && stored.instance.is_none())?; + let at = self.results.iter().rposition(|stored| stored.type_id == TypeId::of::())?; self.take_at::(at) } diff --git a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs index 11848b282..ef093c41e 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs @@ -811,10 +811,11 @@ impl HostedParamDialog { let (params, patterns) = (params.into_owned(), patterns.into_owned()); let saved = remember.and_then(|key| MemoryStage::peek_saved::>(ctx, &key)); ParamDialog::open_values(¶ms, &patterns, saved.as_ref(), &mut fields); + let id = dialog_id(key.as_str()); HostedParamDialog { - id: dialog_id(key.as_str()), + id, key, - instance: None, + instance: Some(id), opening: fields.clone(), fields, build: Box::new(build), @@ -841,6 +842,9 @@ impl HostedParamDialog { } /// Names this dialog among others made with the same key (one per document, say), so they keep separate state. + /// Without it the dialog is the one of its key: dialogs of different keys stay open together, and the same key + /// opened again replaces it. Its result is taken with [`DialogHost::take_keyed`](crate::dialog::DialogHost::take_keyed) + /// or by id. #[must_use] pub fn instance(mut self, instance: impl std::hash::Hash + std::fmt::Debug) -> Self { self.id = dialog_id((self.key.as_str(), instance)); diff --git a/crates/ui-patterns/tests/it/form_dialogs/dialog.rs b/crates/ui-patterns/tests/it/form_dialogs/dialog.rs index 4049eb429..103de9c37 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/dialog.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/dialog.rs @@ -174,7 +174,7 @@ fn a_held_ok_refused_keeps_the_dialog_open_and_an_accepted_one_closes() { on_dialog(&host, |dialog| dialog.answer_ok(Ok(()))); assert_eq!(host.borrow().held_state(dialog_id("blur")), Some(HeldOk::Accepted), "the accepted OK closes at the next pass"); fixture.idle(4); - let answered = host.borrow_mut().take::().and_then(DialogResult::ok); + let answered = host.borrow_mut().take_keyed::().map(|(result, _)| result).and_then(DialogResult::ok); assert_eq!(answered.map(|result| result.fields), Some(filed.fields), "the accepted OK closes the dialog with the held values"); } @@ -188,7 +188,7 @@ fn a_hosted_dialog_reopened_under_the_same_key_opens() { let mut fixture = Fixture::new(move |ui| { let mut host = shown.borrow_mut(); host.run(ui); - if let Some(result) = host.take::() { + if let Some(result) = host.take_keyed::().map(|(result, _)| result) { if again.replace(false) { host.open(HostedParamDialog::new(ui.ctx(), "blur", DialogFields::new(), blur)); } else { @@ -323,7 +323,7 @@ fn a_remembered_value_its_row_refuses_opens_as_the_default() { let mut host = shown.borrow_mut(); host.run(ui); // The app remembers an answer where it takes it, after its command ran. - if let Some(DialogResult::Ok(result)) = host.take::() { + if let Some(DialogResult::Ok(result)) = host.take_keyed::().map(|(result, _)| result) { result.remember(ui.ctx()); } }); @@ -460,3 +460,33 @@ fn a_field_set_before_the_first_show_resets_to_the_opening_value() { fixture.hold(Modifiers::NONE).settle(); assert_eq!(on_dialog(&host, |dialog| dialog.fields().number("radius")).flatten(), Some(10.0), "Reset restores the opening value"); } + +/// A Sharpen dialog over the host's values. +fn sharpen(fields: &mut DialogFields) -> ParamDialog<'_> { + ParamDialog::new("sharpen", "Sharpen", Param::parse_list(SPEC)).values(Bind::bound(fields)) +} + +// Hosted dialogs of two commands stay open together and each answers by its key; the same command opened again +// replaces only its own dialog. +#[trial] +fn hosted_dialogs_of_two_commands_stay_open_together() { + let (host, twice) = (Rc::new(RefCell::new(DialogHost::new())), Rc::new(Cell::new(false))); + let mut fixture = host_fixture(&host, &twice); + let ctx = fixture.ctx().clone(); + host.borrow_mut().open(HostedParamDialog::new(&ctx, "blur", DialogFields::new(), blur)); + host.borrow_mut().open(HostedParamDialog::new(&ctx, "sharpen", DialogFields::new(), sharpen)); + fixture.idle(4); + assert_eq!(host.borrow().stack().ids().count(), 2, "the second command's dialog leaves the first open"); + + host.borrow_mut().open(HostedParamDialog::new(&ctx, "blur", DialogFields::new(), blur)); + fixture.idle(4); + let replaced = host.borrow_mut().take_keyed::(); + assert!(matches!(replaced, Some((DialogResult::Cancel, Some(ref key))) if key == "blur"), "the same command replaces its own dialog"); + assert!(host.borrow().get_by_id::(dialog_id("sharpen")).is_some(), "the other command's dialog stays open"); + + for key in ["blur", "sharpen"] { + assert!(host.borrow_mut().confirm_by_id(&ctx, dialog_id(key)), "{key}: OK answers"); + let answered = host.borrow_mut().take_keyed::(); + assert!(matches!(answered, Some((DialogResult::Ok(ref result), Some(ref named))) if named == key && result.key == key), "{key}: its OK comes back by its key"); + } +} From 55fd41e76cbbfd65e7107a98f05881b54e69f1bc Mon Sep 17 00:00:00 2001 From: aiongg Date: Sun, 15 Nov 2026 17:01:32 +0000 Subject: [PATCH 3/5] tfix3-f3: one held OK (HeldAnswer) for every hosted family: HostedParamDialog and HostedWorkspace use it, HostedAdjustDialog gains held_ok/take_ok/answer_ok --- crates/imaging/src/ui/adjust/hosted.rs | 81 +++++++++++- crates/imaging/tests/it/hosted_dialogs.rs | 46 ++++++- .../src/canvas/full_window/hosted.rs | 70 +++-------- crates/ui-panels/src/dialog/held.rs | 119 ++++++++++++++++++ crates/ui-panels/src/dialog/host.rs | 15 +-- crates/ui-panels/src/dialog/mod.rs | 4 +- .../src/patterns/form_dialogs/dialog.rs | 71 +++-------- 7 files changed, 281 insertions(+), 125 deletions(-) create mode 100644 crates/ui-panels/src/dialog/held.rs diff --git a/crates/imaging/src/ui/adjust/hosted.rs b/crates/imaging/src/ui/adjust/hosted.rs index 3df34fa14..c822ae6c1 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_ui::contract::{Bind, Sampler as _, Show as _}; +use craft_theme::variant::Tone; +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,12 +292,22 @@ 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( + 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 { @@ -286,14 +327,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 +358,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