Skip to main content

PercentSlider

Struct PercentSlider 

Source
pub struct PercentSlider<'a> { /* private fields */ }
Expand description

A 0–100% slider whose central UI element is the percentage value itself.

let mut share = 45.0_f32;
ui.add(
    PercentSlider::new(&mut share)
        .label("Cache window")
        .callout_fmt(|p| {
            let mins = (p * 60.0 / 100.0).round() as i32;
            format!("{mins} min")
        }),
);

Implementations§

Source§

impl<'a> PercentSlider<'a>

Source

pub fn new(value: &'a mut f32) -> Self

Create a slider bound to value, clamped to 0.0..=100.0.

Source

pub fn label(self, label: impl Into<WidgetText>) -> Self

Add a small label in the top-left of the header row.

The label also names the slider for screen readers. Without one, name the returned response with egui::Response::accessible_name, or with egui::Response::labelled_by when a caption is drawn beside it.

Source

pub fn accent(self, accent: Accent) -> Self

Pick the fill colour from one of the theme accents. Default: Accent::Sky.

Source

pub fn show_ticks(self, show: bool) -> Self

Show or hide the quartile tick row beneath the track. Default: true.

Source

pub fn step(self, step: f32) -> Self

Snap the value to multiples of step percentage points. Default: continuous. Common values: 5.0 for “round to 5%”, 25.0 for quartile snap.

Mutually exclusive with steps and stops; calling this clears either of them.

Source

pub fn steps(self, n: usize) -> Self

Snap to n evenly-spaced positions, including both endpoints. steps(2) snaps to {0, 100}, steps(5) to {0, 25, 50, 75, 100}, steps(7) to seven positions whose spacing the widget computes for you (so the awkward 100 / 6 math stays out of caller code).

When set, the tick row renders at exactly those positions and the arrow keys jump between adjacent stops. Values below 2 are promoted to 2. Mutually exclusive with step and stops; calling this clears either of them.

Source

pub fn stops(self, positions: impl IntoIterator<Item = f32>) -> Self

Snap to an explicit, possibly non-uniform list of positions in 0.0..=100.0. Out-of-range, NaN, and duplicate values are filtered out; the result is sorted ascending. If fewer than two valid positions remain, falls back to [0.0, 100.0].

When set, the tick row renders at exactly these positions and the arrow keys jump between adjacent stops. Mutually exclusive with step and steps; calling this clears either of them.

Source

pub fn decimals(self, n: usize) -> Self

Number of decimal places in the headline value. Default: 0.

Source

pub fn callout_fmt(self, fmt: impl Fn(f32) -> String + 'a) -> Self

Supply a callback to format the entire drag-callout text. The callback receives the current percentage in 0.0..=100.0 and returns the string to render in the callout above the thumb while the user drags. When unset, no callout is shown.

The callback has full control over the text. The widget does not prepend the percentage. Common patterns:

ⓘ
// Just the absolute quantity (the headline already shows the percent):
.callout_fmt(|p| format!("{} min", (p * 60.0 / 100.0).round() as i32))

// Percent and absolute together:
.callout_fmt(|p| {
    let mins = (p * 60.0 / 100.0).round() as i32;
    format!("{}% \u{00B7} {} min", p.round() as i32, mins)
})
Source

pub fn desired_width(self, width: f32) -> Self

Override the slider width. Defaults to ui.available_width().

Source

pub fn handle(self, handle: SliderHandle) -> Self

Pick the thumb shape. Default: SliderHandle::Circle. Switch to SliderHandle::Line for a thin vertical bar instead of the standard circular knob.

Trait Implementations§

Source§

impl<'a> Debug for PercentSlider<'a>

Source§

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

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

impl<'a> Widget for PercentSlider<'a>

Source§

fn ui(self, ui: &mut Ui) -> Response

Allocate space, interact, paint, and return a Response. Read more
Source§

fn boxed<'a>(self) -> Box<dyn FnOnce(&mut Ui) -> Response + 'a>
where Self: Sized + 'a,

Box this widget for dynamic dispatch.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<'a> Freeze for PercentSlider<'a>

§

impl<'a> Unpin for PercentSlider<'a>

§

impl<'a> UnsafeUnpin for PercentSlider<'a>

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> 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> 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, 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, 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.