GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
//! Modal dialogs. Fields live in `UiState::dialog` (string/number JSON) so agents can fill them
//! through `ui.dialog.set` and press OK with `ui.dialog.confirm`.
//!
//! Each dialog is a file here exporting a [`DialogSpec`] (heading, body, OK action, buttons) plus
//! a row in the `registry!` below that maps its `Dialog::kind` strings to it. Dialogs inside the
//! shared modal frame only supply a body and a confirm action; a few (Preferences, Keyboard
//! Shortcuts, Workspaces, Find Font) draw their own window.

mod about;
pub mod action_batch;
pub mod action_insert_menu_item;
pub mod action_new;
pub mod action_playback;
pub mod action_select_object;
pub mod action_set_options;
pub mod action_stop;
pub mod artboard_options;
pub mod attributes_panel_options;
pub mod blend_options;
mod brush_art_options;
pub(crate) use brush_art_options::{brush_key_color_canvas, brush_key_color_input, cancel_brush_key_color, refresh_brush_key_color};
pub mod brush_delete;
pub mod brush_options;
pub mod brush_replace_art;
pub mod bullets_numbering;
pub mod char_style_options;
pub mod check_spelling;
pub mod color_balance;
pub mod color_group_options;
pub mod color_guide_options;
pub(crate) mod color_picker;
mod command;
pub mod composite_fonts;
pub mod confirm;
pub mod corners;
pub mod custom_dictionary;
pub mod dimension;
mod document_setup;
mod duotone;
pub mod dxf_import;
pub mod dxf_options;
pub mod edit_selection;
pub mod edit_views;
mod effect;
pub mod effect_gallery;
pub mod envelope;
pub mod eps_options;
pub mod expand;
mod export_as;
mod export_for_screens;
pub mod eyedropper;
pub mod file_info;
pub mod find_links;
pub mod find_replace;
pub mod flare_options;
pub mod flatten;
pub mod flattener_presets;
pub(crate) mod form;
pub(crate) mod free_distort;
pub mod freehand;
mod gradient_stop;
pub mod graph_data;
pub mod graph_design;
pub mod graph_type;
pub mod graphic_style_options;
pub mod halftone;
pub mod hpgl_options;
pub mod hyphenation;
pub mod import_pdf;
pub mod indexed_color;
pub mod javascript_files;
pub mod justification;
pub mod kinsoku_sets;
pub mod layer_options;
pub mod layers_panel_options;
pub mod liquify;
pub mod live_paint_bucket;
pub mod live_paint_gaps;
pub mod live_paint_selection;
pub mod missing_fonts;
pub mod missing_links;
pub(crate) mod modal;
pub mod navigator_panel_options;
pub mod new_brush;
pub mod new_color_group;
mod new_document;
pub mod new_swatch;
pub mod new_text_style;
pub mod object_mosaic;
pub mod objects_on_path_options;
pub mod office_export;
pub mod package;
pub mod para_style_options;
pub(crate) mod path_ops;
pub mod pathfinder_options;
pub mod pdf_presets;
pub mod perspective_grid;
pub mod perspective_options;
pub mod perspective_plane;
pub mod perspective_presets;
pub mod pixel_snap_options;
pub mod place;
pub mod placement_options;
pub mod plugin;
mod png_options;
pub mod print;
pub mod print_presets;
pub mod profile_mismatch;
pub mod psd_import;
mod psd_options;
pub mod raster_effects;
pub mod rearrange_artboards;
pub mod recolor;
mod recovery;
pub mod relink;
pub mod rename_artboards;
pub(crate) mod revolve;
pub(crate) mod revolve_gizmo;
pub(crate) mod revolve_lights;
pub mod safe_mode;
pub mod saturate;
pub mod save_brush_library;
mod save_changes;
pub mod save_for_web;
pub(crate) mod save_options;
mod save_pdf;
pub mod save_style_library;
pub mod save_swatch_library;
pub mod save_symbol_library;
pub mod script_check;
pub mod script_run;
pub mod shape_builder_options;
pub(crate) mod shapes;
pub mod shortcut_import;
pub(crate) mod simplify_slider;
pub mod slices;
pub mod smart_punctuation;
pub(crate) mod smooth_slider;
pub mod spot_colors;
pub mod svg_filter;
pub(crate) mod svg_options;
pub mod swatch_conflict;
pub mod swatch_options;
pub mod symbol_delete;
pub mod symbol_options;
pub mod symbolism_options;
pub mod tate_chu_yoko;
mod text_export;
pub mod text_import;
mod three_d_classic;
pub(crate) mod three_d_materials;
mod tiff_bmp_tga;
pub mod tile_edge_color;
mod tools;
pub mod trace_presets;
pub(crate) mod transform;
pub mod transform_each;
pub mod variables;
pub mod warichu;
mod warp_effect;
pub mod width_point;

use serde_json::{Value, json};

pub use color_picker::open as open_color_picker;
pub use document_setup::open as open_document_setup;
pub use effect::open as open_effect_dialog;
pub(crate) use effect::{FILM_GRAIN_FIELDS, PAINT_DAUB_BRUSH_TYPE_LABELS, SHADES, shown_choices};
pub use export_as::{export_selection, open as open_export_as};
pub(crate) use export_for_screens::drop_unread_screen_settings;
pub use export_for_screens::open as open_export_for_screens;
pub(crate) use export_for_screens::{
    KIND as EXPORT_FOR_SCREENS, PresetPick as ScreenPresetPick, artboard_name_editing as export_artboard_name_editing,
    export_preset as screen_export_preset, formats as screen_formats, import_presets as screen_import_presets,
    open_assets as open_export_for_screens_assets, preset_list as screen_preset_list, saved_options as screen_saved_options,
    saved_rows as screen_saved_rows, settings_format as screen_settings_format,
};
pub(crate) use form::{humanize, number_list_text, plugin_color_button, ppi_suffix, read_number_list, unit_suffix};
pub use new_document::{create_without_dialog as new_document_without_dialog, open as open_new_document, preset_card};
pub(crate) use new_document::{popup_open as new_document_popup_open, preset_name};
pub use png_options::open as open_raster_options;
pub(crate) use save_pdf::{SAVE_SUMMARY as SAVE_PDF_SUMMARY, save_summary as save_pdf_summary, summary_open as pdf_summary_open};
pub use save_pdf::{open as open_save_pdf, open_preset as open_pdf_preset};
pub use tools::open_tool_dialog;

use crate::state::Dialog;
use crate::{VectorcraftApp, widgets};

type DialogResult = Result<Value, String>;

/// A dialog's Reset ([`DialogSpec::reset`]).
type ResetFn = fn(&VectorcraftApp, &mut Dialog) -> Result<(), String>;

/// The shared dialog frame's inner margin, and the least room it leaves at the window's edges.
const MARGIN: i8 = 22;
const EDGE_GAP: f32 = 8.0;

/// An action requested by a dialog that draws its buttons inside its body.
#[derive(Default)]
pub(crate) enum DialogAction {
    #[default]
    None,
    Confirm,
    Cancel,
}

