From d059b92889b837bad7c8af039d8092d0bb5bfdd2 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 22 Nov 2026 04:55:45 +0700 Subject: [PATCH] ui-widgets: Radio::detail shows a muted line after the radio's label through the label step; form radio rows use it --- crates/ui-contract/src/contract/label/step.rs | 4 +++ crates/ui-contract/src/contract/mod.rs | 6 +++++ .../src/patterns/form_dialogs/rows.rs | 19 +++++-------- crates/ui-widgets/src/choice/radio.rs | 17 ++++++++---- crates/ui-widgets/src/choice/radio/stories.rs | 7 +++-- crates/ui-widgets/tests/it/label/hit.rs | 27 +++++++++++++++++++ 6 files changed, 59 insertions(+), 21 deletions(-) diff --git a/crates/ui-contract/src/contract/label/step.rs b/crates/ui-contract/src/contract/label/step.rs index deeae58f4..dcb81b4d3 100644 --- a/crates/ui-contract/src/contract/label/step.rs +++ b/crates/ui-contract/src/contract/label/step.rs @@ -504,5 +504,9 @@ pub(crate) fn show_leaf_in_flow(mut child: C, ui: &mut Ui) -> C::Output let facts = crate::contract::run::TypeFacts::of::(); let id = crate::contract::steps::id(ui, &child.common(crate::contract::Internal::new()).id, false, true, facts.style_key, facts.name); let side = SideLabel::new().layout(FieldLayout::After); + let side = match child.label_detail() { + Some(detail) => side.detail(detail), + None => side, + }; flow_leaf(ui, (id, id), (env.enabled, env.size), label, side, child, None) } diff --git a/crates/ui-contract/src/contract/mod.rs b/crates/ui-contract/src/contract/mod.rs index 1d56f437e..2a4423329 100644 --- a/crates/ui-contract/src/contract/mod.rs +++ b/crates/ui-contract/src/contract/mod.rs @@ -399,6 +399,12 @@ pub trait Component: Meta + Sized { Self::SHOWS_LABEL } + /// The muted line a `LABELLED_AFTER` control's label shows after its text (`SideLabel::detail`), as a radio's + /// `detail`; none by default. It reads props only. + fn label_detail(&self) -> Option { + None + } + /// How far the component paints outside its rect by design, in points (a graph node's ports sit on its edge). /// Steps 13 and 14 clip to the rect grown by this much; the paint-bounds suite case reads it. fn paint_outset(_tokens: &crate::style::UiTokens) -> f32 { diff --git a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs index a574aa2cb..3390b08fe 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs @@ -13,8 +13,8 @@ use super::words::ParamWords; use crate::buttons::{Button, IconButton, 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 _, - SideLabel, Sizing, clicked, + Bind, BodyCx, CanvasGesture, Entry, NoValue, Outcome, Props as _, Remember as _, Sampler as _, Show as _, ShowContainer as _, SideLabel, Sizing, + clicked, }; use crate::display::Text; use crate::form::{Field, FieldGroup, SliderField, SliderFieldLayout}; @@ -1017,20 +1017,15 @@ impl<'p> Pass<'p> { if let Some(tip) = line.param.tooltip_text() { group = group.tooltip(tip.clone()); } - let group = group.show(ui, |ui, cx| { + let group = group.show(ui, |ui, _| { let mut radios = |ui: &mut Ui| { for (at, (id, label)) in options.iter().enumerate() { - let radio = Radio::new(id.clone()).enabled(line.param.option_enabled(id) && line.enabled); + let radio = Radio::new(id.clone()).enabled(line.param.option_enabled(id) && line.enabled).label(label.clone()); match details.get(at) { - // An option with a detail line draws it under its label through the body's label step. - Some(detail) => { - let side = SideLabel::new().layout(FieldLayout::After).detail(detail.clone()); - cx.labelled(ui, id, label.clone(), side, radio); - } - None => { - radio.label(label.clone()).show(ui); - } + Some(detail) => radio.detail(detail.clone()), + None => radio, } + .show(ui); } }; if in_row { diff --git a/crates/ui-widgets/src/choice/radio.rs b/crates/ui-widgets/src/choice/radio.rs index 64ef18ad9..928c79f2e 100644 --- a/crates/ui-widgets/src/choice/radio.rs +++ b/crates/ui-widgets/src/choice/radio.rs @@ -160,19 +160,22 @@ pub struct Radio { value: T, checked: bool, invalid: bool, + /// The line after the label ([`Radio::detail`]). + detail: Option, } impl Radio { /// A radio button standing for `value`. pub fn new(value: T) -> Self { - Radio { common: Common::new(), value, checked: false, invalid: false } + Radio { common: Common::new(), value, checked: false, invalid: false, detail: None } } - /// A dimmed line under the label (muted text role, small text token), inside the radio's hit area. + /// A dimmed line after the label (the label's detail: muted text), inside the radio's hit area; it joins the + /// radio's description. #[must_use] - pub fn detail(self, text: impl Into) -> Self { - let _ = text; - todo!("co.K26") + pub fn detail(mut self, text: impl Into) -> Self { + self.detail = Some(text.into()); + self } } @@ -195,6 +198,10 @@ impl Component for Radio { } } + fn label_detail(&self) -> Option { + self.detail.clone() + } + fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 { Vec2::splat(cx.icon()) } diff --git a/crates/ui-widgets/src/choice/radio/stories.rs b/crates/ui-widgets/src/choice/radio/stories.rs index f86cee71c..1285c7298 100644 --- a/crates/ui-widgets/src/choice/radio/stories.rs +++ b/crates/ui-widgets/src/choice/radio/stories.rs @@ -111,16 +111,15 @@ fn radio_group_with_icons_and_counts(ui: &mut Ui, data: &mut StoryData) -> Story .into() } -/// Rows with a description under the label: the `SideLabel`'s detail joins the radio's accessible description. +/// Rows with a description after the label (`Radio::detail`): it joins the radio's accessible description. #[story(RadioGroup<'static, u8>, covers = [], drive = Drive::Never)] fn radio_group_with_descriptions(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let rows = [(1u8, "Fast", "Lower quality, smaller file"), (2, "Balanced", "The default for most photos"), (3, "Best", "Full quality, largest file")]; RadioGroup::new("Export", data.bind("export", 2u8)) - .show(ui, |ui, cx| { + .show(ui, |ui, _| { for (value, label, description) in rows { - let side = SideLabel::new().layout(FieldLayout::After).detail(description); - cx.labelled(ui, value, label.into(), side, Radio::new(value)); + Radio::new(value).label(label).detail(description).show(ui); } }) .into() diff --git a/crates/ui-widgets/tests/it/label/hit.rs b/crates/ui-widgets/tests/it/label/hit.rs index e0c2a2e27..0686c7bfa 100644 --- a/crates/ui-widgets/tests/it/label/hit.rs +++ b/crates/ui-widgets/tests/it/label/hit.rs @@ -81,6 +81,33 @@ fn a_click_on_a_radio_label_picks_it_in_that_pass() { assert_eq!(picked.get(), 2, "the release on Medium's label picks it in its own pass"); } +/// A radio's detail line follows its label and is part of its hit area: a click on it picks the radio. +#[trial] +fn a_radio_detail_follows_its_label_and_a_click_on_it_picks_the_radio() { + let mut frames = Frames::new(); + let picked = Cell::new(1u8); + let mut show = |ui: &mut Ui| { + let mut value = picked.get(); + in_region(ui, REGION, |ui| { + RadioGroup::new("Quality", Bind::bound(&mut value)).show(ui, |ui, _| { + Stack::new().show(ui, |ui, _| { + Radio::new(1u8).label("Fast").detail("Smaller file").show(ui); + Radio::new(2u8).label("Best").detail("Largest file").show(ui); + }); + }) + }); + picked.set(value); + }; + frames.frame(vec![], &mut show); + let (label, detail) = (text_rect(&frames, "Best"), text_rect(&frames, "Largest file")); + let follows = detail.left() >= label.right() || detail.top() >= label.bottom(); + assert!(follows, "the detail follows the label: {detail:?} after {label:?}"); + for event in [moved(detail.center()), press(detail.center()), release(detail.center())] { + frames.frame(vec![event], &mut show); + } + assert_eq!(picked.get(), 2, "a click on the detail picks its radio"); +} + #[trial] fn a_long_label_after_its_box_wraps_with_the_box_on_its_first_line() { const LONG: &str = "Show the transform controls on the selected layer whenever the move tool is active";