1use 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#[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 pub const DEFAULT_WIDTH: f32 = 16.0;
113
114 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 pub fn default(mut self, default: impl Into<T>) -> Self {
157 self.default = Some(default.into());
158 self
159 }
160
161 pub fn on_release(mut self, on_release: Message) -> Self {
168 self.on_release = Some(on_release);
169 self
170 }
171
172 pub fn width(mut self, width: impl Into<Pixels>) -> Self {
174 self.width = width.into().0;
175 self
176 }
177
178 pub fn height(mut self, height: impl Into<Length>) -> Self {
180 self.height = height.into();
181 self
182 }
183
184 pub fn step(mut self, step: T) -> Self {
186 self.step = step;
187 self
188 }
189
190 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 #[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 #[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 let new_value = 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 new_value
293 };
294
295 let increment = |value: T| -> Option<T> {
296 let step = if state.keyboard_modifiers.shift() {
297 self.shift_step.unwrap_or(self.step)
298 } else {
299 self.step
300 }
301 .into();
302
303 let steps = (value.into() / step).round();
304 let new_value = step * (steps + 1.0);
305
306 if new_value > (*self.range.end()).into() {
307 return Some(*self.range.end());
308 }
309
310 T::from_f64(new_value)
311 };
312
313 let decrement = |value: T| -> Option<T> {
314 let step = if state.keyboard_modifiers.shift() {
315 self.shift_step.unwrap_or(self.step)
316 } else {
317 self.step
318 }
319 .into();
320
321 let steps = (value.into() / step).round();
322 let new_value = step * (steps - 1.0);
323
324 if new_value < (*self.range.start()).into() {
325 return Some(*self.range.start());
326 }
327
328 T::from_f64(new_value)
329 };
330
331 let change = |new_value: T| {
332 if (self.value.into() - new_value.into()).abs() > f64::EPSILON {
333 shell.publish((self.on_change)(new_value));
334
335 self.value = new_value;
336 }
337 };
338
339 match event {
340 Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
341 | Event::Touch(touch::Event::FingerPressed { .. }) => {
342 if let Some(cursor_position) =
343 cursor.position_over(layout.bounds())
344 {
345 if state.keyboard_modifiers.control()
346 || state.keyboard_modifiers.command()
347 {
348 let _ = self.default.map(change);
349 state.is_dragging = false;
350 } else {
351 let _ = locate(cursor_position).map(change);
352 state.is_dragging = true;
353 }
354
355 shell.capture_event();
356 }
357 }
358 Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
359 | Event::Touch(touch::Event::FingerLifted { .. })
360 | Event::Touch(touch::Event::FingerLost { .. }) => {
361 if is_dragging {
362 if let Some(on_release) = self.on_release.clone() {
363 shell.publish(on_release);
364 }
365 state.is_dragging = false;
366
367 shell.capture_event();
368 }
369 }
370 Event::Mouse(mouse::Event::CursorMoved { .. })
371 | Event::Touch(touch::Event::FingerMoved { .. }) => {
372 if is_dragging {
373 let _ = cursor.position().and_then(locate).map(change);
374
375 shell.capture_event();
376 }
377 }
378 Event::Mouse(mouse::Event::WheelScrolled { delta })
379 if state.keyboard_modifiers.control() =>
380 {
381 if cursor.is_over(layout.bounds()) {
382 let delta = match *delta {
383 mouse::ScrollDelta::Lines { x: _, y } => y,
384 mouse::ScrollDelta::Pixels { x: _, y } => y,
385 };
386
387 if delta < 0.0 {
388 let _ = decrement(current_value).map(change);
389 } else {
390 let _ = increment(current_value).map(change);
391 }
392
393 shell.capture_event();
394 }
395 }
396 Event::Keyboard(keyboard::Event::KeyPressed { key, .. }) => {
397 if cursor.is_over(layout.bounds()) {
398 match key {
399 Key::Named(key::Named::ArrowUp) => {
400 let _ = increment(current_value).map(change);
401 }
402 Key::Named(key::Named::ArrowDown) => {
403 let _ = decrement(current_value).map(change);
404 }
405 _ => (),
406 }
407
408 shell.capture_event();
409 }
410 }
411 Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers)) => {
412 state.keyboard_modifiers = *modifiers;
413 }
414 _ => {}
415 }
416
417 let current_status = if state.is_dragging {
418 Status::Dragged
419 } else if cursor.is_over(layout.bounds()) {
420 Status::Hovered
421 } else {
422 Status::Active
423 };
424
425 if let Event::Window(window::Event::RedrawRequested(_now)) = event {
426 self.status = Some(current_status);
427 } else if self.status.is_some_and(|status| status != current_status) {
428 shell.request_redraw();
429 }
430 }
431
432 fn draw(
433 &self,
434 _tree: &Tree,
435 renderer: &mut Renderer,
436 theme: &Theme,
437 _style: &renderer::Style,
438 layout: Layout<'_>,
439 _cursor: mouse::Cursor,
440 _viewport: &Rectangle,
441 ) {
442 let bounds = layout.bounds();
443
444 let style =
445 theme.style(&self.class, self.status.unwrap_or(Status::Active));
446
447 let (handle_width, handle_height, handle_border_radius) =
448 match style.handle.shape {
449 HandleShape::Circle { radius } => {
450 (radius * 2.0, radius * 2.0, radius.into())
451 }
452 HandleShape::Rectangle {
453 width,
454 border_radius,
455 } => (f32::from(width), bounds.width, border_radius),
456 };
457
458 let value = self.value.into() as f32;
459 let (range_start, range_end) = {
460 let (start, end) = self.range.clone().into_inner();
461
462 (start.into() as f32, end.into() as f32)
463 };
464
465 let offset = if range_start >= range_end {
466 0.0
467 } else {
468 (bounds.height - handle_width) * (value - range_end)
469 / (range_start - range_end)
470 };
471
472 let rail_x = bounds.x + bounds.width / 2.0;
473
474 renderer.fill_quad(
475 renderer::Quad {
476 bounds: Rectangle {
477 x: rail_x - style.rail.width / 2.0,
478 y: bounds.y,
479 width: style.rail.width,
480 height: offset + handle_width / 2.0,
481 },
482 border: style.rail.border,
483 ..renderer::Quad::default()
484 },
485 style.rail.backgrounds.1,
486 );
487
488 renderer.fill_quad(
489 renderer::Quad {
490 bounds: Rectangle {
491 x: rail_x - style.rail.width / 2.0,
492 y: bounds.y + offset + handle_width / 2.0,
493 width: style.rail.width,
494 height: bounds.height - offset - handle_width / 2.0,
495 },
496 border: style.rail.border,
497 ..renderer::Quad::default()
498 },
499 style.rail.backgrounds.0,
500 );
501
502 renderer.fill_quad(
503 renderer::Quad {
504 bounds: Rectangle {
505 x: rail_x - handle_height / 2.0,
506 y: bounds.y + offset,
507 width: handle_height,
508 height: handle_width,
509 },
510 border: Border {
511 radius: handle_border_radius,
512 width: style.handle.border_width,
513 color: style.handle.border_color,
514 },
515 ..renderer::Quad::default()
516 },
517 style.handle.background,
518 );
519 }
520
521 fn mouse_interaction(
522 &self,
523 tree: &Tree,
524 layout: Layout<'_>,
525 cursor: mouse::Cursor,
526 _viewport: &Rectangle,
527 _renderer: &Renderer,
528 ) -> mouse::Interaction {
529 let state = tree.state.downcast_ref::<State>();
530 let bounds = layout.bounds();
531 let is_mouse_over = cursor.is_over(bounds);
532
533 if state.is_dragging {
534 mouse::Interaction::Grabbing
535 } else if is_mouse_over {
536 mouse::Interaction::Grab
537 } else {
538 mouse::Interaction::default()
539 }
540 }
541}
542
543impl<'a, T, Message, Theme, Renderer>
544 From<VerticalSlider<'a, T, Message, Theme>>
545 for Element<'a, Message, Theme, Renderer>
546where
547 T: Copy + Into<f64> + num_traits::FromPrimitive + 'a,
548 Message: Clone + 'a,
549 Theme: Catalog + 'a,
550 Renderer: core::Renderer + 'a,
551{
552 fn from(
553 slider: VerticalSlider<'a, T, Message, Theme>,
554 ) -> Element<'a, Message, Theme, Renderer> {
555 Element::new(slider)
556 }
557}
558
559#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
560struct State {
561 is_dragging: bool,
562 keyboard_modifiers: keyboard::Modifiers,
563}