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