Skip to main content

ValueFormat

Struct ValueFormat 

Source
pub struct ValueFormat<'a> {
    pub prefix: Atoms<'a>,
    pub suffix: Atoms<'a>,
    pub min_decimals: usize,
    pub max_decimals: Option<usize>,
    pub custom_formatter: Option<Arc<dyn Fn(f64, RangeInclusive<usize>) -> String + 'a>>,
    pub custom_parser: Option<Arc<dyn Fn(&str) -> Option<f64> + 'a>>,
    pub update_while_editing: bool,
}
Available on crate feature egui only.
Expand description

How a number is written, read back, and applied while being edited.

Owned by crate::DragValue, and by crate::Slider and crate::RangeSlider for the numbers they show beside the rail.

Fields§

§prefix: Atoms<'a>

Shown before the number, e.g. "x: ".

§suffix: Atoms<'a>

Shown after the number, e.g. a unit.

§min_decimals: usize

Show at least this many decimals.

§max_decimals: Option<usize>

Show at most this many decimals, and round values to it.

§custom_formatter: Option<Arc<dyn Fn(f64, RangeInclusive<usize>) -> String + 'a>>

Turns a value into text. Default: crate::style::NumberFormatter.

§custom_parser: Option<Arc<dyn Fn(&str) -> Option<f64> + 'a>>

Turns text back into a value. Default: accepts plain decimal numbers.

§update_while_editing: bool

Apply each keystroke while typing, rather than on enter.

Implementations§

Source§

impl<'a> ValueFormat<'a>

Source

