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: f32Inclusive minimum slider value.
max: f32Inclusive maximum slider value.
step: Option<f32>Positive snapping interval, or None for continuous values.
Implementations§
Source§impl SliderSpec
impl SliderSpec
Sourcepub fn new(min: f32, max: f32) -> Self
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);Sourcepub fn with_step(self, step: f32) -> Self
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));Sourcepub fn sanitized(self) -> Self
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));Sourcepub fn span(self) -> f32
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);Sourcepub fn clamp_value(self, value: f32) -> f32
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);Sourcepub fn snap_value(self, value: f32) -> f32
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);Sourcepub fn fraction_for_value(self, value: f32) -> f32
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);Sourcepub fn value_for_fraction(self, fraction: f32) -> f32
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);Sourcepub fn nudge_value(self, value: f32, direction: f32, large: bool) -> f32
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);Sourcepub fn clamp_range_value(self, value: SliderRangeValue) -> SliderRangeValue
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));Sourcepub fn set_range_thumb(
self,
value: SliderRangeValue,
thumb: SliderThumb,
next: f32,
) -> SliderRangeValue
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));Sourcepub fn nearest_thumb(self, value: SliderRangeValue, target: f32) -> SliderThumb
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
impl Clone for SliderSpec
Source§fn clone(&self) -> SliderSpec
fn clone(&self) -> SliderSpec
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more