diff --git a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs index e97a7625a..68129782f 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs @@ -38,6 +38,7 @@ use crate::runtime::overlay::CloseReason; use crate::runtime::words::CommonWords; use crate::sections::{DialogRole, FooterButton}; +use crate::contract::LabelScheme; use craft_theme::Srgba; use craft_theme::variant::Tone; use egui::{Context, Pos2, Rect, Response, Ui}; @@ -72,21 +73,40 @@ pub enum ParamLayout { /// Preview check box at their start: PhotoCraft's filter dialogs. The other rows keep the form's label and control /// columns. Stacked, + /// A document dialog, as production's: the captions at the row start with their control right after them, the + /// option groups shaded, the preview pane under the rows and the buttons across the foot. + Document, } impl ParamLayout { /// Whether a dialog that previews stands its buttons in a column beside the body. fn side_buttons(self) -> bool { - !matches!(self, ParamLayout::Stacked) + !matches!(self, ParamLayout::Stacked | ParamLayout::Document) } /// How the range rows arrange their slider and number field. fn sliders(self) -> SliderFieldLayout { match self { ParamLayout::Stacked => SliderFieldLayout::Stacked, - ParamLayout::Beside | ParamLayout::Below => SliderFieldLayout::Inline, + ParamLayout::Beside | ParamLayout::Below | ParamLayout::Document => SliderFieldLayout::Inline, } } + + /// The label scheme the rows take, when the layout sets one. + fn labels(self) -> Option { + (self == ParamLayout::Document).then_some(LabelScheme::Inline) + } +} + +/// The layout a [`ParamDialog`] takes when it names none: set once for an app on the root's values +/// (`RootEnv::values`), so every dialog of the app lays out alike. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct DialogLayout(pub ParamLayout); + +impl crate::runtime::env::EnvValue for DialogLayout { + fn root() -> Self { + DialogLayout::default() + } } /// One tab of a tabbed [`ParamDialog`]: a title and the rows it shows. @@ -330,7 +350,7 @@ pub struct ParamDialog<'a> { reset_to: Option, /// How the rows, the preview pane and the buttons are arranged. #[prop] - layout: ParamLayout, + layout: Option, /// Whether the title row has a close action. #[prop] closable: bool, @@ -445,7 +465,7 @@ impl<'a> ParamDialog<'a> { history: false, collection_body: false, reset_to: None, - layout: ParamLayout::default(), + layout: None, closable: true, previews: false, preview_on: None, @@ -584,6 +604,7 @@ impl<'a> ParamDialog<'a> { host_notes, } = self; let env = Env::of(ui); + let layout = layout.unwrap_or_else(|| env.view().get::().0); let preview_label = env.words::().preview.clone(); // A read-only dialog has one button, so there is no second one for Alt to turn into Reset. let alt_resets = alt_resets && !read_only; @@ -675,6 +696,7 @@ impl<'a> ParamDialog<'a> { patterns: &patterns, preview_colors: &preview_colors, sliders: layout.sliders(), + labels: layout.labels(), canvas, read_only, }; @@ -744,7 +766,7 @@ impl<'a> ParamDialog<'a> { }); } (Some(state), layout) => { - let zoom = if layout == ParamLayout::Stacked { ZoomRow::Below } else { ZoomRow::Above }; + let zoom = if matches!(layout, ParamLayout::Stacked | ParamLayout::Document) { ZoomRow::Below } else { ZoomRow::Above }; Stack::new().show(ui, |ui, _| { show_rows(ui, values); shown_preview = show_preview(ui, &preview_label, state, &mut extras, click_centres, zoom, &mut live); @@ -947,6 +969,8 @@ struct DialogRows<'p> { patterns: &'p [PresetRow], preview_colors: &'p [(String, Srgba)], sliders: SliderFieldLayout, + /// The label scheme the layout sets for the rows. + labels: Option, canvas: Option, /// The dialog shows values only. read_only: bool, @@ -960,6 +984,9 @@ impl DialogRows<'_> { .preview_colors(self.preview_colors) .sliders(self.sliders) .read_only(self.read_only || read_only); + if let Some(scheme) = self.labels { + rows = rows.labels(scheme); + } if let Some(canvas) = self.canvas { rows = rows.canvas(canvas); } diff --git a/crates/ui-patterns/src/patterns/form_dialogs/mod.rs b/crates/ui-patterns/src/patterns/form_dialogs/mod.rs index 3e4e2b1cd..bdcf80ba4 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/mod.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/mod.rs @@ -18,7 +18,7 @@ mod path_row; mod preview; pub use dialog::{ - BelowRows, HostedParamDialog, ParamDialog, ParamDialogOutput, ParamDialogWords, ParamLayout, ParamResult, ParamTab, ResetFooter, ResetPick, + BelowRows, DialogLayout, HostedParamDialog, ParamDialog, ParamDialogOutput, ParamDialogWords, ParamLayout, ParamResult, ParamTab, ResetFooter, ResetPick, SharedOverlay, SharedRows, set_param_field, }; pub use form::{FormCx, FormDialog, FormDialogWords, FormOutput}; diff --git a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs index 38096d840..10cc4222c 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs @@ -13,7 +13,7 @@ use super::words::ParamWords; use crate::buttons::{Button, ToggleButton}; use crate::choice::{Checkbox, Radio, RadioGroup, Swatch}; use crate::contract::{ - Bind, BodyCx, CanvasGesture, Entry, FieldLayout, NoValue, Outcome, Props as _, Remember as _, Sampler as _, Show as _, ShowContainer as _, + LabelScheme, Pad, Bind, BodyCx, CanvasGesture, Entry, FieldLayout, NoValue, Outcome, Props as _, Remember as _, Sampler as _, Show as _, ShowContainer as _, SideLabel, TooltipSpec, clicked, }; use crate::form::{Field, FieldGroup, SliderField, SliderFieldLayout}; @@ -33,6 +33,7 @@ use craft_behavior::{EditEvent, IconId, Mixed, NumberCodec}; use craft_host::PickRequest; use craft_theme::Srgba; +use crate::style::SurfaceKind; use egui::{Id, Response, Ui}; use std::collections::BTreeMap; use std::ops::RangeInclusive; @@ -139,6 +140,7 @@ pub struct ParamRows<'a> { patterns: &'a [PresetRow], preview_colors: &'a [(String, Srgba)], sliders: SliderFieldLayout, + labels: Option, } impl<'a> ParamRows<'a> { @@ -154,6 +156,7 @@ impl<'a> ParamRows<'a> { patterns: &[], preview_colors: &[], sliders: SliderFieldLayout::Inline, + labels: None, } } @@ -198,9 +201,16 @@ impl<'a> ParamRows<'a> { self } + /// How the rows' captions sit: a document dialog's `LabelScheme::Inline`, in which the option groups (sections) + /// are also shaded. Without it the group's scheme comes from its surroundings. + pub fn labels(mut self, scheme: LabelScheme) -> Self { + self.labels = Some(scheme); + self + } + /// Shows the rows across the width offered. pub fn show(self, ui: &mut Ui) -> ParamRowsOutput { - let ParamRows { label, params, mut fields, mut seed, read_only, canvas, patterns, preview_colors, sliders } = self; + let ParamRows { label, params, mut fields, mut seed, read_only, canvas, patterns, preview_colors, sliders, labels } = self; let env = Env::of(ui); let words = env.words::(); let units = env.units(); @@ -208,11 +218,17 @@ impl<'a> ParamRows<'a> { let mut pass = Pass::new(&words, &units, patterns, canvas); pass.previews = preview_colors; pass.sliders = sliders; + pass.shaded = labels == Some(LabelScheme::Inline); let mut changed = false; let mut values = None; let mut edited = Vec::new(); let mut group = |ui: &mut Ui| { - FieldGroup::new(label.clone()).show(ui, |ui, cx| { + let group = FieldGroup::new(label.clone()); + let group = match labels { + Some(scheme) => group.labels(scheme), + None => group, + }; + group.show(ui, |ui, cx| { let before = match fields.reborrow().read(cx) { Some(Mixed::Same(held)) => held, _ => DialogFields::new(), @@ -296,6 +312,8 @@ struct Pass<'p> { previews: &'p [(String, Srgba)], /// How range rows arrange their slider and number field (`ParamRows::sliders`). sliders: SliderFieldLayout, + /// The option groups are shaded: a card under each section's rows (`ParamRows::labels`). + shaded: bool, } /// A parameter as the pass shows it: its key, its label without the colon, and whether it takes input. @@ -346,6 +364,7 @@ impl<'p> Pass<'p> { canvas, previews: &[], sliders: SliderFieldLayout::Inline, + shaded: false, } } @@ -411,11 +430,22 @@ impl<'p> Pass<'p> { let end = tail.iter().position(is_section).unwrap_or(tail.len()); let (body, after) = tail.split_at(end); if head.visible(&self.work) { - Section::fixed(plain(&head.label)).id_salt(head.key.as_str()).show(ui, |ui, _| { - for param in body { - self.row(ui, param); - } - }); + if self.shaded { + // A document dialog's option group: a caption over its rows on a card. + Section::fixed(plain(&head.label)).id_salt(head.key.as_str()).strength(HeaderStrength::Caption).show(ui, |ui, _| { + Stack::new().fill_width().surface(SurfaceKind::Raised).pad(Pad::Control).show(ui, |ui, _| { + for param in body { + self.row(ui, param); + } + }); + }); + } else { + Section::fixed(plain(&head.label)).id_salt(head.key.as_str()).show(ui, |ui, _| { + for param in body { + self.row(ui, param); + } + }); + } } rest = after; } diff --git a/crates/ui-patterns/tests/it/form_dialogs/document_layout.rs b/crates/ui-patterns/tests/it/form_dialogs/document_layout.rs new file mode 100644 index 000000000..f3b758e91 --- /dev/null +++ b/crates/ui-patterns/tests/it/form_dialogs/document_layout.rs @@ -0,0 +1,75 @@ +//! A document dialog (`ParamLayout::Document`) lays its rows out as production's dialogs do: captions at the row start +//! with their control right after them, the preview under the rows, the buttons across the foot. + +use craft_ui::contract::Bind; +use craft_ui::patterns::form_dialogs::{DialogFields, DialogLayout, Param, ParamDialog, ParamLayout}; +use craft_ui::testing::{Fixture, trial}; +use egui::{Rect, vec2}; + +/// A dialog of two number rows, with a preview pane, in `layout`. +fn dialog(layout: ParamLayout) -> Fixture { + let mut fixture = Fixture::new(move |ui| { + let mut fields = DialogFields::new(); + let rows = vec![Param::number("pages", "Size", 1.0, 1.0, 0), Param::number("margin", "Corner radius", 2.0, 1.0, 0)]; + ParamDialog::new("document", "Split document", rows).layout(layout).previews(true).values(Bind::bound(&mut fields)).show(ui); + }); + fixture.room(vec2(900.0, 700.0)); + fixture.settle(); + fixture +} + +fn rect_of(fixture: &Fixture, name: &str) -> Rect { + fixture.by_label(name).unwrap_or_else(|| panic!("{name:?} is shown")).rect +} + +fn caption(fixture: &Fixture, text: &str) -> Rect { + fixture.texts().iter().find(|run| run.text.trim_end_matches(':') == text).map(|run| run.rect).unwrap_or_else(|| panic!("{text:?} is painted")) +} + +#[trial] +fn a_document_dialog_sets_controls_after_their_captions_and_the_buttons_across_the_foot() { + let beside = dialog(ParamLayout::Beside); + let document = dialog(ParamLayout::Document); + let (short, long) = (rect_of(&document, "Size"), rect_of(&document, "Corner radius")); + assert!(short.min.x + 0.5 < long.min.x, "the captions share no column: {short:?} {long:?}"); + let gap = short.min.x - caption(&document, "Size").max.x; + assert!((0.0..24.0).contains(&gap), "the control follows its caption closely: {gap}"); + let ok = rect_of(&document, "OK"); + assert!(ok.min.y > long.max.y, "the buttons sit under the rows: {ok:?} {long:?}"); + let ok_beside = rect_of(&beside, "OK"); + assert!(ok_beside.min.y < rect_of(&beside, "Corner radius").max.y, "the other layouts keep the buttons beside the rows"); +} + +/// The rows of a section of a document dialog sit on a card inside the dialog's text column; elsewhere they do not. +#[trial] +fn a_document_dialog_shades_its_option_groups() { + let start = |layout: ParamLayout| { + let mut fixture = Fixture::new(move |ui| { + let mut fields = DialogFields::new(); + let rows = vec![Param::number("size", "Size", 1.0, 1.0, 0), Param::section("Marks"), Param::number("weight", "Weight", 2.0, 1.0, 0)]; + ParamDialog::new("shaded", "Add marks", rows).layout(layout).values(Bind::bound(&mut fields)).show(ui); + }); + fixture.room(vec2(900.0, 700.0)); + fixture.settle(); + (caption(&fixture, "Size").min.x, caption(&fixture, "Weight").min.x) + }; + let (outer, inner) = start(ParamLayout::Document); + assert!(inner > outer + 2.0, "the group's rows are inset on their card: {outer} {inner}"); + let (outer, inner) = start(ParamLayout::Beside); + assert!((inner - outer).abs() < 24.0 || inner <= outer + 2.0, "a plain dialog keeps its rows: {outer} {inner}"); +} + +/// An app sets the layout of every dialog once, on the root's values; a dialog that names a layout keeps its own. +#[trial] +fn the_roots_dialog_layout_is_the_default_of_every_dialog() { + let mut fixture = Fixture::new(|ui| { + let mut fields = DialogFields::new(); + let rows = vec![Param::number("size", "Size", 1.0, 1.0, 0), Param::number("radius", "Corner radius", 2.0, 1.0, 0)]; + ParamDialog::new("default", "Marks", rows).values(Bind::bound(&mut fields)).show(ui); + }); + fixture.root_value(DialogLayout(ParamLayout::Document)); + fixture.room(vec2(900.0, 700.0)); + fixture.settle(); + let (short, long) = (rect_of(&fixture, "Size"), rect_of(&fixture, "Corner radius")); + assert!(short.min.x + 0.5 < long.min.x, "the root's layout applies: {short:?} {long:?}"); +} diff --git a/crates/ui-patterns/tests/it/form_dialogs/mod.rs b/crates/ui-patterns/tests/it/form_dialogs/mod.rs index 85743f28c..8a34ae69a 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/mod.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/mod.rs @@ -3,6 +3,7 @@ mod about; mod dialog; +mod document_layout; mod export; mod files; mod hosted;