/// How a dialog draws and applies itself. Specs start from [`DialogSpec::FORM`] and override what
/// differs.
pub(crate) struct DialogSpec {
    /// Draws its own window instead of the shared frame (the frame fields below are then unused).
    pub window: Option<fn(&mut VectorcraftApp, &egui::Context)>,
    /// The heading (and window title), in the UI language: the spec translates its own text and
    /// leaves names in it (a document's, a plug-in's) as they are.
    pub heading: fn(&Dialog) -> String,
    /// Optional heading band inside the shared window; normal dialogs use `modal::heading`.
    pub header: Option<fn(&mut egui::Ui, &str)>,
    /// Draws the fields. Returns true to close the dialog as Cancel would.
    pub body: fn(&mut VectorcraftApp, &mut egui::Ui, &mut Dialog) -> bool,
    /// Optional body with inline buttons; replaces `body` and the shared footer.
    pub body_with_actions: Option<fn(&mut VectorcraftApp, &mut egui::Ui, &mut Dialog) -> DialogAction>,
    /// OK, Enter or `ui.dialog.confirm`: applies the dialog and closes it when done.
    pub confirm: fn(&mut VectorcraftApp, &Dialog) -> DialogResult,
    /// The OK button's label. None: no OK button, Enter does nothing and Cancel reads "Close".
    pub ok: Option<&'static str>,
    /// The Cancel button's label when it is not "Cancel" ("No"). Only with an `ok`.
    pub cancel: Option<&'static str>,
    /// An extra button left of Cancel that sets `discard: true` and confirms ("Don't Save").
    pub discard: Option<&'static str>,
    /// A button left of Cancel, as (label, field), that sets `field: true` and confirms, as OK
    /// does otherwise (Transform Each's Copy).
    pub alternate: Option<(&'static str, &'static str)>,
    /// Puts the fields back to their defaults. While Alt (Option on macOS) is held, the Cancel
    /// button of a dialog with an `ok` reads Reset; Reset (or `ui.dialogReset`) runs this and the
    /// dialog stays open. An error leaves the dialog as it was.
    pub reset: Option<ResetFn>,
    pub min_width: f32,
    pub max_width: Option<f32>,
    /// The body runs a live preview interaction that Cancel rolls back.
    pub preview: bool,
    /// The fields that a dialog of a kind reads but may not hold yet, which `ui.dialog.set` also
    /// takes (see [`accepts_field`]).
    pub inputs: fn(&str) -> &'static [&'static str],
    /// Whether the dialog passes a field it neither holds nor lists in `inputs` on to what it
    /// runs (a parameter of the generic dialog's command), so `ui.dialog.set` takes it too.
    pub passes: fn(&Dialog, &str) -> bool,
    /// The OK button's label when it depends on the app (Export for Screens' "Download" on the
    /// web); `ok` when none. Only with an `ok`.
    pub ok_label: Option<fn(&VectorcraftApp) -> &'static str>,
    /// Can OK (or Enter) apply the dialog as it is? None: always. The body says why not.
    pub ok_enabled: Option<fn(&VectorcraftApp, &Dialog) -> bool>,
    /// What a dialog draws at the left end of its button row, level with the buttons, instead of
    /// in its body (Transform Each's Preview checkbox).
    pub footer_left: Option<fn(&mut egui::Ui, &mut Dialog)>,
}

impl DialogSpec {
    /// A text field per value; OK just closes. Also the fallback for unregistered kinds.
    pub const FORM: Self = Self {
        window: None,
        heading: |_| tl!("Dialog").into(),
        header: None,
        body: |app, ui, d| {
            form::grid(ui, d, app.session.general_unit());
            false
        },
        body_with_actions: None,
        confirm: |app, _| {
            app.ui.dialog = None;
            Ok(Value::Null)
        },
        ok: Some("OK"),
        cancel: None,
        discard: None,
        alternate: None,
        reset: None,
        min_width: 320.0,
        max_width: None,
        preview: false,
        inputs: |_| &[],
        passes: |_, _| false,
        ok_label: None,
        ok_enabled: None,
        footer_left: None,
    };

    /// A dialog that draws its own window and confirms through its module.
    const fn window(show: fn(&mut VectorcraftApp, &egui::Context), confirm: fn(&mut VectorcraftApp, &Dialog) -> DialogResult) -> Self {
        Self { window: Some(show), confirm, ..Self::FORM }
    }
}

/// The dialog registry: one row per dialog with its [`DialogKind`] variant, the `Dialog::kind`
/// strings it handles and its [`DialogSpec`]. Adding a dialog is a new file plus one row.
macro_rules! registry {
    ($($variant:ident: [$($kind:pat),+] => $spec:expr,)+) => {
        /// Every registered dialog.
        #[derive(Clone, Copy, Debug, PartialEq, Eq)]
        pub enum DialogKind {
            $($variant,)+
        }

        impl DialogKind {
            /// The dialog handling a `Dialog::kind` string.
            pub fn of(kind: &str) -> Option<Self> {
                match kind {
                    $($($kind)|+ => Some(Self::$variant),)+
                    _ => None,
                }
            }

            /// Every variant, in registry order.
            pub const ALL: &[DialogKind] = &[$(Self::$variant,)+];

            fn spec(self) -> &'static DialogSpec {
                match self {
                    $(Self::$variant => {
                        static SPEC: DialogSpec = $spec;
                        &SPEC
                    })+
                }
            }
        }
    };
}

