form_dialogs: a Range row with Param::length shows its value in the shown unit

Only Number rows read Param::length, so a Range row over points showed
points in an inch or millimeter document.

- SliderField::lengths makes its number field a length field: it shows
  and reads the value in the shown unit at that unit's step and
  decimals, while the binding, the range and the slider stay in the
  stored unit.
- Range rows pass Param::length to their SliderField. A popup-slider
  range row builds a length field too, and a set step, which is in the
  stored unit, steps the field by the same length in the shown unit.
This commit is contained in:
dotCooCoo committed 2026-11-28 19:46:38 -08:00
1 parent b1fc0c8a18
commit e7d4d7e3e5
4 files changed
+79 -12

No files matched your search

@@ -634,7 +634,7 @@ pub(crate) struct RowOptions {
resettable: bool,
/// The label a footer row shows while on ([`Param::in_footer`]).
footer_on: Option<Label>,
/// The lengths a number row keeps and shows ([`Param::length`]).
/// The lengths a number or range row keeps and shows ([`Param::length`]).
length: Option<LengthUnits>,
/// The codec a number row reads and writes with ([`Param::codec`]).
codec: Option<NumberCodec>,
@@ -1080,7 +1080,8 @@ impl Param {
self
}
/// Draws a `Number` row as a length: the stored unit in the binding, the shown unit in the field.
/// Draws a `Number` or `Range` row as a length: the stored unit in the binding, the range and a range row's
/// slider, the shown unit in the field.
#[must_use]
pub fn length(mut self, lengths: LengthUnits) -> Self {
self.options.get_or_insert_default().length = Some(lengths);
@@ -884,6 +884,9 @@ impl<'p> Pass<'p> {
if let Some(kind) = line.param.unit {
slider = slider.unit_kind(kind);
}
if let Some(lengths) = option.length() {
slider = slider.lengths(lengths);
}
if let Some(text) = line.param.unit_label_text() {
slider = slider.unit(text.clone());
}
@@ -920,18 +923,30 @@ impl<'p> Pass<'p> {
// Match the ordinary range row's number field precision when the caller sets no step.
let default_step = if range.end() - range.start() <= 10.0 { 0.01 } else { 1.0 };
let step = set_step.unwrap_or(default_step);
let kind = line.param.unit;
let lengths = line.param.option().length();
let kind = lengths.map(|lengths| lengths.shown).or(line.param.unit);
let symbol: Option<Label> = line.param.unit_label_text().cloned().or_else(|| kind.map(|kind| self.units.symbol(kind).into()));
let default = line.param.default_value().and_then(number_of).map(Some);
line.show(ui, |ui| {
Row::new().show(ui, |ui, _| {
let mut field = NumberField::new(line.name.clone(), line.bind(Some(value), default))
.range(range.clone())
.popup_slider(range.clone())
.snap_popup_slider(set_step.is_some())
.commit_decimals(decimals_for_step(step))
.scrub_by_label();
if set_step.is_some() || default_step != 1.0 {
let bind = line.bind(Some(value), default);
let mut field = match lengths {
// A length field keeps the shown unit's decimals and, without a set step, its step; a set step is
// in the stored unit, so the field steps by it in the shown unit.
Some(lengths) => {
let field = NumberField::length(line.name.clone(), bind, lengths);
match (set_step, self.units.length_scale(lengths)) {
(Some(step), Some(scale)) => field.step(step * scale),
_ => field,
}
}
None => NumberField::new(line.name.clone(), bind).commit_decimals(decimals_for_step(step)),
}
.range(range.clone())
.popup_slider(range.clone())
.snap_popup_slider(set_step.is_some())
.scrub_by_label();
if lengths.is_none() && (set_step.is_some() || default_step != 1.0) {
field = field.step(step).snap_typed(set_step.is_some());
}
if let Some(kind) = kind {
@@ -1203,6 +1203,39 @@ fn a_length_row_reads_with_its_codec() {
assert!(plain.zip(coded).is_some_and(|(plain, coded)| coded > plain), "the codec read the typed text: {plain:?} against {coded:?}");
}
/// A range row with lengths shows its value in the shown unit and keeps the stored one, with a permanent slider and
/// with a popup one; a bare typed number is in the shown unit.
#[trial]
fn a_range_row_shows_a_length_in_the_shown_unit() {
let lengths = craft_ui::runtime::env::LengthUnits::new(UnitKind::Points, UnitKind::Inches, 72.0);
let depth = Param::new("depth", "Depth", ParamKind::range(0.0, 720.0, 72.0, SliderScale::Linear)).length(lengths);
for popup in [false, true] {
let row = if popup { depth.clone().popup_slider() } else { depth.clone() };
let values = Rc::new(RefCell::new(DialogFields::new()));
values.borrow_mut().set("depth", FieldValue::Number(72.0));
let mut fixture = rows(vec![row], &values);
fixture.room(Vec2::new(1280.0, 800.0)).settle();
let texts = fixture.texts();
assert!(texts.iter().any(|run| run.text == "in"), "the shown unit's symbol, popup {popup}");
assert!(!texts.iter().any(|run| run.text == "pt" || run.text.starts_with("72")), "no stored-unit text, popup {popup}");
let tree = fixture.tree();
let field = fixture.by_role(Role::SPIN_BUTTON).into_iter().find(|target| tree.name_of(target.node) == Some("Depth")).expect("the field");
fixture.click(&field).settle();
fixture.key(Key::A, Modifiers::COMMAND).type_text("2").key(Key::Enter, Modifiers::NONE).settle();
let stored = values.borrow().number("depth");
assert!(stored.is_some_and(|points| (points - 144.0).abs() < 1e-6), "2 in is 144 pt, popup {popup}: {stored:?}");
}
// A set step is in the stored unit: Up on a popup row's field moves 72 pt, one inch.
let values = Rc::new(RefCell::new(DialogFields::new()));
values.borrow_mut().set("depth", FieldValue::Number(72.0));
let mut fixture = rows(vec![depth.popup_slider().step(72.0)], &values);
let field = fixture.by_role(Role::SPIN_BUTTON).into_iter().next().expect("the popup row's field");
fixture.click(&field).settle();
fixture.key(Key::ArrowUp, Modifiers::NONE).settle();
let stepped = values.borrow().number("depth");
assert!(stepped.is_some_and(|points| (points - 144.0).abs() < 1e-6), "one step of 72 pt: {stepped:?}");
}
/// 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() {
+20 -2
View File
@@ -7,7 +7,7 @@ use crate::contract::{
Bind, BindCheck, BindMut, Common, Component, Cx, Edit as _, EditResponse, FieldLayout, MeasureCx, Outcome, PaintUi, Props as _, Role, SideLabel,
Sizing, Validity, WordSetCx as _,
};
use crate::runtime::env::{Label, UnitKind};
use crate::runtime::env::{Label, LengthUnits, UnitKind};
use crate::style::{ComponentStyle, StyleCx};
use crate::text::{NumberField, RangeAlert};
use crate::value_drag::{Slider, SliderScale, SliderThumb, TrackFace, TrackPress};
@@ -108,6 +108,11 @@ pub struct SliderField<'a> {
/// What the number counts: the field shows its unit and reads its spellings.
#[prop]
unit_kind: Option<UnitKind>,
/// The lengths the value is kept and shown in ([`NumberField::length`]): the binding, the range, the slider and its
/// step stay in `lengths.stored`, and the number field shows and reads the value in `lengths.shown`, at that unit's
/// own step and decimals. `step`, `typed_decimals`, `field_decimals` and `unit_kind` then apply to the slider only.
#[prop]
lengths: Option<LengthUnits>,
/// A unit shown after the number, for a quantity with no `UnitKind` (a font axis).
#[prop(into)]
unit: Option<Label>,
@@ -186,6 +191,7 @@ impl<'a> SliderField<'a> {
wheel_step: None,
track_press: TrackPress::OnRelease,
unit_kind: None,
lengths: None,
unit: None,
fill: true,
track_face: None,
@@ -225,6 +231,7 @@ impl<'a> SliderField<'a> {
field_decimals: self.field_decimals,
wheel_step: self.wheel_step,
unit_kind: self.unit_kind,
lengths: self.lengths,
unit: self.unit.clone(),
fill: self.fill,
thumb: self.thumb,
@@ -256,6 +263,7 @@ struct Look {
field_decimals: FieldDecimals,
wheel_step: Option<f64>,
unit_kind: Option<UnitKind>,
lengths: Option<LengthUnits>,
unit: Option<Label>,
fill: bool,
thumb: SliderThumb,
@@ -329,6 +337,11 @@ impl Look {
if let Some(check) = check {
bind = bind.check_with(check, |check, value| value.map_or(Validity::Ok, |value| lifted(check.validity(&value))));
}
if let Some(lengths) = self.lengths {
// The step, the snap and the decimals below are in the stored unit; a length field keeps the shown unit's own.
let field = NumberField::length(name.clone(), bind, lengths).range(self.range.clone()).scrub_by_label();
return self.finish_field(field);
}
let step = self.field_step();
let typed_decimals = self.typed_decimals.map(|places| places.min(12));
let field = NumberField::new(name.clone(), bind)
@@ -351,11 +364,16 @@ impl Look {
Some(decimals) => field.fixed_decimals(decimals.min(12)),
None => field,
};
let field = if self.select_on_focus { field.select_on_focus() } else { field };
let field = match self.unit_kind {
Some(kind) => field.unit_kind(kind),
None => field,
};
self.finish_field(field)
}
/// `field` with the parts a length field and a plain one share: focus selection, sizing and the unit text.
fn finish_field<'c>(&self, field: NumberField<'c>) -> NumberField<'c> {
let field = if self.select_on_focus { field.select_on_focus() } else { field };
let field = match self.field_sizing {
Some(sizing) => field.sizing(sizing),
None => field,