Skip to main content

NumberInputState

Struct NumberInputState 

Source
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: f64

Committed numeric value, always within [min, max].

§min: f64

Inclusive lower bound.

§max: f64

Inclusive upper bound.

§step: f64

Increment applied per Up/Down/scroll tick.

§integer: bool

When 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

Source

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);
Source

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);
Source

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);
Source

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

Source§

fn clone(&self) -> NumberInputState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NumberInputState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for NumberInputState

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.