registry! {
    DimensionOptions: [dimension::KIND] => dimension::SPEC,
    NewDocument: [new_document::KIND] => new_document::SPEC,
    Shape: ["rectangle", "roundedRectangle", "ellipse", "polygon", "star", "lineSegment", "arc", "spiral", "rectangularGrid", "polarGrid"] => shapes::SPEC,
    Transform: ["move", "rotate", "scale", "reflect", "shear"] => transform::SPEC,
    PathOp: ["average", "averageJoin", "offsetPath", "simplify", "splitIntoGrid"] => path_ops::SPEC,
    DocumentSetup: ["documentSetup"] => document_setup::SPEC,
    ArtboardOptions: [artboard_options::KIND] => artboard_options::SPEC,
    ExportForScreens: ["exportForScreens"] => export_for_screens::SPEC,
    Recolor: [recolor::KIND] => recolor::SPEC,
    Command: ["command"] => command::SPEC,
    Variables: [variables::KIND] => variables::SPEC,
    Effect: ["effect"] => effect::SPEC,
    SaveChanges: [crate::unsaved::KIND] => save_changes::SPEC,
    Preferences: ["preferences"] => DialogSpec::window(crate::prefs_dialog::show, |app, _| crate::prefs_dialog::confirm(app)),
    Shortcuts: ["shortcuts"] => DialogSpec::window(crate::shortcut_editor::show, |app, _| crate::shortcut_editor::confirm(app)),
    Workspaces: ["newWorkspace", "manageWorkspaces"] => DialogSpec::window(crate::workspaces::show, |app, _| crate::workspaces::confirm(app)),
    NewToolbar: [crate::custom_toolbars::KIND] => DialogSpec::window(crate::custom_toolbars::show_dialog, |app, _| crate::custom_toolbars::confirm(app)),
    ManageToolbars: [crate::custom_toolbars::MANAGE] => DialogSpec::window(crate::custom_toolbars::show_manage, |app, _| crate::custom_toolbars::confirm_manage(app)),
    FindFont: ["findFont"] => DialogSpec::window(crate::find_font::show, |app, _| crate::find_font::confirm(app)),
    SwatchOptions: [swatch_options::KIND] => swatch_options::SPEC,
    Confirm: [confirm::KIND] => confirm::SPEC,
    BrushDelete: [brush_delete::KIND] => brush_delete::SPEC,
    BrushReplaceArt: [brush_replace_art::KIND] => brush_replace_art::SPEC,
    Message: [confirm::MESSAGE] => confirm::MESSAGE_SPEC,
    Replace: [confirm::REPLACE] => confirm::REPLACE_SPEC,
    ReplaceFiles: [confirm::REPLACE_FILES] => confirm::REPLACE_FILES_SPEC,
    ResetPreferences: [confirm::RESET_PREFS] => confirm::RESET_PREFS_SPEC,
    ResetActions: [confirm::RESET_ACTIONS] => confirm::RESET_ACTIONS_SPEC,
    MissingFontEdit: [confirm::MISSING_FONT_EDIT] => confirm::MISSING_FONT_EDIT_SPEC,
    NewSwatch: [new_swatch::KIND] => new_swatch::SPEC,
    NewColorGroup: [new_color_group::KIND] => new_color_group::SPEC,
    ColorGroupOptions: [color_group_options::KIND] => color_group_options::SPEC,
    GraphData: [graph_data::KIND] => graph_data::SPEC,
    GraphDesign: [graph_design::DESIGN] => graph_design::DESIGN_SPEC,
    GraphColumn: [graph_design::COLUMN] => graph_design::COLUMN_SPEC,
    GraphMarker: [graph_design::MARKER] => graph_design::MARKER_SPEC,
    GraphType: [graph_type::KIND] => graph_type::SPEC,
    GradientStop: ["gradientStop"] => gradient_stop::SPEC,
    ColorPicker: [color_picker::KIND] => color_picker::SPEC,
    GraphicStyleOptions: [graphic_style_options::KIND] => graphic_style_options::SPEC,
    CharStyleOptions: [char_style_options::KIND] => char_style_options::SPEC,
    ParaStyleOptions: [para_style_options::KIND] => para_style_options::SPEC,
    NewTextStyle: [new_text_style::KIND] => new_text_style::SPEC,
    EffectExists: [effect::EXISTS] => effect::EXISTS_SPEC,
    ColorGuideOptions: [color_guide_options::KIND] => color_guide_options::SPEC,
    ColorBalance: [color_balance::KIND] => color_balance::SPEC,
    Saturate: [saturate::KIND] => saturate::SPEC,
    IndexedColor: [indexed_color::KIND] => indexed_color::SPEC,
    SaveSwatchLibrary: [save_swatch_library::KIND] => save_swatch_library::SPEC,
    TileEdgeColor: [tile_edge_color::KIND] => tile_edge_color::SPEC,
    EyedropperOptions: [eyedropper::KIND] => eyedropper::SPEC,
    FlattenTransparency: [flatten::KIND] => flatten::SPEC,
    FlattenerPresets: [flattener_presets::KIND] => flattener_presets::SPEC,
    ImageTracePreset: [trace_presets::KIND] => trace_presets::SPEC,
    SaveStyleLibrary: [save_style_library::KIND] => save_style_library::SPEC,
    SaveSymbolLibrary: [save_symbol_library::KIND] => save_symbol_library::SPEC,
    SaveBrushLibrary: [save_brush_library::KIND] => save_brush_library::SPEC,
    Expand: [expand::KIND] => expand::SPEC,
    SpotColors: [spot_colors::KIND] => spot_colors::SPEC,
    TransformEach: [transform_each::KIND] => transform_each::SPEC,
    WidthPoint: [width_point::KIND] => width_point::SPEC,
    Corners: [corners::KIND] => corners::SPEC,
    Hyphenation: [hyphenation::KIND] => hyphenation::SPEC,
    Justification: [justification::KIND] => justification::SPEC,
    BulletsAndNumbering: [bullets_numbering::KIND] => bullets_numbering::SPEC,
    TateChuYokoSettings: [tate_chu_yoko::KIND] => tate_chu_yoko::SPEC,
    WarichuSettings: [warichu::KIND] => warichu::SPEC,
    CompositeFonts: [composite_fonts::KIND] => composite_fonts::SPEC,
    KinsokuSets: [kinsoku_sets::KIND] => kinsoku_sets::SPEC,
    SavePdf: [save_pdf::KIND] => save_pdf::SPEC,
    SvgOptions: [svg_options::KIND] => svg_options::SPEC,
    JavaScriptFiles: [javascript_files::KIND] => javascript_files::SPEC,
    SvgFilter: [svg_filter::KIND] => svg_filter::SPEC,
    NewDocumentMore: [new_document::MORE] => new_document::MORE_SPEC,
    NewDocumentProfiles: [new_document::PROFILES] => new_document::MORE_SPEC,
    Place: [place::KIND] => place::SPEC,
    RasterOptions: ["pngOptions", "jpgOptions", "webpOptions", "gifOptions", "png8Options"] => png_options::SPEC,
    ExportAs: ["exportAs"] => export_as::SPEC,
    ImportPdf: [import_pdf::KIND] => import_pdf::SPEC,
    SwatchConflict: [swatch_conflict::KIND] => swatch_conflict::SPEC,
    ProfileMismatchOpen: [profile_mismatch::OPEN_KIND] => profile_mismatch::OPEN_SPEC,
    ProfileMismatchPaste: [profile_mismatch::PASTE_KIND] => profile_mismatch::PASTE_SPEC,
    FileInfo: [file_info::KIND] => file_info::SPEC,
    RasterEffectsSettings: [raster_effects::KIND] => raster_effects::SPEC,
    EffectGallery: [effect_gallery::KIND] => effect_gallery::SPEC,
    MissingLinks: [missing_links::KIND] => missing_links::SPEC,
    MissingFonts: [missing_fonts::KIND] => missing_fonts::SPEC,
    FindLinks: [find_links::KIND] => find_links::SPEC,
    FindReplace: [find_replace::KIND] => find_replace::SPEC,
    TextImport: [text_import::KIND] => text_import::SPEC,
    PdfPresets: [pdf_presets::KIND] => pdf_presets::SPEC,
    PdfPreset: [save_pdf::PRESET_KIND] => save_pdf::PRESET_SPEC,
    PdfFlattenerOptions: [save_pdf::CUSTOM_KIND] => save_pdf::CUSTOM_SPEC,
    SaveOptions: [save_options::KIND] => save_options::SPEC,
    TextExport: [text_export::KIND] => text_export::SPEC,
    OfficeExport: [office_export::KIND] => office_export::SPEC,
    DxfOptions: [dxf_options::KIND] => dxf_options::SPEC,
    PlacementOptions: [placement_options::KIND] => placement_options::SPEC,
    Relink: [relink::KIND] => relink::SPEC,
    Package: [package::KIND] => package::SPEC,
    ActionBatch: [action_batch::KIND] => action_batch::SPEC,
    ActionSetOptions: [action_set_options::KIND] => action_set_options::SPEC,
    ActionNew: [action_new::KIND] => action_new::SPEC,
    ActionPlayback: [action_playback::KIND] => action_playback::SPEC,
    ActionStopRecord: [action_stop::RECORD_KIND] => action_stop::RECORD_SPEC,
    ActionStopMessage: [action_stop::MESSAGE_KIND] => action_stop::MESSAGE_SPEC,
    ActionInsertMenuItem: [action_insert_menu_item::KIND] => action_insert_menu_item::SPEC,
    ActionSelectObject: [action_select_object::KIND] => action_select_object::SPEC,
    SliceOptions: [slices::OPTIONS] => slices::OPTIONS_SPEC,
    DivideSlices: [slices::DIVIDE] => slices::DIVIDE_SPEC,
    EpsOptions: [eps_options::KIND] => eps_options::SPEC,
    EpsFlattenerOptions: [eps_options::CUSTOM_KIND] => eps_options::CUSTOM_SPEC,
    HpglOptions: [hpgl_options::KIND] => hpgl_options::SPEC,
    Recovery: [crate::recovery::KIND] => recovery::SPEC,
    PluginStopped: [safe_mode::STOPPED] => safe_mode::STOPPED_SPEC,
    SafeMode: [safe_mode::KIND] => safe_mode::SPEC,
    DxfImport: [dxf_import::KIND] => dxf_import::SPEC,
    PsdImport: [psd_import::KIND] => psd_import::SPEC,
    RasterFormatOptions: ["tiffOptions", "bmpOptions", "tgaOptions"] => png_options::SPEC,
    SaveForWeb: [save_for_web::KIND] => save_for_web::SPEC,
    PsdOptions: ["psdOptions"] => png_options::SPEC,
    Print: [print::KIND] => print::SPEC,
    PrintPreset: [print::PRESET_KIND] => print::PRESET_SPEC,
    PrintPresets: [print_presets::KIND] => print_presets::SPEC,
    Plugin: [plugin::KIND] => plugin::SPEC,
    PluginExport: [plugin::EXPORT_KIND] => plugin::EXPORT_SPEC,
    PluginImport: [plugin::IMPORT_KIND] => plugin::IMPORT_SPEC,
    PluginInstall: [plugin::INSTALL_KIND] => plugin::INSTALL_SPEC,
    ScriptCheck: [script_check::KIND] => script_check::SPEC,
    ScriptRun: [script_run::KIND] => script_run::SPEC,
    ScriptGrowth: [script_run::GROWTH_KIND] => script_run::GROWTH_SPEC,
    ScriptClose: [script_run::CLOSE_KIND] => script_run::CLOSE_SPEC,
    ScriptNotice: [script_run::NOTICE_KIND] => script_run::NOTICE_SPEC,
    ScriptFiles: [script_run::PICKER_KIND] => script_run::PICKER_SPEC,
    VectorHalftone: [halftone::KIND] => halftone::SPEC,
    ObjectMosaic: [object_mosaic::KIND] => object_mosaic::SPEC,
    SimplifySlider: [simplify_slider::KIND] => simplify_slider::SPEC,
    SmoothSlider: [smooth_slider::KIND] => smooth_slider::SPEC,
    SmartPunctuation: [smart_punctuation::KIND] => smart_punctuation::SPEC,
    PerspectiveGrid: [perspective_grid::KIND] => perspective_grid::SPEC,
    Envelope: [envelope::WARP, envelope::MESH, envelope::OPTIONS] => envelope::SPEC,
    LiquifyOptions: [liquify::KIND] => liquify::SPEC,
    FreehandOptions: [freehand::KIND] => freehand::SPEC,
    ShapeBuilderOptions: [shape_builder_options::KIND] => shape_builder_options::SPEC,
    ObjectsOnPathOptions: [objects_on_path_options::KIND] => objects_on_path_options::SPEC,
    FlareOptions: [flare_options::KIND] => flare_options::SPEC,
    SymbolOptions: [symbol_options::KIND] => symbol_options::SPEC,
    DeleteSymbol: [symbol_delete::KIND] => symbol_delete::SPEC,
    DeleteUsedSymbol: [symbol_delete::USED] => symbol_delete::USED_SPEC,
    SymbolismOptions: [symbolism_options::KIND] => symbolism_options::SPEC,
    PerspectiveGridPresets: [perspective_presets::KIND] => perspective_presets::SPEC,
    PerspectiveGridOptions: [perspective_options::KIND] => perspective_options::SPEC,
    BlendOptions: [blend_options::KIND] => blend_options::SPEC,
    LivePaintGaps: [live_paint_gaps::KIND] => live_paint_gaps::SPEC,
    LivePaintSelectionOptions: [live_paint_selection::KIND] => live_paint_selection::SPEC,
    LivePaintBucketOptions: [live_paint_bucket::KIND] => live_paint_bucket::SPEC,
    BrushOptions: [brush_options::KIND] => brush_options::SPEC,
    NewBrush: [new_brush::KIND] => new_brush::SPEC,
    EditSelection: [edit_selection::KIND] => edit_selection::SPEC,
    EditViews: [edit_views::KIND] => edit_views::SPEC,
    EditCustomDictionary: [custom_dictionary::KIND] => custom_dictionary::SPEC,
    CheckSpelling: [check_spelling::KIND] => check_spelling::SPEC,
    PerspectivePlane: [perspective_plane::KIND] => perspective_plane::SPEC,
    LayerOptions: [layer_options::KIND] => layer_options::SPEC,
    LayersPanelOptions: [layers_panel_options::KIND] => layers_panel_options::SPEC,
    NavigatorPanelOptions: [navigator_panel_options::KIND] => navigator_panel_options::SPEC,
    AttributesPanelOptions: [attributes_panel_options::KIND] => attributes_panel_options::SPEC,
    PathfinderOptions: [pathfinder_options::KIND] => pathfinder_options::SPEC,
    PixelSnapOptions: [pixel_snap_options::KIND] => pixel_snap_options::SPEC,
    RearrangeArtboards: [rearrange_artboards::KIND] => rearrange_artboards::SPEC,
    RenameArtboards: [rename_artboards::KIND] => rename_artboards::SPEC,
    PathToolOptions: [crate::addons::pathtools::KIND] => crate::addons::pathtools::OPTIONS_SPEC,
    DivideDashes: [crate::addons::pathtools::dashes::KIND] => crate::addons::pathtools::dashes::SPEC,
    SmoothBridge: [crate::addons::pathtools::bridge::KIND] => crate::addons::pathtools::bridge::SPEC,
    TangentCircle: [crate::addons::pathtools::tangent_circle::KIND] => crate::addons::pathtools::tangent_circle::SPEC,
    SetCorners: [crate::addons::pathtools::set_corners::KIND] => crate::addons::pathtools::set_corners::SPEC,
    NearestSwatch: [crate::addons::color::nearest::KIND] => crate::addons::color::nearest::SPEC,
    SwatchLegend: [crate::addons::color::legend::KIND] => crate::addons::color::legend::SPEC,
    TextTweaker: [crate::addons::typetools::KIND] => crate::addons::typetools::SPEC,
    SelectByArtboards: [crate::addons::select::KIND] => crate::addons::select::SPEC,
    SelectByLinkStatus: [crate::addons::select::link_status::KIND] => crate::addons::select::link_status::SPEC,
    ArrangeTools: [crate::addons::arrange::GRID, crate::addons::arrange::REPLACE, crate::addons::arrange::SIZE, crate::addons::arrange::OPACITY, crate::addons::arrange::SORT] => crate::addons::arrange::SPEC,
    ShortcutImport: [shortcut_import::KIND] => DialogSpec::window(shortcut_import::show, shortcut_import::confirm),
}

