1use crate::core::border::{self, Border};
32use crate::core::keyboard;
33use crate::core::keyboard::key::{self, Key};
34use crate::core::layout;
35use crate::core::mouse;
36use crate::core::renderer;
37use crate::core::touch;
38use crate::core::widget::tree::{self, Tree};
39use crate::core::window;
40use crate::core::{
41 self, Background, Color, Element, Event, Layout, Length, Pixels, Point, Rectangle, Shell, Size,
42 Theme, Widget,
43};
44
45use std::ops::RangeInclusive;
46
47pub struct Slider<'a, T, Message, Theme = crate::Theme>
84where
85 Theme: Catalog,
86{
87 range: RangeInclusive<T>,
88 step: T,
89 shift_step: Option<T>,
90 value: T,
91 default: Option<T>,
92 on_change: Box<dyn Fn(T) -> Message + 'a>,
93 on_release: Option<Message>,
94 width: Length,
95 height: f32,
96 class: Theme::Class<'a>,
97 status: Option<Status>,
98}
99
100impl<'a, T, Message, Theme> Slider<'a, T, Message, Theme>
101where
102 T: Copy + From<u8> + PartialOrd,
103 Message: Clone,
104 Theme: Catalog,
105{
106 pub const DEFAULT_HEIGHT: f32 = 16.0;
108
109 pub fn new<F>(range: RangeInclusive<T>, value: T, on_change: F) -> Self
118 where
119 F: 'a + Fn(T) -> Message,
120 {
121 let value = if value >= *range.start() {
122 value
123 } else {
124 *range.start()
125 };
126
127 let value = if value <= *range.end() {
128 value
129 } else {
130 *range.end()
131 };
132
133 Slider {
134 value,
135 default: None,
136 range,
137 step: T::from(1),
138 shift_step: None,
139 on_change: Box::new(on_change),
140 on_release: None,
141 width: Length::Fill,
142 height: Self::DEFAULT_HEIGHT,
143 class: Theme::default(),
144 status: None,
145 }
146 }
147
148 pub fn default(mut self, default: impl Into<T>) -> Self {
152 self.default = Some(default.into());
153 self
154 }
155
156 pub fn on_release(mut self, on_release: Message) -> Self {
163 self.on_release = Some(on_release);
164 self
165 }
166
167 pub fn width(mut self, width: impl Into<Length>) -> Self {
169 self.width = width.into();
170 self
171 }
172
173 pub fn height(mut self, height: impl Into<Pixels>) -> Self {
175 self.height = height.into().0;
176 self
177 }
178
179 pub fn step(mut self, step: impl Into<T>) -> Self {
181 self.step = step.into();
182 self
183 }
184
185 pub fn shift_step(mut self, shift_step: impl Into<T>) -> Self {
189 self.shift_step = Some(shift_step.into());
190 self
191 }
192
193 #[must_use]
195 pub fn style(mut self, style: impl Fn(&Theme, Status) -> Style + 'a) -> Self
196 where
197 Theme::Class<'a>: From<StyleFn<'a, Theme>>,
198 {
199 self.class = (Box::new(style) as StyleFn<'a, Theme>).into();
200 self
201 }
202
203 #[cfg(feature = "advanced")]
205 #[must_use]
206 pub fn class(mut self, class: impl Into<Theme::Class<'a>>) -> Self {
207 self.class = class.into();
208 self
209 }
210}
211
212impl<T, Message, Theme, Renderer> Widget<Message, Theme, Renderer> for Slider<'_, T, Message, Theme>
213where
214 T: Copy + Into<f64> + num_traits::FromPrimitive,
215 Message: Clone,
216 Theme: Catalog,
217 Renderer: core::Renderer,
218{
219 fn tag(&self) -> tree::Tag {
220 tree::Tag::of::<State>()
221 }
222
223 fn state(&self) -> tree::State {
224 tree::State::new(State::default())
225 }
226
227 fn size(&self) -> Size<Length> {
228 Size {
229 width: self.width,
230 height: Length::Shrink,
231 }
232 }
233
234 fn layout(
235 &mut self,
236 _tree: &mut Tree,
237 _renderer: &Renderer,
238 limits: &layout::Limits,
239 ) -> layout::Node {
240 layout::atomic(limits, self.width, self.height)
241 }
242
243 fn update(
244 &mut self,
245 tree: &mut Tree,
246 event: &Event,
247 layout: Layout<'_>,
248 cursor: mouse::Cursor,
249 _renderer: &Renderer,
250 shell: &mut Shell<'_, Message>,
251 _viewport: &Rectangle,
252 ) {
253 let state = tree.state.downcast_mut::<State>();
254
255 let mut update = || {
256 let current_value = self.value;
257
258 let locate = |cursor_position: Point| -> Option<T> {
259 let bounds = layout.bounds();
260
261 if cursor_position.x <= bounds.x {
262 Some(*self.range.start())
263 } else if cursor_position.x >= bounds.x + bounds.width {
264 Some(*self.range.end())
265 } else {
266 let step = if state.keyboard_modifiers.shift() {
267 self.shift_step.unwrap_or(self.step)
268 } else {
269 self.step
270 }
271 .into();
272
273 let start = (*self.range.start()).into();
274 let end = (*self.range.end()).into();
275
276 let percent = f64::from(cursor_position.x - bounds.x) / f64::from(bounds.width);
277
278 let steps = (percent * (end - start) / step).round();
279 let value = steps * step + start;
280
281 T::from_f64(value.min(end))
282 }
283 };
284
285 let increment = |value: T| -> Option<T> {
286 let step = if state.keyboard_modifiers.shift() {
287 self.shift_step.unwrap_or(self.step)
288 } else {
289 self.step
290 }
291 .into();
292
293 let steps = (value.into() / step).round();
294 let new_value = step * (steps + 1.0);
295
296 if new_value > (*self.range.end()).into() {
297 return Some(*self.range.end());
298 }
299
300 T::from_f64(new_value)
301 };
302
303 let decrement = |value: T| -> Option<T> {
304 let step = if state.keyboard_modifiers.shift() {
305 self.shift_step.unwrap_or(self.step)
306 } else {
307 self.step
308 }
309 .into();
310
311 let steps = (value.into() / step).round();
312 let new_value = step * (steps - 1.0);
313
314 if new_value < (*self.range.start()).into() {
315 return Some(*self.range.start());
316 }
317
318 T::from_f64(new_value)
319 };
320
321 let change = |new_value: T| {
322 if (self.value.into() - new_value.into()).abs() > f64::EPSILON {
323 shell.publish((self.on_change)(new_value));
324
325 self.value = new_value;
326 }
327 };
328
329 match &event {
330 Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
331 | Event::Touch(touch::Event::FingerPressed { .. }) => {
332 if let Some(cursor_position) = cursor.position_over(layout.bounds()) {
333 if state.keyboard_modifiers.command() {
334 let _ = self.default.map(change);
335 state.is_dragging = false;
336 } else {
337 let _ = locate(cursor_position).map(change);
338 state.is_dragging = true;
339 }
340
341 shell.capture_event();
342 }
343 }
344 Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
345 | Event::Touch(touch::Event::FingerLifted { .. })
346 | Event::Touch(touch::Event::FingerLost { .. }) => {
347 if state.is_dragging {
348 if let Some(on_release) = self.on_release.clone() {
349 shell.publish(on_release);
350 }
351 state.is_dragging = false;
352 }
353 }
354 Event::Mouse(mouse::Event::CursorMoved { .. })
355 | Event::Touch(touch::Event::FingerMoved { .. }) => {
356 if state.is_dragging {
357 let _ = cursor.land().position().and_then(locate).map(change);
358
359 shell.capture_event();
360 }
361 }
362 Event::Mouse(mouse::Event::WheelScrolled { delta })
363 if state.keyboard_modifiers.control() =>
364 {
365 if cursor.is_over(layout.bounds()) {
366 let delta = match delta {
367 mouse::ScrollDelta::Lines { x: _, y } => y,
368 mouse::ScrollDelta::Pixels { x: _, y } => y,
369 };
370
371 if *delta < 0.0 {
372 let _ = decrement(current_value).map(change);
373 } else {
374 let _ = increment(current_value).map(change);
375 }
376
377 shell.capture_event();
378 }
379 }
380 Event::Keyboard(keyboard::Event::KeyPressed { key, .. }) => {
381 if cursor.is_over(layout.bounds()) {
382 match key {
383 Key::Named(key::Named::ArrowUp) => {
384 let _ = increment(current_value).map(change);
385 shell.capture_event();
386 }
387 Key::Named(key::Named::ArrowDown) => {
388 let _ = decrement(current_value).map(change);
389 shell.capture_event();
390 }
391 _ => (),
392 }
393 }
394 }
395 Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers)) => {
396 state.keyboard_modifiers = *modifiers;
397 }
398 _ => {}
399 }
400 };
401
402 update();
403
404 let current_status = if state.is_dragging {
405 Status::Dragged
406 } else if cursor.is_over(layout.bounds()) {
407 Status::Hovered
408 } else {
409 Status::Active
410 };
411
412 if let Event::Window(window::Event::RedrawRequested(_now)) = event {
413 self.status = Some(current_status);
414 } else if self.status.is_some_and(|status| status != current_status) {
415 shell.request_redraw();
416 }
417 }
418
419 fn draw(
420 &self,
421 _tree: &Tree,
422 renderer: &mut Renderer,
423 theme: &Theme,
424 _style: &renderer::Style,
425 layout: Layout<'_>,
426 _cursor: mouse::Cursor,
427 _viewport: &Rectangle,
428 ) {
429 let bounds = layout.bounds();
430
431 let style = theme.style(&self.class, self.status.unwrap_or(Status::Active));
432
433 let (handle_width, handle_height, handle_border_radius) = match style.handle.shape {
434 HandleShape::Circle { radius } => (radius * 2.0, radius * 2.0, radius.into()),
435 HandleShape::Rectangle {
436 width,
437 border_radius,
438 } => (f32::from(width), bounds.height, border_radius),
439 };
440
441 let value = self.value.into() as f32;
442 let (range_start, range_end) = {
443 let (start, end) = self.range.clone().into_inner();
444
445 (start.into() as f32, end.into() as f32)
446 };
447
448 let offset = if range_start >= range_end {
449 0.0
450 } else {
451 (bounds.width - handle_width) * (value - range_start) / (range_end - range_start)
452 };
453
454 let rail_y = bounds.y + bounds.height / 2.0;
455
456 renderer.fill_quad(
457 renderer::Quad {
458 bounds: Rectangle {
459 x: bounds.x,
460 y: rail_y - style.rail.width / 2.0,
461 width: offset + handle_width / 2.0,
462 height: style.rail.width,
463 },
464 border: style.rail.border,
465 ..renderer::Quad::default()
466 },
467 style.rail.backgrounds.0,
468 );
469
470 renderer.fill_quad(
471 renderer::Quad {
472 bounds: Rectangle {
473 x: bounds.x + offset + handle_width / 2.0,
474 y: rail_y - style.rail.width / 2.0,
475 width: bounds.width - offset - handle_width / 2.0,
476 height: style.rail.width,
477 },
478 border: style.rail.border,
479 ..renderer::Quad::default()
480 },
481 style.rail.backgrounds.1,
482 );
483
484 renderer.fill_quad(
485 renderer::Quad {
486 bounds: Rectangle {
487 x: bounds.x + offset,
488 y: rail_y - handle_height / 2.0,
489 width: handle_width,
490 height: handle_height,
491 },
492 border: Border {
493 radius: handle_border_radius,
494 width: style.handle.border_width,
495 color: style.handle.border_color,
496 },
497 ..renderer::Quad::default()
498 },
499 style.handle.background,
500 );
501 }
502
503 fn mouse_interaction(
504 &self,
505 tree: &Tree,
506 layout: Layout<'_>,
507 cursor: mouse::Cursor,
508 _viewport: &Rectangle,
509 _renderer: &Renderer,
510 ) -> mouse::Interaction {
511 let state = tree.state.downcast_ref::<State>();
512
513 if state.is_dragging {
514 if cfg!(target_os = "windows") {
517 mouse::Interaction::Pointer
518 } else {
519 mouse::Interaction::Grabbing
520 }
521 } else if cursor.is_over(layout.bounds()) {
522 if cfg!(target_os = "windows") {
523 mouse::Interaction::Pointer
524 } else {
525 mouse::Interaction::Grab
526 }
527 } else {
528 mouse::Interaction::default()
529 }
530 }
531}
532
533impl<'a, T, Message, Theme, Renderer> From<Slider<'a, T, Message, Theme>>
534 for Element<'a, Message, Theme, Renderer>
535where
536 T: Copy + Into<f64> + num_traits::FromPrimitive + 'a,
537 Message: Clone + 'a,
538 Theme: Catalog + 'a,
539 Renderer: core::Renderer + 'a,
540{
541 fn from(slider: Slider<'a, T, Message, Theme>) -> Element<'a, Message, Theme, Renderer> {
542 Element::new(slider)
543 }
544}
545
546#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
547struct State {
548 is_dragging: bool,
549 keyboard_modifiers: keyboard::Modifiers,
550}
551
552#[derive(Debug, Clone, Copy, PartialEq, Eq)]
554pub enum Status {
555 Active,
557 Hovered,
559 Dragged,
561}
562
563#[derive(Debug, Clone, Copy, PartialEq)]
565pub struct Style {
566 pub rail: Rail,
568 pub handle: Handle,
570}
571
572impl Style {
573 pub fn with_circular_handle(mut self, radius: impl Into<Pixels>) -> Self {
576 self.handle.shape = HandleShape::Circle {
577 radius: radius.into().0,
578 };
579 self
580 }
581}
582
583#[derive(Debug, Clone, Copy, PartialEq)]
585pub struct Rail {
586 pub backgrounds: (Background, Background),
588 pub width: f32,
590 pub border: Border,
592}
593
594#[derive(Debug, Clone, Copy, PartialEq)]
596pub struct Handle {
597 pub shape: HandleShape,
599 pub background: Background,
601 pub border_width: f32,
603 pub border_color: Color,
605}
606
607#[derive(Debug, Clone, Copy, PartialEq)]
609pub enum HandleShape {
610 Circle {
612 radius: f32,
614 },
615 Rectangle {
617 width: u16,
619 border_radius: border::Radius,
621 },
622}
623
624pub trait Catalog: Sized {
626 type Class<'a>;
628
629 fn default<'a>() -> Self::Class<'a>;
631
632 fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
634}
635
636pub type StyleFn<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Style + 'a>;
638
639impl Catalog for Theme {
640 type Class<'a> = StyleFn<'a, Self>;
641
642 fn default<'a>() -> Self::Class<'a> {
643 Box::new(default)
644 }
645
646 fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
647 class(self, status)
648 }
649}
650
651pub fn default(theme: &Theme, status: Status) -> Style {
653 let palette = theme.extended_palette();
654
655 let color = match status {
656 Status::Active => palette.primary.base.color,
657 Status::Hovered => palette.primary.strong.color,
658 Status::Dragged => palette.primary.weak.color,
659 };
660
661 Style {
662 rail: Rail {
663 backgrounds: (color.into(), palette.background.strong.color.into()),
664 width: 4.0,
665 border: Border {
666 radius: 2.0.into(),
667 width: 0.0,
668 color: Color::TRANSPARENT,
669 },
670 },
671 handle: Handle {
672 shape: HandleShape::Circle { radius: 7.0 },
673 background: color.into(),
674 border_color: Color::TRANSPARENT,
675 border_width: 0.0,
676 },
677 }
678}