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
use super::super::{super::Rotation, style, Defaults, IcedRenderer, Primitive};
use iced::{button, mouse, Element, Layout, Point, Rectangle};

impl button::Renderer for IcedRenderer {
    // TODO: what if this gets large enough to not be copied around?
    type Style = style::button::Style;

    const DEFAULT_PADDING: iced::Padding = iced::Padding::ZERO;

    fn draw<M>(
        &mut self,
        _defaults: &Self::Defaults,
        bounds: Rectangle,
        cursor_position: Point,
        is_disabled: bool,
        is_pressed: bool,
        style: &Self::Style,
        content: &Element<'_, M, Self>,
        content_layout: Layout<'_>,
    ) -> Self::Output {
        let is_mouse_over = bounds.contains(cursor_position);

        let (maybe_image, text_color) = if is_disabled {
            style.disabled()
        } else if is_mouse_over {
            if is_pressed {
                style.pressed()
            } else {
                style.hovered()
            }
        } else {
            style.active()
        };

        let (content, _) = content.draw(
            self,
            &Defaults { text_color },
            content_layout,
            cursor_position,
            &bounds,
        );

        let primitive = if let Some((handle, color)) = maybe_image {
            let background = Primitive::Image {
                handle: (handle, Rotation::None),
                bounds,
                color,
                source_rect: None,
            };

            Primitive::Group {
                primitives: vec![background, content],
            }
        } else {
            content
        };

        let mouse_interaction = if is_mouse_over {
            mouse::Interaction::Pointer
        } else {
            mouse::Interaction::default()
        };

        (primitive, mouse_interaction)
    }
}