iced_widget/
vertical_slider.rs

1//! Sliders let users set a value by moving an indicator.
2//!
3//! # Example
4//! ```no_run
5//! # mod iced { pub mod widget { pub use iced_widget::*; } pub use iced_widget::Renderer; pub use iced_widget::core::*; }
6//! # pub type Element<'a, Message> = iced_widget::core::Element<'a, Message, iced_widget::Theme, iced_widget::Renderer>;
7//! #
8//! use iced::widget::slider;
9//!
10//! struct State {
11//!    value: f32,
12//! }
13//!
14//! #[derive(Debug, Clone)]
15//! enum Message {
16//!     ValueChanged(f32),
17//! }
18//!
19//! fn view(state: &State) -> Element<'_, Message> {
20//!     slider(0.0..=100.0, state.value, Message::ValueChanged).into()
21//! }
22//!
23//! fn update(state: &mut State, message: Message) {
24//!     match message {
25//!         Message::ValueChanged(value) => {
26//!             state.value = value;
27//!         }
28//!     }
29//! }
30//! ```
31use std::ops::RangeInclusive;
32
33pub use crate::slider::{
34    Catalog, Handle, HandleShape, Status, Style, StyleFn, default,
35};
36
37use crate::core::border::Border;
38use crate::core::keyboard;
39use crate::core::keyboard::key::{self, Key};
40use crate::core::layout::{self, Layout};
41use crate::core::mouse;
42use crate::core::renderer;
43use crate::core::touch;
44use crate::core::widget::tree::{self, Tree};
45use crate::core::window;
46use crate::core::{
47    self, Clipboard, Element, Event, Length, Pixels, Point, Rectangle, Shell,
48    Size, Widget,
49};
50
51/// An vertical bar and a handle that selects a single value from a range of
52/// values.
53///
54/// A [`VerticalSlider`] will try to fill the vertical space of its container.
55///
56/// The [`VerticalSlider`] range of numeric values is generic and its step size defaults
57/// to 1 unit.
58///
59/// # Example
60/// ```no_run
61/// # mod iced { pub mod widget { pub use iced_widget::*; } pub use iced_widget::Renderer; pub use iced_widget::core::*; }
62/// # pub type Element<'a, Message> = iced_widget::core::Element<'a, Message, iced_widget::Theme, iced_widget::Renderer>;
63/// #
64/// use iced::widget::vertical_slider;
65///
66/// struct State {
67///    value: f32,
68/// }
69///
70/// #[derive(Debug, Clone)]
71/// enum Message {
72///     ValueChanged(f32),
73/// }
74///
75/// fn view(state: &State) -> Element<'_, Message> {
76///     vertical_slider(0.0..=100.0, state.value, Message::ValueChanged).into()
77/// }
78///
79/// fn update(state: &mut State, message: Message) {
80///     match message {
81///         Message::ValueChanged(value) => {
82///             state.value = value;
83///         }
84///     }
85/// }
86/// ```
87pub struct VerticalSlider<'a, T, Message, Theme = crate::Theme>
88where
89    Theme: Catalog,
90{
91    range: RangeInclusive<T>,
92    step: T,
93    shift_step: Option<T>,
94    value: T,
95    default: Option<T>,
96    on_change: Box<dyn Fn(T) -> Message + 'a>,
97    on_release: Option<Message>,
98    width: f32,
99    height: Length,
100    class: Theme::Class<'a>,
101    status: Option<Status>,
102}
103
104impl<'a, T, Message, Theme> VerticalSlider<'a, T, Message, Theme>
105where
106    T: Copy + From<u8> + std::cmp::PartialOrd,
107    Message: Clone,
108    Theme: Catalog,
109{
110    /// The default width of a [`VerticalSlider`].
111    pub const DEFAULT_WIDTH: f32 = 16.0;
112
113    /// Creates a new [`VerticalSlider`].
114    ///
115    /// It expects:
116    ///   * an inclusive range of possible values
117    ///   * the current value of the [`VerticalSlider`]
118    ///   * a function that will be called when the [`VerticalSlider`] is dragged.
119    ///     It receives the new value of the [`VerticalSlider`] and must produce a
120    ///     `Message`.
121    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        VerticalSlider {
138            value,
139            default: None,
140            range,
141            step: T::from(1),
142            shift_step: None,
143            on_change: Box::new(on_change),
144            on_release: None,
145            width: Self::DEFAULT_WIDTH,
146            height: Length::Fill,
147            class: Theme::default(),
148            status: None,
149        }
150    }
151
152    /// Sets the optional default value for the [`VerticalSlider`].
153    ///
154    /// If set, the [`VerticalSlider`] will reset to this value when ctrl-clicked or command-clicked.
155    pub fn default(mut self, default: impl Into<T>) -> Self {
156        self.default = Some(default.into());
157        self
158    }
159
160    /// Sets the release message of the [`VerticalSlider`].
161    /// This is called when the mouse is released from the slider.
162    ///
163    /// Typically, the user's interaction with the slider is finished when this message is produced.
164    /// This is useful if you need to spawn a long-running task from the slider's result, where
165    /// the default on_change message could create too many events.
166    pub fn on_release(mut self, on_release: Message) -> Self {
167        self.on_release = Some(on_release);
168        self
169    }
170
171    /// Sets the width of the [`VerticalSlider`].
172    pub fn width(mut self, width: impl Into<Pixels>) -> Self {
173        self.width = width.into().0;
174        self
175    }
176
177    /// Sets the height of the [`VerticalSlider`].
178    pub fn height(mut self, height: impl Into<Length>) -> Self {
179        self.height = height.into();
180        self
181    }
182
183    /// Sets the step size of the [`VerticalSlider`].
184    pub fn step(mut self, step: T) -> Self {
185        self.step = step;
186        self
187    }
188
189    /// Sets the optional "shift" step for the [`VerticalSlider`].
190    ///
191    /// If set, this value is used as the step while the shift key is pressed.
192    pub fn shift_step(mut self, shift_step: impl Into<T>) -> Self {
193        self.shift_step = Some(shift_step.into());
194        self
195    }
196
197    /// Sets the style of the [`VerticalSlider`].
198    #[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    /// Sets the style class of the [`VerticalSlider`].
208    #[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>
217    for VerticalSlider<'_, T, Message, Theme>
218where
219    T: Copy + Into<f64> + num_traits::FromPrimitive,
220    Message: Clone,
221    Theme: Catalog,
222    Renderer: core::Renderer,
223{
224    fn tag(&self) -> tree::Tag {
225        tree::Tag::of::<State>()
226    }
227
228    fn state(&self) -> tree::State {
229        tree::State::new(State::default())
230    }
231
232    fn size(&self) -> Size<Length> {
233        Size {
234            width: Length::Shrink,
235            height: self.height,
236        }
237    }
238
239    fn layout(
240        &mut self,
241        _tree: &mut Tree,
242        _renderer: &Renderer,
243        limits: &layout::Limits,
244    ) -> layout::Node {
245        layout::atomic(limits, self.width, self.height)
246    }
247
248    fn update(
249        &mut self,
250        tree: &mut Tree,
251        event: &Event,
252        layout: Layout<'_>,
253        cursor: mouse::Cursor,
254        _renderer: &Renderer,
255        _clipboard: &mut dyn Clipboard,
256        shell: &mut Shell<'_, Message>,
257        _viewport: &Rectangle,
258    ) {
259        let state = tree.state.downcast_mut::<State>();
260        let is_dragging = state.is_dragging;
261        let current_value = self.value;
262
263        let locate = |cursor_position: Point| -> Option<T> {
264            let bounds = layout.bounds();
265
266            if cursor_position.y >= bounds.y + bounds.height {
267                Some(*self.range.start())
268            } else if cursor_position.y <= bounds.y {
269                Some(*self.range.end())
270            } else {
271                let step = if state.keyboard_modifiers.shift() {
272                    self.shift_step.unwrap_or(self.step)
273                } else {
274                    self.step
275                }
276                .into();
277
278                let start = (*self.range.start()).into();
279                let end = (*self.range.end()).into();
280
281                let percent = 1.0
282                    - f64::from(cursor_position.y - bounds.y)
283                        / f64::from(bounds.height);
284
285                let steps = (percent * (end - start) / step).round();
286                let value = steps * step + start;
287
288                T::from_f64(value.min(end))
289            }
290        };
291
292        let increment = |value: T| -> Option<T> {
293            let step = if state.keyboard_modifiers.shift() {
294                self.shift_step.unwrap_or(self.step)
295            } else {
296                self.step
297            }
298            .into();
299
300            let steps = (value.into() / step).round();
301            let new_value = step * (steps + 1.0);
302
303            if new_value > (*self.range.end()).into() {
304                return Some(*self.range.end());
305            }
306
307            T::from_f64(new_value)
308        };
309
310        let decrement = |value: T| -> Option<T> {
311            let step = if state.keyboard_modifiers.shift() {
312                self.shift_step.unwrap_or(self.step)
313            } else {
314                self.step
315            }
316            .into();
317
318            let steps = (value.into() / step).round();
319            let new_value = step * (steps - 1.0);
320
321            if new_value < (*self.range.start()).into() {
322                return Some(*self.range.start());
323            }
324
325            T::from_f64(new_value)
326        };
327
328        let change = |new_value: T| {
329            if (self.value.into() - new_value.into()).abs() > f64::EPSILON {
330                shell.publish((self.on_change)(new_value));
331
332                self.value = new_value;
333            }
334        };
335
336        match event {
337            Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
338            | Event::Touch(touch::Event::FingerPressed { .. }) => {
339                if let Some(cursor_position) =
340                    cursor.position_over(layout.bounds())
341                {
342                    if state.keyboard_modifiers.control()
343                        || state.keyboard_modifiers.command()
344                    {
345                        let _ = self.default.map(change);
346                        state.is_dragging = false;
347                    } else {
348                        let _ = locate(cursor_position).map(change);
349                        state.is_dragging = true;
350                    }
351
352                    shell.capture_event();
353                }
354            }
355            Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
356            | Event::Touch(touch::Event::FingerLifted { .. })
357            | Event::Touch(touch::Event::FingerLost { .. }) => {
358                if is_dragging {
359                    if let Some(on_release) = self.on_release.clone() {
360                        shell.publish(on_release);
361                    }
362                    state.is_dragging = false;
363
364                    shell.capture_event();
365                }
366            }
367            Event::Mouse(mouse::Event::CursorMoved { .. })
368            | Event::Touch(touch::Event::FingerMoved { .. }) => {
369                if is_dragging {
370                    let _ = cursor.position().and_then(locate).map(change);
371
372                    shell.capture_event();
373                }
374            }
375            Event::Mouse(mouse::Event::WheelScrolled { delta })
376                if state.keyboard_modifiers.control() =>
377            {
378                if cursor.is_over(layout.bounds()) {
379                    let delta = match *delta {
380                        mouse::ScrollDelta::Lines { x: _, y } => y,
381                        mouse::ScrollDelta::Pixels { x: _, y } => y,
382                    };
383
384                    if delta < 0.0 {
385                        let _ = decrement(current_value).map(change);
386                    } else {
387                        let _ = increment(current_value).map(change);
388                    }
389
390                    shell.capture_event();
391                }
392            }
393            Event::Keyboard(keyboard::Event::KeyPressed { key, .. }) => {
394                if cursor.is_over(layout.bounds()) {
395                    match key {
396                        Key::Named(key::Named::ArrowUp) => {
397                            let _ = increment(current_value).map(change);
398                        }
399                        Key::Named(key::Named::ArrowDown) => {
400                            let _ = decrement(current_value).map(change);
401                        }
402                        _ => (),
403                    }
404
405                    shell.capture_event();
406                }
407            }
408            Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers)) => {
409                state.keyboard_modifiers = *modifiers;
410            }
411            _ => {}
412        }
413
414        let current_status = if state.is_dragging {
415            Status::Dragged
416        } else if cursor.is_over(layout.bounds()) {
417            Status::Hovered
418        } else {
419            Status::Active
420        };
421
422        if let Event::Window(window::Event::RedrawRequested(_now)) = event {
423            self.status = Some(current_status);
424        } else if self.status.is_some_and(|status| status != current_status) {
425            shell.request_redraw();
426        }
427    }
428
429    fn draw(
430        &self,
431        _tree: &Tree,
432        renderer: &mut Renderer,
433        theme: &Theme,
434        _style: &renderer::Style,
435        layout: Layout<'_>,
436        _cursor: mouse::Cursor,
437        _viewport: &Rectangle,
438    ) {
439        let bounds = layout.bounds();
440
441        let style =
442            theme.style(&self.class, self.status.unwrap_or(Status::Active));
443
444        let (handle_width, handle_height, handle_border_radius) =
445            match style.handle.shape {
446                HandleShape::Circle { radius } => {
447                    (radius * 2.0, radius * 2.0, radius.into())
448                }
449                HandleShape::Rectangle {
450                    width,
451                    border_radius,
452                } => (f32::from(width), bounds.width, border_radius),
453            };
454
455        let value = self.value.into() as f32;
456        let (range_start, range_end) = {
457            let (start, end) = self.range.clone().into_inner();
458
459            (start.into() as f32, end.into() as f32)
460        };
461
462        let offset = if range_start >= range_end {
463            0.0
464        } else {
465            (bounds.height - handle_width) * (value - range_end)
466                / (range_start - range_end)
467        };
468
469        let rail_x = bounds.x + bounds.width / 2.0;
470
471        renderer.fill_quad(
472            renderer::Quad {
473                bounds: Rectangle {
474                    x: rail_x - style.rail.width / 2.0,
475                    y: bounds.y,
476                    width: style.rail.width,
477                    height: offset + handle_width / 2.0,
478                },
479                border: style.rail.border,
480                ..renderer::Quad::default()
481            },
482            style.rail.backgrounds.1,
483        );
484
485        renderer.fill_quad(
486            renderer::Quad {
487                bounds: Rectangle {
488                    x: rail_x - style.rail.width / 2.0,
489                    y: bounds.y + offset + handle_width / 2.0,
490                    width: style.rail.width,
491                    height: bounds.height - offset - handle_width / 2.0,
492                },
493                border: style.rail.border,
494                ..renderer::Quad::default()
495            },
496            style.rail.backgrounds.0,
497        );
498
499        renderer.fill_quad(
500            renderer::Quad {
501                bounds: Rectangle {
502                    x: rail_x - handle_height / 2.0,
503                    y: bounds.y + offset,
504                    width: handle_height,
505                    height: handle_width,
506                },
507                border: Border {
508                    radius: handle_border_radius,
509                    width: style.handle.border_width,
510                    color: style.handle.border_color,
511                },
512                ..renderer::Quad::default()
513            },
514            style.handle.background,
515        );
516    }
517
518    fn mouse_interaction(
519        &self,
520        tree: &Tree,
521        layout: Layout<'_>,
522        cursor: mouse::Cursor,
523        _viewport: &Rectangle,
524        _renderer: &Renderer,
525    ) -> mouse::Interaction {
526        let state = tree.state.downcast_ref::<State>();
527        let bounds = layout.bounds();
528        let is_mouse_over = cursor.is_over(bounds);
529
530        if state.is_dragging {
531            mouse::Interaction::Grabbing
532        } else if is_mouse_over {
533            mouse::Interaction::Grab
534        } else {
535            mouse::Interaction::default()
536        }
537    }
538}
539
540impl<'a, T, Message, Theme, Renderer>
541    From<VerticalSlider<'a, T, Message, Theme>>
542    for Element<'a, Message, Theme, Renderer>
543where
544    T: Copy + Into<f64> + num_traits::FromPrimitive + 'a,
545    Message: Clone + 'a,
546    Theme: Catalog + 'a,
547    Renderer: core::Renderer + 'a,
548{
549    fn from(
550        slider: VerticalSlider<'a, T, Message, Theme>,
551    ) -> Element<'a, Message, Theme, Renderer> {
552        Element::new(slider)
553    }
554}
555
556#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
557struct State {
558    is_dragging: bool,
559    keyboard_modifiers: keyboard::Modifiers,
560}