Skip to main content

SliderState

Struct SliderState 

Source
pub struct SliderState { /* private fields */ }
Expand description

State management for a slider widget

Manages the current value and min/max bounds. All values are automatically clamped to stay within the specified range.

§Examples

use tui_slider::SliderState;

let mut state = SliderState::new(50.0, 0.0, 100.0);

// Direct value manipulation
state.increase(10.0);
assert_eq!(state.value(), 60.0);

state.decrease(5.0);
assert_eq!(state.value(), 55.0);

Implementations§

Source§

impl SliderState

Source

pub fn new(value: f64, min: f64, max: f64) -> Self

Creates a new slider state with the given value and bounds

§Arguments
  • value - Initial value
  • min - Minimum value
  • max - Maximum value
§Panics

Panics if min >= max

§Examples
use tui_slider::SliderState;

let state = SliderState::new(50.0, 0.0, 100.0);
assert_eq!(state.value(), 50.0);
assert_eq!(state.min(), 0.0);
assert_eq!(state.max(), 100.0);
use tui_slider::SliderState;

// This will panic because min >= max
let state = SliderState::new(50.0, 100.0, 0.0);
Source

pub fn value(&self) -> f64

Gets the current value

§Examples
use tui_slider::SliderState;

let state = SliderState::new(75.0, 0.0, 100.0);
assert_eq!(state.value(), 75.0);
Source

pub fn set_value(&mut self, value: f64)

Sets the value (automatically clamped to min..max range)

§Examples
use tui_slider::SliderState;

let mut state = SliderState::new(50.0, 0.0, 100.0);
state.set_value(75.0);
assert_eq!(state.value(), 75.0);

// Values are clamped to the valid range
state.set_value(150.0);
assert_eq!(state.value(), 100.0);
Source

pub fn min(&self) -> f64

Gets the minimum value

§Examples
use tui_slider::SliderState;

let state = SliderState::new(50.0, 0.0, 100.0);
assert_eq!(state.min(), 0.0);
Source

pub fn max(&self) -> f64

Gets the maximum value

§Examples
use tui_slider::SliderState;

let state = SliderState::new(50.0, 0.0, 100.0);
assert_eq!(state.max(), 100.0);
Source

pub fn set_min(&mut self, min: f64)

Sets the minimum value

§Panics

Panics if the new min is >= max

§Examples
use tui_slider::SliderState;

let mut state = SliderState::new(50.0, 0.0, 100.0);
state.set_min(-10.0);
assert_eq!(state.min(), -10.0);
Source

pub fn set_max(&mut self, max: f64)

Sets the maximum value

§Panics

Panics if the new max is <= min

§Examples
use tui_slider::SliderState;

let mut state = SliderState::new(50.0, 0.0, 100.0);
state.set_max(200.0);
assert_eq!(state.max(), 200.0);
Source

pub fn percentage(&self) -> f64

Gets the value as a percentage (0.0 to 1.0)

§Examples
use tui_slider::SliderState;

let state = SliderState::new(50.0, 0.0, 100.0);
assert_eq!(state.percentage(), 0.5);

let state = SliderState::new(25.0, 0.0, 100.0);
assert_eq!(state.percentage(), 0.25);
Source

pub fn set_percentage(&mut self, percentage: f64)

Sets the value from a percentage (0.0 to 1.0)

The percentage is automatically clamped to the 0.0-1.0 range.

§Examples
use tui_slider::SliderState;

let mut state = SliderState::new(0.0, 0.0, 100.0);
state.set_percentage(0.75);
assert_eq!(state.value(), 75.0);

state.set_percentage(0.5);
assert_eq!(state.value(), 50.0);
Source

pub fn increase(&mut self, step: f64)

Increases the value by a step

The result is automatically clamped to the maximum value.

§Examples
use tui_slider::SliderState;

let mut state = SliderState::new(50.0, 0.0, 100.0);
state.increase(10.0);
assert_eq!(state.value(), 60.0);

// Won't exceed max
state.increase(100.0);
assert_eq!(state.value(), 100.0);
Source

pub fn step_up(&mut self)

Increases the value by the configured step size

This is a convenience method that uses the step size set via set_step().

§Examples
use tui_slider::SliderState;

let mut state = SliderState::new(50.0, 0.0, 100.0);
state.set_step(5.0);
state.step_up();
assert_eq!(state.value(), 55.0);
Source

pub fn decrease(&mut self, step: f64)

Decreases the value by a step

The result is automatically clamped to the minimum value.

§Examples
use tui_slider::SliderState;

let mut state = SliderState::new(50.0, 0.0, 100.0);
state.decrease(10.0);
assert_eq!(state.value(), 40.0);

// Won't go below min
state.decrease(100.0);
assert_eq!(state.value(), 0.0);
Source

pub fn step_down(&mut self)

Decreases the value by the configured step size

This is a convenience method that uses the step size set via set_step().

§Examples
use tui_slider::SliderState;

let mut state = SliderState::new(50.0, 0.0, 100.0);
state.set_step(5.0);
state.step_down();
assert_eq!(state.value(), 45.0);
Source

pub fn step(&self) -> f64

Gets the current step size

§Examples
use tui_slider::SliderState;

let state = SliderState::new(50.0, 0.0, 100.0);
assert_eq!(state.step(), 1.0);
Source

pub fn set_step(&mut self, step: f64)

Sets the step size for increment/decrement operations

The step size determines how much the value changes when using step_up() and step_down() methods.

§Arguments
  • step - The step size (must be positive)
§Panics

Panics if step is not positive (step <= 0.0)

§Examples
use tui_slider::SliderState;

