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
use smallvec::SmallVec;
use std::fmt::Debug;
use kas::class::HasText;
use kas::draw::TextClass;
use kas::event::{VirtualKeyCode, VirtualKeyCodes};
use kas::prelude::*;
#[handler(handle=noauto)]
#[widget(config=noauto)]
#[derive(Clone, Debug, Default, Widget)]
pub struct TextButton<M: Clone + Debug + 'static> {
#[widget_core]
core: kas::CoreData,
keys: VirtualKeyCodes,
label: AccelString,
msg: M,
}
impl<M: Clone + Debug + 'static> WidgetConfig for TextButton<M> {
fn configure(&mut self, mgr: &mut Manager) {
mgr.add_accel_keys(self.id(), &self.keys);
mgr.add_accel_keys(self.id(), self.label.keys());
}
fn key_nav(&self) -> bool {
true
}
}
impl<M: Clone + Debug + 'static> Layout for TextButton<M> {
fn size_rules(&mut self, size_handle: &mut dyn SizeHandle, axis: AxisInfo) -> SizeRules {
let sides = size_handle.button_surround();
let margins = size_handle.outer_margins();
let frame_rules = SizeRules::extract_fixed(axis.is_vertical(), sides.0 + sides.1, margins);
let content_rules = size_handle.text_bound(self.label.get(false), TextClass::Button, axis);
content_rules.surrounded_by(frame_rules, true)
}
fn set_rect(&mut self, rect: Rect, _align: AlignHints) {
self.core.rect = rect;
}
fn draw(&self, draw_handle: &mut dyn DrawHandle, mgr: &event::ManagerState, disabled: bool) {
draw_handle.button(self.core.rect, self.input_state(mgr, disabled));
let text = self.label.get(mgr.show_accel_labels());
let align = (Align::Centre, Align::Centre);
draw_handle.text(self.core.rect, text, TextClass::Button, align);
}
}
impl<M: Clone + Debug + 'static> TextButton<M> {
pub fn new<S: Into<AccelString>>(label: S, msg: M) -> Self {
TextButton {
core: Default::default(),
keys: SmallVec::new(),
label: label.into(),
msg,
}
}
pub fn with_keys(mut self, keys: &[VirtualKeyCode]) -> Self {
self.set_keys(keys);
self
}
pub fn set_msg(&mut self, msg: M) {
self.msg = msg;
}
pub fn set_keys(&mut self, keys: &[VirtualKeyCode]) {
self.keys = SmallVec::from_slice(keys);
}
}
impl<M: Clone + Debug + 'static> HasText for TextButton<M> {
fn get_text(&self) -> &str {
self.label.get(false)
}
fn set_cow_string(&mut self, text: CowString) -> TkAction {
self.label = text.into();
TkAction::Redraw
}
}
impl<M: Clone + Debug + 'static> event::Handler for TextButton<M> {
type Msg = M;
#[inline]
fn activation_via_press(&self) -> bool {
true
}
fn handle(&mut self, _: &mut Manager, event: Event) -> Response<M> {
match event {
Event::Activate => self.msg.clone().into(),
event => Response::Unhandled(event),
}
}
}