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
use {Color, Colorable, FontSize, Ui, Widget};
use position::{Dimension, Scalar};
use std;
use text;
use utils;
use widget;
#[derive(Clone, Debug, WidgetCommon_)]
pub struct Text<'a> {
#[conrod(common_builder)]
pub common: widget::CommonBuilder,
pub text: &'a str,
pub style: Style,
}
#[derive(Copy, Clone, Debug, Default, PartialEq, WidgetStyle_)]
pub struct Style {
#[conrod(default = "theme.font_size_medium")]
pub font_size: Option<FontSize>,
#[conrod(default = "theme.label_color")]
pub color: Option<Color>,
#[conrod(default = "Some(Wrap::Whitespace)")]
pub maybe_wrap: Option<Option<Wrap>>,
#[conrod(default = "1.0")]
pub line_spacing: Option<Scalar>,
#[conrod(default = "text::Justify::Left")]
pub justify: Option<text::Justify>,
#[conrod(default = "theme.font_id")]
pub font_id: Option<Option<text::font::Id>>,
}
#[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::default(),
text: text,
style: Style::default(),
}
}
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 font_id(mut self, font_id: text::font::Id) -> Self {
self.style.font_id = Some(Some(font_id));
self
}
pub fn with_style(mut self, style: Style) -> Self {
self.style = style;
self
}
pub fn left_justify(self) -> Self {
self.justify(text::Justify::Left)
}
pub fn center_justify(self) -> Self {
self.justify(text::Justify::Center)
}
pub fn right_justify(self) -> Self {
self.justify(text::Justify::Right)
}
builder_methods!{
pub font_size { style.font_size = Some(FontSize) }
pub justify { style.justify = Some(text::Justify) }
pub line_spacing { style.line_spacing = Some(Scalar) }
}
}
impl<'a> Widget for Text<'a> {
type State = State;
type Style = Style;
type Event = ();
fn init_state(&self, _: widget::id::Generator) -> Self::State {
State {
string: String::new(),
line_infos: Vec::new(),
}
}
fn style(&self) -> Self::Style {
self.style.clone()
}
fn default_x_dimension(&self, ui: &Ui) -> Dimension {
let font = match self.style.font_id(&ui.theme)
.or(ui.fonts.ids().next())
.and_then(|id| ui.fonts.get(id))
{
Some(font) => font,
None => return Dimension::Absolute(0.0),
};
let font_size = self.style.font_size(&ui.theme);
let mut max_width = 0.0;
for line in self.text.lines() {
let width = text::line::width(line, font, font_size);
max_width = utils::partial_max(max_width, width);
}
Dimension::Absolute(max_width)
}
fn default_y_dimension(&self, ui: &Ui) -> Dimension {
use position::Sizeable;
let font = match self.style.font_id(&ui.theme)
.or(ui.fonts.ids().next())
.and_then(|id| ui.fonts.get(id))
{
Some(font) => font,
None => return Dimension::Absolute(0.0),
};
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, font, font_size)
.wrap_by_character(max_w)
.count(),
Wrap::Whitespace =>
text::line::infos(text, font, 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(self, args: widget::UpdateArgs<Self>) -> Self::Event {
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 font = match style.font_id(&ui.theme)
.or(ui.fonts.ids().next())
.and_then(|id| ui.fonts.get(id))
{
Some(font) => font,
None => return,
};
let new_line_infos = || match maybe_wrap {
None =>
text::line::infos(text, font, font_size),
Some(Wrap::Character) =>
text::line::infos(text, font, font_size).wrap_by_character(rect.w()),
Some(Wrap::Whitespace) =>
text::line::infos(text, font, 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
}
}