let mut state = SliderState::new(50.0, 0.0, 100.0);
state.set_step(5.0);
state.step_up();
assert_eq!(state.value(), 55.0);

state.set_step(10.0);
state.step_up();
assert_eq!(state.value(), 65.0);
use tui_slider::SliderState;

let mut state = SliderState::new(50.0, 0.0, 100.0);
state.set_step(-1.0); // Panics!
Source

pub fn with_step(value: f64, min: f64, max: f64, step: f64) -> Self

Creates a new slider state with a custom step size

§Arguments
  • value - Initial value
  • min - Minimum value
  • max - Maximum value
  • step - Step size for increment/decrement operations
§Panics

Panics if min >= max or if step <= 0.0

§Examples
use tui_slider::SliderState;

let mut state = SliderState::with_step(50.0, 0.0, 100.0, 5.0);
assert_eq!(state.step(), 5.0);
state.step_up();
assert_eq!(state.value(), 55.0);
Source

pub fn set_from_position(&mut self, position: u16, length: u16)

Sets the value from a position within a given length

§Arguments
  • position - Position in the slider (0 to length)
  • length - Total length of the slider
§Examples
use tui_slider::SliderState;

let mut state = SliderState::new(0.0, 0.0, 100.0);
state.set_from_position(50, 100);
assert_eq!(state.value(), 50.0);
Source

pub fn position(&self, length: u16) -> u16

Gets the position within a given length

§Examples
use tui_slider::SliderState;

let state = SliderState::new(50.0, 0.0, 100.0);
assert_eq!(state.position(100), 50);

let state = SliderState::new(25.0, 0.0, 100.0);
assert_eq!(state.position(100), 25);
Source

pub fn range(&self) -> f64

Returns the range (max - min)

§Examples
use tui_slider::SliderState;

let state = SliderState::new(50.0, 0.0, 100.0);
assert_eq!(state.range(), 100.0);

let state = SliderState::new(50.0, 25.0, 75.0);
assert_eq!(state.range(), 50.0);
Source

pub fn is_at_min(&self) -> bool

Returns true if the slider is at its minimum value

§Examples
use tui_slider::SliderState;

let state = SliderState::new(0.0, 0.0, 100.0);
assert!(state.is_at_min());

let state = SliderState::new(50.0, 0.0, 100.0);
assert!(!state.is_at_min());
Source

pub fn is_at_max(&self) -> bool

Returns true if the slider is at its maximum value

§Examples
use tui_slider::SliderState;

let state = SliderState::new(100.0, 0.0, 100.0);
assert!(state.is_at_max());

let state = SliderState::new(50.0, 0.0, 100.0);
assert!(!state.is_at_max());
Source

pub fn is_at_middle(&self) -> bool

Returns true if the slider is at or near the middle of its range

§Examples
use tui_slider::SliderState;

let state = SliderState::new(50.0, 0.0, 100.0);
assert!(state.is_at_middle());

let state = SliderState::new(0.0, 0.0, 100.0);
assert!(!state.is_at_middle());
Source

pub fn is_low(&self) -> bool

Returns true if the slider value is in the lower third of its range

§Examples
use tui_slider::SliderState;

let state = SliderState::new(20.0, 0.0, 100.0);
assert!(state.is_low());

let state = SliderState::new(80.0, 0.0, 100.0);
assert!(!state.is_low());
Source

pub fn is_medium(&self) -> bool

Returns true if the slider value is in the middle third of its range

§Examples
use tui_slider::SliderState;

let state = SliderState::new(50.0, 0.0, 100.0);
assert!(state.is_medium());

let state = SliderState::new(10.0, 0.0, 100.0);
assert!(!state.is_medium());
Source

pub fn is_high(&self) -> bool

Returns true if the slider value is in the upper third of its range

§Examples
use tui_slider::SliderState;

let state = SliderState::new(80.0, 0.0, 100.0);
assert!(state.is_high());

let state = SliderState::new(20.0, 0.0, 100.0);
assert!(!state.is_high());
Source

pub fn distance_from_min(&self) -> f64

Returns the distance from the minimum value

§Examples
use tui_slider::SliderState;

let state = SliderState::new(75.0, 0.0, 100.0);
assert_eq!(state.distance_from_min(), 75.0);

let state = SliderState::new(75.0, 25.0, 100.0);
assert_eq!(state.distance_from_min(), 50.0);
Source

pub fn distance_from_max(&self) -> f64

Returns the distance from the maximum value

§Examples
use tui_slider::SliderState;

let state = SliderState::new(75.0, 0.0, 100.0);
assert_eq!(state.distance_from_max(), 25.0);

let state = SliderState::new(75.0, 25.0, 100.0);
assert_eq!(state.distance_from_max(), 25.0);
Source

pub fn value_string(&self, decimals: usize) -> String

Returns a formatted string representation of the current value

§Examples
use tui_slider::SliderState;

let state = SliderState::new(75.5, 0.0, 100.0);
assert_eq!(state.value_string(1), "75.5");
assert_eq!(state.value_string(0), "76");
Source

pub fn percentage_string(&self) -> String

Returns a formatted percentage string (e.g., “75%”)

§Examples
use tui_slider::SliderState;

let state = SliderState::new(75.0, 0.0, 100.0);
assert_eq!(state.percentage_string(), "75%");

let state = SliderState::new(50.0, 0.0, 100.0);
assert_eq!(state.percentage_string(), "50%");

Trait Implementations§

Source§

impl Clone for SliderState

Source§

fn clone(&self) -> SliderState

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for SliderState

Source§

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

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

impl Default for SliderState

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