pub fn prefix(self, prefix: impl IntoAtoms<'a>) -> ValueFormat<'a>

Show a prefix before the number, e.g. “x: “.

Goes in front of any prefix already set, so .prefix("b").prefix("a") shows ab.

Source

pub fn suffix(self, suffix: impl IntoAtoms<'a>) -> ValueFormat<'a>

Add a suffix to the number, this can be e.g. a unit (“°” or “ m“).

Goes after any suffix already set, so .suffix("a").suffix("b") shows ab.

Source

pub fn min_decimals(self, min_decimals: usize) -> ValueFormat<'a>

Set a minimum number of decimals to display.

Source

pub fn max_decimals(self, max_decimals: usize) -> ValueFormat<'a>

Set a maximum number of decimals to display. Values will also be rounded to this number of decimals.

Source

pub fn max_decimals_opt(self, max_decimals: Option<usize>) -> ValueFormat<'a>

Set the maximum number of decimals to display, or None to let the widget pick.

Values will also be rounded to this number of decimals when it is set.

Source

pub fn fixed_decimals(self, num_decimals: usize) -> ValueFormat<'a>

Set an exact number of decimals to display. Values will also be rounded to this number of decimals.

Source

pub fn custom_formatter( self, formatter: impl Fn(f64, RangeInclusive<usize>) -> String + 'a, ) -> ValueFormat<'a>

Set custom formatter defining how numbers are converted into text.

A custom formatter takes a f64 for the numeric value and a RangeInclusive<usize> representing the decimal range i.e. minimum and maximum number of decimal places shown.

See also: Self::custom_parser

Source

pub fn custom_parser( self, parser: impl Fn(&str) -> Option<f64> + 'a, ) -> ValueFormat<'a>

Set custom parser defining how the text input is parsed into a number.

A custom parser takes an &str to parse into a number and returns a f64 if it was successfully parsed or None otherwise.

See also: Self::custom_formatter

Source

pub fn binary(self, min_width: usize, twos_complement: bool) -> ValueFormat<'a>

Set custom_formatter and custom_parser to display and parse numbers as binary integers. Floating point numbers are not supported.

min_width specifies the minimum number of displayed digits; if the number is shorter than this, it will be prefixed with additional 0s to match min_width.

If twos_complement is true, negative values will be displayed as the 2’s complement representation. Otherwise they will be prefixed with a ‘-’ sign.

§Panics

Panics if min_width is 0.

Source

pub fn octal(self, min_width: usize, twos_complement: bool) -> ValueFormat<'a>

Set custom_formatter and custom_parser to display and parse numbers as octal integers. Floating point numbers are not supported.

min_width specifies the minimum number of displayed digits; if the number is shorter than this, it will be prefixed with additional 0s to match min_width.

If twos_complement is true, negative values will be displayed as the 2’s complement representation. Otherwise they will be prefixed with a ‘-’ sign.

§Panics

Panics if min_width is 0.

Source

pub fn hexadecimal( self, min_width: usize, twos_complement: bool, upper: bool, ) -> ValueFormat<'a>

Set custom_formatter and custom_parser to display and parse numbers as hexadecimal integers. Floating point numbers are not supported.

min_width specifies the minimum number of displayed digits; if the number is shorter than this, it will be prefixed with additional 0s to match min_width.

If twos_complement is true, negative values will be displayed as the 2’s complement representation. Otherwise they will be prefixed with a ‘-’ sign.

§Panics

Panics if min_width is 0.

Source

pub fn update_while_editing(self, update: bool) -> ValueFormat<'a>

Update the value on each key press when text-editing the value.

Default: true. If false, the value will only be updated when user presses enter or deselects the value.

Source

pub fn round(&self, value: f64) -> f64

Rounds value to max_decimals, if set.

Trait Implementations§

Source§

impl<'a> Clone for ValueFormat<'a>

Source§

fn clone(&self) -> ValueFormat<'a>

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 Default for ValueFormat<'_>

Source§

fn default() -> ValueFormat<'_>

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

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for ValueFormat<'a>

§

impl<'a> !Send for ValueFormat<'a>

§

impl<'a> !Sync for ValueFormat<'a>

§

impl<'a> !UnwindSafe for ValueFormat<'a>

§

impl<'a> Forget for ValueFormat<'a>

§

impl<'a> Freeze for ValueFormat<'a>
where *const u8 is !null: Freeze, PhantomData<Atom<'a>>: Freeze, *const ArcInner<dyn Fn(f64, RangeInclusive<usize>) -> String + 'a> is !null: Freeze, PhantomData<ArcInner<dyn Fn(f64, RangeInclusive<usize>) -> String + 'a>>: Freeze, *const ArcInner<dyn Fn(&str) -> Option<f64> + 'a> is !null: Freeze, PhantomData<ArcInner<dyn Fn(&str) -> Option<f64> + 'a>>: Freeze,

§

impl<'a> Move for ValueFormat<'a>

§

impl<'a> Unpin for ValueFormat<'a>
where *const u8 is !null: Unpin, NonZero<u64>: Unpin, Arc<RichText>: Unpin, Arc<LayoutJob>: Unpin, Arc<Galley>: Unpin, &'a str: Unpin, String: Unpin, &'static str: Unpin, &'static [u8]: Unpin, Arc<[u8]>: Unpin, Box<dyn FnOnce(&Ui, IntoSizedArgs) -> IntoSizedResult<'static> + 'a>: Unpin, Arc<dyn Fn(&Ui, AtomPaintArgs) + 'a>: Unpin, Box<WidgetAtom<'a>>: Unpin, Box<ContainerAtom<'a>>: Unpin, Arc<dyn Fn(f64, RangeInclusive<usize>) -> String + 'a>: Unpin, Arc<dyn Fn(&str) -> Option<f64> + 'a>: Unpin,

§

impl<'a> UnsafeUnpin for ValueFormat<'a>
where *const u8 is !null: UnsafeUnpin, PhantomData<Atom<'a>>: UnsafeUnpin, *const ArcInner<dyn Fn(f64, RangeInclusive<usize>) -> String + 'a> is !null: UnsafeUnpin, PhantomData<ArcInner<dyn Fn(f64, RangeInclusive<usize>) -> String + 'a>>: UnsafeUnpin, *const ArcInner<dyn Fn(&str) -> Option<f64> + 'a> is !null: UnsafeUnpin, PhantomData<ArcInner<dyn Fn(&str) -> Option<f64> + 'a>>: UnsafeUnpin,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more