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
use super::internal;
use crate::prelude::*;
use std::{cell::RefCell, rc::Rc};
/// Modifiers for changing the text properties of a view.
pub trait TextModifiers: internal::Modifiable {
/// Sets the text content of the view.
fn text<T: ToStringLocalized, R: Res<T> + 'static>(mut self, value: R) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
let value_store = Rc::new(RefCell::new(None::<R>));
let value_store_for_binding = value_store.clone();
value.set_or_bind(cx, move |cx, val| {
*value_store_for_binding.borrow_mut() = Some(val);
let cx: &mut EventContext<'_> = &mut EventContext::new_with_current(cx, entity);
let text_data = value_store_for_binding
.borrow()
.as_ref()
.map(|value| value.get_value(cx).to_string_local(cx));
let Some(text_data) = text_data else {
return;
};
// cx.text_context.set_text(entity, &text_data);
cx.style.text.insert(entity, text_data);
cx.style.needs_text_update(entity);
cx.needs_relayout();
cx.needs_redraw();
});
let value_store_for_locale = value_store;
let locale = cx.environment().locale;
locale.set_or_bind(cx, move |cx, _| {
let cx: &mut EventContext<'_> = &mut EventContext::new_with_current(cx, entity);
let text_data = value_store_for_locale
.borrow()
.as_ref()
.map(|value| value.get_value(cx).to_string_local(cx));
let Some(text_data) = text_data else {
return;
};
// cx.text_context.set_text(entity, &text_data);
cx.style.text.insert(entity, text_data);
cx.style.needs_text_update(entity);
cx.needs_relayout();
cx.needs_redraw();
});
});
self
}
modifier!(
/// Sets the font that should be used by the view.
///
/// The font name refers to the name assigned when the font is added to context.
font_family,
Vec<FamilyOwned>,
SystemFlags::REFLOW
);
modifier!(
/// Sets the font weight that should be used by the view.
font_weight,
FontWeight,
SystemFlags::REFLOW
);
modifier!(
/// Sets the font style that should be used by the view.
font_slant,
FontSlant,
SystemFlags::REFLOW
);
modifier!(
/// Sets the font stretch that should be used by the view if the font supports it.
font_width,
FontWidth,
SystemFlags::REFLOW
);
modifier!(
/// Sets the font variation settings that should be used by the view.
font_variation_settings,
Vec<FontVariation>,
SystemFlags::REFLOW
);
/// Sets the text color of the view.
fn color<U: Clone + Into<Color>>(mut self, value: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, move |cx| {
value.set_or_bind(cx, move |cx, v| {
cx.style.font_color.insert(entity, v.get_value(cx).into());
cx.style.needs_text_update(entity);
cx.needs_redraw(entity);
});
});
self
}
/// Sets the font size of the view.
fn font_size<U: Into<FontSize>>(mut self, value: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, move |cx| {
value.set_or_bind(cx, move |cx, v| {
cx.style.font_size.insert(entity, v.get_value(cx).into());
cx.style.needs_text_update(entity);
});
});
self
}
modifier!(
/// Sets the ext caret color of the view.
caret_color,
Color,
SystemFlags::REDRAW
);
modifier!(
/// Sets the color used to highlight selected text within the view.
selection_color,
Color,
SystemFlags::REDRAW
);
modifier!(
/// Sets whether the text of the view should be allowed to wrap.
text_wrap,
bool,
SystemFlags::REFLOW
);
modifier!(
/// Sets the horizontal alignment of text within the view.
text_align,
TextAlign,
SystemFlags::REFLOW
);
modifier!(
/// Sets the text overflow.
text_overflow,
TextOverflow,
SystemFlags::REFLOW
);
modifier!(
/// Sets the max number of .
line_clamp,
LineClamp,
SystemFlags::REFLOW
);
modifier!(
/// Sets the max number of .
text_decoration_line,
TextDecorationLine,
SystemFlags::REFLOW
);
modifier!(
/// Sets the width of the text stroke.
/// This sets Skia's [`skia_safe::textlayout::TextStyle`]'s foreground [`skia_safe::Paint`] to
/// draw a stroke on the text.
///
/// See also [`Self::text_stroke_style`].
text_stroke_width,
Length,
SystemFlags::REFLOW
);
modifier!(
/// Sets the paint style of the text stroke.
/// You can either draw text with a stroke, or just the stroke outline.
///
/// If you desire to paint the text stroke in a separate colour to the
/// fill property, Skia does not seem to support this (see the
/// [discussion on the PR to add text stroke](https://github.com/vizia/vizia/pull/528)).
///
/// A workaround would involve a [`ZStack`] to overlay two copies of the
/// same text, one with a stroke and one without. These can have two
/// different colours. See also the [Flutter documentation](
/// https://api.flutter.dev/flutter/painting/TextStyle-class.html#painting.TextStyle.6)
/// on achieving this.
text_stroke_style,
TextStrokeStyle,
SystemFlags::REFLOW
);
}
impl<V> TextModifiers for Handle<'_, V> {}