summaryrefslogtreecommitdiffstats
path: root/wgpu/src/renderer/button.rs
blob: 275c870f6564d008dc8f75a4a535b134665fb53c (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
use crate::{Primitive, Renderer};
use iced_native::{
    button, Align, Background, Button, Color, Layout, Length, MouseCursor,
    Node, Point, Style,
};

impl button::Renderer for Renderer {
    fn node<Message>(&self, button: &Button<Message, Self>) -> Node {
        let style = Style::default()
            .width(button.width)
            .padding(button.padding)
            .min_width(Length::Units(100))
            .align_self(button.align_self)
            .align_items(Align::Stretch);

        Node::with_children(style, vec![button.content.node(self)])
    }

    fn draw<Message>(
        &mut self,
        button: &Button<Message, Self>,
        layout: Layout<'_>,
        cursor_position: Point,
    ) -> Self::Output {
        let bounds = layout.bounds();

        let (content, _) = button.content.draw(
            self,
            layout.children().next().unwrap(),
            cursor_position,
        );

        (
            Primitive::Group {
                primitives: vec![
                    Primitive::Quad {
                        bounds,
                        background: button.background.unwrap_or(
                            Background::Color(Color {
                                r: 0.8,
                                b: 0.8,
                                g: 0.8,
                                a: 1.0,
                            }),
                        ),
                        border_radius: button.border_radius,
                    },
                    content,
                ],
            },
            if bounds.contains(cursor_position) {
                MouseCursor::Pointer
            } else {
                MouseCursor::OutOfBounds
            },
        )
    }
}