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

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

    fn draw(
        &mut self,
        bounds: Rectangle,
        cursor_position: Point,
        is_pressed: bool,
        style: &Box<dyn StyleSheet>,
        (content, _): Self::Output,
    ) -> Self::Output {
        let is_mouse_over = bounds.contains(cursor_position);

        // TODO: Render proper shadows
        let styling = if is_mouse_over {
            if is_pressed {
                style.pressed()
            } else {
                style.hovered()
            }
        } else {
            style.active()
        };

        (
            match styling.background {
                None => content,
                Some(background) => Primitive::Group {
                    primitives: vec![
                        Primitive::Quad {
                            bounds: Rectangle {
                                x: bounds.x + 1.0,
                                y: bounds.y + styling.shadow_offset,
                                ..bounds
                            },
                            background: Background::Color(
                                [0.0, 0.0, 0.0, 0.5].into(),
                            ),
                            border_radius: styling.border_radius,
                        },
                        Primitive::Quad {
                            bounds,
                            background,
                            border_radius: styling.border_radius,
                        },
                        content,
                    ],
                },
            },
            if is_mouse_over {
                MouseCursor::Pointer
            } else {
                MouseCursor::OutOfBounds
            },
        )
    }
}