diff --git a/crates/ui-contract/src/primitives/text_adapter.rs b/crates/ui-contract/src/primitives/text_adapter.rs index edef650ed..7a6e72bb8 100644 --- a/crates/ui-contract/src/primitives/text_adapter.rs +++ b/crates/ui-contract/src/primitives/text_adapter.rs @@ -340,6 +340,10 @@ pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut Some(DraftChange::Revert) => state.end = Some(End::Revert), None => {} } + // A plain Enter in an editable multi-line field inserts a line, so no default button around the field takes it. + if focused_before && input.options.multiline() && !input.read_only && state.preedit.is_none() { + input.intents.take(Intent::Submit); + } // A clearing field takes Escape while it shows text, with or without a draft, and passes it on once it is empty. let takes_escape = if input.options.clear_on_escape { !base.is_empty() || state.preedit.is_some() } else { state.draft.is_some() || state.preedit.is_some() }; diff --git a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs index a79d24100..8c63a347e 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs @@ -26,7 +26,7 @@ use crate::runtime::words::CommonWords; use crate::sections::{DialogRole, FooterButton}; use egui::load::SizedTexture; -use egui::{Context, Response, Ui}; +use egui::{Context, Pos2, Response, Ui}; use serde_json::Value; use std::borrow::Cow; @@ -102,6 +102,9 @@ pub struct ParamDialogOutput { /// Whether the Preview check box is on and held: the host shows the unfiltered picture in the preview pane while /// it is. pub comparing: bool, + /// The point of the preview picture a click landed on this pass (`PreviewOutput::clicked`): an eyedropper on the + /// preview samples there. + pub preview_click: Option, } impl Outcome for ParamDialogOutput { @@ -176,6 +179,10 @@ pub struct ParamDialog<'a> { /// The picture the preview pane shows; without one the dialog has no preview. #[prop] preview: Option, + /// Whether a click on the preview centres it on the point clicked (`PreviewPane::click_centres`); a dialog whose + /// preview clicks pick a colour turns it off. The default is true. + #[prop] + click_centres: bool, /// Controls before the buttons in the footer (Color Table's Load and Save). #[prop(func = "FnMut(&mut Ui)")] footer_extra: Option>, @@ -237,6 +244,7 @@ impl<'a> ParamDialog<'a> { layout: ParamLayout::default(), closable: true, preview: None, + click_centres: true, footer_extra: None, below: None, shaped: None, @@ -326,6 +334,7 @@ impl<'a> ParamDialog<'a> { layout, closable, preview, + click_centres, footer_extra, below, shaped, @@ -377,6 +386,7 @@ impl<'a> ParamDialog<'a> { let mut live = Live::default(); let mut rows = None; + let mut preview_click = None; let shown = form.show(ui, |ui, cx, values| { if cx.first() && !values_opened { let saved = remember.as_ref().and_then(|key| cx.saved::>(key).clone()); @@ -396,13 +406,13 @@ impl<'a> ParamDialog<'a> { (Some(texture), ParamLayout::Beside) => { Row::new().show(ui, |ui, _| { show_rows(ui, values); - Stack::new().show(ui, |ui, _| show_preview(ui, &preview_label, texture, &mut live)); + Stack::new().show(ui, |ui, _| preview_click = show_preview(ui, &preview_label, texture, click_centres, &mut live)); }); } (Some(texture), ParamLayout::Below) => { Stack::new().show(ui, |ui, _| { show_rows(ui, values); - show_preview(ui, &preview_label, texture, &mut live); + preview_click = show_preview(ui, &preview_label, texture, click_centres, &mut live); }); } (None, _) => show_rows(ui, values), @@ -423,6 +433,7 @@ impl<'a> ParamDialog<'a> { values: shown.value, preview_on: shown.preview_on, comparing: shown.comparing, + preview_click, } } } @@ -557,11 +568,12 @@ impl ResetFooter { } } -/// The zoom row and the pane. -fn show_preview(ui: &mut Ui, label: &Label, texture: &SizedTexture, live: &mut Live) { +/// The zoom row and the pane; returns the picture point a click landed on. +fn show_preview(ui: &mut Ui, label: &Label, texture: &SizedTexture, click_centres: bool, live: &mut Live) -> Option { let zoom = PreviewZoom::new(&live.view).show(ui); - let pane = PreviewPane::new(label.clone(), *texture, &live.view).show(ui); + let pane = PreviewPane::new(label.clone(), *texture, &live.view).click_centres(click_centres).show(ui); live.view = zoom.view.or(pane.view).unwrap_or(pane.shown); + pane.clicked } /// Builds the dialog over the host-owned values of a [`HostedParamDialog`] for one pass. @@ -638,6 +650,11 @@ impl HostedParamDialog { self.requests.take_path() } + /// The point of the preview picture the last click landed on (`ParamDialogOutput::preview_click`), once. + pub fn take_preview_click(&mut self) -> Option { + self.requests.take_preview_click() + } + /// Answers the colour request `key`: the dialog writes the colour into its values in its next pass. pub fn answer_color(&mut self, key: impl Into, color: craft_theme::Srgba) { self.requests.answer_color(key, color); @@ -682,7 +699,7 @@ fn read_value(kind: &ParamKind, value: Value) -> Option { ParamKind::Color { .. } => FieldValue::Color(serde_json::from_value(value).ok()?), ParamKind::Point { .. } => FieldValue::Point(serde_json::from_value(value).ok()?), ParamKind::Grid { .. } => FieldValue::Grid(serde_json::from_value(value).ok()?), - ParamKind::Custom(_) | ParamKind::Section => return None, + ParamKind::ReadOnlyText { .. } | ParamKind::Custom(_) | ParamKind::Section => return None, }) } @@ -700,6 +717,9 @@ impl StackDialog for HostedParamDialog { if let Some(mut rows) = shown.rows { self.requests.absorb(&mut rows); } + if let Some(point) = shown.preview_click { + self.requests.request_preview_click(point); + } match (shown.result, shown.closed) { (Some(result), _) => Some(DialogResult::Ok(result)), (None, Some(_)) => Some(DialogResult::Cancel), diff --git a/crates/ui-patterns/src/patterns/form_dialogs/export/options.rs b/crates/ui-patterns/src/patterns/form_dialogs/export/options.rs index 06187a8ad..8dcd76438 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/export/options.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/export/options.rs @@ -4,7 +4,6 @@ use crate::contract::{Bind, Entry, NoValue, Outcome, Props as _, Show as _, ShowContainer as _}; use crate::dialog::{DialogId, DialogResult, DialogStack, StackDialog, dialog_id}; -use crate::display::Text; use crate::form::Field; use crate::layout_components::{Row, Stack}; use crate::patterns::form_dialogs::{DialogFields, FieldValue, Param, ParamDialog, ParamKind, ParamResult}; @@ -438,11 +437,7 @@ fn params(words: &FormatOptionsWords, start: FormatOptions) -> Vec { match start { FormatOptions::Jpeg(options) => jpeg_params(words, options), FormatOptions::Tiff(options) => vec![ - Param::custom("note", words.layers_note.clone(), { - let note = words.layers_note.clone(); - move |ui, _, _| Text::new(note.clone()).show(ui) - }) - .full_width(), + Param::read_only_text("note", words.layers_note.clone(), words.layers_note.clone()).full_width(), Param::new(DISCARD, words.discard_layers.clone(), ParamKind::bool(options.discard_layers)), Param::new(DONT_SHOW, words.dont_show.clone(), ParamKind::bool(options.dont_show_again)), ], diff --git a/crates/ui-patterns/src/patterns/form_dialogs/host_requests.rs b/crates/ui-patterns/src/patterns/form_dialogs/host_requests.rs index 150203c0a..e12bb9e2d 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/host_requests.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/host_requests.rs @@ -3,6 +3,7 @@ use super::rows::ParamRowsOutput; use crate::runtime::requests::{ColorRequest, EyedropperRequest, HostAnswer, PathRequest}; use craft_theme::Srgba; +use egui::Pos2; use std::path::PathBuf; /// A request to render a preview of `options`, numbered so the host's answer can be matched to the settings it was @@ -23,14 +24,15 @@ impl PreviewRequest { } } -/// The requests of parameter rows that the host answers: the colour picker, the file dialog and the eyedropper. A -/// hosted dialog embeds one, feeds it each pass's rows with [`absorb`](HostRequests::absorb) and offers the +/// The requests of parameter rows that the host answers: the colour picker, the file dialog and the eyedropper, and +/// the click on a dialog's preview picture (an eyedropper there samples it). A hosted dialog embeds one, feeds it each pass's rows with [`absorb`](HostRequests::absorb) and offers the /// `take_*` methods to its host. A request stays until it is taken; a newer one of the same kind replaces it. #[derive(Clone, Debug, Default)] pub struct HostRequests { color: Option, path: Option, eyedropper: Option, + preview_click: Option, answers: Vec, } @@ -68,6 +70,11 @@ impl HostRequests { self.eyedropper = Some(request); } + /// Files a click on the preview picture at `point`, in the picture's units; it replaces one not taken yet. + pub fn request_preview_click(&mut self, point: Pos2) { + self.preview_click = Some(point); + } + /// The colour parameter whose button was pressed, once. pub fn take_color(&mut self) -> Option { self.color.take() @@ -83,6 +90,11 @@ impl HostRequests { self.eyedropper.take() } + /// The preview picture's point the last click landed on, once. + pub fn take_preview_click(&mut self) -> Option { + self.preview_click.take() + } + /// Answers the colour request `key` with `color`: the dialog writes it into its values in its next pass. pub fn answer_color(&mut self, key: impl Into, color: Srgba) { self.color = None; @@ -107,10 +119,11 @@ impl HostRequests { /// Moves the requests and answers of `other` in; its requests replace the ones not taken yet. pub fn extend(&mut self, other: HostRequests) { - let HostRequests { color, path, eyedropper, answers } = other; + let HostRequests { color, path, eyedropper, preview_click, answers } = other; self.color = color.or(self.color.take()); self.path = path.or(self.path.take()); self.eyedropper = eyedropper.or(self.eyedropper.take()); + self.preview_click = preview_click.or(self.preview_click.take()); self.answers.extend(answers); } diff --git a/crates/ui-patterns/src/patterns/form_dialogs/param.rs b/crates/ui-patterns/src/patterns/form_dialogs/param.rs index 051a9b876..67f3fc599 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/param.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/param.rs @@ -136,6 +136,12 @@ pub enum ParamKind { /// The most digits shown after the decimal mark; 0 for an integer. decimals: u32, }, + /// Read-only text the user can select and copy: see [`Param::read_only_text`]. It has no value. + #[non_exhaustive] + ReadOnlyText { + /// The text shown; line breaks start new lines. + text: Label, + }, /// A row the host draws: see [`Param::custom`]. It has no value of its own. Custom(CustomRow), /// A section heading over the rows that follow it: see [`Param::section`]. It has no value. @@ -239,7 +245,7 @@ impl ParamKind { ParamKind::Channel { default, .. } => FieldValue::Int(i64::try_from(*default).unwrap_or(0)), ParamKind::Text { default } => FieldValue::Text(default.clone()), ParamKind::OptionalNumber { default, .. } => FieldValue::Number((*default)?), - ParamKind::Custom(_) | ParamKind::Section => return None, + ParamKind::ReadOnlyText { .. } | ParamKind::Custom(_) | ParamKind::Section => return None, }) } @@ -364,6 +370,8 @@ pub(crate) struct RowOptions { eyedropper_armed: bool, full_width: bool, not_remembered: bool, + /// The lines a multi-line text row shows. + lines: Option, units: Option, } @@ -449,6 +457,13 @@ impl Param { Self::new(key, label, ParamKind::Custom(CustomRow::new(draw))) } + /// A row of read-only `text` beside the label, wrapped to the form's width: the user can select and copy it, and + /// Tab passes over it. It has no value, so nothing is remembered or in the result. With + /// [`full_width`](Param::full_width) it has no label and spans the form (a note over the rows). + pub fn read_only_text(key: impl Into, label: impl Into