From f9b127a2b761e871a67a2abb20a148d2806a0552 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 01:49:49 +0000 Subject: [PATCH] Side labels can show a short caption in place of the name: LabelKind::Short keeps the text label's column and colon while the label's text names the control; WarpFields shows H and V beside the distortion fields --- crates/imaging/src/ui/warp/fields.rs | 24 +++++++++++------ crates/ui-contract/src/contract/label/leaf.rs | 6 ++--- .../ui-contract/src/contract/label/place.rs | 2 +- .../ui-contract/src/contract/label/props.rs | 13 +++++++++ crates/ui-contract/src/contract/label/step.rs | 2 +- crates/ui-widgets/tests/it/label/mod.rs | 1 + crates/ui-widgets/tests/it/label/short.rs | 27 +++++++++++++++++++ 7 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 crates/ui-widgets/tests/it/label/short.rs diff --git a/crates/imaging/src/ui/warp/fields.rs b/crates/imaging/src/ui/warp/fields.rs index 17ad84775..faa71c47f 100644 --- a/crates/imaging/src/ui/warp/fields.rs +++ b/crates/imaging/src/ui/warp/fields.rs @@ -3,12 +3,12 @@ use craft_behavior::{EditEvent, IconId}; use craft_ui::buttons::ToggleButton; -use craft_ui::contract::{Bind, Entry, FieldLayout, Props as _, Show as _, ShowContainer as _, Sizing}; +use craft_ui::contract::{Bind, Entry, FieldLayout, LabelKind, Props as _, Show as _, ShowContainer as _, SideLabel, Sizing}; use craft_ui::display::Divider; use craft_ui::form::Field; use craft_ui::layout_components::Row; use craft_ui::pickers::Select; -use craft_ui::runtime::env::{Env, UnitKind}; +use craft_ui::runtime::env::{Env, Label, UnitKind}; use craft_ui::text::NumberField; use egui::Ui; @@ -32,6 +32,10 @@ craft_ui::words! { bend: "warpFields.bend" = "Bend"; horizontal: "warpFields.horizontal" = "Horizontal distortion"; vertical: "warpFields.vertical" = "Vertical distortion"; + /// The caption shown beside the Horizontal distortion field. + horizontal_caption: "warpFields.horizontalCaption" = "H"; + /// The caption shown beside the Vertical distortion field. + vertical_caption: "warpFields.verticalCaption" = "V"; } } @@ -295,13 +299,17 @@ impl<'a> WarpFields<'a> { edits.push(WarpFieldsEdit::orientation(!values.vertical)); } type FieldEdit = fn(EditEvent) -> WarpFieldsEdit; - let fields: [(_, f64, FieldEdit); 3] = [ - (&words.bend, values.bend, WarpFieldsEdit::Bend), - (&words.horizontal, values.horizontal, WarpFieldsEdit::Horizontal), - (&words.vertical, values.vertical_distortion, WarpFieldsEdit::Vertical), + let fields: [(&Label, Option<&Label>, f64, FieldEdit); 3] = [ + (&words.bend, None, values.bend, WarpFieldsEdit::Bend), + (&words.horizontal, Some(&words.horizontal_caption), values.horizontal, WarpFieldsEdit::Horizontal), + (&words.vertical, Some(&words.vertical_caption), values.vertical_distortion, WarpFieldsEdit::Vertical), ]; - for (name, value, edit) in fields { - Field::new(name.clone()).layout(caption).sizing(Sizing::Content).show(ui, |ui, _| { + for (name, short, value, edit) in fields { + let mut field = Field::new(name.clone()).layout(caption).sizing(Sizing::Content); + if let Some(short) = short { + field = field.side(SideLabel::new().kind(LabelKind::Short(short.clone()))); + } + field.show(ui, |ui, _| { let field = NumberField::new(name.clone(), Bind::given(Some(value))) .range(DISTORTION_RANGE) .unit_kind(UnitKind::Percent) diff --git a/crates/ui-contract/src/contract/label/leaf.rs b/crates/ui-contract/src/contract/label/leaf.rs index 2a4b3c0cc..8c66ec361 100644 --- a/crates/ui-contract/src/contract/label/leaf.rs +++ b/crates/ui-contract/src/contract/label/leaf.rs @@ -348,11 +348,11 @@ impl Component for LabelLeaf { } } -/// The colon a beside text label draws after its name, unless the name ends in one. The label's node and the +/// The colon a beside text label draws after its shown text, unless that text ends in one. The label's node and the /// control keep the name as given. pub(super) fn colon(env: &Env, input: &LabelIn<'_>) -> Option { - let text = env.view().resolve(input.text); - let beside_text = input.side.layout == FieldLayout::Beside && input.side.kind == LabelKind::Text; + let text = env.view().resolve(input.side.kind.shown(input.text)); + let beside_text = input.side.layout == FieldLayout::Beside && matches!(input.side.kind, LabelKind::Text | LabelKind::Short(_)); if !beside_text || text.as_str().is_empty() || text.as_str().ends_with([':', '\u{ff1a}']) { return None; } diff --git a/crates/ui-contract/src/contract/label/place.rs b/crates/ui-contract/src/contract/label/place.rs index dfcd1ff76..52b549c19 100644 --- a/crates/ui-contract/src/contract/label/place.rs +++ b/crates/ui-contract/src/contract/label/place.rs @@ -156,7 +156,7 @@ pub(crate) fn control_after(ui: &mut Ui, env: &Env, input: &LabelIn<'_>) -> Plac } let reset = scope::last_reset(&ctx, input.scope); let slot = if reset.is_some() && input.side.reset_dot { env.tokens.size.handle.sm } else { 0.0 }; - let mut leaf = LabelLeaf::new(input.text.clone(), slot); + let mut leaf = LabelLeaf::new(input.side.kind.shown(input.text).clone(), slot); leaf.resettable = reset.is_some(); leaf.colon = colon(env, input); match &input.side.kind { diff --git a/crates/ui-contract/src/contract/label/props.rs b/crates/ui-contract/src/contract/label/props.rs index 1ef798c1c..ec8d56b01 100644 --- a/crates/ui-contract/src/contract/label/props.rs +++ b/crates/ui-contract/src/contract/label/props.rs @@ -20,6 +20,19 @@ pub enum LabelKind { /// An icon or a short text shown in place of the label; the text still names the control. Glyph labels share /// a column of their own, apart from the text labels'. The label's detail joins the description and is not shown. Glyph(GlyphLabel), + /// A shorter text shown in place of the label ("H" for "Horizontal distortion"), as a text label in every other + /// way: its column, colon, icon and detail. The label's text still names the control. + Short(Label), +} + +impl LabelKind { + /// The text a visible text label shows: the short text of `Short`, else `text`. + pub(super) fn shown<'a>(&'a self, text: &'a Label) -> &'a Label { + match self { + LabelKind::Short(short) => short, + _ => text, + } + } } /// What a glyph label shows. diff --git a/crates/ui-contract/src/contract/label/step.rs b/crates/ui-contract/src/contract/label/step.rs index b491c07b1..d8cbc84f3 100644 --- a/crates/ui-contract/src/contract/label/step.rs +++ b/crates/ui-contract/src/contract/label/step.rs @@ -149,7 +149,7 @@ pub(crate) fn run(ui: &mut Ui, env: &Env, input: &LabelIn<'_>, trailing: Option< return Placed { label: Rect::NOTHING, control: hidden_control(&ctx, input, flow, gap), trailing: Rect::NOTHING }; } let slot = if reset.is_some() && input.side.reset_dot { slot_side } else { 0.0 }; - let mut leaf = LabelLeaf::new(input.text.clone(), slot); + let mut leaf = LabelLeaf::new(input.side.kind.shown(input.text).clone(), slot); leaf.scrub = input.enabled && (input.side.scrub || scope::last_scrub(&ctx, input.scope)); leaf.resettable = reset.is_some(); leaf.differs = reset.is_some_and(|fact| fact.differs); diff --git a/crates/ui-widgets/tests/it/label/mod.rs b/crates/ui-widgets/tests/it/label/mod.rs index f59d68639..1663c974f 100644 --- a/crates/ui-widgets/tests/it/label/mod.rs +++ b/crates/ui-widgets/tests/it/label/mod.rs @@ -5,6 +5,7 @@ pub mod group; mod hit; mod inside; pub mod kit; +mod short; mod tooltip; use self::kit::{Dial, Field, Seen, Wide}; diff --git a/crates/ui-widgets/tests/it/label/short.rs b/crates/ui-widgets/tests/it/label/short.rs new file mode 100644 index 000000000..2191f315e --- /dev/null +++ b/crates/ui-widgets/tests/it/label/short.rs @@ -0,0 +1,27 @@ +//! A short caption: the field shows the short text with its colon, and the long label names the control. + +use craft_ui::contract::{Bind, LabelKind, Show as _, ShowContainer as _, SideLabel}; +use craft_ui::form::Field; +use craft_ui::testing::{Fixture, trial}; +use craft_ui::text::NumberField; + +const NAME: &str = "Horizontal distortion"; + +#[trial] +fn a_short_caption_shows_in_place_of_the_label_and_the_label_names_the_control() { + let mut fixture = Fixture::new(|ui| { + Field::new(NAME).side(SideLabel::new().kind(LabelKind::Short("H".into()))).show(ui, |ui, _| { + NumberField::new(NAME, Bind::given(Some(10.0))).show(ui); + }); + }); + fixture.settle(); + let texts = fixture.texts(); + let caption = texts.iter().find(|run| run.text == "H").expect("the short caption is shown"); + assert!( + texts.iter().any(|run| run.text.trim() == ":" && run.rect.min.x >= caption.rect.min.x), + "the short caption takes the colon of a beside text label" + ); + assert!(texts.iter().all(|run| run.text != NAME), "the long label is not shown"); + let control = fixture.by_label(NAME).expect("the long label names the control"); + assert!(control.rect.min.x >= caption.rect.max.x, "the control follows its caption"); +}