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
use {
Align,
Backend,
CharacterCache,
Color,
Colorable,
Dimension,
FontSize,
Scalar,
Ui,
Widget,
};
use std;
use text;
use utils;
use widget;
pub struct Text<'a> {
pub common: widget::CommonBuilder,
pub text: &'a str,
pub style: Style,
}
pub const KIND: widget::Kind = "Text";
widget_style!{
KIND;
style Style {
- font_size: FontSize { theme.font_size_medium }
- color: Color { theme.label_color }
- maybe_wrap: Option<Wrap> { Some(Wrap::Whitespace) }
- line_spacing: Scalar { 1.0 }
- text_align: Align { Align::Start }
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Wrap {
Character,
Whitespace,
}
#[derive(Clone, Debug, PartialEq)]
pub struct State {
pub string: String,
pub line_infos: Vec<text::line::Info>,
}
impl<'a> Text<'a> {
pub fn new(text: &'a str) -> Self {
Text {
common: widget::CommonBuilder::new(),
text: text,
style: Style::new(),
}
}
pub fn no_line_wrap(mut self) -> Self {
self.style.maybe_wrap = Some(None);
self
}
pub fn wrap_by_word(mut self) -> Self {
self.style.maybe_wrap = Some(Some(Wrap::Whitespace));
self
}
pub fn wrap_by_character(mut self) -> Self {
self.style.maybe_wrap = Some(Some(Wrap::Character));
self
}
pub fn with_style(mut self, style: Style) -> Self {
self.style = style;
self
}
pub fn align_text_left(self) -> Self {
self.align_text_to(Align::Start)
}
pub fn align_text_middle(self) -> Self {
self.align_text_to(Align::Middle)
}
pub fn align_text_right(self) -> Self {
self.align_text_to(Align::End)
}
builder_methods!{
pub font_size { style.font_size = Some(FontSize) }
pub align_text_to { style.text_align = Some(Align) }
pub line_spacing { style.line_spacing = Some(Scalar) }
}
}
impl<'a> Widget for Text<'a> {
type State = State;
type Style = Style;
fn common(&self) -> &widget::CommonBuilder {
&self.common
}
fn common_mut(&mut self) -> &mut widget::CommonBuilder {
&mut self.common
}
fn unique_kind(&self) -> &'static str {
KIND
}
fn init_state(&self) -> State {
State {
string: String::new(),
line_infos: Vec::new(),
}
}
fn style(&self) -> Style {
self.style.clone()
}
fn default_x_dimension<B: Backend>(&self, ui: &Ui<B>) -> Dimension {
let font_size = self.style.font_size(&ui.theme);
let mut max_width = 0.0;
for line in self.text.lines() {
let width = ui.glyph_cache.width(font_size, line);
max_width = utils::partial_max(max_width, width);
}
Dimension::Absolute(max_width)
}
fn default_y_dimension<B: Backend>(&self, ui: &Ui<B>) -> Dimension {
use position::Sizeable;
let text = &self.text;
let font_size = self.style.font_size(&ui.theme);
let num_lines = match self.style.maybe_wrap(&ui.theme) {
None => text.lines().count(),
Some(wrap) => match self.get_w(ui) {
None => text.lines().count(),
Some(max_w) => match wrap {
Wrap::Character =>
text::line::infos(text, &ui.glyph_cache, font_size)
.wrap_by_character(max_w)
.count(),
Wrap::Whitespace =>
text::line::infos(text, &ui.glyph_cache, font_size)
.wrap_by_whitespace(max_w)
.count(),
},
},
};
let line_spacing = self.style.line_spacing(&ui.theme);
let height = text::height(std::cmp::max(num_lines, 1), font_size, line_spacing);
Dimension::Absolute(height)
}
fn update<B: Backend>(self, args: widget::UpdateArgs<Self, B>) {
let widget::UpdateArgs { rect, state, style, ui, .. } = args;
let Text { text, .. } = self;
let maybe_wrap = style.maybe_wrap(ui.theme());
let font_size = style.font_size(ui.theme());
let new_line_infos = || match maybe_wrap {
None =>
text::line::infos(text, ui.glyph_cache(), font_size),
Some(Wrap::Character) =>
text::line::infos(text, ui.glyph_cache(), font_size).wrap_by_character(rect.w()),
Some(Wrap::Whitespace) =>
text::line::infos(text, ui.glyph_cache(), font_size).wrap_by_whitespace(rect.w()),
};
if &state.string[..] != text {
state.update(|state| {
state.string = text.to_owned();
state.line_infos = new_line_infos().collect();
});
} else {
use utils::write_if_different;
use std::borrow::Cow;
let maybe_new_line_infos = {
let line_infos = &state.line_infos[..];
match write_if_different(line_infos, new_line_infos()) {
Cow::Owned(new) => Some(new),
_ => None,
}
};
if let Some(new_line_infos) = maybe_new_line_infos {
state.update(|state| state.line_infos = new_line_infos);
}
}
}
}
impl<'a> Colorable for Text<'a> {
fn color(mut self, color: Color) -> Self {
self.style.color = Some(color);
self
}
}