Struct SliderSpec

Source
pub struct SliderSpec {
    pub min: f32,
    pub max: f32,
    pub step: Option<f32>,
}
Expand description

Numeric domain and optional snapping interval for a slider.

§Examples

use ailloli_ui_core::SliderSpec;

let spec = SliderSpec::new(0.0, 10.0).with_step(2.0);
assert_eq!(spec.snap_value(3.0), 4.0);

Fields§

§min: f32

Inclusive minimum slider value.

§max: f32

Inclusive maximum slider value.

§step: Option<f32>

Positive snapping interval, or None for continuous values.

Implementations§

Source§

impl SliderSpec

Source

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

Creates a continuous, sanitized slider domain.

Reversed bounds are swapped, equal bounds are expanded upward by adding 1.0 to the maximum, and any non-finite input bound resets the domain to the default 0.0..=100.0. At magnitudes where that increment is not representable, equal bounds can remain equal.

§Examples
use ailloli_ui_core::SliderSpec;
assert_eq!(SliderSpec::new(10.0, 0.0).min, 0.0);
Source

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

Sets a snapping interval and sanitizes the complete specification.

A non-positive or non-finite step is discarded and produces a continuous slider.

§Examples
use ailloli_ui_core::SliderSpec;
assert_eq!(SliderSpec::new(0.0, 10.0).with_step(2.0).step, Some(2.0));
Source

pub fn sanitized(self) -> Self

Returns finite, ascending bounds and a valid optional step.

Equal bounds are widened by adding 1.0 to max; f32 rounding can leave very large equal bounds unchanged.

§Examples
use ailloli_ui_core::SliderSpec;
let spec = SliderSpec { min: 4.0, max: 4.0, step: Some(-1.0) }.sanitized();
assert_eq!((spec.max, spec.step), (5.0, None));
Source

pub fn span(self) -> f32

Returns max - min for the sanitized domain.

The result is normally positive and finite. It can be zero when two large equal bounds cannot be widened by 1.0, or infinity when the difference between finite extreme bounds overflows.

§Examples
use ailloli_ui_core::SliderSpec;
assert_eq!(SliderSpec::new(10.0, 30.0).span(), 20.0);
Source

pub fn clamp_value(self, value: f32) -> f32

Clamps value to the inclusive domain; non-finite values select min.

§Examples
use ailloli_ui_core::SliderSpec;
assert_eq!(SliderSpec::new(0.0, 10.0).clamp_value(12.0), 10.0);
Source

pub fn snap_value(self, value: f32) -> f32

Clamps a value and rounds it to the nearest step relative to min.

Half-step ties follow f32::round and therefore round away from zero in step coordinates. Continuous specifications only clamp the value.

§Examples
use ailloli_ui_core::SliderSpec;
assert_eq!(SliderSpec::new(0.0, 10.0).with_step(2.0).snap_value(3.0), 4.0);
Source

pub fn fraction_for_value(self, value: f32) -> f32

Maps a value into the inclusive normalized interval 0.0..=1.0.

A zero or overflowing span caused by extreme finite bounds can make the IEEE-754 division return NaN.

§Examples
use ailloli_ui_core::SliderSpec;
assert_eq!(SliderSpec::new(10.0, 30.0).fraction_for_value(20.0), 0.5);
Source

pub fn value_for_fraction(self, fraction: f32) -> f32

Maps a normalized fraction into the domain and applies step snapping.

Fractions are clamped to 0.0..=1.0; a non-finite fraction selects the minimum. Intermediate arithmetic can overflow for extreme finite bounds; the final snapping pass then applies its normal non-finite-value rules.

§Examples
use ailloli_ui_core::SliderSpec;
assert_eq!(SliderSpec::new(10.0, 30.0).value_for_fraction(0.75), 25.0);
Source

pub fn nudge_value(self, value: f32, direction: f32, large: bool) -> f32

Moves a value one small or large increment in direction.

A small increment is the configured step or 1% of the domain. A large increment is always 10% of the domain before optional step snapping. Only the sign of direction matters; zero leaves the value at its nearest valid step, and a non-finite direction ultimately selects min.

§Examples
use ailloli_ui_core::SliderSpec;
let spec = SliderSpec::new(0.0, 100.0).with_step(5.0);
assert_eq!(spec.nudge_value(50.0, 1.0, false), 55.0);
Source

pub fn clamp_range_value(self, value: SliderRangeValue) -> SliderRangeValue

Clamps, snaps, and orders both thumbs of a range value.

§Examples
use ailloli_ui_core::{SliderRangeValue, SliderSpec};
let spec = SliderSpec::new(0.0, 100.0).with_step(10.0);
assert_eq!(spec.clamp_range_value(SliderRangeValue::new(12.0, 83.0)), SliderRangeValue::new(10.0, 80.0));
Source

pub fn set_range_thumb( self, value: SliderRangeValue, thumb: SliderThumb, next: f32, ) -> SliderRangeValue

Moves one range thumb without allowing it to cross the other.

Both the existing range and next are clamped and snapped first. When a thumb reaches the other, the result is a zero-width range.

§Examples
use ailloli_ui_core::{SliderRangeValue, SliderSpec, SliderThumb};
let spec = SliderSpec::new(0.0, 100.0);
let range = spec.set_range_thumb(SliderRangeValue::new(20.0, 80.0), SliderThumb::Start, 90.0);
assert_eq!(range, SliderRangeValue::new(80.0, 80.0));
Source

pub fn nearest_thumb(self, value: SliderRangeValue, target: f32) -> SliderThumb

Returns the nearest thumb for a target value.

The range and target are clamped before comparison. Exact ties prefer SliderThumb::End.

§Examples
use ailloli_ui_core::{SliderRangeValue, SliderSpec, SliderThumb};
let thumb = SliderSpec::new(0.0, 100.0).nearest_thumb(SliderRangeValue::new(20.0, 80.0), 50.0);
assert_eq!(thumb, SliderThumb::End);

Trait Implementations§

Source§

impl Clone for SliderSpec

Source§

fn clone(&self) -> SliderSpec

Returns a copy 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 SliderSpec

Source§

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

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

impl Default for SliderSpec

Source§

fn default() -> Self

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

impl PartialEq for SliderSpec

Source§

fn eq(&self, other: &SliderSpec) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for SliderSpec

Source§

impl StructuralPartialEq for SliderSpec

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

Source§

type Output = T

Should always be Self
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.