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
use kas::draw::TextClass;
use kas::text::format::{EditableText, FormattableText};
use kas::{event, prelude::*};
#[derive(Clone, Default, Debug, Widget)]
pub struct Label<T: FormattableText + 'static> {
#[widget_core]
core: CoreData,
label: Text<T>,
}
impl<T: FormattableText + 'static> Layout for Label<T> {
#[inline]
fn size_rules(&mut self, size_handle: &mut dyn SizeHandle, axis: AxisInfo) -> SizeRules {
size_handle.text_bound(&mut self.label, TextClass::Label, axis)
}
fn set_rect(&mut self, _: &mut Manager, rect: Rect, align: AlignHints) {
self.core.rect = rect;
self.label.update_env(|env| {
env.set_bounds(rect.size.into());
env.set_align(align.unwrap_or(Align::Default, Align::Centre));
});
}
#[cfg(feature = "min_spec")]
default fn draw(&self, draw_handle: &mut dyn DrawHandle, mgr: &ManagerState, disabled: bool) {
let state = self.input_state(mgr, disabled);
draw_handle.text_effects(self.core.rect.pos, &self.label, TextClass::Label, state);
}
#[cfg(not(feature = "min_spec"))]
fn draw(&self, draw_handle: &mut dyn DrawHandle, mgr: &ManagerState, disabled: bool) {
let state = self.input_state(mgr, disabled);
draw_handle.text_effects(self.core.rect.pos, &self.label, TextClass::Label, state);
}
}
#[cfg(feature = "min_spec")]
impl Layout for AccelLabel {
fn draw(&self, draw_handle: &mut dyn DrawHandle, mgr: &ManagerState, disabled: bool) {
let state = self.input_state(mgr, disabled);
let accel = mgr.show_accel_labels();
draw_handle.text_accel(
self.core.rect.pos,
&self.label,
accel,
TextClass::Label,
state,
);
}
}
#[cfg(feature = "min_spec")]
impl<'a> Layout for Label<&'a str> {
fn draw(&self, draw_handle: &mut dyn DrawHandle, mgr: &ManagerState, disabled: bool) {
let state = self.input_state(mgr, disabled);
draw_handle.text(
self.core.rect.pos,
self.label.as_ref(),
TextClass::Label,
state,
);
}
}
#[cfg(feature = "min_spec")]
impl Layout for StringLabel {
fn draw(&self, draw_handle: &mut dyn DrawHandle, mgr: &ManagerState, disabled: bool) {
let state = self.input_state(mgr, disabled);
draw_handle.text(
self.core.rect.pos,
self.label.as_ref(),
TextClass::Label,
state,
);
}
}
impl<T: FormattableText + 'static> From<T> for Label<T> {
fn from(label: T) -> Self {
Label::new(label)
}
}
impl<'a> From<&'a str> for Label<String> {
fn from(label: &'a str) -> Self {
Label::new(label.to_string())
}
}
impl<T: FormattableText + 'static> Label<T> {
#[inline]
pub fn new(label: T) -> Self {
Label {
core: Default::default(),
label: Text::new_multi(label),
}
}
pub fn set_text(&mut self, text: T) -> TkAction {
kas::text::util::set_text_and_prepare(&mut self.label, text, self.core.rect.size)
}
}
impl<T: FormattableText + 'static> HasStr for Label<T> {
fn get_str(&self) -> &str {
self.label.as_str()
}
}
impl<T: FormattableText + EditableText + 'static> HasString for Label<T> {
fn set_string(&mut self, string: String) -> TkAction {
kas::text::util::set_string_and_prepare(&mut self.label, string, self.core.rect.size)
}
}
pub type StrLabel = Label<&'static str>;
pub type StringLabel = Label<String>;
pub type AccelLabel = Label<AccelString>;
impl AccelLabel {
pub fn keys(&self) -> &[event::VirtualKeyCode] {
self.label.text().keys()
}
}
impl SetAccel for AccelLabel {
fn set_accel_string(&mut self, string: AccelString) -> TkAction {
let mut action = TkAction::empty();
if self.label.text().keys() != string.keys() {
action |= TkAction::RECONFIGURE;
}
action | kas::text::util::set_text_and_prepare(&mut self.label, string, self.core.rect.size)
}
}