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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use std::fmt::{self, Debug};
use std::rc::Rc;
use kas::draw::{color::Rgb, TextClass};
use kas::event::{self, VirtualKeyCode, VirtualKeyCodes};
use kas::prelude::*;
#[derive(Clone, Widget)]
#[handler(noauto)]
#[widget(config=noauto)]
#[widget_derive(class_traits)]
pub struct Button<L: Widget<Msg = VoidMsg>, M: 'static> {
#[widget_core]
core: kas::CoreData,
keys1: VirtualKeyCodes,
frame_size: Size,
frame_offset: Offset,
ideal_size: Size,
color: Option<Rgb>,
#[widget_derive]
#[widget]
pub label: L,
on_push: Option<Rc<dyn Fn(&mut Manager) -> Option<M>>>,
}
impl<L: Widget<Msg = VoidMsg>, M: 'static> Debug for Button<L, M> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Button")
.field("core", &self.core)
.field("keys1", &self.keys1)
.field("frame_size", &self.frame_size)
.field("frame_offset", &self.frame_offset)
.field("ideal_size", &self.ideal_size)
.field("color", &self.color)
.field("label", &self.label)
.finish_non_exhaustive()
}
}
impl<L: Widget<Msg = VoidMsg>, M: 'static> WidgetConfig for Button<L, M> {
fn configure(&mut self, mgr: &mut Manager) {
mgr.add_accel_keys(self.id(), &self.keys1);
}
fn key_nav(&self) -> bool {
true
}
fn hover_highlight(&self) -> bool {
true
}
}
impl<L: Widget<Msg = VoidMsg>, M: 'static> Layout for Button<L, M> {
fn size_rules(&mut self, size_handle: &mut dyn SizeHandle, axis: AxisInfo) -> SizeRules {
let frame_rules = size_handle.button_surround(axis.is_vertical());
let content_rules = self.label.size_rules(size_handle, axis);
let (rules, offset, size) = frame_rules.surround_as_margin(content_rules);
self.frame_size.set_component(axis, size);
self.frame_offset.set_component(axis, offset);
self.ideal_size.set_component(axis, rules.ideal_size());
rules
}
fn set_rect(&mut self, mgr: &mut Manager, rect: Rect, align: AlignHints) {
let mut rect = align
.complete(Align::Centre, Align::Centre)
.aligned_rect(self.ideal_size, rect);
self.core.rect = rect;
rect.pos += self.frame_offset;
rect.size -= self.frame_size;
self.label.set_rect(mgr, rect, align);
}
fn draw(&self, draw_handle: &mut dyn DrawHandle, mgr: &event::ManagerState, disabled: bool) {
draw_handle.button(self.core.rect, self.color, self.input_state(mgr, disabled));
self.label.draw(draw_handle, mgr, disabled);
}
}
impl<L: Widget<Msg = VoidMsg>> Button<L, VoidMsg> {
#[inline]
pub fn new(label: L) -> Self {
Button {
core: Default::default(),
keys1: Default::default(),
frame_size: Default::default(),
frame_offset: Default::default(),
ideal_size: Default::default(),
color: None,
label,
on_push: None,
}
}
#[inline]
pub fn on_push<M, F>(self, f: F) -> Button<L, M>
where
F: Fn(&mut Manager) -> Option<M> + 'static,
{
Button {
core: self.core,
keys1: self.keys1,
frame_size: self.frame_size,
frame_offset: self.frame_offset,
ideal_size: self.ideal_size,
color: self.color,
label: self.label,
on_push: Some(Rc::new(f)),
}
}
}
impl<L: Widget<Msg = VoidMsg>, M: 'static> Button<L, M> {
#[inline]
pub fn new_on<F>(label: L, f: F) -> Self
where
F: Fn(&mut Manager) -> Option<M> + 'static,
{
Button::new(label).on_push(f)
}
#[inline]
pub fn new_msg(label: L, msg: M) -> Self
where
M: Clone,
{
Self::new_on(label, move |_| Some(msg.clone()))
}
pub fn with_keys(mut self, keys: &[VirtualKeyCode]) -> Self {
self.keys1.clear();
self.keys1.extend_from_slice(keys);
self
}
pub fn set_color(&mut self, color: Option<Rgb>) {
self.color = color;
}
pub fn with_color(mut self, color: Rgb) -> Self {
self.color = Some(color);
self
}
}
impl<L: Widget<Msg = VoidMsg>, M: 'static> Handler for Button<L, M> {
type Msg = M;
#[inline]
fn activation_via_press(&self) -> bool {
true
}
fn handle(&mut self, mgr: &mut Manager, event: Event) -> Response<M> {
match event {
Event::Activate => Response::none_or_msg(self.on_push.as_ref().and_then(|f| f(mgr))),
_ => Response::Unhandled,
}
}
}
impl<L: Widget<Msg = VoidMsg>, M: 'static> SendEvent for Button<L, M> {
fn send(&mut self, mgr: &mut Manager, id: WidgetId, event: Event) -> Response<M> {
if id < self.label.id() {
self.label.send(mgr, id, event).void_into()
} else {
debug_assert_eq!(id, self.id());
Manager::handle_generic(self, mgr, event)
}
}
}
#[derive(Clone, Widget)]
#[handler(handle=noauto)]
#[widget(config=noauto)]
pub struct TextButton<M: 'static> {
#[widget_core]
core: kas::CoreData,
keys1: VirtualKeyCodes,
frame_size: Size,
frame_offset: Offset,
ideal_size: Size,
color: Option<Rgb>,
label: Text<AccelString>,
on_push: Option<Rc<dyn Fn(&mut Manager) -> Option<M>>>,
}
impl<M: 'static> Debug for TextButton<M> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("TextButton")
.field("core", &self.core)
.field("keys1", &self.keys1)
.field("frame_size", &self.frame_size)
.field("frame_offset", &self.frame_offset)
.field("ideal_size", &self.ideal_size)
.field("color", &self.color)
.field("label", &self.label)
.finish_non_exhaustive()
}
}
impl<M: 'static> WidgetConfig for TextButton<M> {
fn configure(&mut self, mgr: &mut Manager) {
mgr.add_accel_keys(self.id(), &self.keys1);
mgr.add_accel_keys(self.id(), self.label.text().keys());
}
fn key_nav(&self) -> bool {
true
}
fn hover_highlight(&self) -> bool {
true
}
}
impl<M: 'static> Layout for TextButton<M> {
fn size_rules(&mut self, size_handle: &mut dyn SizeHandle, axis: AxisInfo) -> SizeRules {
let frame_rules = size_handle.button_surround(axis.is_vertical());
let content_rules = size_handle.text_bound(&mut self.label, TextClass::Button, axis);
let (rules, offset, size) = frame_rules.surround_as_margin(content_rules);
self.frame_size.set_component(axis, size);
self.frame_offset.set_component(axis, offset);
self.ideal_size.set_component(axis, rules.ideal_size());
rules
}
fn set_rect(&mut self, _: &mut Manager, rect: Rect, align: AlignHints) {
let rect = align
.complete(Align::Stretch, Align::Centre)
.aligned_rect(self.ideal_size, rect);
self.core.rect = rect;
let size = rect.size - self.frame_size;
self.label.update_env(|env| {
env.set_bounds(size.into());
env.set_align((Align::Centre, Align::Centre));
});
}
fn draw(&self, draw_handle: &mut dyn DrawHandle, mgr: &event::ManagerState, disabled: bool) {
draw_handle.button(self.core.rect, self.color, self.input_state(mgr, disabled));
let pos = self.core.rect.pos + self.frame_offset;
let accel = mgr.show_accel_labels();
let state = self.input_state(mgr, disabled);
draw_handle.text_accel(pos, &self.label, accel, TextClass::Button, state);
}
}
impl TextButton<VoidMsg> {
#[inline]
pub fn new<S: Into<AccelString>>(label: S) -> Self {
let label = label.into();
let text = Text::new_single(label);
TextButton {
core: Default::default(),
keys1: Default::default(),
frame_size: Default::default(),
frame_offset: Default::default(),
ideal_size: Default::default(),
color: None,
label: text,
on_push: None,
}
}
#[inline]
pub fn on_push<M, F>(self, f: F) -> TextButton<M>
where
F: Fn(&mut Manager) -> Option<M> + 'static,
{
TextButton {
core: self.core,
keys1: self.keys1,
frame_size: self.frame_size,
frame_offset: self.frame_offset,
ideal_size: self.ideal_size,
color: self.color,
label: self.label,
on_push: Some(Rc::new(f)),
}
}
}
impl<M: 'static> TextButton<M> {
#[inline]
pub fn new_on<S: Into<AccelString>, F>(label: S, f: F) -> Self
where
F: Fn(&mut Manager) -> Option<M> + 'static,
{
TextButton::new(label).on_push(f)
}
#[inline]
pub fn new_msg<S: Into<AccelString>>(label: S, msg: M) -> Self
where
M: Clone,
{
Self::new_on(label, move |_| Some(msg.clone()))
}
pub fn with_keys(mut self, keys: &[VirtualKeyCode]) -> Self {
self.keys1.clear();
self.keys1.extend_from_slice(keys);
self
}
pub fn set_color(&mut self, color: Option<Rgb>) {
self.color = color;
}
pub fn with_color(mut self, color: Rgb) -> Self {
self.color = Some(color);
self
}
}
impl<M: 'static> HasStr for TextButton<M> {
fn get_str(&self) -> &str {
self.label.as_str()
}
}
impl<M: 'static> SetAccel for TextButton<M> {
fn set_accel_string(&mut self, string: AccelString) -> TkAction {
let mut action = TkAction::empty();
if self.label.text().keys() != string.keys() {
action |= TkAction::RECONFIGURE;
}
let avail = self.core.rect.size.clamped_sub(self.frame_size);
action | kas::text::util::set_text_and_prepare(&mut self.label, string, avail)
}
}
impl<M: 'static> event::Handler for TextButton<M> {
type Msg = M;
#[inline]
fn activation_via_press(&self) -> bool {
true
}
fn handle(&mut self, mgr: &mut Manager, event: Event) -> Response<M> {
match event {
Event::Activate => Response::none_or_msg(self.on_push.as_ref().and_then(|f| f(mgr))),
_ => Response::Unhandled,
}
}
}