/// The button labels the shared dialog frame can show (OK, discard, the fixed Cancel/Close and
/// Reset), so the catalog tests can insist they are translated.
pub fn button_labels() -> Vec<&'static str> {
    let mut v = vec!["Cancel", "Close"];
    for spec in std::iter::once(&DialogSpec::FORM).chain(DialogKind::ALL.iter().map(|k| k.spec())) {
        v.extend(spec.ok);
        v.extend(spec.cancel);
        v.extend(spec.discard);
        v.extend(spec.alternate.map(|(label, _)| label));
        v.extend(spec.reset.map(|_| "Reset"));
    }
    v.sort_unstable();
    v.dedup();
    v
}

/// The spec for a `Dialog::kind` ([`DialogSpec::FORM`] when unregistered).
fn spec(kind: &str) -> &'static DialogSpec {
    static FALLBACK: DialogSpec = DialogSpec::FORM;
    DialogKind::of(kind).map_or(&FALLBACK, DialogKind::spec)
}

/// The `__` fields that `ui.dialog.set` can set on `d`: controls of its kind that the dialog holds
/// under a `__` name, each set as the control sets it. Every other `__` field of a dialog holds
/// what its OK acts on (the command and its fixed params, the targets, a file, the preset being
/// edited) or state the dialog keeps for itself, and `ui.dialog.set` refuses it
/// ([`protected_field`]), so a request cannot change what OK does after a person opened the dialog.
/// Flags that a button sets together with other fields are refused too: the style dialogs'
/// `__reset*` (Reset Panel also clears the section's fields, so scripts run
/// `ui.charStyleOptions.resetPanel` or `ui.paraStyleOptions.resetPanel`) and Artboard Options'
/// `__fit`, `__sizeChoice`, `__refx` and `__refy` (set with the size and position they record), and
/// Scribble's `__scribbleSetting` (the menu sets the preset's parameters with it).
fn hidden_controls(d: &Dialog) -> &'static [&'static str] {
    match DialogKind::of(&d.kind) {
        // Duotone's tone row, and the 3D effects' tab, gizmo, light and Show axis.
        Some(DialogKind::Effect) => &["__gizmo", "__light", "__showAxis", "__tab", "__tone"],
        // The preset name being typed and the format whose settings the gear shows.
        Some(DialogKind::ExportForScreens) => &["__savePreset", "__settings"],
        // Keywords typed but not added yet.
        Some(DialogKind::FileInfo) => &["__keyword"],
        // The preset name being typed.
        Some(DialogKind::FlattenTransparency) => &["__presetName"],
        // The page shown.
        Some(DialogKind::GraphType) => &[graph_type::PAGE],
        // The Resolution menu's Other.
        Some(DialogKind::RasterOptions | DialogKind::RasterFormatOptions | DialogKind::PsdOptions) => &["__otherPpi"],
        // The category shown, the search text and the three settings kept outside the preferences.
        Some(DialogKind::Preferences) => &["__category", "__search", "__smartGuides", "__snapToGrid", "__taskBar"],
        // The section shown, the sheet the preview shows and the preset name being typed.
        Some(DialogKind::Print | DialogKind::PrintPreset) => &["__savePresetAs", "__section", "__sheet"],
        // The section shown, All or a range of artboards, the bleed link, the two password
        // checkboxes and the preset name being typed.
        Some(DialogKind::SavePdf | DialogKind::PdfPreset) => {
            &["__allArtboards", "__bleedLinked", "__requireOpenPassword", "__restrictPermissions", "__savePresetAs", "__section"]
        }
        // The selected rows and Randomize's seed.
        Some(DialogKind::Recolor) => &["__seed", "__sel"],
        // The view, zoom, download speed, selected color table entries, the color table entry whose
        // Color Picker opens and the preset name being typed.
        Some(DialogKind::SaveForWeb) => &["__color", "__colors", "__editColor", "__kbps", "__presetName", "__view", "__zoom"],
        // The link that keeps width and height in proportion.
        Some(DialogKind::Shape | DialogKind::TransformEach) => &["__constrain"],
        // The attention filter, the Keys section, the search text and the key being recorded.
        Some(DialogKind::ShortcutImport) => &["__attention", "__keysOpen", "__query", "__recording"],
        // Set Size of Each's Width and Height checkboxes.
        Some(DialogKind::ArrangeTools) if d.kind == crate::addons::arrange::SIZE => &["__height", "__width"],
        // The search field.
        Some(DialogKind::SelectByArtboards | DialogKind::SelectByLinkStatus) => &["__search"],
        _ => &[],
    }
}

