diff --git a/crates/ui-patterns/src/patterns/form_dialogs/param.rs b/crates/ui-patterns/src/patterns/form_dialogs/param.rs index 5840f228a8..4aca47e64a 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/param.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/param.rs @@ -483,6 +483,10 @@ pub(crate) struct RowOptions { beside_next: bool, /// A radio row's options stand in one row with no caption ([`Param::options_in_row`]). options_in_row: bool, + /// A radio row's options stand in a column with no caption, in a document dialog too ([`Param::stacked_options`]). + stacked_options: bool, + /// The option of the radio row before this one that the row stands under ([`Param::under_option`]). + under_option: Option, beside: bool, not_remembered: bool, /// The lines a multi-line text row shows. @@ -964,6 +968,30 @@ impl Param { self } + /// For a [`ParamKind::Radio`] row: the options stand in a column with no caption over or beside them, in a document + /// dialog too (production's Split); the label still names the group for assistive technology. Other kinds are + /// unchanged. + #[must_use] + pub fn stacked_options(mut self) -> Self { + self.options.get_or_insert_default().stacked_options = true; + self + } + + /// The row belongs to the option `id` of the radio row it follows: it stands under that option, one indent step in + /// (production's Pages per file under Number of pages), or right after the option, with no caption of its own, + /// when the radio row's options stand in a row ([`options_in_row`](Param::options_in_row); production's colour + /// well after From colour). Only the rows that directly follow a radio row nest; elsewhere the mark is ignored. + #[must_use] + pub fn under_option(mut self, id: impl Into) -> Self { + self.options.get_or_insert_default().under_option = Some(id.into()); + self + } + + /// The option of the radio row before this one that the row stands under ([`under_option`](Param::under_option)). + pub(crate) fn nested_under(&self) -> Option<&str> { + self.options.as_deref()?.under_option.as_deref() + } + /// 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] @@ -1155,6 +1183,11 @@ impl RowOptions { self.options_in_row } + /// Whether a radio row's options stand in a column with no caption. + pub(crate) fn stacked_options(&self) -> bool { + self.stacked_options + } + /// The lines of a multi-line text row; `None` for a single line. pub(crate) fn lines(&self) -> Option { self.lines diff --git a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs index 59d326ebfa..a23157406c 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs @@ -337,11 +337,20 @@ struct Line<'r> { enabled: bool, /// The row stands in a shared line: it takes its content's width, not the form's. compact: bool, + /// The rows that stand under this radio row's options ([`Param::under_option`]); empty for other rows. + nested: &'r [Param], + /// The row stands right after its option in a row of radio options: its control alone, which its own name + /// labels, with no caption. + bare: bool, } impl Line<'_> { /// Shows `body` in a field named by the row: its label beside the control, its tooltip over both. fn show(&self, ui: &mut Ui, mut body: impl FnMut(&mut Ui)) { + if self.bare { + Row::new().id_salt(self.key).enabled(self.enabled).show(ui, |ui, _| body(ui)); + return; + } let mut field = Field::new(self.name.clone()).id_salt(self.key).enabled(self.enabled); // In a document dialog a custom row is a group of controls (page range options, say): its name heads it. if matches!(self.param.kind, ParamKind::Custom(_)) && Env::of(ui).view().get::() == LabelScheme::Inline { @@ -497,6 +506,15 @@ impl<'p> Pass<'p> { }); continue; } + // The rows under a radio row's options stand with them, in the radio row. + let nested = + if matches!(head.kind, ParamKind::Radio { .. }) { tail.iter().take_while(|param| param.nested_under().is_some()).count() } else { 0 }; + if nested > 0 { + let (under, after) = tail.split_at(nested); + self.row_with(ui, head, false, under); + rest = after; + continue; + } let beside = tail.iter().take_while(|param| param.option().beside()).count(); if beside == 0 || !head.visible(&self.work) { self.row(ui, head, false); @@ -515,10 +533,20 @@ impl<'p> Pass<'p> { } fn row(&mut self, ui: &mut Ui, param: &Param, compact: bool) { + self.row_with(ui, param, compact, &[]); + } + + /// Shows `param`'s row with the rows `nested` under its options. + fn row_with(&mut self, ui: &mut Ui, param: &Param, compact: bool, nested: &[Param]) { + self.line(ui, param, compact, nested, false); + } + + /// Shows `param`'s row; a `bare` row is its control alone, with no caption. + fn line(&mut self, ui: &mut Ui, param: &Param, compact: bool, nested: &[Param], bare: bool) { if !param.visible(&self.work) { return; } - let line = Line { param, key: param.key.as_str(), name: plain(¶m.label), enabled: param.enabled(&self.work), compact }; + let line = Line { param, key: param.key.as_str(), name: plain(¶m.label), enabled: param.enabled(&self.work), compact, nested, bare }; let held = self.work.get(line.key).cloned(); self.show_row(ui, &line); param.drop_dependents(held.as_ref(), &mut self.work); @@ -777,8 +805,7 @@ impl<'p> Pass<'p> { let first = patterns.clone().next(); let current = patterns.find(|key| Some(key.as_str()) == stored).or(first).cloned(); // 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(), compact: line.compact }; + let line = Line { enabled: line.enabled && current.is_some(), name: line.name.clone(), ..*line }; line.show(ui, |ui| { let picked = PresetPicker::pattern(line.name.clone(), Bind::given(current.clone()), source, Bind::kept()).show(ui).picked; if let Some(id) = picked.filter(|id| rows.iter().any(|row| matches!(row.kind, PresetKind::Item) && row.key == *id)) { @@ -846,6 +873,10 @@ impl<'p> Pass<'p> { self.radio_group(ui, line, options, true); return; } + if line.param.option().stacked_options() { + self.radio_group(ui, line, options, false); + return; + } // A document dialog sets the caption and its options on one line, as production's do; the caption is left out // under a heading of the same name. if inline { @@ -872,7 +903,8 @@ impl<'p> Pass<'p> { if let Some(tip) = line.param.tooltip_text() { group = group.tooltip(tip.clone()); } - let radios = |ui: &mut Ui, cx: &mut BodyCx<'_>| { + let nested = line.nested; + let mut radios = |ui: &mut Ui, cx: &mut BodyCx<'_>| { for (id, label) in options { let mut radio = Radio::new(id.clone()).enabled(line.param.option_enabled(id) && line.enabled); if let Some(reason) = line.param.disabled_reason(id) { @@ -887,13 +919,14 @@ impl<'p> Pass<'p> { radio.label(label.clone()).show(ui); } } + self.under(ui, nested, id, in_row); } }; let group = group.show(ui, |ui, _| { if in_row { - Row::new().wrap(true).show(ui, radios); + Row::new().wrap(true).show(ui, &mut radios); } else { - Stack::new().show(ui, radios); + Stack::new().show(ui, &mut radios); } }); if let Some(v) = group.output.value().cloned() { @@ -901,6 +934,26 @@ impl<'p> Pass<'p> { } } + /// The rows of `nested` that stand under the option `id`: right after it in a row of options (`in_row`), each its + /// control alone; otherwise one indent step in under it, each at its content's width. + fn under(&mut self, ui: &mut Ui, nested: &[Param], id: &str, in_row: bool) { + let own: Vec<&Param> = nested.iter().filter(|param| param.nested_under() == Some(id)).collect(); + if own.is_empty() { + return; + } + if in_row { + for param in own { + self.line(ui, param, true, &[], true); + } + return; + } + Stack::new().id_salt(("under", id)).indent(1).show(ui, |ui, _| { + for param in &own { + self.line(ui, param, true, &[], false); + } + }); + } + /// An angle row: the dial and the field over `range` at `decimals` digits. The field clamps a typed angle into /// the range and raises the range alert. fn angle(&mut self, ui: &mut Ui, line: &Line<'_>, default: f64, range: std::ops::RangeInclusive, decimals: u32) { @@ -1112,6 +1165,10 @@ impl<'p> Pass<'p> { fn custom(&mut self, ui: &mut Ui, line: &Line<'_>) { let ParamKind::Custom(row) = &line.param.kind else { return }; let mut context = CustomContext::new(line.name.clone(), self.canvas); + // In a document dialog a group under a heading of the same name shows no caption of its own, as a radio row's. + let inline = Env::of(ui).view().get::() == LabelScheme::Inline; + let headed = inline && self.heading.as_deref() == Some(plain(&line.name).as_str()); + let line = &Line { bare: line.bare || headed, name: line.name.clone(), ..*line }; line.show_body(ui, |ui| { let _ = row.draw(ui, &mut self.work, &mut context); }); diff --git a/crates/ui-patterns/tests/it/form_dialogs/params.rs b/crates/ui-patterns/tests/it/form_dialogs/params.rs index 6481eede35..55fdac1368 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/params.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/params.rs @@ -630,3 +630,72 @@ fn a_row_beside_the_previous_one_shares_its_line() { 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:?}"); } + +/// The texts of a document form (`LabelScheme::Inline`) over `params`, with their rects. +fn inline_texts(params: Vec) -> Vec<(String, egui::Rect)> { + 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(); + fixture.texts().iter().map(|text| (text.text.clone(), text.rect)).collect() +} + +/// The rect of the first text that contains `name`. +fn text_at(texts: &[(String, egui::Rect)], name: &str) -> Option { + texts.iter().find(|(text, _)| text.contains(name)).map(|(_, rect)| *rect) +} + +/// Production's Split: a radio row with `stacked_options` stands its options in a column with no caption, also in a +/// document form, and a row marked `under_option` stands under its option, before the next option. +#[trial] +fn stacked_options_nest_each_rows_under_its_option() { + let ways = vec![("pages".to_owned(), Label::from("Number of pages")), ("size".to_owned(), Label::from("File size"))]; + let params = vec![ + Param::new("mode", "Split by", ParamKind::radio(ways, Some("pages".to_owned()))).stacked_options(), + Param::number("pages", "Pages per file", 1.0, 1.0, 0).under_option("pages"), + Param::number("size", "At most", 2.0, 0.1, 1).under_option("size"), + ]; + let texts = inline_texts(params); + assert!(text_at(&texts, "Split by").is_none(), "no caption over or beside stacked options: {texts:?}"); + let (pages, per_file, size, at_most) = ( + text_at(&texts, "Number of pages").expect("the first option"), + text_at(&texts, "Pages per file").expect("the first option's row"), + text_at(&texts, "File size").expect("the second option"), + text_at(&texts, "At most").expect("the second option's row"), + ); + assert!( + pages.bottom() <= per_file.top() + 1.0 && per_file.bottom() <= size.top() + 1.0, + "the row stands between its option and the next: {texts:?}" + ); + assert!(size.bottom() <= at_most.top() + 1.0, "the second row stands under the second option: {texts:?}"); +} + +/// Production's Background: a row under an option of a row of options stands right after that option on its line, +/// its control alone (no caption), before the next option. +#[trial] +fn a_row_under_an_option_in_a_row_follows_it_with_no_caption() { + let sources = vec![("colour".to_owned(), Label::from("From colour")), ("file".to_owned(), Label::from("File"))]; + let params = vec![ + Param::new("source", "Source", ParamKind::radio(sources, Some("colour".to_owned()))).options_in_row(), + Param::number("strength", "Strength", 3.0, 1.0, 0).under_option("colour"), + ]; + let texts = inline_texts(params); + assert!(text_at(&texts, "Strength").is_none(), "the nested control shows no caption: {texts:?}"); + let (colour, file) = (text_at(&texts, "From colour").expect("the first option"), text_at(&texts, "File").expect("the second option")); + let value = texts.iter().find(|(text, _)| text.trim() == "3").map(|(_, rect)| *rect).expect("the nested field shows its value"); + assert!((value.center().y - colour.center().y).abs() < 6.0, "on the options' line: {texts:?}"); + assert!(value.left() > colour.right() && value.right() < file.left(), "between its option and the next: {texts:?}"); +} + +/// In a document form a group row under a heading of its own name shows the name once (production's Enhance: Pages +/// over the page range options), as a radio row does. +#[trial] +fn a_group_row_under_a_heading_of_its_name_shows_the_name_once() { + let params = vec![Param::section("Pages"), Param::custom("pages", "Pages", |ui, _, _| ui.label("The options"))]; + let texts = inline_texts(params); + let names = texts.iter().filter(|(text, _)| text.trim() == "Pages").count(); + assert_eq!(names, 1, "the heading alone names the group: {texts:?}"); + assert!(text_at(&texts, "The options").is_some(), "the group shows: {texts:?}"); +}