summaryrefslogtreecommitdiffstats
path: root/wgpu/src/renderer/widget/slider.rs
blob: 220feace77cafee0f4ccb38c3a3e86e9a5423895 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use crate::{
    slider::{HandleShape, StyleSheet},
    Primitive, Renderer,
};
use iced_native::{mouse, slider, Background, Color, Point, Rectangle};

const HANDLE_HEIGHT: f32 = 22.0;

impl slider::Renderer for Renderer {
    type Style = Box<dyn StyleSheet>;

    fn height(&self) -> u32 {
        30
    }

    fn draw(
        &mut self,
        bounds: Rectangle,
        cursor_position: Point,
        range: std::ops::RangeInclusive<f32>,
        value: f32,
        is_dragging: bool,
        style_sheet: &Self::Style,
    ) -> Self::Output {
        let is_mouse_over = bounds.contains(cursor_position);

        let style = if is_dragging {
            style_sheet.dragging()
        } else if is_mouse_over {
            style_sheet.hovered()
        } else {
            style_sheet.active()
        };

        let rail_y = bounds.y + (bounds.height / 2.0).round();

        let (rail_top, rail_bottom) = (
            Primitive::Quad {
                bounds: Rectangle {
                    x: bounds.x,
                    y: rail_y,
                    width: bounds.width,
                    height: 2.0,
                },
                background: Background::Color(style.rail_colors.0),
                border_radius: 0,
                border_width: 0,
                border_color: Color::TRANSPARENT,
            },
            Primitive::Quad {
                bounds: Rectangle {
                    x: bounds.x,
                    y: rail_y + 2.0,
                    width: bounds.width,
                    height: 2.0,
                },
                background: Background::Color(style.rail_colors.1),
                border_radius: 0,
                border_width: 0,
                border_color: Color::TRANSPARENT,
            },
        );

        let (range_start, range_end) = range.into_inner();

        let (handle_width, handle_height, handle_border_radius) =
            match style.handle.shape {
                HandleShape::Circle { radius } => {
                    (f32::from(radius * 2), f32::from(radius * 2), radius)
                }
                HandleShape::Rectangle {
                    width,
                    border_radius,
                } => (f32::from(width), HANDLE_HEIGHT, border_radius),
            };

        let handle_offset = (bounds.width - handle_width)
            * ((value - range_start) / (range_end - range_start).max(1.0));

        let handle = Primitive::Quad {
            bounds: Rectangle {
                x: bounds.x + handle_offset.round(),
                y: rail_y - handle_height / 2.0,
                width: handle_width,
                height: handle_height,
            },
            background: Background::Color(style.handle.color),
            border_radius: handle_border_radius,
            border_width: style.handle.border_width,
            border_color: style.handle.border_color,
        };

        (
            Primitive::Group {
                primitives: vec![rail_top, rail_bottom, handle],
            },
            if is_dragging {
                mouse::Interaction::Grabbing
            } else if is_mouse_over {
                mouse::Interaction::Grab
            } else {
                mouse::Interaction::default()
            },
        )
    }
}