/// The controls of [`hidden_controls`] that a button adds to the dialog, which `ui.dialog.set` can
/// set only while the dialog holds them: Save PDF's and Print's `__savePresetAs`, whose presence
/// makes OK save a preset instead of writing the PDF or printing.
const ADDED_CONTROLS: &[&str] = &["__savePresetAs"];

/// Whether `field` is a control of `d` that `ui.dialog.set` can set: one of the controls of `d`'s
/// kind ([`hidden_controls`]), and for one that a button adds ([`ADDED_CONTROLS`]), one `d` holds.
fn settable_control(d: &Dialog, field: &str) -> bool {
    hidden_controls(d).contains(&field) && (!ADDED_CONTROLS.contains(&field) || d.fields.contains_key(field))
}

/// Whether `field` is a `__` field of `d` that `ui.dialog.set` refuses: any `__` field, held or
/// not, except the controls of `d`'s kind ([`settable_control`]).
pub fn protected_field(d: &Dialog, field: &str) -> bool {
    field.starts_with("__") && !settable_control(d, field)
}

/// Whether `ui.dialog.set` can set `field` on `d`: a `__` field only when it is one of the controls
/// of `d`'s kind ([`protected_field`]); otherwise a field it holds, `discard` on a dialog with a
/// discard button, `preview` on one with a live preview, one of its spec's `inputs` for its kind,
/// or one it `passes` on.
pub fn accepts_field(d: &Dialog, field: &str) -> bool {
    if field.starts_with("__") {
        return !protected_field(d, field);
    }
    let spec = spec(&d.kind);
    d.fields.contains_key(field)
        || (field == "discard" && spec.discard.is_some())
        || (field == "preview" && spec.preview)
        || (spec.inputs)(&d.kind).contains(&field)
        || (spec.passes)(d, field)
}

