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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
// 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
//! Widget traits
use fmt;
use crate;
use crate;
use crate;
use crate;
use crateIdentifyWidget;
use crateWidgetId;
use autoimpl;
use crateEventState;
use crate;
use crateTkAction;
use kas_macros as macros;
/// Base functionality for [`Widget`]s
///
/// # Implementing WidgetCore
///
/// Implementations of this trait are generated via macro.
/// **Directly implementing this trait is not supported**.
/// See [`Widget`] trait documentation.
/// Listing of a [`Widget`]'s children
///
/// This trait enumerates child widgets (that is, components of the widget which
/// are themselves widgets).
///
/// Enumerated widgets are automatically configured, via recursion, when their
/// parent is. See [`Widget::configure`].
///
/// # Implementing WidgetChildren
///
/// Implementations of this trait are usually generated via macro.
/// See [`Widget`] trait documentation.
///
/// In a few cases, namely widgets which may add/remove children dynamically,
/// this trait should be implemented directly.
///
/// Note that parents are responsible for ensuring that newly added children
/// get configured, either by sending [`TkAction::RECONFIGURE`] by calling
/// [`ConfigMgr::configure`].
/// Positioning and drawing routines for [`Widget`]s
///
/// This trait is related to [`Widget`], but may be used independently.
///
/// # Implementing Layout
///
/// There are three cases:
///
/// - For a non-widget, all methods must be implemented directly.
/// - For a [`Widget`] without using the `layout` macro property,
/// the [`Self::set_rect`] and [`Self::find_id`] methods gain default
/// implementations (generated via macro).
/// - For a [`Widget`] where the `#[widget{ layout = .. }]` property
/// is set (see [`macros::widget`] documentation), all methods have a
/// default implementation. Custom implementations may use [`AutoLayout`] to
/// access these default implementations.
///
/// # Solving layout
///
/// Layout is resolved as follows:
///
/// 1. [`Widget::configure`] is called (widgets only), and may be used to load assets
/// 2. [`Self::size_rules`] is called at least once for each axis
/// 3. [`Self::set_rect`] is called to position elements. This may use data cached by `size_rules`.
/// 4. [`Self::find_id`] may be used to find the widget under the mouse and [`Self::draw`] to draw
/// elements.
///
/// Usually, [`Layout::size_rules`] methods are called recursively. To instead
/// solve layout for a single widget/layout object, it may be useful to use
/// [`layout::solve_size_rules`] or [`layout::SolveCache`].
/// The Widget trait
///
/// Widgets implement a family of traits, of which this trait is the final
/// member:
///
/// - [`WidgetCore`] — base functionality
/// - [`WidgetChildren`] — enumerates children
/// - [`Layout`] — handles sizing and positioning for self and children
/// - [`Widget`] — configuration, some aspects of layout, event handling
///
/// # Implementing Widget
///
/// To implement a widget, use the [`macros::widget`] macro. **This is the
/// only supported method of implementing `Widget`.**
///
/// The [`macros::widget`] macro only works within [`macros::impl_scope`].
/// Other trait implementations can be detected within this scope:
///
/// - [`WidgetCore`] is always generated
/// - [`WidgetChildren`] is generated if no direct implementation is present
/// - [`Layout`] is generated if the `layout` attribute property is set, and
/// no direct implementation is found. In other cases where a direct
/// implementation of the trait is found, (default) method implementations
/// may be injected where not already present.
/// - [`Widget`] is generated if no direct implementation is present,
/// otherwise some (default) method implementations are injected where
/// these methods are not directly implemented.
///
/// Some simple examples follow. See also
/// [examples apps](https://github.com/kas-gui/kas/tree/master/examples)
/// and [`kas_widgets` code](https://github.com/kas-gui/kas/tree/master/crates/kas-widgets).
/// ```
/// # extern crate kas_core as kas;
/// use kas::event;
/// use kas::prelude::*;
/// use kas::theme::TextClass;
/// use std::fmt::Debug;
///
/// impl_scope! {
/// /// A text label
/// #[derive(Clone, Debug)]
/// #[widget]
/// pub struct AccelLabel {
/// core: widget_core!(),
/// class: TextClass,
/// label: Text<AccelString>,
/// }
///
/// impl Self {
/// /// Construct from `label`
/// pub fn new(label: impl Into<AccelString>) -> Self {
/// AccelLabel {
/// core: Default::default(),
/// class: TextClass::AccelLabel(true),
/// label: Text::new(label.into()),
/// }
/// }
///
/// /// Set text class (inline)
/// pub fn with_class(mut self, class: TextClass) -> Self {
/// self.class = class;
/// self
/// }
///
/// /// Get the accelerator keys
/// pub fn keys(&self) -> &[event::VirtualKeyCode] {
/// self.label.text().keys()
/// }
/// }
///
/// impl Layout for Self {
/// fn size_rules(&mut self, size_mgr: SizeMgr, mut axis: AxisInfo) -> SizeRules {
/// axis.set_default_align_hv(Align::Default, Align::Center);
/// size_mgr.text_rules(&mut self.label, self.class, axis)
/// }
///
/// fn set_rect(&mut self, mgr: &mut ConfigMgr, rect: Rect) {
/// self.core.rect = rect;
/// mgr.text_set_size(&mut self.label, self.class, rect.size, None);
/// }
///
/// fn draw(&mut self, mut draw: DrawMgr) {
/// draw.text_effects(self.rect(), &self.label, self.class);
/// }
/// }
/// }
///
/// impl_scope! {
/// /// A push-button with a text label
/// #[derive(Debug)]
/// #[widget {
/// layout = button: self.label;
/// navigable = true;
/// hover_highlight = true;
/// }]
/// pub struct TextButton<M: Clone + Debug + 'static> {
/// core: widget_core!(),
/// #[widget]
/// label: AccelLabel,
/// message: M,
/// }
///
/// impl Self {
/// /// Construct a button with given `label`
/// pub fn new(label: impl Into<AccelString>, message: M) -> Self {
/// TextButton {
/// core: Default::default(),
/// label: AccelLabel::new(label).with_class(TextClass::Button),
/// message,
/// }
/// }
/// }
/// impl Widget for Self {
/// fn configure(&mut self, mgr: &mut ConfigMgr) {
/// 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| {
/// mgr.push_msg(self.message.clone());
/// Response::Used
/// })
/// }
/// }
/// }
/// ```
/// Extension trait over widgets