diff --git a/crates/ui-patterns/src/patterns/form_dialogs/param.rs b/crates/ui-patterns/src/patterns/form_dialogs/param.rs index 3eb1f86a0..0df3669eb 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/param.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/param.rs @@ -479,6 +479,7 @@ pub(crate) struct RowOptions { eyedropper: bool, eyedropper_armed: bool, full_width: bool, + beside: bool, not_remembered: bool, /// The lines a multi-line text row shows. lines: Option, @@ -914,6 +915,14 @@ impl Param { self } + /// The row stands on the same line as the row before it (production's Font, Size, Underline and Colour on one + /// line), wrapping to the next line when the form is too narrow. The first row of a form or group ignores it. + #[must_use] + pub fn beside_previous(mut self) -> Self { + self.options.get_or_insert_default().beside = true; + self + } + /// A [`ParamKind::Text`] row without a Browse button edits multi-line text in a field `lines` lines tall: Enter /// inserts a line break and the dialog's default button leaves it to the field. `lines` below 2 keeps a /// single-line field, whose Enter answers the default button. Other kinds are unchanged. @@ -1066,6 +1075,11 @@ impl RowOptions { self.eyedropper_armed } + /// Whether the row shares the line of the row before it. + pub(crate) fn beside(&self) -> bool { + self.beside + } + /// Whether a custom row spans the width of the form. pub(crate) fn full_width(&self) -> bool { self.full_width diff --git a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs index 18427dffb..fc88e7c77 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::{ - LabelScheme, Pad, Bind, BodyCx, CanvasGesture, Entry, FieldLayout, NoValue, Outcome, Props as _, Remember as _, Sampler as _, Show as _, ShowContainer as _, + LabelScheme, Pad, Sizing, 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}; @@ -327,6 +327,8 @@ struct Pass<'p> { shaded: bool, /// The heading the rows stand under (`ParamRows::heading`). heading: Option, + /// The rows being shown share one line ([`Param::beside_previous`]). + in_line: bool, } /// A parameter as the pass shows it: its key, its label without the colon, and whether it takes input. @@ -335,6 +337,8 @@ struct Line<'r> { key: &'r str, name: Label, enabled: bool, + /// The row stands in a shared line: it takes its content's width, not the form's. + content: bool, } impl Line<'_> { @@ -345,6 +349,9 @@ impl Line<'_> { if matches!(self.param.kind, ParamKind::Custom(_)) && Env::of(ui).view().get::() == LabelScheme::Inline { field = field.layout(FieldLayout::Above); } + if self.content { + field = field.sizing(Sizing::Content); + } if let Some(tip) = self.param.tooltip_text() { field = field.tooltip(tip.clone()); } @@ -383,6 +390,7 @@ impl<'p> Pass<'p> { sliders: SliderFieldLayout::Inline, shaded: false, heading: None, + in_line: false, } } @@ -440,9 +448,7 @@ impl<'p> Pass<'p> { fn rows(&mut self, ui: &mut Ui, params: &[Param]) { let is_section = |param: &Param| matches!(param.kind, ParamKind::Section); let first = params.iter().position(is_section).unwrap_or(params.len()); - for param in ¶ms[..first] { - self.row(ui, param); - } + self.run(ui, ¶ms[..first]); let mut rest = ¶ms[first..]; while let Some((head, tail)) = rest.split_first() { let end = tail.iter().position(is_section).unwrap_or(tail.len()); @@ -453,16 +459,12 @@ impl<'p> Pass<'p> { // 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); - } + self.run(ui, body); }); }); } else { Section::fixed(plain(&head.label)).id_salt(head.key.as_str()).show(ui, |ui, _| { - for param in body { - self.row(ui, param); - } + self.run(ui, body); }); } self.heading = outer; @@ -471,11 +473,34 @@ impl<'p> Pass<'p> { } } + /// Shows `params` in order: a row marked [`Param::beside_previous`] shares the line of the shown row before it. + fn run(&mut self, ui: &mut Ui, params: &[Param]) { + let mut rest = params; + while let Some((head, tail)) = rest.split_first() { + let beside = tail.iter().take_while(|param| param.option().beside()).count(); + if beside == 0 || !head.visible(&self.work) { + self.row(ui, head); + rest = tail; + continue; + } + let (line, after) = tail.split_at(beside); + self.in_line = true; + Row::new().wrap(true).show(ui, |ui, _| { + self.row(ui, head); + for param in line { + self.row(ui, param); + } + }); + self.in_line = false; + rest = after; + } + } + fn row(&mut self, ui: &mut Ui, param: &Param) { if !param.visible(&self.work) { return; } - let line = Line { param, key: param.key.as_str(), name: plain(¶m.label), enabled: param.enabled(&self.work) }; + let line = Line { param, key: param.key.as_str(), name: plain(¶m.label), enabled: param.enabled(&self.work), content: self.in_line }; let held = self.work.get(line.key).cloned(); self.show_row(ui, &line); param.drop_dependents(held.as_ref(), &mut self.work); @@ -700,7 +725,7 @@ impl<'p> Pass<'p> { .or_else(|| rows.iter().find(is_pattern)) .map(|row| row.key.clone()); // With no pattern to offer the row is disabled and writes nothing. - let line = Line { param: line.param, key: line.key, name: line.name.clone(), enabled: line.enabled && current.is_some() }; + let line = Line { param: line.param, key: line.key, name: line.name.clone(), enabled: line.enabled && current.is_some(), content: line.content }; line.show(ui, |ui| { let picked = PresetPicker::pattern(line.name.clone(), Bind::given(current.clone()), rows, Bind::kept()).show(ui).picked; if let Some(id) = picked.filter(|id| rows.iter().filter(is_pattern).any(|row| row.key == *id)) { diff --git a/crates/ui-patterns/tests/it/form_dialogs/params.rs b/crates/ui-patterns/tests/it/form_dialogs/params.rs index 47faa1db7..6a2dcd1b3 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/params.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/params.rs @@ -1,7 +1,7 @@ //! The parameter model: the spec-string parser, kinds, defaults and fit rules, and the rows' predicates, read-only //! text and tooltips. -use craft_ui::contract::{Bind, Role}; +use craft_ui::contract::{Bind, LabelScheme, Role}; use craft_ui::patterns::blend_keys::{BlendKeyEntry, BlendShortcut}; use craft_ui::patterns::form_dialogs::{DialogFields, FieldValue, NumberUnit, Param, ParamDialog, ParamKind, ParamRows, set_param_field}; use craft_ui::patterns::presets::PresetRow; @@ -365,3 +365,23 @@ fn an_optional_number_is_held_to_its_bounds_and_remembered_while_off() { toggle(&mut fixture); assert_eq!(values.borrow().number("quality"), Some(7.0), "the box on again brings back the value typed before"); } + +/// A row marked `beside_previous` stands on the line of the row before it, where an unmarked one stands below. +#[trial] +fn a_row_beside_the_previous_one_shares_its_line() { + let top = |beside: bool| { + let second = Param::number("second", "Second", 1.0, 1.0, 0); + let params = vec![Param::number("first", "First", 1.0, 1.0, 0), if beside { second.beside_previous() } else { second }]; + let values = Rc::new(RefCell::new(DialogFields::new())); + let shown = values.clone(); + let mut fixture = Fixture::new(move |ui| { + let _ = ParamRows::new("Form", ¶ms, Bind::bound(&mut *shown.borrow_mut())).labels(LabelScheme::Inline).show(ui); + }); + fixture.settle(); + let at = |name: &str| fixture.texts().iter().find(|text| text.text.contains(name)).map(|text| text.rect.center().y); + at("First").zip(at("Second")).map(|(first, second)| second - first) + }; + let (stacked, beside) = (top(false), top(true)); + assert!(stacked.is_some_and(|gap| gap > 10.0), "unmarked rows stack: {stacked:?}"); + assert!(beside.is_some_and(|gap| gap.abs() < 3.0), "a beside row shares the line: {beside:?}"); +}