mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 02:54:25 +00:00
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
This commit is contained in:
@@ -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<ParamResult> {
|
||||
self.held.take()
|
||||
|
||||
@@ -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<R> {
|
||||
pub closed: Option<CloseReason>,
|
||||
/// The answer, the pass the dialog closed as OK.
|
||||
pub result: Option<R>,
|
||||
/// The answer OK filed in this pass for the host to judge, from a dialog that holds its OK ([`Hold`]).
|
||||
pub held: Option<R>,
|
||||
}
|
||||
|
||||
impl<R> SizingOutput<R> {
|
||||
@@ -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<S> {
|
||||
pub(super) on: bool,
|
||||
pub(super) finish: bool,
|
||||
pub(super) waiting: bool,
|
||||
pub(super) refusal: Option<(Label, S)>,
|
||||
}
|
||||
|
||||
impl<S> Default for Hold<S> {
|
||||
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<Bind<'a, bool>>,
|
||||
values: Bind<'a, S>,
|
||||
hold: Hold<S>,
|
||||
mut rows: impl FnMut(&mut Ui, &mut S),
|
||||
answer: impl FnOnce(&S) -> R,
|
||||
answer: impl Fn(&S) -> R,
|
||||
) -> SizingOutput<R> {
|
||||
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<S> = 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.
|
||||
|
||||
@@ -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<CanvasSizeValues>,
|
||||
}
|
||||
|
||||
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<CanvasSizeValues>) -> 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<Build>,
|
||||
/// 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<CanvasSizeResult>,
|
||||
/// The values the host refused the held OK for: the refusal shows until they change.
|
||||
refused_at: Option<CanvasSizeValues>,
|
||||
#[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<CanvasSizeResult> {
|
||||
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<Self::Result> {
|
||||
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<HeldOk> {
|
||||
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<DialogResult<CanvasSizeResult>> {
|
||||
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()
|
||||
}
|
||||
|
||||
|
||||
@@ -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<ImageSizeValues>,
|
||||
}
|
||||
|
||||
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<ImageSizeValues>) -> 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<Build>,
|
||||
/// 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<ImageSizeResult>,
|
||||
/// The values the host refused the held OK for: the refusal shows until they change.
|
||||
refused_at: Option<ImageSizeValues>,
|
||||
#[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<ImageSizeResult> {
|
||||
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<Self::Result> {
|
||||
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<HeldOk> {
|
||||
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<DialogResult<ImageSizeResult>> {
|
||||
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()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user