summaryrefslogtreecommitdiffstats
path: root/pure/src/widget/button.rs
blob: 89acb7f52f23ba4bdb9bcd188ed7ab48a593ad6d (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use crate::widget::{Element, Tree, Widget};

use iced_native::event::{self, Event};
use iced_native::layout;
use iced_native::mouse;
use iced_native::renderer;
use iced_native::widget::button;
use iced_native::{
    Clipboard, Hasher, Layout, Length, Padding, Point, Rectangle, Shell,
};
use iced_style::button::StyleSheet;

use std::any::Any;

pub use button::State;

pub struct Button<'a, Message, Renderer> {
    content: Element<'a, Message, Renderer>,
    on_press: Option<Message>,
    style_sheet: Box<dyn StyleSheet + 'a>,
    width: Length,
    height: Length,
    padding: Padding,
}

impl<'a, Message, Renderer> Button<'a, Message, Renderer> {
    pub fn new(content: impl Into<Element<'a, Message, Renderer>>) -> Self {
        Button {
            content: content.into(),
            on_press: None,
            style_sheet: Default::default(),
            width: Length::Shrink,
            height: Length::Shrink,
            padding: Padding::new(5),
        }
    }

    pub fn on_press(mut self, on_press: Message) -> Self {
        self.on_press = Some(on_press);
        self
    }
}

impl<'a, Message, Renderer> Widget<Message, Renderer>
    for Button<'a, Message, Renderer>
where
    Message: 'static + Clone,
    Renderer: 'static + iced_native::Renderer,
{
    fn tag(&self) -> std::any::TypeId {
        std::any::TypeId::of::<State>()
    }

    fn state(&self) -> Box<dyn Any> {
        Box::new(State::new())
    }

    fn children(&self) -> &[Element<Message, Renderer>] {
        std::slice::from_ref(&self.content)
    }

    fn width(&self) -> Length {
        self.width
    }

    fn height(&self) -> Length {
        self.height
    }

    fn hash_layout(&self, state: &mut Hasher) {
        use std::hash::Hash;

        self.tag().hash(state);
        self.width.hash(state);
        self.height.hash(state);
        self.padding.hash(state);
        self.content.as_widget().hash_layout(state);
    }

    fn layout(
        &self,
        renderer: &Renderer,
        limits: &layout::Limits,
    ) -> layout::Node {
        button::layout(
            renderer,
            limits,
            self.width,
            self.height,
            self.padding,
            |renderer, limits| {
                self.content.as_widget().layout(renderer, &limits)
            },
        )
    }

    fn on_event(
        &mut self,
        tree: &mut Tree,
        event: Event,
        layout: Layout<'_>,
        cursor_position: Point,
        renderer: &Renderer,
        clipboard: &mut dyn Clipboard,
        shell: &mut Shell<'_, Message>,
    ) -> event::Status {
        if let event::Status::Captured = self.content.as_widget_mut().on_event(
            &mut tree.children[0],
            event.clone(),
            layout.children().next().unwrap(),
            cursor_position,
            renderer,
            clipboard,
            shell,
        ) {
            return event::Status::Captured;
        }

        button::update(
            event,
            layout,
            cursor_position,
            shell,
            &self.on_press,
            || tree.state_mut::<State>(),
        )
    }

    fn draw(
        &self,
        tree: &Tree,
        renderer: &mut Renderer,
        _style: &renderer::Style,
        layout: Layout<'_>,
        cursor_position: Point,
        _viewport: &Rectangle,
    ) {
        let bounds = layout.bounds();
        let content_layout = layout.children().next().unwrap();

        let styling = button::draw(
            renderer,
            bounds,
            cursor_position,
            self.on_press.is_some(),
            self.style_sheet.as_ref(),
            || tree.state::<State>(),
        );

        self.content.as_widget().draw(
            &tree.children[0],
            renderer,
            &renderer::Style {
                text_color: styling.text_color,
            },
            content_layout,
            cursor_position,
            &bounds,
        );
    }

    fn mouse_interaction(
        &self,
        _tree: &Tree,
        layout: Layout<'_>,
        cursor_position: Point,
        _viewport: &Rectangle,
        _renderer: &Renderer,
    ) -> mouse::Interaction {
        button::mouse_interaction(
            layout,
            cursor_position,
            self.on_press.is_some(),
        )
    }
}

impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>>
    for Button<'a, Message, Renderer>
where
    Message: Clone + 'static,
    Renderer: iced_native::Renderer + 'static,
{
    fn into(self) -> Element<'a, Message, Renderer> {
        Element::new(self)
    }
}