From 4ebf340fb5a0157fea6cdfd3d174ec3c5b832910 Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 23:42:54 +0000 Subject: [PATCH] Hosted Image Size and Canvas Size can hold their OK for the host, as the hosted parameter dialog does; a hosted parameter dialog can start holding its OK once open --- .../src/patterns/form_dialogs/dialog.rs | 5 + .../src/patterns/form_dialogs/sizing.rs | 48 +++++++++- .../patterns/form_dialogs/sizing/canvas.rs | 94 ++++++++++++++++-- .../src/patterns/form_dialogs/sizing/image.rs | 96 +++++++++++++++++-- 4 files changed, 223 insertions(+), 20 deletions(-) diff --git a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs index fb0d69635..2c96ea111 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs @@ -950,6 +950,11 @@ impl HostedParamDialog { self } + /// [`held_ok`](HostedParamDialog::held_ok) for a dialog already open. + pub fn hold_ok(&mut self) { + self.hold = true; + } + /// The OK filed by a held dialog, once. pub fn take_ok(&mut self) -> Option { self.held.take() diff --git a/crates/ui-patterns/src/patterns/form_dialogs/sizing.rs b/crates/ui-patterns/src/patterns/form_dialogs/sizing.rs index a076310f1..7b3ab18dd 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/sizing.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/sizing.rs @@ -1,7 +1,8 @@ //! The size dialogs `ImageSize` and `CanvasSize`: the rows, the readouts and the dialog around them that the two share, //! and the `Resample` and `CanvasExtension` choices. -use super::FormDialog; +use super::{FormDialog, ResetFooter, ResetPick}; +use crate::notices::{Notice, NoticeLayout}; use super::size_model::{LinkedSize, MAX_DOCUMENT_SIDE}; use crate::contract::{Bind, Entry, NoValue, Outcome, Props as _, Show as _, ShowContainer as _}; use crate::dialog::{DialogResult, FieldRefusal}; @@ -212,6 +213,8 @@ pub struct SizingOutput { pub closed: Option, /// The answer, the pass the dialog closed as OK. pub result: Option, + /// The answer OK filed in this pass for the host to judge, from a dialog that holds its OK ([`Hold`]). + pub held: Option, } impl SizingOutput { @@ -282,6 +285,22 @@ fn set_size_json(size: &mut LinkedSize, field: &str, value: &serde_json::Value) }) } +/// The held OK of a hosted size dialog, as `ParamDialog` holds its own: with `on`, OK files its answer for the host +/// instead of closing; `waiting` keeps OK disabled until the host answers; `finish` closes the dialog with the answer +/// the host accepted; `refusal` is the host's reason, shown under the rows until a value changes. +pub(super) struct Hold { + pub(super) on: bool, + pub(super) finish: bool, + pub(super) waiting: bool, + pub(super) refusal: Option<(Label, S)>, +} + +impl Default for Hold { + fn default() -> Self { + Hold { on: false, finish: false, waiting: false, refusal: None } + } +} + /// A size dialog on `FormDialog`: `rows` edits the values and `answer` makes the result from them at OK. OK, Cancel /// and Alt-for-Reset follow `ParamDialog`; OK is refused while the pixel size is empty or over the limit. pub(super) fn show_form<'a, S: SizeForm + Default, R>( @@ -289,18 +308,37 @@ pub(super) fn show_form<'a, S: SizeForm + Default, R>( title: Label, open: Option>, values: Bind<'a, S>, + hold: Hold, mut rows: impl FnMut(&mut Ui, &mut S), - answer: impl FnOnce(&S) -> R, + answer: impl Fn(&S) -> R, ) -> SizingOutput { - let mut dialog = FormDialog::new(title.clone(), values).outer_gesture(true); + // The footer `FormDialog` builds for this pass, made the same way, so its `pick` reads `FormOutput::picked`. + let footer = ResetFooter::new(ui, None, true); + let mut dialog = FormDialog::new(title.clone(), values).outer_gesture(true).accept_stays_open(hold.on); + if hold.waiting { + dialog = dialog.ok_enabled(false); + } if let Some(open) = open { dialog = dialog.opened(open); } - let shown = dialog.show(ui, |ui, _, state| { + let mut latest: Option = None; + let shown = dialog.show(ui, |ui, form, state| { + // A measuring run closes nothing: the real run of the pass makes the close. + if hold.finish && !crate::runtime::measure::active(ui.ctx()) { + form.request_close_as(CloseReason::Activated); + } FieldGroup::new(title.clone()).show(ui, |ui, _| rows(ui, state)); + if let Some((reason, _)) = hold.refusal.as_ref().filter(|(_, refused)| *refused == *state) { + Notice::new(Tone::Bad, reason.clone()).layout(NoticeLayout::Inline).show(ui); + } + latest = Some(state.clone()); size_valid(state.pixels()) }); - SizingOutput { response: shown.response, closed: shown.closed, result: shown.accepted.map(|state| answer(&state)) } + let held = match (footer.pick(shown.picked), latest) { + (Some(ResetPick::Accept), Some(state)) if hold.on && size_valid(state.pixels()) => Some(answer(&state)), + _ => None, + }; + SizingOutput { response: shown.response, closed: shown.closed, result: shown.accepted.map(|state| answer(&state)), held } } /// The rows both dialogs show, with the words and units they need. diff --git a/crates/ui-patterns/src/patterns/form_dialogs/sizing/canvas.rs b/crates/ui-patterns/src/patterns/form_dialogs/sizing/canvas.rs index 75a9475fe..b7406edb6 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/sizing/canvas.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/sizing/canvas.rs @@ -1,13 +1,14 @@ //! `CanvasSize`: the Canvas Size dialog. -use super::{CanvasExtension, Kit, LinkedSize, SizeForm, SizingOutput, set_size_json, show_form, size_json, size_valid}; +use super::{CanvasExtension, Hold, Kit, LinkedSize, SizeForm, SizingOutput, set_size_json, show_form, size_json, size_valid}; use crate::choice::Checkbox; use crate::contract::{Bind, Props as _, Show as _, ShowContainer as _}; -use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, StackDialog, dialog_id}; +use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, HeldAnswer, HeldOk, StackDialog, dialog_id}; use crate::display::Text; use crate::form::Field; use crate::pickers::{Anchor9, ReferencePoint, Select}; use crate::runtime::env::{ColumnSize, Label, UnitKind}; +use crate::runtime::overlay::CloseReason; use crate::sections::Section; use craft_behavior::PixelSize; use craft_theme::variant::Tone; @@ -163,17 +164,25 @@ pub struct CanvasSize<'a> { /// The bytes one pixel takes, for the file size readouts. #[prop(quantity = "bytes per pixel")] bytes_per_pixel: f64, + /// The held OK of a hosted dialog. + hold: Hold, } impl<'a> CanvasSize<'a> { /// The dialog over `values`. pub fn new(values: Bind<'a, CanvasSizeValues>) -> Self { - CanvasSize { values, open: None, bytes_per_pixel: 4.0 } + CanvasSize { values, open: None, bytes_per_pixel: 4.0, hold: Hold::default() } + } + + /// Holds the OK of a hosted dialog ([`Hold`]). + fn hold_ok(mut self, hold: Hold) -> Self { + self.hold = hold; + self } /// Shows the dialog. pub fn show(self, ui: &mut Ui) -> CanvasSizeOutput { - let CanvasSize { values, open, bytes_per_pixel } = self; + let CanvasSize { values, open, bytes_per_pixel, hold } = self; let kit = Kit::new(ui); let words = kit.words(); @@ -183,6 +192,7 @@ impl<'a> CanvasSize<'a> { title, open, values, + hold, |ui, state| { let size = state.size.original(); let has_background = state.has_background; @@ -251,6 +261,12 @@ pub struct HostedCanvasSize { open: bool, values: CanvasSizeValues, build: Box, + /// Whether OK files its answer for the host instead of closing ([`held_ok`](HostedCanvasSize::held_ok)). + hold: bool, + /// The OK the host holds back until it answers `answer_ok`. + held: HeldAnswer, + /// The values the host refused the held OK for: the refusal shows until they change. + refused_at: Option, #[cfg(feature = "testing")] facts: crate::testing::StoryOutput, } @@ -266,6 +282,9 @@ impl HostedCanvasSize { open: true, values, build: Box::new(build), + hold: false, + held: HeldAnswer::new(), + refused_at: None, #[cfg(feature = "testing")] facts: crate::testing::StoryOutput::default(), } @@ -275,14 +294,57 @@ impl HostedCanvasSize { pub fn values(&self) -> &CanvasSizeValues { &self.values } + + /// Makes OK file its answer for the host to take instead of closing: the host checks it and answers with + /// [`answer_ok`](Self::answer_ok). OK stays disabled until then. + #[must_use] + pub fn held_ok(mut self) -> Self { + self.hold = true; + self + } + + /// [`held_ok`](Self::held_ok) for a dialog already open. + pub fn hold_ok(&mut self) { + self.hold = true; + } + + /// The OK filed by a held dialog, once. + pub fn take_ok(&mut self) -> Option { + self.held.take() + } + + /// The host's answer to the held OK: `Ok` closes the dialog with the held answer in the next pass; `Err` enables + /// OK again and shows the label under the rows until a value changes. Without a held OK it does nothing. + pub fn answer_ok(&mut self, answer: Result<(), Label>) { + if answer.is_err() { + self.refused_at = Some(self.values.clone()); + } + self.held.answer(answer); + } } impl StackDialog for HostedCanvasSize { type Result = CanvasSizeResult; - /// Answers what OK answers; `None` while OK is refused (a side out of range). + /// Answers what OK answers; `None` while OK is refused (a side out of range). A 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: &egui::Context) -> Option { - size_valid(self.values.pixels()).then(|| self.values.result()) + if self.held.is_accepted() { + return self.held.finish(); + } + if self.held.is_waiting() { + return None; + } + let result = size_valid(self.values.pixels()).then(|| self.values.result())?; + if self.hold { + self.held.file(result); + return None; + } + Some(result) + } + + fn held_state(&self) -> Option { + self.held.state() } /// `width`, `height`, `unit` and `widthUnit` (as the size rows show them), `relative`, `anchor` and `extension`. @@ -326,11 +388,29 @@ impl StackDialog for HostedCanvasSize { } fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option> { - let shown = (self.build)(Bind::bound(&mut self.values)).opened(Bind::bound(&mut self.open)).show(ui); + let finish = self.held.is_accepted(); + let hold = Hold { + on: self.hold, + finish, + waiting: self.held.is_waiting(), + refusal: self.held.refusal().zip(self.refused_at.as_ref()).map(|((reason, _), at)| (reason.clone(), at.clone())), + }; + let mut shown = (self.build)(Bind::bound(&mut self.values)).hold_ok(hold).opened(Bind::bound(&mut self.open)).show(ui); #[cfg(feature = "testing")] { self.facts = crate::contract::Outcome::story_facts(&shown); } + if let Some(result) = shown.held.take() { + self.held.file(result); + } + if self.refused_at.as_ref().is_some_and(|at| *at != self.values) { + self.refused_at = None; + self.held.clear_refusal(); + } + // The host accepted the held answer: the pass that closes the dialog answers it. + if finish && shown.result.is_none() && matches!(shown.closed, Some(CloseReason::Activated)) { + return Some(self.held.finish().map_or(DialogResult::Cancel, DialogResult::Ok)); + } shown.hosted() } diff --git a/crates/ui-patterns/src/patterns/form_dialogs/sizing/image.rs b/crates/ui-patterns/src/patterns/form_dialogs/sizing/image.rs index f1bbc78ce..fc6d5b0bf 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/sizing/image.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/sizing/image.rs @@ -1,12 +1,13 @@ //! `ImageSize`: the Image Size dialog. -use super::{Kit, LinkedSize, Resample, SizeForm, SizingOutput, set_size_json, show_form, size_json, size_valid}; +use super::{Hold, Kit, LinkedSize, Resample, SizeForm, SizingOutput, set_size_json, show_form, size_json, size_valid}; use crate::choice::Checkbox; use crate::contract::{Bind, Props as _, Show as _, ShowContainer as _}; -use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, StackDialog, dialog_id}; +use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, HeldAnswer, HeldOk, StackDialog, dialog_id}; use crate::form::Field; use crate::pickers::Select; -use crate::runtime::env::{ColumnSize, UnitKind}; +use crate::runtime::env::{ColumnSize, Label, UnitKind}; +use crate::runtime::overlay::CloseReason; use crate::runtime::{keys, overlay}; use craft_behavior::PixelSize; use craft_keymap::{Chord, Mods}; @@ -162,17 +163,25 @@ pub struct ImageSize<'a> { /// document's depth, no alpha (1/8 for Bitmap, 1 for Indexed). #[prop(quantity = "bytes per pixel")] bytes_per_pixel: f64, + /// The held OK of a hosted dialog. + hold: Hold, } impl<'a> ImageSize<'a> { /// The dialog over `values`. pub fn new(values: Bind<'a, ImageSizeValues>) -> Self { - ImageSize { values, open: None, methods: Vec::new(), bytes_per_pixel: 4.0 } + ImageSize { values, open: None, methods: Vec::new(), bytes_per_pixel: 4.0, hold: Hold::default() } + } + + /// Holds the OK of a hosted dialog ([`Hold`]). + fn hold_ok(mut self, hold: Hold) -> Self { + self.hold = hold; + self } /// Shows the dialog. pub fn show(self, ui: &mut Ui) -> ImageSizeOutput { - let ImageSize { values, open, methods, bytes_per_pixel } = self; + let ImageSize { values, open, methods, bytes_per_pixel, hold } = self; let kit = Kit::new(ui); let entries = Resample::entries(kit.words(), &methods); let first_method = entries.first().and_then(|entry| entry.value().copied()); @@ -184,6 +193,7 @@ impl<'a> ImageSize<'a> { title, open, values, + hold, |ui, state| { if let (false, Some(first)) = (entries.iter().any(|entry| entry.value() == Some(&state.method)), first_method) { state.method = first; @@ -259,6 +269,12 @@ pub struct HostedImageSize { open: bool, values: ImageSizeValues, build: Box, + /// Whether OK files its answer for the host instead of closing ([`held_ok`](HostedImageSize::held_ok)). + hold: bool, + /// The OK the host holds back until it answers `answer_ok`. + held: HeldAnswer, + /// The values the host refused the held OK for: the refusal shows until they change. + refused_at: Option, #[cfg(feature = "testing")] facts: crate::testing::StoryOutput, } @@ -274,6 +290,9 @@ impl HostedImageSize { open: true, values, build: Box::new(build), + hold: false, + held: HeldAnswer::new(), + refused_at: None, #[cfg(feature = "testing")] facts: crate::testing::StoryOutput::default(), } @@ -283,14 +302,57 @@ impl HostedImageSize { pub fn values(&self) -> &ImageSizeValues { &self.values } + + /// Makes OK file its answer for the host to take instead of closing: the host checks it and answers with + /// [`answer_ok`](Self::answer_ok). OK stays disabled until then. + #[must_use] + pub fn held_ok(mut self) -> Self { + self.hold = true; + self + } + + /// [`held_ok`](Self::held_ok) for a dialog already open. + pub fn hold_ok(&mut self) { + self.hold = true; + } + + /// The OK filed by a held dialog, once. + pub fn take_ok(&mut self) -> Option { + self.held.take() + } + + /// The host's answer to the held OK: `Ok` closes the dialog with the held answer in the next pass; `Err` enables + /// OK again and shows the label under the rows until a value changes. Without a held OK it does nothing. + pub fn answer_ok(&mut self, answer: Result<(), Label>) { + if answer.is_err() { + self.refused_at = Some(self.values.clone()); + } + self.held.answer(answer); + } } impl StackDialog for HostedImageSize { type Result = ImageSizeResult; - /// Answers what OK answers; `None` while OK is refused (a side out of range). + /// Answers what OK answers; `None` while OK is refused (a side out of range). A 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: &egui::Context) -> Option { - size_valid(self.values.pixels()).then(|| self.values.result()) + if self.held.is_accepted() { + return self.held.finish(); + } + if self.held.is_waiting() { + return None; + } + let result = size_valid(self.values.pixels()).then(|| self.values.result())?; + if self.hold { + self.held.file(result); + return None; + } + Some(result) + } + + fn held_state(&self) -> Option { + self.held.state() } /// `width`, `height`, `unit` and `widthUnit` (as the size rows show them), `resolution`, `resolutionUnit`, @@ -359,11 +421,29 @@ impl StackDialog for HostedImageSize { } fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option> { - let shown = (self.build)(Bind::bound(&mut self.values)).opened(Bind::bound(&mut self.open)).show(ui); + let finish = self.held.is_accepted(); + let hold = Hold { + on: self.hold, + finish, + waiting: self.held.is_waiting(), + refusal: self.held.refusal().zip(self.refused_at.as_ref()).map(|((reason, _), at)| (reason.clone(), at.clone())), + }; + let mut shown = (self.build)(Bind::bound(&mut self.values)).hold_ok(hold).opened(Bind::bound(&mut self.open)).show(ui); #[cfg(feature = "testing")] { self.facts = crate::contract::Outcome::story_facts(&shown); } + if let Some(result) = shown.held.take() { + self.held.file(result); + } + if self.refused_at.as_ref().is_some_and(|at| *at != self.values) { + self.refused_at = None; + self.held.clear_refusal(); + } + // The host accepted the held answer: the pass that closes the dialog answers it. + if finish && shown.result.is_none() && matches!(shown.closed, Some(CloseReason::Activated)) { + return Some(self.held.finish().map_or(DialogResult::Cancel, DialogResult::Ok)); + } shown.hosted() }