summaryrefslogtreecommitdiffstats
path: root/wgpu/src/renderer/widget/slider.rs
blob: 386decb5d4154459aa6144e29768fe5325027a2d (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
use crate::{Primitive, Renderer};
use iced_native::{slider, Background, Color, MouseCursor, Point, Rectangle};

const HANDLE_WIDTH: f32 = 8.0;
const HANDLE_HEIGHT: f32 = 22.0;

impl slider::Renderer for Renderer {
    fn height(&self) -> u32 {
        30
    }

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

        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([0.6, 0.6, 0.6, 0.5].into()),
                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(Color::WHITE),
                border_radius: 0,
                border_width: 0,
                border_color: Color::TRANSPARENT,
            },
        );

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

        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(
                if is_dragging {
                    [0.85, 0.85, 0.85]
                } else if is_mouse_over {
                    [0.90, 0.90, 0.90]
                } else {
                    [0.95, 0.95, 0.95]
                }
                .into(),
            ),
            border_radius: 4,
            border_width: 1,
            border_color: Color::from_rgb(0.6, 0.6, 0.6),
        };

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