/// Set `field` of `d` to `value` for `ui.dialog.set`, as the dialog's own control for it does: a
/// Pattern Brush Options corner tile set this way is saved by OK, as one chosen from its menu is,
/// an Edit Data Set field set this way is no longer the row's empty text value, and a Layer
/// Options setting set this way for several rows is sent to every row (Template also sets Lock
/// and Dim Images, as its checkbox does). A DXF Options preset set this way sets its fields, as
/// one picked from its menu does, and the preset it shows follows the DXF options set this way.
/// A Graph Data Column Width set this way sets every column, as Cell Style's does. A Width Point
/// Edit side width set this way for several points sets it on every point, as typing it does. An
/// Import PDF `allPages` set this way to the other pick sets Import PDF pages as links back to its
/// default.
pub fn set_field(d: &mut Dialog, field: String, value: Value) {
    if d.kind == brush_options::KIND {
        brush_art_options::note_set(d, &field);
    }
    if d.kind == variables::KIND {
        variables::note_set(d, &field);
    }
    if d.kind == layer_options::KIND {
        layer_options::note_set(d, &field, &value);
    }
    if d.kind == import_pdf::KIND {
        import_pdf::note_set(d, &field, &value);
    }
    if d.kind == "preferences" {
        crate::prefs_dialog::note_set(d, &field, &value);
    }
    if d.kind == new_document::KIND || new_document::popup_open(d) {
        new_document::note_set(d, &field);
    }
    // Create Object Mosaic's Resize Using Percentages shows the new size the other way, as its checkbox does.
    if d.kind == object_mosaic::KIND && field == "percentages" {
        object_mosaic::set_percentages(d, &value);
        return;
    }
    let dxf = (d.kind == dxf_options::KIND).then(|| field.clone());
    let mosaic = (d.kind == object_mosaic::KIND).then(|| field.clone());
    let graph = (d.kind == graph_data::KIND).then(|| field.clone());
    let width = (d.kind == width_point::KIND).then(|| field.clone());
    d.fields.insert(field, value);
    if let Some(field) = dxf {
        dxf_options::note_set(d, &field);
    }
    if let Some(field) = mosaic {
        object_mosaic::note_set(d, &field);
    }
    if let Some(field) = graph {
        graph_data::note_set(d, &field);
    }
    if let Some(field) = width {
        width_point::note_set(d, &field);
    }
}

/// The fields `ui.dialog.set` can set on `d`, sorted: those it holds other than its `__` ones, the
/// `__` controls of its kind, `discard`, `preview` and the spec's `inputs` for its kind.
pub fn field_names(d: &Dialog) -> Vec<String> {
    let spec = spec(&d.kind);
    let mut names: Vec<String> = d.fields.keys().filter(|k| !k.starts_with("__")).cloned().collect();
    names.extend(hidden_controls(d).iter().filter(|f| settable_control(d, f)).map(|f| f.to_string()));
    names.extend(spec.discard.map(|_| "discard".to_string()));
    names.extend(spec.preview.then(|| "preview".to_string()));
    names.extend((spec.inputs)(&d.kind).iter().map(|i| i.to_string()));
    names.sort_unstable();
    names.dedup();
    names
}

/// Run a command and close the dialog (whether or not the command succeeded).
fn run_and_close(app: &mut VectorcraftApp, id: &str, params: Value) -> DialogResult {
    let r = app.run(id, params);
    app.ui.dialog = None;
    r
}

/// Set a tool's options with `tool.setOption` (`params`) and close the dialog; when the tool
/// refuses one of them, nothing is set and the dialog stays open.
fn set_tool_options_and_close(app: &mut VectorcraftApp, params: Value) -> DialogResult {
    let r = app.run("tool.setOption", params);
    if r.is_ok() {
        app.ui.dialog = None;
    }
    r
}

/// The field of a dialog opened over another (Import Shortcut Set over Keyboard Shortcuts) that
/// holds the dialog under it, which Cancel and Escape show again.
pub(crate) const PARENT: &str = "__parent";

/// The dialog `d` was opened over, as it was.
pub(crate) fn parent(d: &Dialog) -> Option<Dialog> {
    d.fields.get(PARENT).and_then(|v| serde::Deserialize::deserialize(v).ok())
}

/// `ui.dialog.set` changed `field` of the open dialog from `old`: a choice that sets other fields
/// sets them as choosing it in the dialog does (Export for Screens' `exportPreset`, EPS Options'
/// and Save PDF's `flattenerPreset`, which drop the Custom… options), a change
/// to Export for Screens' sub-folder controls ends an applied preset's item folders, as it does in
/// the dialog, Transform Each's Relative / Absolute keeps the Scale fields' size, and Recolor
/// Artwork's `group` selects that color group and its `groupAction` runs the button at once. Save
/// PDF's bleed link links the bleed as its button does. A value Composite Fonts' Show Sample or
/// Save PDF's bleed link can't take is refused.
pub(crate) fn field_set(app: &mut VectorcraftApp, field: &str, old: Option<&Value>) -> Result<(), String> {
    if app.ui.dialog.as_ref().is_some_and(|d| d.kind == transform_each::KIND) {
        transform_each::field_set(app, field, old);
        return Ok(());
    }
    if app.ui.dialog.as_ref().is_some_and(|d| d.kind == recolor::KIND) {
        return recolor::field_set(app, field);
    }
    if let Some(d) = app.ui.dialog.as_ref().filter(|d| d.kind == composite_fonts::KIND) {
        return composite_fonts::field_set(d, field);
    }
    if let Some(d) = app.ui.dialog.as_mut().filter(|d| d.kind == save_pdf::KIND || d.kind == save_pdf::PRESET_KIND) {
        return save_pdf::field_set(d, field);
    }
    if let Some(d) = app.ui.dialog.as_mut().filter(|d| d.kind == eps_options::KIND) {
        eps_options::field_set(d, field);
        return Ok(());
    }
    let Some(d) = app.ui.dialog.as_mut().filter(|d| d.kind == export_for_screens::KIND) else { return Ok(()) };
    match field {
        "exportPreset" => export_for_screens::preset_set(app),
        "subfolders" | "subfolderBy" => {
            if d.fields.get(field) != old {
                d.fields.remove("__itemFolders");
            }
            Ok(())
        }
        _ => Ok(()),
    }
}

/// Close the open dialog as Cancel does, rolling back a live preview and showing the dialog it was
/// opened over (`ui.dialog.cancel`).
pub fn cancel(app: &mut VectorcraftApp) {
    cancel_brush_key_color(app, true);
    missing_fonts::requeue_replaced(app);
    // More Settings' pop-up and Save Preset end on their own; New Document stays.
    if app.ui.dialog.as_mut().is_some_and(new_document::close_popup) {
        return;
    }
    if let Some(d) = app.ui.dialog.take() {
        if d.kind == "preferences" {
            crate::original_editor::cancel(app);
        }
        if d.kind == swatch_conflict::KIND {
            crate::placing::cancel_swatch_paste(app);
        }
        if d.kind == profile_mismatch::OPEN_KIND {
            profile_mismatch::cancel_open(app);
        }
        if d.kind == profile_mismatch::PASTE_KIND {
            crate::placing::cancel_swatch_paste(app);
        }
        if d.kind == plugin::INSTALL_KIND {
            app.pending_plugin_install = None;
        }
        if d.kind == safe_mode::STOPPED {
            safe_mode::cancel_stopped(app);
        }
        if d.kind == confirm::MISSING_FONT_EDIT {
            confirm::answer_missing_font_edit(app, &d, false);
        }
        // The growth question's Cancel button is its Stop button.
        if d.kind == script_run::GROWTH_KIND {
            app.session.script_request_stop();
        }
        if d.kind == script_run::NOTICE_KIND {
            script_run::notice_cancel(app, &d);
        }
        if d.kind == script_run::PICKER_KIND {
            script_run::picker_cancel(app, &d);
        }
        missing_links::cancelled(app, &d);
        // Cancel discards Recolor Artwork's own history of color edits with them.
        if d.kind == recolor::KIND {
            app.ui.recolor_history = Default::default();
        }
        // Find Font's next open starts with an empty replacement search.
        if d.kind == "findFont"
            && let Some(ctx) = app.ui_ctx.clone()
        {
            crate::find_font::clear_search(&ctx);
        }
        // Toolbar-only Flare options start no preview and must not cancel a gesture
        // that was already active before opening them. The generic parameter dialog has a spec
        // without a preview, yet previews some commands (Grid Repeat Options) when asked to.
        let owns_preview = d.kind != flare_options::KIND || d.fields.contains_key(form::PREVIEWED);
        if (spec(&d.kind).preview && owns_preview) || d.fields.contains_key(form::PREVIEWED) {
            let _ = app.session.cancel_interaction();
        }
        app.ui.dialog = parent(&d);
    }
    settle(app);
}

