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
impl SliderState
Sourcepub fn new(value: f64, min: f64, max: f64) -> Self
pub fn new(value: f64, min: f64, max: f64) -> Self
Creates a new slider state with the given value and bounds
§Arguments
value- Initial valuemin- Minimum valuemax- 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);Sourcepub fn value(&self) -> f64
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);Sourcepub fn set_value(&mut self, value: f64)
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);Sourcepub fn min(&self) -> f64
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);Sourcepub fn max(&self) -> f64
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);Sourcepub fn percentage(&self) -> f64
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);Sourcepub fn set_percentage(&mut self, percentage: f64)
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);Sourcepub fn increase(&mut self, step: f64)
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);Sourcepub fn step_up(&mut self)
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);Sourcepub fn decrease(&mut self, step: f64)
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);Sourcepub fn step_down(&mut self)
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);Sourcepub fn step(&self) -> f64
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);Sourcepub fn set_step(&mut self, step: f64)
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!Sourcepub fn with_step(value: f64, min: f64, max: f64, step: f64) -> Self
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 valuemin- Minimum valuemax- Maximum valuestep- 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);Sourcepub fn set_from_position(&mut self, position: u16, length: u16)
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);Sourcepub fn position(&self, length: u16) -> u16
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);Sourcepub fn range(&self) -> f64
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);Sourcepub fn is_at_min(&self) -> bool
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());Sourcepub fn is_at_max(&self) -> bool
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());Sourcepub fn is_at_middle(&self) -> bool
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());Sourcepub fn is_low(&self) -> bool
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());Sourcepub fn is_medium(&self) -> bool
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());Sourcepub fn is_high(&self) -> bool
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());Sourcepub fn distance_from_min(&self) -> f64
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);Sourcepub fn distance_from_max(&self) -> f64
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);Sourcepub fn value_string(&self, decimals: usize) -> String
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");Sourcepub fn percentage_string(&self) -> String
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
impl Clone for SliderState
Source§fn clone(&self) -> SliderState
fn clone(&self) -> SliderState
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SliderState
impl Debug for SliderState
Auto Trait Implementations§
impl Freeze for SliderState
impl RefUnwindSafe for SliderState
impl Send for SliderState
impl Sync for SliderState
impl Unpin for SliderState
impl UnwindSafe for SliderState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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