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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
//     https://www.apache.org/licenses/LICENSE-2.0

//! Push-buttons

use crate::AccelLabel;
use kas::draw::color::Rgb;
use kas::event::{VirtualKeyCode, VirtualKeyCodes};
use kas::prelude::*;
use kas::theme::TextClass;
use std::fmt::Debug;
use std::rc::Rc;

impl_scope! {
    /// A push-button with a generic label
    ///
    /// Default alignment of content is centered.
    #[autoimpl(Debug ignore self.on_press)]
    #[autoimpl(class_traits using self.inner where W: trait)]
    #[derive(Clone)]
    #[widget {
        layout = button(self.color): self.inner;
        navigable = true;
        hover_highlight = true;
    }]
    pub struct Button<W: Widget> {
        core: widget_core!(),
        keys1: VirtualKeyCodes,
        color: Option<Rgb>,
        #[widget]
        pub inner: W,
        on_press: Option<Rc<dyn Fn(&mut EventMgr)>>,
    }

    impl<W: Widget> Button<W> {
        /// Construct a button with given `inner` widget
        #[inline]
        pub fn new(inner: W) -> Self {
            Button {
                core: Default::default(),
                keys1: Default::default(),
                color: None,
                inner,
                on_press: None,
            }
        }

        /// Set event handler `f`
        ///
        /// This closure is called when the button is activated.
        #[inline]
        #[must_use]
        pub fn on_press<F>(self, f: F) -> Button<W>
        where
            F: Fn(&mut EventMgr) + 'static,
        {
            Button {
                core: self.core,
                keys1: self.keys1,
                color: self.color,
                inner: self.inner,
                on_press: Some(Rc::new(f)),
            }
        }
    }

    impl Self {
        /// Construct a button with a given `inner` widget and event handler `f`
        ///
        /// This closure is called when the button is activated.
        #[inline]
        pub fn new_on<F>(inner: W, f: F) -> Self
        where
            F: Fn(&mut EventMgr) + 'static,
        {
            Button::new(inner).on_press(f)
        }

        /// Construct a button with a given `inner` and payload `msg`
        ///
        /// When the button is activated, a clone of `msg` is sent to the
        /// parent widget. The parent (or an ancestor) should handle this using
        /// [`Widget::handle_message`].
        #[inline]
        pub fn new_msg<M: Clone + Debug + 'static>(inner: W, msg: M) -> Self {
            Self::new_on(inner, move |mgr| mgr.push_msg(msg.clone()))
        }

        /// Add accelerator keys (chain style)
        #[must_use]
        pub fn with_keys(mut self, keys: &[VirtualKeyCode]) -> Self {
            self.keys1.clear();
            self.keys1.extend_from_slice(keys);
            self
        }

        /// Set button color
        pub fn set_color(&mut self, color: Option<Rgb>) {
            self.color = color;
        }

        /// Set button color (chain style)
        #[must_use]
        pub fn with_color(mut self, color: Rgb) -> Self {
            self.color = Some(color);
            self
        }
    }

    impl Widget for Self {
        fn configure(&mut self, mgr: &mut ConfigMgr) {
            mgr.add_accel_keys(self.id_ref(), &self.keys1);
        }

        fn handle_event(&mut self, mgr: &mut EventMgr, event: Event) -> Response {
            event.on_activate(mgr, self.id(), |mgr| {
                if let Some(f) = self.on_press.as_ref() {
                    f(mgr);
                }
                Response::Used
            })
        }
    }
}

impl_scope! {
    /// A push-button with a text label
    ///
    /// This is a specialised variant of [`Button`] supporting key shortcuts from an
    /// [`AccelString`] label and using a custom text class (and thus theme colour).
    ///
    /// Default alignment of content is centered.
    #[autoimpl(Debug ignore self.on_press)]
    #[derive(Clone)]
    #[widget {
        layout = button(self.color): self.label;
        navigable = true;
        hover_highlight = true;
    }]
    pub struct TextButton {
        core: widget_core!(),
        keys1: VirtualKeyCodes,
        #[widget]
        label: AccelLabel,
        color: Option<Rgb>,
        on_press: Option<Rc<dyn Fn(&mut EventMgr)>>,
    }

    impl Self {
        /// Construct a button with given `label`
        #[inline]
        pub fn new<S: Into<AccelString>>(label: S) -> Self {
            TextButton {
                core: Default::default(),
                keys1: Default::default(),
                label: AccelLabel::new(label).with_class(TextClass::Button),
                color: None,
                on_press: None,
            }
        }

        /// Set event handler `f`
        ///
        /// This closure is called when the button is activated.
        #[inline]
        #[must_use]
        pub fn on_press<F>(self, f: F) -> TextButton
        where
            F: Fn(&mut EventMgr) + 'static,
        {
            TextButton {
                core: self.core,
                keys1: self.keys1,
                color: self.color,
                label: self.label,
                on_press: Some(Rc::new(f)),
            }
        }

        /// Construct a button with a given `label` and event handler `f`
        ///
        /// This closure is called when the button is activated.
        #[inline]
        pub fn new_on<S: Into<AccelString>, F>(label: S, f: F) -> Self
        where
            F: Fn(&mut EventMgr) + 'static,
        {
            TextButton::new(label).on_press(f)
        }

        /// Construct a button with a given `label` and payload `msg`
        ///
        /// When the button is activated, a clone of `msg` is sent to the
        /// parent widget. The parent (or an ancestor) should handle this using
        /// [`Widget::handle_message`].
        #[inline]
        pub fn new_msg<S: Into<AccelString>, M: Clone + Debug + 'static>(label: S, msg: M) -> Self {
            Self::new_on(label, move |mgr| mgr.push_msg(msg.clone()))
        }

        /// Add accelerator keys (chain style)
        ///
        /// These keys are added to those inferred from the label via `&` marks.
        #[must_use]
        pub fn with_keys(mut self, keys: &[VirtualKeyCode]) -> Self {
            self.keys1.clear();
            self.keys1.extend_from_slice(keys);
            self
        }

        /// Set button color
        pub fn set_color(&mut self, color: Option<Rgb>) {
            self.color = color;
        }

        /// Set button color (chain style)
        #[must_use]
        pub fn with_color(mut self, color: Rgb) -> Self {
            self.color = Some(color);
            self
        }
    }

    impl HasStr for Self {
        fn get_str(&self) -> &str {
            self.label.get_str()
        }
    }

    impl SetAccel for Self {
        #[inline]
        fn set_accel_string(&mut self, string: AccelString) -> TkAction {
            self.label.set_accel_string(string)
        }
    }

    impl Widget for Self {
        fn configure(&mut self, mgr: &mut ConfigMgr) {
            mgr.add_accel_keys(self.id_ref(), &self.keys1);
            mgr.add_accel_keys(self.id_ref(), self.label.keys());
        }

        fn handle_event(&mut self, mgr: &mut EventMgr, event: Event) -> Response {
            event.on_activate(mgr, self.id(), |mgr| {
                if let Some(f) = self.on_press.as_ref() {
                    f(mgr);
                }
                Response::Used
            })
        }
    }
}