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            }
365            Event::Mouse(mouse::Event::CursorMoved { .. })
366            | Event::Touch(touch::Event::FingerMoved { .. }) => {
367                if is_dragging {
368                    let _ =
369                        cursor.land().position().and_then(locate).map(change);
370
371                    shell.capture_event();
372                }
373            }
374            Event::Mouse(mouse::Event::WheelScrolled { delta })
375                if state.keyboard_modifiers.control() =>
376            {
377                if cursor.is_over(layout.bounds()) {
378                    let delta = match *delta {
379                        mouse::ScrollDelta::Lines { x: _, y } => y,
380                        mouse::ScrollDelta::Pixels { x: _, y } => y,
381                    };
382
383                    if delta < 0.0 {
384                        let _ = decrement(current_value).map(change);
385                    } else {
386                        let _ = increment(current_value).map(change);
387                    }
388
389                    shell.capture_event();
390                }
391            }
392            Event::Keyboard(keyboard::Event::KeyPressed { key, .. }) => {
393                if cursor.is_over(layout.bounds()) {
394                    match key {
395                        Key::Named(key::Named::ArrowUp) => {
396                            let _ = increment(current_value).map(change);
397                            shell.capture_event();
398                        }
399                        Key::Named(key::Named::ArrowDown) => {
400                            let _ = decrement(current_value).map(change);
401                            shell.capture_event();
402                        }
403                        _ => (),
404                    }
405                }
406            }
407            Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers)) => {
408                state.keyboard_modifiers = *modifiers;
409            }
410            _ => {}
411        }
412
413        let current_status = if state.is_dragging {
414            Status::Dragged
415        } else if cursor.is_over(layout.bounds()) {
416            Status::Hovered
417        } else {
418            Status::Active
419        };
420
421        if let Event::Window(window::Event::RedrawRequested(_now)) = event {
422            self.status = Some(current_status);
423        } else if self.status.is_some_and(|status| status != current_status) {
424            shell.request_redraw();
425        }
426    }
427
428    fn draw(
429        &self,
430        _tree: &Tree,
431        renderer: &mut Renderer,
432        theme: &Theme,
433        _style: &renderer::Style,
434        layout: Layout<'_>,
435        _cursor: mouse::Cursor,
436        _viewport: &Rectangle,
437    ) {
438        let bounds = layout.bounds();
439
440        let style =
441            theme.style(&self.class, self.status.unwrap_or(Status::Active));
442
443        let (handle_width, handle_height, handle_border_radius) =
444            match style.handle.shape {
445                HandleShape::Circle { radius } => {
446                    (radius * 2.0, radius * 2.0, radius.into())
447                }
448                HandleShape::Rectangle {
449                    width,
450                    border_radius,
451                } => (f32::from(width), bounds.width, border_radius),
452            };
453
454        let value = self.value.into() as f32;
455        let (range_start, range_end) = {
456            let (start, end) = self.range.clone().into_inner();
457
458            (start.into() as f32, end.into() as f32)
459        };
460
461        let offset = if range_start >= range_end {
462            0.0
463        } else {
464            (bounds.height - handle_width) * (value - range_end)
465                / (range_start - range_end)
466        };
467
468        let rail_x = bounds.x + bounds.width / 2.0;
469
470        renderer.fill_quad(
471            renderer::Quad {
472                bounds: Rectangle {
473                    x: rail_x - style.rail.width / 2.0,
474                    y: bounds.y,
475                    width: style.rail.width,
476                    height: offset + handle_width / 2.0,
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: rail_x - style.rail.width / 2.0,
488                    y: bounds.y + offset + handle_width / 2.0,
489                    width: style.rail.width,
490                    height: bounds.height - offset - handle_width / 2.0,
491                },
492                border: style.rail.border,
493                ..renderer::Quad::default()
494            },
495            style.rail.backgrounds.0,
496        );
497
498        renderer.fill_quad(
499            renderer::Quad {
500                bounds: Rectangle {
501                    x: rail_x - handle_height / 2.0,
502                    y: bounds.y + offset,
503                    width: handle_height,
504                    height: handle_width,
505                },
506                border: Border {
507                    radius: handle_border_radius,
508                    width: style.handle.border_width,
509                    color: style.handle.border_color,
510                },
511                ..renderer::Quad::default()
512            },
513            style.handle.background,
514        );
515    }
516
517    fn mouse_interaction(
518        &self,
519        tree: &Tree,
520        layout: Layout<'_>,
521        cursor: mouse::Cursor,
522        _viewport: &Rectangle,
523        _renderer: &Renderer,
524    ) -> mouse::Interaction {
525        let state = tree.state.downcast_ref::<State>();
526
527        if state.is_dragging {
528            mouse::Interaction::Grabbing
529        } else if cursor.is_over(layout.bounds()) {
530            mouse::Interaction::Grab
531        } else {
532            mouse::Interaction::default()
533        }
534    }
535}
536
537impl<'a, T, Message, Theme, Renderer>
538    From<VerticalSlider<'a, T, Message, Theme>>
539    for Element<'a, Message, Theme, Renderer>
540where
541    T: Copy + Into<f64> + num_traits::FromPrimitive + 'a,
542    Message: Clone + 'a,
543    Theme: Catalog + 'a,
544    Renderer: core::Renderer + 'a,
545{
546    fn from(
547        slider: VerticalSlider<'a, T, Message, Theme>,
548    ) -> Element<'a, Message, Theme, Renderer> {
549        Element::new(slider)
550    }
551}
552
553#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
554struct State {
555    is_dragging: bool,
556    keyboard_modifiers: keyboard::Modifiers,
557}