1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use crate::{Bus, Css, Element, Length, Widget};
pub use iced_style::slider::{Handle, HandleShape, Style, StyleSheet};
use dodrio::bumpalo;
use std::{ops::RangeInclusive, rc::Rc};
#[allow(missing_debug_implementations)]
pub struct Slider<'a, T, Message> {
_state: &'a mut State,
range: RangeInclusive<T>,
step: T,
value: T,
on_change: Rc<Box<dyn Fn(T) -> Message>>,
width: Length,
style: Box<dyn StyleSheet>,
}
impl<'a, T, Message> Slider<'a, T, Message>
where
T: Copy + From<u8> + std::cmp::PartialOrd,
{
pub fn new<F>(
state: &'a mut State,
range: RangeInclusive<T>,
value: T,
on_change: F,
) -> Self
where
F: 'static + Fn(T) -> Message,
{
let value = if value >= *range.start() {
value
} else {
*range.start()
};
let value = if value <= *range.end() {
value
} else {
*range.end()
};
Slider {
_state: state,
value,
range,
step: T::from(1),
on_change: Rc::new(Box::new(on_change)),
width: Length::Fill,
style: Default::default(),
}
}
pub fn width(mut self, width: Length) -> Self {
self.width = width;
self
}
pub fn style(mut self, style: impl Into<Box<dyn StyleSheet>>) -> Self {
self.style = style.into();
self
}
pub fn step(mut self, step: T) -> Self {
self.step = step;
self
}
}
impl<'a, T, Message> Widget<Message> for Slider<'a, T, Message>
where
T: 'static + Copy + Into<f64> + num_traits::FromPrimitive,
Message: 'static,
{
fn node<'b>(
&self,
bump: &'b bumpalo::Bump,
bus: &Bus<Message>,
_style_sheet: &mut Css<'b>,
) -> 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.into());
let max = bumpalo::format!(in bump, "{}", end.into());
let value = bumpalo::format!(in bump, "{}", self.value.into());
let step = bumpalo::format!(in bump, "{}", self.step.into());
let on_change = self.on_change.clone();
let event_bus = bus.clone();
input(bump)
.attr("type", "range")
.attr("step", step.into_bump_str())
.attr("min", min.into_bump_str())
.attr("max", max.into_bump_str())
.attr("value", value.into_bump_str())
.attr("style", "width: 100%")
.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::<f64>() {
if let Some(value) = T::from_f64(value) {
event_bus.publish(on_change(value));
}
}
})
.finish()
}
}
impl<'a, T, Message> From<Slider<'a, T, Message>> for Element<'a, Message>
where
T: 'static + Copy + Into<f64> + num_traits::FromPrimitive,
Message: 'static,
{
fn from(slider: Slider<'a, T, Message>) -> Element<'a, Message> {
Element::new(slider)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct State;
impl State {
pub fn new() -> Self {
Self
}
}