//! `ParamDialog`: a dialog from a parameter list, with an optional preview, and `HostedParamDialog`, the same dialog //! owned by a `DialogHost`. A command form is a `ParamDialog` without a preview. use super::fields::{DialogFields, FieldValue}; use crate::runtime::requests::{ColorRequest, HostAnswer, PathRequest}; use super::form::FormDialog; use super::host_requests::HostRequests; use super::param::{Param, ParamKind, open_fields, set_color_field}; use super::preview::{PreviewPane, PreviewView, PreviewZoom}; use super::rows::{ParamRows, ParamRowsOutput}; use crate::choice::Checkbox; use crate::contract::{Bind, BodyCx, NoValue, Outcome, Props as _, Remember as _, SaveKey, Show as _, ShowContainer as _}; use crate::dialog::{Dialog, DialogId, DialogResult, DialogStack, FooterHooks, StackDialog, dialog_id}; use crate::layout_components::{Row, Stack}; use crate::runtime::env::{Env, Label}; use crate::runtime::memory::{MemoryStage, unique}; use crate::runtime::overlay::CloseReason; use crate::runtime::words::CommonWords; use crate::sections::{DialogRole, FooterButton}; use egui::load::SizedTexture; use egui::{Response, Ui}; use std::borrow::Cow; use std::cell::Cell; use std::rc::Rc; #[cfg(feature = "testing")] mod stories; craft_ui::words! { /// The words of the parameter dialog beside the common OK and Cancel. pub struct ParamDialogWords { reset: "paramDialog.reset" = "Reset"; preview: "paramDialog.preview" = "Preview"; } } /// Where the preview pane sits. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] #[non_exhaustive] pub enum ParamLayout { /// The preview pane to the right of the rows, under the zoom row. #[default] Beside, /// The preview pane under the rows. Below, } /// What a parameter dialog answers with at OK. #[derive(Clone, Debug, PartialEq)] #[non_exhaustive] pub struct ParamResult { /// The dialog's key: the command the values are for. pub key: String, /// The values the user confirmed, keyed by parameter. pub fields: DialogFields, remember: Option<(SaveKey, DialogFields)>, } impl ParamResult { /// Stores the values of a dialog built with [`ParamDialog::remember`] for the next open. The app calls it after /// its command succeeds, so a failed command leaves the previous values; without a remember key it does nothing. pub fn remember(&self, ui: &Ui) { if let Some((key, values)) = &self.remember { let mut stage = MemoryStage::open(ui.ctx(), unique(("craft_ui::param_result", key.as_str()))); *stage.saved::>(key) = Some(values.clone()); stage.write_back(); } } } /// What a parameter dialog keeps while it is open: the view of the preview pane. #[derive(Clone, Default)] struct Live { view: PreviewView, } /// What the footer of a dialog with a preview keeps while it is open: the Preview check box is off. #[derive(Clone, Default)] struct PreviewOff(bool); /// What a parameter dialog reports. #[derive(Debug)] #[non_exhaustive] pub struct ParamDialogOutput { /// The dialog's response. pub response: Response, /// How the dialog closed in this pass; `None` while it stays open. pub closed: Option, /// The answer, the pass the dialog closed as OK. pub result: Option, /// The rows' output of this pass, with the colour, path and eyedropper requests; `None` when the dialog is closed. pub rows: Option, /// The values after the pass when they differ from the binding's or the dialog opened in this pass: a `values` /// binding that does not write through applies them, the opening snapshot included. pub values: Option, /// Whether the Preview check box is on: the host renders the preview while it is. pub preview_on: bool, } impl Outcome for ParamDialogOutput { type Value = NoValue; fn response(&self) -> &Response { &self.response } } /// A dialog from a parameter list: one row per [`Param`], the Preview check box with a preview pane when the host /// gives a picture, and OK, Cancel and a footer extra. /// /// - Keys: Tab walks the fields, Enter is OK unless a field owns it, Escape cancels. Holding Alt turns Cancel into /// Reset, which restores the values from when the dialog opened (or `reset_to`) and keeps the dialog open. /// - Values: the `values` binding holds them; without one the dialog keeps its own between passes. The first pass /// fills the parameters' defaults and, with `remember`, the values saved by [`ParamResult::remember`]. /// - Preview: the host renders from the reported values while `preview_on` and hands the picture to `preview`. Drag /// pans the picture, the zoom buttons scale it. A dialog with a preview has its buttons in a column beside the /// body, with the Preview check box and the footer extra under them. /// - A command form is a dialog with no preview and no remember key. #[must_use = "show the dialog with `show`"] #[derive(crate::contract::Pattern, crate::contract::Props)] pub struct ParamDialog<'a> { key: String, title: Label, params: Cow<'a, [Param]>, /// Binds the values to `values`. #[prop(name = "values")] bound: Option>, /// Binds the open state to `open`. #[prop(name = "opened")] open: Option>, /// Saves the values under `key` when the app calls [`ParamResult::remember`], and opens with them. #[prop] remember: Option, /// Whether OK can be pressed; Enter does nothing while it cannot. #[prop] ok_enabled: bool, /// Why OK is disabled: the tooltip of the disabled button. #[prop] ok_reason: Option