2019-11-21 13:47:20 +01:00
|
|
|
use crate::{Bus, Element, Length, Widget};
|
2019-09-14 20:54:50 +02:00
|
|
|
|
2019-09-15 18:53:13 +02:00
|
|
|
use dodrio::bumpalo;
|
2019-11-21 13:47:20 +01:00
|
|
|
use std::{ops::RangeInclusive, rc::Rc};
|
2019-09-14 20:54:50 +02:00
|
|
|
|
2019-11-21 13:47:20 +01:00
|
|
|
pub struct Slider<'a, Message> {
|
|
|
|
|
_state: &'a mut State,
|
|
|
|
|
range: RangeInclusive<f32>,
|
|
|
|
|
value: f32,
|
|
|
|
|
on_change: Rc<Box<dyn Fn(f32) -> Message>>,
|
|
|
|
|
width: Length,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, Message> Slider<'a, Message> {
|
|
|
|
|
/// Creates a new [`Slider`].
|
|
|
|
|
///
|
|
|
|
|
/// It expects:
|
|
|
|
|
/// * the local [`State`] of the [`Slider`]
|
|
|
|
|
/// * an inclusive range of possible values
|
|
|
|
|
/// * the current value of the [`Slider`]
|
|
|
|
|
/// * a function that will be called when the [`Slider`] is dragged.
|
|
|
|
|
/// It receives the new value of the [`Slider`] and must produce a
|
|
|
|
|
/// `Message`.
|
|
|
|
|
///
|
|
|
|
|
/// [`Slider`]: struct.Slider.html
|
|
|
|
|
/// [`State`]: struct.State.html
|
|
|
|
|
pub fn new<F>(
|
|
|
|
|
state: &'a mut State,
|
|
|
|
|
range: RangeInclusive<f32>,
|
|
|
|
|
value: f32,
|
|
|
|
|
on_change: F,
|
|
|
|
|
) -> Self
|
|
|
|
|
where
|
|
|
|
|
F: 'static + Fn(f32) -> Message,
|
|
|
|
|
{
|
|
|
|
|
Slider {
|
|
|
|
|
_state: state,
|
|
|
|
|
value: value.max(*range.start()).min(*range.end()),
|
|
|
|
|
range,
|
|
|
|
|
on_change: Rc::new(Box::new(on_change)),
|
|
|
|
|
width: Length::Fill,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the width of the [`Slider`].
|
|
|
|
|
///
|
|
|
|
|
/// [`Slider`]: struct.Slider.html
|
|
|
|
|
pub fn width(mut self, width: Length) -> Self {
|
|
|
|
|
self.width = width;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-15 18:53:13 +02:00
|
|
|
|
|
|
|
|
impl<'a, Message> Widget<Message> for Slider<'a, Message>
|
|
|
|
|
where
|
|
|
|
|
Message: 'static + Copy,
|
|
|
|
|
{
|
|
|
|
|
fn node<'b>(
|
|
|
|
|
&self,
|
|
|
|
|
bump: &'b bumpalo::Bump,
|
|
|
|
|
bus: &Bus<Message>,
|
|
|
|
|
) -> dodrio::Node<'b> {
|
|
|
|
|
use dodrio::builder::*;
|
|
|
|
|
use wasm_bindgen::JsCast;
|
|
|
|
|
|
|
|
|
|
let (start, end) = self.range.clone().into_inner();
|
|
|
|
|
|
|
|
|
|
let min = bumpalo::format!(in bump, "{}", start);
|
|
|
|
|
let max = bumpalo::format!(in bump, "{}", end);
|
|
|
|
|
let value = bumpalo::format!(in bump, "{}", self.value);
|
|
|
|
|
|
|
|
|
|
let on_change = self.on_change.clone();
|
|
|
|
|
let event_bus = bus.clone();
|
|
|
|
|
|
|
|
|
|
// TODO: Make `step` configurable
|
2019-09-21 13:38:14 +02:00
|
|
|
// TODO: Complete styling
|
2019-09-15 18:53:13 +02:00
|
|
|
label(bump)
|
|
|
|
|
.children(vec![input(bump)
|
|
|
|
|
.attr("type", "range")
|
|
|
|
|
.attr("step", "0.01")
|
|
|
|
|
.attr("min", min.into_bump_str())
|
|
|
|
|
.attr("max", max.into_bump_str())
|
|
|
|
|
.attr("value", value.into_bump_str())
|
|
|
|
|
.on("input", move |root, vdom, event| {
|
|
|
|
|
let slider = match event.target().and_then(|t| {
|
|
|
|
|
t.dyn_into::<web_sys::HtmlInputElement>().ok()
|
|
|
|
|
}) {
|
|
|
|
|
None => return,
|
|
|
|
|
Some(slider) => slider,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Ok(value) = slider.value().parse::<f32>() {
|
|
|
|
|
event_bus.publish(on_change(value), root);
|
|
|
|
|
vdom.schedule_render();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.finish()])
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-14 20:54:50 +02:00
|
|
|
|
|
|
|
|
impl<'a, Message> From<Slider<'a, Message>> for Element<'a, Message>
|
|
|
|
|
where
|
2019-09-15 18:53:13 +02:00
|
|
|
Message: 'static + Copy,
|
2019-09-14 20:54:50 +02:00
|
|
|
{
|
|
|
|
|
fn from(slider: Slider<'a, Message>) -> Element<'a, Message> {
|
|
|
|
|
Element::new(slider)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-21 13:47:20 +01:00
|
|
|
|
|
|
|
|
pub struct State;
|
|
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Self
|
|
|
|
|
}
|
|
|
|
|
}
|