Add step member to slider widgets

Both the native and the web slider now have a member `step` to control
the least possible change of the slider's value. It defaults to 1.0
for all sliders and can be adjusted with the step method.
This commit is contained in:
Sebastian Zivota 2020-06-02 20:34:13 +02:00
parent baa1389f71
commit c3643eaf6d
5 changed files with 81 additions and 33 deletions

View file

@ -16,6 +16,8 @@ use std::{ops::RangeInclusive, rc::Rc};
///
/// A [`Slider`] will try to fill the horizontal space of its container.
///
/// The step size defaults to 1.0.
///
/// [`Slider`]: struct.Slider.html
///
/// # Example
@ -37,6 +39,7 @@ use std::{ops::RangeInclusive, rc::Rc};
pub struct Slider<'a, Message> {
_state: &'a mut State,
range: RangeInclusive<f32>,
step: f32,
value: f32,
on_change: Rc<Box<dyn Fn(f32) -> Message>>,
width: Length,
@ -69,6 +72,7 @@ impl<'a, Message> Slider<'a, Message> {
_state: state,
value: value.max(*range.start()).min(*range.end()),
range,
step: 1.0,
on_change: Rc::new(Box::new(on_change)),
width: Length::Fill,
style: Default::default(),
@ -90,6 +94,14 @@ impl<'a, Message> Slider<'a, Message> {
self.style = style.into();
self
}
/// Sets the step size of the [`Slider`].
///
/// [`Slider`]: struct.Slider.html
pub fn step(mut self, step: f32) -> Self {
self.step = step;
self
}
}
impl<'a, Message> Widget<Message> for Slider<'a, Message>
@ -110,15 +122,15 @@ where
let min = bumpalo::format!(in bump, "{}", start);
let max = bumpalo::format!(in bump, "{}", end);
let value = bumpalo::format!(in bump, "{}", self.value);
let step = bumpalo::format!(in bump, "{}", self.step);
let on_change = self.on_change.clone();
let event_bus = bus.clone();
// TODO: Make `step` configurable
// TODO: Styling
input(bump)
.attr("type", "range")
.attr("step", "0.01")
.attr("step", step.into_bump_str())
.attr("min", min.into_bump_str())
.attr("max", max.into_bump_str())
.attr("value", value.into_bump_str())