pub struct NumberInputState {
pub value: f64,
pub min: f64,
pub max: f64,
pub step: f64,
pub integer: bool,
pub editing: Option<String>,
pub parse_error: Option<String>,
}Expand description
State for a numeric stepper field (clamp + step, integer or float).
A numeric stepper renders the value as an editable field with ▾/▴
affordances. When focused it adjusts via Up/Down (or k/j) and the scroll
wheel, or the user can type a value directly and press Enter to commit it.
The committed value is always clamped into
[min, max] (and rounded to a whole number in integer mode).
Create with NumberInputState::new (float) or
NumberInputState::integer, then pass to
Context::number_input each frame.
§Example
let mut qty = NumberInputState::integer(3, 0, 10).step(1.0);
let r = ui.number_input(&mut qty);
if r.changed {
// qty.value was adjusted this frame
}Available since 0.21.0.
Fields§
§value: f64Committed numeric value, always within [min, max].
min: f64Inclusive lower bound.
max: f64Inclusive upper bound.
step: f64Increment applied per Up/Down/scroll tick.
integer: boolWhen true, the value is whole-number only and rendered without a decimal point.
editing: Option<String>In-progress typed text; Some while the user is editing the field.
parse_error: Option<String>Last parse failure from Enter on an invalid buffer, if any.
Implementations§
Source§impl NumberInputState
impl NumberInputState
Sourcepub fn new(value: f64, min: f64, max: f64) -> Self
pub fn new(value: f64, min: f64, max: f64) -> Self
Float stepper with the given starting value and inclusive range.
value is clamped into [min, max] immediately. If min > max the two
bounds are swapped so the range is always well-formed.
§Example
let s = NumberInputState::new(1.5, 0.0, 10.0);
assert_eq!(s.value, 1.5);
assert!(!s.integer);Sourcepub fn integer(value: i64, min: i64, max: i64) -> Self
pub fn integer(value: i64, min: i64, max: i64) -> Self
Integer stepper (rounds value, renders without a decimal point).
Convenience constructor that sets integer = true and a default step of
1.0. value is clamped into [min, max].
§Example
let s = NumberInputState::integer(42, 0, 100);
assert_eq!(s.value, 42.0);
assert!(s.integer);Sourcepub fn step(self, step: f64) -> Self
pub fn step(self, step: f64) -> Self
Set the per-tick increment (consumes self, builder style).
Negative or zero steps are coerced to 0.0 (no adjustment).
§Example
let s = NumberInputState::new(0.0, 0.0, 1.0).step(0.1);
assert!((s.step - 0.1).abs() < f64::EPSILON);Sourcepub fn clamped(&self) -> f64
pub fn clamped(&self) -> f64
Clamp value into [min, max] (and round if integer).
Used internally after every adjustment and typed commit, and exposed so
callers that mutate value directly can
re-normalize it.
§Example
let mut s = NumberInputState::integer(0, 0, 10);
s.value = 99.0;
assert_eq!(s.clamped(), 10.0);
s.value = 3.7;
assert_eq!(s.clamped(), 4.0);Trait Implementations§
Source§impl Clone for NumberInputState
impl Clone for NumberInputState
Source§fn clone(&self) -> NumberInputState
fn clone(&self) -> NumberInputState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more