/// Whether the open dialog has a Reset ([`DialogSpec::reset`]).
pub(crate) fn can_reset(app: &VectorcraftApp) -> bool {
    app.ui.dialog.as_ref().is_some_and(|d| spec(&d.kind).reset.is_some())
}

/// Reset (`ui.dialogReset`, or Cancel while Alt or Option is held): the open dialog's fields go
/// back to their defaults and it stays open → the dialog; an error when no dialog is open, the
/// open one has no Reset, or its Reset fails (the dialog is then left as it was).
pub(crate) fn reset(app: &mut VectorcraftApp) -> DialogResult {
    let Some(mut d) = app.ui.dialog.clone() else { return Err("no dialog open".into()) };
    let Some(reset) = spec(&d.kind).reset else { return Err("the open dialog has no Reset".into()) };
    reset(app, &mut d)?;
    let out = serde_json::to_value(&d).unwrap_or_default();
    app.ui.dialog = Some(d);
    Ok(out)
}

/// Apply the open dialog (OK).
pub fn confirm(app: &mut VectorcraftApp) -> DialogResult {
    cancel_brush_key_color(app, true);
    missing_fonts::requeue_replaced(app);
    let Some(d) = app.ui.dialog.clone() else { return Err("no dialog open".into()) };
    // A file dialog it shows off the UI thread confirms the dialog as it is again.
    let r = crate::picks::as_entry(app, || crate::picks::Entry::Confirm(Box::new(d.clone())), |app| (spec(&d.kind).confirm)(app, &d));
    settle(app);
    r
}

/// After a dialog closes, and each frame: a Missing Fonts dialog that another dialog replaced waits
/// for its turn again, the search a Missing Fonts or Find Linked Files dialog started stops once
/// that dialog is gone, and with no dialog open the choice about a stopped plug-in that another
/// dialog replaced opens again, else the questions about the next restored document's linked files
/// ([`crate::recovery::ask_next`]), else the next document's Missing Fonts dialog (one dialog at a
/// time).
pub(crate) fn settle(app: &mut VectorcraftApp) {
    crate::placing::settle_swatch_paste(app);
    profile_mismatch::settle(app);
    missing_fonts::requeue_replaced(app);
    missing_fonts::stop_when_closed(app);
    find_links::stop_when_closed(app);
    safe_mode::reopen(app);
    crate::recovery::ask_next(app);
    missing_fonts::open_next(app);
}

/// The field of a dialog that a save panel opened (its format's options): Enter doesn't confirm it
/// until Enter has been let go.
pub(crate) const AFTER_PANEL: &str = "__afterPanel";

/// Marks the open dialog, if any, as one a save panel opened ([`AFTER_PANEL`]): the Enter that
/// closed the panel, still held, doesn't confirm it.
pub(crate) fn opened_by_panel(app: &mut VectorcraftApp) {
    if let Some(d) = app.ui.dialog.as_mut() {
        d.fields.insert(AFTER_PANEL.into(), json!(true));
    }
}

