From 2d16976d72713f28f43b855daf78af4dcde623a0 Mon Sep 17 00:00:00 2001 From: aiongg Date: Wed, 11 Nov 2026 23:27:53 +0000 Subject: [PATCH] Whole-number fields: table counts and warp distortion write whole numbers; a fine key step moves a share of the step only where the field's decimals show it --- crates/imaging/src/ui/warp/fields.rs | 7 +++-- crates/pages/src/ui/table/properties.rs | 2 +- crates/ui-widgets/src/text/number.rs | 10 +++++--- crates/ui-widgets/src/text/number/rule.rs | 7 ++--- crates/ui-widgets/tests/it/text/number.rs | 31 ++++++++++++++++++++--- 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/crates/imaging/src/ui/warp/fields.rs b/crates/imaging/src/ui/warp/fields.rs index 0fa1a5205..b1b08870c 100644 --- a/crates/imaging/src/ui/warp/fields.rs +++ b/crates/imaging/src/ui/warp/fields.rs @@ -302,8 +302,11 @@ impl<'a> WarpFields<'a> { ]; for (name, value, edit) in fields { Field::new(name.clone()).layout(caption).sizing(Sizing::Content).show(ui, |ui, _| { - let field = - NumberField::new(name.clone(), Bind::given(Some(value))).range(DISTORTION_RANGE).unit_kind(UnitKind::Percent).show(ui); + let field = NumberField::new(name.clone(), Bind::given(Some(value))) + .range(DISTORTION_RANGE) + .unit_kind(UnitKind::Percent) + .fixed_decimals(0) + .show(ui); for event in field.edit.events.iter().cloned() { edits.push(edit(event.map(|typed| typed.unwrap_or(value)))); } diff --git a/crates/pages/src/ui/table/properties.rs b/crates/pages/src/ui/table/properties.rs index 5d9e17e8b..3d26c7ebe 100644 --- a/crates/pages/src/ui/table/properties.rs +++ b/crates/pages/src/ui/table/properties.rs @@ -277,7 +277,7 @@ impl<'a> TableProperties<'a> { fn count(ui: &mut Ui, caption: &Label, value: usize, least: usize, change: fn(usize) -> TableChange) -> Option { let mut picked = None; Field::new(caption.clone()).show(ui, |ui, _| { - let edit = NumberField::new(caption.clone(), Bind::given(Some(value as f64))).range(least as f64..=f64::INFINITY).step(1.0).show(ui); + let edit = NumberField::new(caption.clone(), Bind::given(Some(value as f64))).range(least as f64..=f64::INFINITY).fixed_decimals(0).show(ui); picked = edit.value().copied().flatten().map(|n| change(n.round().max(least as f64) as usize)); }); picked diff --git a/crates/ui-widgets/src/text/number.rs b/crates/ui-widgets/src/text/number.rs index 58ef5a7c0..a78a7b318 100644 --- a/crates/ui-widgets/src/text/number.rs +++ b/crates/ui-widgets/src/text/number.rs @@ -412,15 +412,17 @@ impl Component for NumberField<'_, V> { /// Arrows, Page keys, the wheel and AccessKit Increment and Decrement step the value, AccessKit sets it, and the /// chevrons open the popups. Up and Down step by the step, with Shift by `LARGE_STEPS` steps and, on a field - /// without a dropdown, with Alt by a tenth; on a field with presets or a popup slider Alt+Down opens the dropdown - /// instead, and the wheel keeps its Alt fine step. A step starts from the applied value, or from 0 kept in the + /// without a dropdown, with Alt by a tenth where the decimals show it (else by the step); on a field with presets + /// or a popup slider Alt+Down opens the dropdown instead, so it has no fine key step, and the wheel keeps its Alt + /// fine step. A step starts from the applied value, or from 0 kept in the /// range while the field is blank. fn update(&mut self, cx: &mut Cx<'_, Self>) { let rule = self.rule(); + let fine_shows = self.decimals() >= decimals_for_step(rule.step / LARGE_STEPS); while let Some((mv, mods)) = cx.intents().take_move() { let (notches, scale) = match mv { - NavMove::Up => (1, key_scale(mods)), - NavMove::Down => (-1, key_scale(mods)), + NavMove::Up => (1, key_scale(mods, fine_shows)), + NavMove::Down => (-1, key_scale(mods, fine_shows)), NavMove::PageUp => (1, LARGE_STEPS), NavMove::PageDown => (-1, LARGE_STEPS), // AccessKit Increment and Decrement: the text keeps the horizontal arrows, so only those arrive. diff --git a/crates/ui-widgets/src/text/number/rule.rs b/crates/ui-widgets/src/text/number/rule.rs index bc96df1e4..3b68b8c03 100644 --- a/crates/ui-widgets/src/text/number/rule.rs +++ b/crates/ui-widgets/src/text/number/rule.rs @@ -82,11 +82,12 @@ pub(super) fn round_to(value: f64, decimals: usize) -> f64 { (value * scale).round() / scale } -/// The scale of a step by an arrow key: Shift moves `LARGE_STEPS` steps, Alt a share of one, both together one. -pub(super) fn key_scale(mods: MoveMods) -> f64 { +/// The scale of a step by an arrow key: Shift moves `LARGE_STEPS` steps, Alt a share of one where the field's +/// decimals show that share (`fine_shows`), both together one. +pub(super) fn key_scale(mods: MoveMods, fine_shows: bool) -> f64 { match (mods.extend, mods.fine) { (true, false) => LARGE_STEPS, - (false, true) => 1.0 / LARGE_STEPS, + (false, true) if fine_shows => 1.0 / LARGE_STEPS, _ => 1.0, } } diff --git a/crates/ui-widgets/tests/it/text/number.rs b/crates/ui-widgets/tests/it/text/number.rs index 1a4b1ecb0..d2f087416 100644 --- a/crates/ui-widgets/tests/it/text/number.rs +++ b/crates/ui-widgets/tests/it/text/number.rs @@ -22,6 +22,8 @@ struct Rig { picked: Cell>, range: Option>, presets: Vec>, + /// The field writes whole numbers (`fixed_decimals(0)`). + whole: bool, } impl Rig { @@ -33,14 +35,15 @@ impl Rig { picked: Cell::new(None), range, presets: Vec::new(), + whole: false, }; rig.idle(3); rig } fn frame(&mut self, events: Vec) { - let Rig { frames, value, rect, picked, range, presets } = self; - frames.frame(events, |ui| show(ui, value, rect, picked, range, presets)); + let Rig { frames, value, rect, picked, range, presets, whole } = self; + frames.frame(events, |ui| show(ui, value, rect, picked, range, presets, *whole)); } fn idle(&mut self, frames: usize) { @@ -50,9 +53,9 @@ impl Rig { } fn focus(&mut self) { - let Rig { frames, value, rect, picked, range, presets } = self; + let Rig { frames, value, rect, picked, range, presets, whole } = self; let at = rect.get().center(); - click(frames, at, &mut |ui| show(ui, value, rect, picked, range, presets)); + click(frames, at, &mut |ui| show(ui, value, rect, picked, range, presets, *whole)); self.idle(2); } @@ -73,6 +76,7 @@ fn show( picked: &Cell>, range: &Option>, presets: &[Entry], + whole: bool, ) { let mut guard = value.borrow_mut(); let mut field = NumberField::new("Size", Bind::bound(&mut *guard)).id_salt("size"); @@ -82,6 +86,9 @@ fn show( if !presets.is_empty() { field = field.presets(presets); } + if whole { + field = field.fixed_decimals(0); + } let out = field.show(ui); rect.set(out.response.rect); if out.preset.is_some() { @@ -105,6 +112,22 @@ fn arrows_step_to_the_next_multiple_and_stay_in_range() { assert_eq!(rig.value(), Some(0.0), "Shift moves ten steps and stops at the start of the range"); } +/// Alt steps a tenth where the field's decimals show it, and a whole step on a whole-number field. +#[trial] +fn a_fine_step_moves_only_as_far_as_the_decimals_show() { + let mut rig = Rig::new(5.0, None); + rig.focus(); + rig.key(Key::ArrowUp, Modifiers::ALT); + let fine = rig.value().unwrap_or_default() - 5.0; + let mut whole = Rig::new(5.0, None); + whole.whole = true; + whole.focus(); + whole.key(Key::ArrowUp, Modifiers::ALT); + let plain = whole.value().unwrap_or_default() - 5.0; + assert!(fine > 0.0 && fine < 1.0, "Alt moves a share of the step on a field with decimals"); + assert_eq!(plain, 1.0, "a whole-number field takes the plain step"); +} + /// A field of `kind` (plain without one) under `host`'s locale, with `text` typed over its value and committed. fn typed(host: Option>, kind: Option, text: &str) -> Option { typed_with(host, text, |field| match kind {