tfix-dialog: a held OK is a state the host reports (HeldOk), and confirm finishes an accepted one

StackDialog::held_state / DialogHost::held_state tell a filed OK (Waiting), an accepted one and a refused one
apart from a refused confirm. HostedParamDialog and HostedWorkspace answer confirm with the accepted held answer;
Export As and Save for Web confirm without a path ask for the save dialog as their buttons do. ui.dialog.confirm
does not report a filed held OK as a refusal.
This commit is contained in:
aiongg
2026-11-15 14:26:31 +00:00
parent ab6201e9a3
commit 2bc5fd1782
10 changed files with 142 additions and 27 deletions
+4 -3
View File
@@ -6,7 +6,7 @@
//! running the command its answer is for stay the app's: an answer waits for the app's `DialogHost::take` as a
//! click's would.
use craft_ui::dialog::{DialogHost, DialogId, StandardPrompt};
use craft_ui::dialog::{DialogHost, DialogId, HeldOk, StandardPrompt};
use egui::Context;
use serde_json::{Value, json};
@@ -32,7 +32,7 @@ pub fn inspect(host: &DialogHost) -> Value {
///
/// - `ui.dialog.set {dialog, field, value}` sets one field ([`DialogHost::set_json_field`]).
/// - `ui.dialog.confirm {dialog, choice?}` answers OK, as the default button would, after setting the `choice` field
/// when one is given (which button of a prompt).
/// when one is given (which button of a prompt). A dialog that holds its OK files it for the app, which answers it.
/// - `ui.dialog.cancel {dialog}` closes the dialog as Escape would: a standard prompt with a button for its Escape
/// answer gives that answer, with its tag; any other dialog closes with its children with Cancel.
pub fn dialog_method(host: &mut DialogHost, ctx: &Context, method: &str, params: &Value) -> Option<Result<Value, String>> {
@@ -58,7 +58,8 @@ fn run(host: &mut DialogHost, ctx: &Context, method: &str, params: &Value, field
if let Some(choice) = params.get("choice") {
host.set_json_field(ctx, id, "choice", choice.clone())?;
}
if !host.confirm_by_id(ctx, id) {
// A held OK is filed for the app, which answers it: OK was taken, not refused.
if !host.confirm_by_id(ctx, id) && host.held_state(id) != Some(HeldOk::Waiting) {
return Err(
"the dialog refuses OK: it is disabled, its answer has no enabled button, or the dialog has no programmatic OK".to_owned()
);
@@ -5,7 +5,7 @@ use std::sync::Arc;
use egui::{Context, Response, Ui};
use crate::contract::Bind;
use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, StackDialog, dialog_id};
use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, 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
@@ -167,6 +167,8 @@ pub struct HostedWorkspace<W: FullWindowWorkspace> {
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<Label>,
#[cfg(feature = "testing")]
facts: crate::testing::StoryOutput,
}
@@ -186,6 +188,7 @@ impl<W: FullWindowWorkspace> HostedWorkspace<W> {
filed: None,
untaken: false,
finish: false,
refused: None,
#[cfg(feature = "testing")]
facts: crate::testing::StoryOutput::default(),
}
@@ -238,6 +241,7 @@ impl<W: FullWindowWorkspace> HostedWorkspace<W> {
self.filed = None;
self.untaken = false;
self.ok_waits = false;
self.refused = Some(reason.clone());
self.workspace.ok_failed(reason);
}
}
@@ -258,6 +262,7 @@ impl<W: FullWindowWorkspace> HostedWorkspace<W> {
self.filed = Some(result);
self.untaken = true;
self.ok_waits = true;
self.refused = None;
}
}
@@ -300,7 +305,13 @@ impl<W: FullWindowWorkspace> StackDialog for HostedWorkspace<W> {
Some(W::KEY)
}
/// OK as the Accept button: a held workspace files the result and answers none, and answers the held result once
/// the app accepted it.
fn confirm(&mut self, ctx: &Context) -> Option<W::Result> {
if self.finish {
self.finish = false;
return self.filed.take();
}
if self.ok_waits {
return None;
}
@@ -312,6 +323,15 @@ impl<W: FullWindowWorkspace> StackDialog for HostedWorkspace<W> {
Some(result)
}
fn held_state(&self) -> Option<HeldOk> {
match (&self.filed, &self.refused) {
(Some(_), _) if self.finish => Some(HeldOk::Accepted),
(Some(_), _) => Some(HeldOk::Waiting),
(None, Some(reason)) => Some(HeldOk::Refused(reason.clone())),
(None, None) => None,
}
}
fn json_fields(&self) -> Option<serde_json::Value> {
self.workspace.json_fields()
}
@@ -40,7 +40,7 @@ mod hosting {
use super::super::HostedWorkspace;
use super::super::stories::{BrushChanged, BrushWorkspace, Flat};
use crate::contract::Role;
use crate::dialog::{DialogHost, DialogResult, FieldRefusal};
use crate::dialog::{DialogHost, DialogResult, FieldRefusal, HeldOk, StackDialog};
use crate::runtime::env::Label;
use crate::testing::access::AccessTree;
use crate::testing::input::key_press;
@@ -187,8 +187,11 @@ mod hosting {
assert_eq!(take_ok(&host), Some(10.0), "a click on the enabled OK files the result again");
host.borrow_mut().get_mut::<Hosted>().expect("open").answer_ok(Err(Label::from("Still nothing")));
let id = host.borrow().get::<Hosted>().map(StackDialog::id).expect("open");
assert_eq!(host.borrow().held_state(id), Some(HeldOk::Refused(Label::from("Still nothing"))), "the refusal is the held OK's state");
let ctx = frames.ctx.clone();
assert!(!host.borrow_mut().confirm::<Hosted>(&ctx), "automation's OK on a held workspace does not close it");
assert_eq!(host.borrow().held_state(id), Some(HeldOk::Waiting), "automation's OK waits for the app's answer");
assert_eq!(take_ok(&host), Some(10.0), "automation's OK files the result");
let revision = || host.borrow().get::<Hosted>().map(|hosted| hosted.workspace().revision).expect("open");
@@ -198,8 +201,10 @@ mod hosting {
assert_eq!(revision(), 1, "a new source moves the workspace's revision");
host.borrow_mut().get_mut::<Hosted>().expect("open").answer_ok(Ok(()));
frames.idle(2, &mut ui);
assert_eq!(host.borrow().held_state(id), Some(HeldOk::Accepted));
assert!(host.borrow_mut().confirm::<Hosted>(&ctx), "automation's OK finishes an accepted held OK at once");
assert_eq!(host.borrow_mut().take::<Hosted>(), Some(DialogResult::Ok(10.0)), "the accepted OK finishes with its result");
frames.idle(2, &mut ui);
assert!(dialog_bounds(&mut frames).is_none(), "the workspace closed");
}
+37 -3
View File
@@ -12,6 +12,7 @@ use serde_json::Value;
use super::stack::{DialogId, DialogStack};
use crate::contract::{Pattern, PatternKind, PatternProbe};
use crate::runtime::env::Label;
/// How a dialog on the [`DialogHost`] finished. `T` is the dialog's own answer type: a struct of
/// values for a form, an enum for a prompt with three buttons.
@@ -24,6 +25,19 @@ pub enum DialogResult<T> {
Cancel,
}
/// Where a dialog's held OK stands ([`StackDialog::held_state`]): an OK that files its answer for the app, which answers
/// it before the dialog closes (a check the app runs, a save path it picks).
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum HeldOk {
/// OK filed its answer; the app has not answered it yet.
Waiting,
/// The app accepted the held OK: the dialog finishes with it at its next pass or [`DialogHost::confirm`].
Accepted,
/// The app refused the last held OK for this reason; OK is enabled again.
Refused(Label),
}
impl<T> DialogResult<T> {
/// The answer, or `None` for Cancel.
pub fn ok(self) -> Option<T> {
@@ -69,11 +83,18 @@ pub trait StackDialog: Any + Send {
/// Answers OK as a click on the default button would, without drawing: runs what OK runs
/// (storing remembered values) and returns the answer. `None` when OK is disabled, the answer
/// is one the dialog offers no enabled button for (a prompt's choice), or the dialog has no
/// programmatic OK (the default). Used by [`DialogHost::confirm`].
/// programmatic OK (the default). A dialog that holds its OK files the answer as its button
/// does and returns `None` ([`held_state`](StackDialog::held_state) is then `Waiting`); once the app
/// accepted the held OK, it returns the held answer. Used by [`DialogHost::confirm`].
fn confirm(&mut self, _ctx: &Context) -> Option<Self::Result> {
None
}
/// Where the dialog's held OK stands; `None` (the default) for a dialog whose OK holds nothing now.
fn held_state(&self) -> Option<HeldOk> {
None
}
/// The dialog's values as a JSON object, keyed as [`set_json_field`](StackDialog::set_json_field) takes them:
/// what automation lists for the open dialog (`ui.inspect`). `None` (the default) for a dialog without values.
fn json_fields(&self) -> Option<Value> {
@@ -174,6 +195,7 @@ trait AnyStackDialog: Send {
fn key(&self) -> Option<String>;
fn instance(&self) -> Option<DialogId>;
fn confirm_erased(&mut self, ctx: &Context) -> Option<Box<dyn Any + Send>>;
fn held_state(&self) -> Option<HeldOk>;
fn json_fields(&self) -> Option<Value>;
fn set_json_field(&mut self, ctx: &Context, field: &str, value: Value) -> Result<(), String>;
fn show_erased(&mut self, ui: &mut Ui, stack: &DialogStack) -> Option<Box<dyn Any + Send>>;
@@ -207,6 +229,10 @@ impl<D: StackDialog> AnyStackDialog for D {
self.confirm(ctx).map(|result| Box::new(DialogResult::Ok(result)) as Box<dyn Any + Send>)
}
fn held_state(&self) -> Option<HeldOk> {
StackDialog::held_state(self)
}
fn json_fields(&self) -> Option<Value> {
StackDialog::json_fields(self)
}
@@ -423,8 +449,10 @@ impl DialogHost {
/// Answers the open `D` with OK, as its default button would (for automation: no pointer, no
/// pass): runs what OK runs, closes the dialog and its children, and keeps the answer for
/// [`take`](DialogHost::take). `false` when no `D` is open, OK is disabled, or `D` has no
/// programmatic OK.
/// [`take`](DialogHost::take). `false` when no `D` is open, OK is disabled, `D` has no
/// programmatic OK, or `D` holds its OK: the answer is filed for the app
/// ([`held_state`](DialogHost::held_state) says `Waiting`), and a `confirm` after the app accepted it
/// closes the dialog with it.
pub fn confirm<D: StackDialog>(&mut self, ctx: &Context) -> bool {
match self.position::<D>() {
Some(at) => self.confirm_at(ctx, at),
@@ -432,6 +460,12 @@ impl DialogHost {
}
}
/// Where the held OK of the open dialog `id` names stands ([`StackDialog::held_state`]); `None` when no such dialog
/// is open or its OK holds nothing now.
pub fn held_state(&self, id: DialogId) -> Option<HeldOk> {
self.open.iter().find(|dialog| dialog.id() == id)?.held_state()
}
/// [`confirm`](DialogHost::confirm) for the open dialog `id` names, whatever its type.
pub fn confirm_by_id(&mut self, ctx: &Context, id: DialogId) -> bool {
match self.position_by_id(id) {
+1 -1
View File
@@ -26,7 +26,7 @@ pub use blocking::{BlockingOverlay, BlockingOverlayStyle};
pub use frame::{DialogFrame, DialogFrameStyle, DialogOutput, FooterCx, FooterHooks};
#[doc(hidden)]
pub use host::dialog_probe;
pub use host::{DialogHost, DialogResult, FieldRefusal, OpenDialog, StackDialog, StartupStage};
pub use host::{DialogHost, DialogResult, FieldRefusal, HeldOk, OpenDialog, StackDialog, StartupStage};
pub use modal::{Dialog, DialogStyle};
pub use prompt::{Prompt, PromptOutput};
pub use prompts::{PromptAnswer, PromptChoice, PromptKind, StandardPrompt, StandardPromptOutput};
@@ -14,7 +14,7 @@ use super::rows::{ParamRows, ParamRowsOutput};
use crate::contract::{Bind, BodyCx, NoValue, Outcome, Remember as _, Sampler as _, SaveKey, Show as _, ShowContainer as _};
use crate::dialog::{Dialog, DialogId, DialogResult, DialogStack, FieldRefusal, FooterHooks, StackDialog, dialog_id};
use crate::dialog::{Dialog, DialogId, DialogResult, DialogStack, FieldRefusal, FooterHooks, HeldOk, StackDialog, dialog_id};
use crate::display::{Shade, Text};
use crate::layout_components::{Row, Stack};
@@ -1140,10 +1140,23 @@ impl StackDialog for HostedParamDialog {
self.instance
}
fn held_state(&self) -> Option<HeldOk> {
match (&self.held, &self.refused) {
(Some(held), _) if held.accepted => Some(HeldOk::Accepted),
(Some(_), _) => Some(HeldOk::Waiting),
(None, Some(refused)) => Some(HeldOk::Refused(refused.reason.clone())),
(None, None) => None,
}
}
/// Answers OK as the button does: none while OK is disabled. The result keeps what a remembering dialog saves. A
/// held dialog files the answer for the host, as its button does, and answers none.
/// held dialog files the answer for the host, as its button does, and answers none; once the host accepted the
/// held answer, it answers that.
fn confirm(&mut self, ctx: &Context) -> Option<ParamResult> {
self.open(ctx);
if self.held.as_ref().is_some_and(|held| held.accepted) {
return self.held.take().map(|held| held.result);
}
if self.held.is_some() {
return None;
}
@@ -6,7 +6,7 @@ use super::{
ExportAsResult, ExportFormat, ExportFormatDefaults, ExportMetadata, ExportOptions, ExportPreview, ExportProfile, ExportSize, ExportWords,
Revisions, SizeEstimate, export_file_name, size_error_label,
};
use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, StackDialog, dialog_id, serde_fields, set_serde_field};
use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, HeldOk, StackDialog, dialog_id, serde_fields, set_serde_field};
use crate::patterns::form_dialogs::form::{read_bind, write_bind};
use crate::patterns::presets::{GroupPath, NamedPresetEvent, NamedPresetRow, PickerField, PresetLibrary, PresetPicker, PresetRow, PresetState};
use crate::runtime::requests::{HostAnswer, PathFilter, PathRequest};
@@ -251,11 +251,7 @@ impl<'a> ExportAsDialog<'a> {
if reset.pick(picked) == Some(ResetPick::Accept)
&& let Some(options) = &latest
{
let format = formats.iter().find(|format| format.id == options.format);
let extension = format.map_or("png", |format| format.extension);
let name = format!("{}.{extension}", export_file_name(&file_name, words.untitled.as_str()));
let filters = format.map(|format| PathFilter::new(format.name, &[format.extension]));
requests.request_path(PathRequest::save(SAVE_KEY, name, filters));
requests.request_path(save_request(&formats, options, &file_name, &words));
}
let result = accepted.zip(answered.into_inner()).map(|(options, path)| answer_of(options, source, path, remember));
let events = events.into_inner();
@@ -268,6 +264,15 @@ impl<'a> ExportAsDialog<'a> {
}
}
/// The save dialog Export asks for: the document's name with the format's extension and filter.
fn save_request(formats: &[ExportFormat], options: &ExportOptions, file_name: &str, words: &ExportWords) -> PathRequest {
let format = formats.iter().find(|format| format.id == options.format);
let extension = format.map_or("png", |format| format.extension);
let name = format!("{}.{extension}", export_file_name(file_name, words.untitled.as_str()));
let filters = format.map(|format| PathFilter::new(format.name, &[format.extension]));
PathRequest::save(SAVE_KEY, name, filters)
}
/// What Export answers with: the confirmed options, the chosen path, the output size, and the options to remember when
/// the dialog has a key.
fn answer_of(options: ExportOptions, source: PixelSize, path: PathBuf, remember: Option<SaveKey>) -> ExportAsResult {
@@ -436,12 +441,25 @@ const EXPORT_KEY: &str = "file.exportAs";
impl StackDialog for HostedExportAs {
type Result = ExportAsResult;
/// Answers what the save answer answers: none until a path was answered or set.
fn confirm(&mut self, _: &egui::Context) -> Option<Self::Result> {
let path = self.path.clone()?;
/// Answers what the save answer answers. Without a path it asks for the save dialog as Export does and answers
/// none until the app answers the path.
fn confirm(&mut self, ctx: &egui::Context) -> Option<Self::Result> {
let Some(path) = self.path.clone() else {
if !self.saving {
let words = Env::of_root(ctx).words::<ExportWords>();
self.requests.request_path(save_request(&self.formats, &self.options, &self.file_name, &words));
self.saving = true;
}
return None;
};
Some(answer_of(self.options.clone(), self.source, path, self.remember.clone()))
}
/// Waiting while the save dialog Export asked for is open.
fn held_state(&self) -> Option<HeldOk> {
self.saving.then_some(HeldOk::Waiting)
}
/// The options by their serde keys, and the path (`path`, null until one was answered or set).
fn json_fields(&self) -> Option<serde_json::Value> {
let mut fields = serde_fields(&self.options)?;
@@ -3,9 +3,9 @@
#[cfg(doc)]
use super::MATTE_COLOR_KEY;
use super::model::{Animation, SaveForWebOptions, SaveForWebResult, WantedVariant, WebPreset, WebVariant};
use super::{SaveForWebDialog, WebPictures, WebSlice, WebView, result_of};
use super::{SaveForWebDialog, WebPictures, WebSlice, WebView, result_of, save_request};
use crate::contract::Bind;
use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, StackDialog, dialog_id, serde_fields, set_serde_field};
use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, HeldOk, StackDialog, dialog_id, serde_fields, set_serde_field};
use crate::patterns::form_dialogs::host_requests::HostRequests;
use crate::runtime::memory::SaveKey;
use crate::runtime::requests::{ColorRequest, PathRequest};
@@ -166,12 +166,24 @@ impl HostedSaveForWeb {
impl StackDialog for HostedSaveForWeb {
type Result = SaveForWebResult;
/// Answers only once the app answered the save request with a path, as the dialog's own Save… does.
/// Answers only once the app answered the save request with a path, as the dialog's own Save… does: without a path
/// it asks for the file dialog as Save… does and answers none.
fn confirm(&mut self, _: &egui::Context) -> Option<Self::Result> {
let path = self.path.clone()?;
let Some(path) = self.path.clone() else {
if !self.saving {
self.requests.request_path(save_request(&self.name, self.options.format));
self.saving = true;
}
return None;
};
Some(result_of(self.options.clone(), path, self.pixels, self.view, self.remember.clone()))
}
/// Waiting while the file dialog Save… asked for is open.
fn held_state(&self) -> Option<HeldOk> {
self.saving.then_some(HeldOk::Waiting)
}
fn json_fields(&self) -> Option<serde_json::Value> {
let mut fields = serde_fields(&self.options)?;
let map = fields.as_object_mut()?;
@@ -3,7 +3,7 @@
//! request slots, disabled options and the remembered values a row refuses.
use craft_ui::contract::{Bind, Role, SaveKey};
use craft_ui::dialog::{DialogHost, DialogResult, dialog_id};
use craft_ui::dialog::{DialogHost, DialogResult, HeldOk, dialog_id};
use craft_ui::patterns::form_dialogs::{DialogFields, FieldValue, HostedParamDialog, Param, ParamDialog, ParamKind, ParamResult};
use craft_ui::patterns::presets::PresetRow;
use craft_ui::runtime::env::Label;
@@ -156,7 +156,9 @@ fn a_held_ok_refused_keeps_the_dialog_open_and_an_accepted_one_closes() {
let ok = fixture.by_label("OK").expect("the OK button");
assert!(fixture.tree().is_disabled_key(ok.node), "OK waits disabled for the host's answer");
assert_eq!(host.borrow().held_state(dialog_id("blur")), Some(HeldOk::Waiting), "the filed OK waits for the host");
on_dialog(&host, |dialog| dialog.answer_ok(Err(Label::from(REFUSED))));
assert_eq!(host.borrow().held_state(dialog_id("blur")), Some(HeldOk::Refused(Label::from(REFUSED))));
fixture.idle(4);
assert!(fixture.by_label(REFUSED).is_some(), "the refusal shows under the rows");
let ok = fixture.by_label("OK").expect("the OK button");
@@ -170,6 +172,7 @@ fn a_held_ok_refused_keeps_the_dialog_open_and_an_accepted_one_closes() {
let filed = on_dialog(&host, HostedParamDialog::take_ok).flatten().expect("confirm files the held OK as the button does");
assert_eq!(filed.fields.bool("smooth"), Some(true), "the held answer has the values confirmed");
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::<HostedParamDialog>().and_then(DialogResult::ok);
assert_eq!(answered.map(|result| result.fields), Some(filed.fields), "the accepted OK closes the dialog with the held values");
@@ -2,7 +2,7 @@
//! closing; OK answers with the dialog's own result type.
use craft_behavior::PixelSize;
use craft_ui::dialog::{DialogHost, DialogResult, StackDialog};
use craft_ui::dialog::{DialogHost, DialogResult, HeldOk, StackDialog};
use craft_ui::patterns::form_dialogs::{
AboutInfo, AboutLink, CanvasSize, CanvasSizeValues, Credits, ExportOptions, ExportOptionsKind, FormatOptions, GuideLayoutDialog,
GuideLayoutValues, GuideRect, GuideUnits, HostedAbout, HostedCanvasSize, HostedExportAs, HostedFormatOptions, HostedGuideLayoutDialog,
@@ -152,6 +152,15 @@ fn hosted_export_as_finishes_when_the_app_answers_its_save_request() {
fixture.click(&export).idle(4);
let request = host.borrow_mut().get_mut::<HostedExportAs>().and_then(HostedExportAs::pick_path).expect("Export asks for a path");
assert!(host.borrow().is_open::<HostedExportAs>(), "the dialog waits for the path");
let id = host.borrow().get::<HostedExportAs>().map(StackDialog::id).expect("open");
assert_eq!(host.borrow().held_state(id), Some(HeldOk::Waiting), "the OK waits for the save dialog");
if let Some(dialog) = host.borrow_mut().get_mut::<HostedExportAs>() {
dialog.cancel_requests();
}
let ctx = fixture.ctx().clone();
assert!(!host.borrow_mut().confirm::<HostedExportAs>(&ctx), "automation's OK without a path waits for one");
let asked = host.borrow_mut().get_mut::<HostedExportAs>().and_then(HostedExportAs::pick_path).expect("automation's OK asks for a path as Export does");
assert_eq!(asked.key, request.key, "the same save request as the button's");
let path = PathBuf::from("/tmp/poster.png");
if let Some(dialog) = host.borrow_mut().get_mut::<HostedExportAs>() {