/// Draws the active dialog and handles shared initial focus, keyboard shortcuts and actions.
/// Inline actions and footer buttons use the same confirmation and preview rollback paths.
pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
    refresh_brush_key_color(app);
    crate::shortcuts::hold_back_keys(app, ctx);
    about::show(app, ctx);
    settle(app);
    confirm::show_waiting(app);
    // The kind of dialog shown last frame: a different one (or none) means this one just opened, and
    // its first field is to take the keyboard focus (`focus_id`, until a field takes it).
    let (shown_id, focus_id) = (egui::Id::unique("dialog-shown"), egui::Id::unique("dialog-focus-pending"));
    let Some(mut d) = app.ui.dialog.clone() else {
        revolve_gizmo::clear(ctx);
        app.ui.dialog_file = None;
        ctx.data_mut(|m| {
            m.remove::<String>(shown_id);
            m.remove::<bool>(focus_id);
        });
        return;
    };
    if !revolve_gizmo::active(&d) {
        revolve_gizmo::clear(ctx);
    }
    let focus_first = ctx.data_mut(|m| {
        if m.get_temp::<String>(shown_id).as_deref() != Some(d.kind.as_str()) {
            m.insert_temp(shown_id, d.kind.clone());
            m.insert_temp(focus_id, true);
        }
        m.get_temp::<bool>(focus_id).unwrap_or(false)
    });
    // Not in the frame of a click (the one on the menu item that opened it): egui takes the focus
    // from a field the click missed, so the field takes it a frame later (#1367).
    let clicked = ctx.input(|i| i.pointer.any_click());
    if focus_first && clicked {
        ctx.request_repaint();
    }
    let focus_first = focus_first && !clicked;
    let spec = spec(&d.kind);
    // Opened over another dialog, which Cancel shows again: that one keeps where it was scrolled to.
    if d.fields.get(PARENT).is_some_and(|p| !p.is_null()) {
        modal::over_parent(ctx);
    }
    if let Some(window) = spec.window {
        return window(app, ctx);
    }
    let mut ok = false;
    let mut ok_clicked = false;
    let mut ok_enabled = true;
    let mut cancel = false;
    let mut reset = false;
    let mut discard = false;
    let mut alternate = false;
    // Opened as a save panel closed: the Enter that closed it isn't this dialog's, until it is let go.
    let after_panel = d.bool(AFTER_PANEL);
    if after_panel && !ctx.input(|i| i.key_down(egui::Key::Enter) || i.key_pressed(egui::Key::Enter)) {
        d.fields.remove(AFTER_PANEL);
    }
    // A body can open another dialog in its place (EPS Options' Custom…): this frame's Enter
    // doesn't confirm that one.
    let opened = d.kind.clone();
    let heading = (spec.heading)(&d);
    let revolves = revolve_gizmo::active(&d);
    if revolves {
        revolve_gizmo::show(app, ctx, &mut d);
        three_d_classic::surface_outline(app, ctx, &d);
    }
    let initial = if revolves { egui::vec2((ctx.content_rect().width() / 2.0 - 226.0).max(0.0), -40.0) } else { egui::vec2(0.0, -40.0) };
    let id = egui::Id::unique(("dialog", d.kind.as_str()));
    let id = if revolves { id.with("revolve") } else { id };
    let canvas_pick = brush_key_color_canvas(app, ctx);
    modal::show_at(ctx, &heading, id, initial, MARGIN, canvas_pick, |ui| {
        // Never wider than the window (a large UI scale in a small window): the text wraps.
        let room = (ctx.content_rect().width() - 2.0 * (f32::from(MARGIN) + EDGE_GAP)).max(EDGE_GAP);
        ui.set_min_width(spec.min_width.min(room));
        ui.set_max_width(spec.max_width.map_or(room, |w| w.min(room)));
        if let Some(header) = spec.header {
            header(ui, &heading);
        } else {
            modal::heading(ui, &heading);
        }
        ui.add_space(12.0);
        // Just opened: its first enabled field takes the keyboard focus, as in the reference app
        // (type a value, press Enter). Only the body's fields can take it.
        if focus_first {
            ui.data_mut(|m| m.insert_temp(widgets::dialog_focus_flag(), true));
        }
        // When the dialog is taller than the window, its body scrolls, and the heading and the
        // buttons stay in view.
        if let Some(body) = spec.body_with_actions {
            match modal::body_rows(ui, |ui| body(app, ui, &mut d)) {
                DialogAction::None => {}
                DialogAction::Confirm => ok = true,
                DialogAction::Cancel => cancel = true,
            }
        } else {
            cancel = modal::body_rows(ui, |ui| (spec.body)(app, ui, &mut d));
        }
        // Taken (the flag is gone): done. Still there (the window's measuring frame): next frame.
        if focus_first && ui.data_mut(|m| m.remove_temp::<bool>(widgets::dialog_focus_flag())).is_none() {
            ui.data_mut(|m| m.remove::<bool>(focus_id));
        }
        if spec.body_with_actions.is_some() {
            return;
        }
        ui.add_space(16.0);
        // The button row is as wide as the fields above it and as tall as the buttons: a
        // right-to-left layout would otherwise take all the room left in the window, so the
        // window could never shrink to its content.
        let row = egui::vec2(ui.min_rect().width(), ui.spacing().interact_size.y);
        ok_enabled = spec.ok_enabled.is_none_or(|f| f(app, &d));
        ui.allocate_ui_with_layout(row, egui::Layout::right_to_left(egui::Align::Center), |ui| {
            if let Some(label) = spec.ok.map(|ok| spec.ok_label.map_or(ok, |f| f(app)))
                && widgets::primary_button_enabled(ui, label, ok_enabled).clicked()
            {
                ok_clicked = true;
            }
            ui.add_space(8.0);
            // While Alt (Option) is held, Cancel reads Reset on a dialog that has one.
            let resets = spec.ok.is_some() && spec.reset.is_some() && ui.input(|i| i.modifiers.alt);
            let label = if resets {
                "Reset"
            } else if spec.ok.is_some() {
                spec.cancel.unwrap_or("Cancel")
            } else {
                "Close"
            };
            if widgets::secondary_button(ui, label).clicked() {
                if resets {
                    reset = true;
                } else {
                    cancel = true;
                }
            }
            if let Some((label, _)) = spec.alternate {
                ui.add_space(8.0);
                alternate = widgets::secondary_button(ui, label).clicked() && ok_enabled;
            }
            if let Some(label) = spec.discard {
                ui.add_space(28.0);
                discard = widgets::secondary_button(ui, label).clicked();
            }
            // The row runs right to left: a left-to-right layout in the room left of the buttons
            // puts its control at the row's left end.
            if let Some(left) = spec.footer_left {
                ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| left(ui, &mut d));
            }
        });
    });
    let rejected = ctx.data_mut(|d| d.remove_temp::<bool>(widgets::dialog_reject_commit_flag())).unwrap_or(false);
    // A field that refused its text when the press on OK took its focus keeps the dialog open, as
    // Enter does. Only a pointer click has such a press; the keyboard and screen readers press OK
    // in one frame.
    let press = egui::Id::unique("dialog-press-pass");
    let (pressed, released) = ctx.input(|i| (i.pointer.any_pressed(), i.pointer.any_released()));
    if pressed {
        let pass = ctx.cumulative_pass_nr();
        ctx.data_mut(|m| m.insert_temp(press, pass));
    }
    let (pressed_at, refused_at) = ctx.data(|m| (m.get_temp::<u64>(press), m.get_temp::<u64>(widgets::dialog_reject_pass())));
    let refused_during_click = released && matches!((refused_at, pressed_at), (Some(r), Some(p)) if r >= p);
    if ok_clicked && !rejected && !refused_during_click {
        ok = true;
    }
    if spec.ok.is_some() && ok_enabled && !after_panel && !rejected && d.kind == opened && crate::shortcuts::pressed_anew(ctx, egui::Key::Enter) {
        ok = true;
    }
    if discard {
        d.fields.insert("discard".into(), json!(true));
        ok = true;
    }
    if alternate && let Some((_, field)) = spec.alternate {
        d.fields.insert(field.into(), json!(true));
        ok = true;
    }
    app.ui.dialog = Some(d);
    if cancel {
        self::cancel(app);
    } else if reset {
        if let Err(e) = self::reset(app) {
            app.status(e);
        }
    } else if ok && let Err(e) = confirm(app) {
        app.status(e);
    }
}

/// What a dropdown shows for `names`, a list mixing built-in labels (`builtin(index)`: translated
/// into `lang`) with names the user saved or a file or the system supplied (shown as they are).
pub(crate) fn shown_names<'a>(lang: crate::i18n::Lang, names: &[&'a str], builtin: impl Fn(usize) -> bool) -> Vec<&'a str> {
    names.iter().enumerate().map(|(k, n)| crate::i18n::label_or_name(lang, n, builtin(k))).collect()
}

/// [`widgets::dropdown_names`] over such a mixed list ([`shown_names`] in the UI language), showing
/// `current` as its entry reads; a `current` that isn't among `names` is shown as given (a caller
/// translates its own [Custom]). Returns the index chosen in `names`.
pub(crate) fn mixed_dropdown(
    ui: &mut egui::Ui,
    id: impl std::hash::Hash + std::fmt::Debug,
    current: &str,
    names: &[&str],
    width: f32,
    builtin: impl Fn(usize) -> bool,
) -> Option<usize> {
    let shown = shown_names(crate::i18n::current(), names, builtin);
    let current = names.iter().position(|n| *n == current).and_then(|i| shown.get(i).copied()).unwrap_or(current);
    widgets::dropdown_names(ui, id, current, &shown, width)
}

#[cfg(test)]
mod tests;
#[cfg(test)]
mod tests_accessibility;
#[cfg(test)]
mod tests_export;
#[cfg(test)]
mod tests_export_selection;
#[cfg(test)]
mod tests_import_pdf;
#[cfg(test)]
mod tests_package;
#[cfg(test)]
mod tests_plugin_export;
#[cfg(test)]
mod tests_plugin_fields;
#[cfg(test)]
mod tests_plugin_import;
#[cfg(test)]
mod tests_raster_formats;
#[cfg(test)]
mod tests_raster_supplied_options;
#[cfg(test)]
mod tests_svg_options_range;

#[cfg(test)]
mod tests_dxf;
#[cfg(test)]
mod tests_screen_assets;
#[cfg(test)]
mod tests_screens;

#[cfg(test)]
mod tests_eps;

#[cfg(test)]
mod tests_metafile;

#[cfg(test)]
mod tests_hpgl;

#[cfg(test)]
mod tests_tiff_bmp_tga;

#[cfg(test)]
mod tests_save_for_web;

#[cfg(test)]
mod tests_psd;

#[cfg(test)]
mod tests_print;

#[cfg(test)]
mod tests_print_presets;

#[cfg(test)]
mod tests_print_advanced;

#[cfg(test)]
mod tests_scale;

#[cfg(test)]
mod tests_perspective;

#[cfg(test)]
mod tests_modal;

#[cfg(test)]
mod tests_scroll;

#[cfg(test)]
mod tests_rectangular_grid;

#[cfg(test)]
mod tests_shortcut_import;

#[cfg(test)]
mod tests_warp;