Length fields: LengthUnits::bare_in reads bare numbers in another unit; a length row honours Param::codec

This commit is contained in:
bflatastic committed 2026-11-22 04:43:11 +07:00
1 parent 3269303dce
commit a7a3b72eb0
4 files changed
+67 -9

No files matched your search

+20 -4
View File
@@ -322,7 +322,7 @@ impl ColumnSize {
}
/// A length kept in one unit and shown in another: what a length field reads and writes (`stored`, the document's
/// unit) and what it shows and parses bare numbers in (`shown`, the user's unit). Both are `UnitKind::Pixels` or any
/// unit) and what it shows and, unless `bare` names another unit, parses bare numbers in (`shown`, the user's unit). Both are `UnitKind::Pixels` or any
/// kind [`UnitKind::per_inch`] answers; the document's resolution sizes pixels.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
@@ -333,12 +333,22 @@ pub struct LengthUnits {
pub shown: UnitKind,
/// Pixels per inch of the document, for `Pixels` on either side.
pub pixels_per_inch: f64,
/// The unit a number typed without a unit is in; `None` for `shown` ([`LengthUnits::bare_in`]).
pub bare: Option<UnitKind>,
}
impl LengthUnits {
/// A length kept in `stored` and shown in `shown`, with `pixels_per_inch` sizing pixels.
pub const fn new(stored: UnitKind, shown: UnitKind, pixels_per_inch: f64) -> Self {
LengthUnits { stored, shown, pixels_per_inch }
LengthUnits { stored, shown, pixels_per_inch, bare: None }
}
/// A number typed without a unit is in `unit` rather than in the shown unit (a preference such as "numbers
/// without units are points").
#[must_use]
pub const fn bare_in(mut self, unit: UnitKind) -> Self {
self.bare = Some(unit);
self
}
}
@@ -565,10 +575,16 @@ impl Units {
}
/// A reader for lengths shown in `lengths.shown`: the spellings of every length unit and of pixels convert to it,
/// and a bare number is in it. Plain numbers when `lengths` is not a pixel or length pair.
/// and a bare number is in it, or in `lengths.bare` when set. Plain numbers when `lengths` is not a pixel or length
/// pair.
pub fn length_reader(&self, lengths: LengthUnits) -> Reader {
let Some(shown) = self.length_per_inch(lengths.shown, lengths.pixels_per_inch) else { return self.reader(None) };
self.reader_over(|other| Some(shown / self.length_per_inch(other, lengths.pixels_per_inch)?))
let factor = |other: UnitKind| Some(shown / self.length_per_inch(other, lengths.pixels_per_inch)?);
let reader = self.reader_over(factor);
match lengths.bare.and_then(factor) {
Some(bare) => reader.bare(bare),
None => reader,
}
}
/// The index of the reader for `kind` in `readers`: 0 for plain numbers, then one per unit kind.
@@ -770,10 +770,12 @@ impl<'p> Pass<'p> {
let option = line.param.option();
let default = line.param.default_value().and_then(number_of).map(Some);
line.show(ui, |ui| {
let mut field = match option.length() {
Some(lengths) => NumberField::length(line.name.clone(), line.bind(Some(value), default), lengths).step(step),
None => line.scalar_field(self.units, line.bind(Some(value), default), decimals, option.codec()).step(step),
};
let mut field = match (option.length(), option.codec()) {
(Some(lengths), Some(codec)) => NumberField::length(line.name.clone(), line.bind(Some(value), default), lengths).codec(codec.clone()),
(Some(lengths), None) => NumberField::length(line.name.clone(), line.bind(Some(value), default), lengths),
(None, codec) => line.scalar_field(self.units, line.bind(Some(value), default), decimals, codec),
}
.step(step);
if let Some(range) = range.clone() {
field = field.range(range);
}
@@ -842,3 +842,24 @@ fn a_disabled_row_shows_its_disabled_value_and_keeps_its_own() {
fixture.click(&paper).settle();
assert_eq!(ink(&fixture), Some(craft_behavior::Mixed::Same(false)), "enabled, it shows its own value again");
}
// A length row reads typed text with the row's codec when it has one, as a plain number row does.
#[trial]
fn a_length_row_reads_with_its_codec() {
let lengths = craft_ui::runtime::env::LengthUnits::new(UnitKind::Points, UnitKind::Points, 72.0);
// A codec that reads a bare number as twice itself.
let doubled = craft_behavior::NumberCodec::new(craft_behavior::Reader::new().bare(2.0), 2);
let params = vec![
Param::number("plain", "Plain", 0.0, 1.0, 2).length(lengths),
Param::number("coded", "Coded", 0.0, 1.0, 2).length(lengths).codec(doubled),
];
let values = Rc::new(RefCell::new(DialogFields::new()));
let mut fixture = rows(params, &values);
for label in ["Plain", "Coded"] {
let field = fixture.by_label(label).expect("the length row");
fixture.click(&field).settle();
fixture.key(Key::A, Modifiers::COMMAND).type_text("3").key(Key::Enter, Modifiers::NONE).settle();
}
let (plain, coded) = (values.borrow().number("plain"), values.borrow().number("coded"));
assert!(plain.zip(coded).is_some_and(|(plain, coded)| coded > plain), "the codec read the typed text: {plain:?} against {coded:?}");
}
@@ -16,12 +16,16 @@ const SAME: f64 = 1e-6;
/// A field over points that shows inches, driven by `drive` after it holds focus.
fn in_inches(start: f64, drive: impl FnOnce(&mut Frames, &mut dyn FnMut(&mut Ui))) -> f64 {
in_lengths(LengthUnits::new(UnitKind::Points, UnitKind::Inches, START), start, drive)
}
/// A field over `lengths`, driven by `drive` after it holds focus.
fn in_lengths(lengths: LengthUnits, start: f64, drive: impl FnOnce(&mut Frames, &mut dyn FnMut(&mut Ui))) -> f64 {
let mut frames = Frames::new();
let value = RefCell::new(Some(start));
let at = Cell::new(Pos2::ZERO);
let mut show = |ui: &mut Ui| {
let mut guard = value.borrow_mut();
let lengths = LengthUnits::new(UnitKind::Points, UnitKind::Inches, START);
at.set(NumberField::length("Width", Bind::bound(&mut *guard), lengths).id_salt("width").show(ui).response.rect.center());
};
frames.idle(3, &mut show);
@@ -58,3 +62,18 @@ fn a_length_field_types_and_steps_in_the_shown_unit() {
let tenth_up = in_inches(START, type_and_commit("1.1"));
assert!((stepped - tenth_up).abs() < SAME, "an arrow moves a tenth of an inch: {stepped} against {tenth_up}");
}
/// With bare numbers in points, a bare number in a picas field reads as points; a spelled unit still reads as written.
#[trial]
fn a_bare_number_reads_in_the_lengths_bare_unit() {
let picas = LengthUnits::new(UnitKind::Points, UnitKind::Picas, START);
let in_points = picas.bare_in(UnitKind::Points);
let bare = in_lengths(in_points, START, type_and_commit("12"));
let spelled = in_lengths(in_points, START, type_and_commit("12 pt"));
assert!((bare - spelled).abs() < SAME, "a bare 12 is 12 points: {bare} against {spelled}");
let in_picas = in_lengths(picas, START, type_and_commit("12"));
assert!(in_picas > bare, "without it a bare 12 is 12 picas: {in_picas} against {bare}");
let picas_spelled = in_lengths(in_points, START, type_and_commit("1 pica"));
let picas_plain = in_lengths(picas, START, type_and_commit("1"));
assert!((picas_spelled - picas_plain).abs() < SAME, "a spelled pica is still a pica");
}