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