Skip to main content

vizia_core/modifiers/
text.rs

1use super::internal;
2use crate::prelude::*;
3use std::{cell::RefCell, rc::Rc};
4
5/// Modifiers for changing the text properties of a view.
6pub trait TextModifiers: internal::Modifiable {
7    /// Sets the text content of the view.
8    fn text<T: ToStringLocalized, R: Res<T> + 'static>(mut self, value: R) -> Self {
9        let entity = self.entity();
10        let current = self.current();
11        self.context().with_current(current, |cx| {
12            let value_store = Rc::new(RefCell::new(None::<R>));
13            let value_store_for_binding = value_store.clone();
14
15            value.set_or_bind(cx, move |cx, val| {
16                *value_store_for_binding.borrow_mut() = Some(val);
17                let cx: &mut EventContext<'_> = &mut EventContext::new_with_current(cx, entity);
18                let text_data = value_store_for_binding
19                    .borrow()
20                    .as_ref()
21                    .map(|value| value.get_value(cx).to_string_local(cx));
22
23                let Some(text_data) = text_data else {
24                    return;
25                };
26
27                // cx.text_context.set_text(entity, &text_data);
28                cx.style.set_text(cx.tree, entity, text_data);
29
30                cx.style.needs_text_update(entity);
31                cx.needs_relayout();
32                cx.needs_redraw();
33            });
34
35            let value_store_for_locale = value_store;
36            let locale = cx.environment().locale;
37            locale.set_or_bind(cx, move |cx, _| {
38                let cx: &mut EventContext<'_> = &mut EventContext::new_with_current(cx, entity);
39                let text_data = value_store_for_locale
40                    .borrow()
41                    .as_ref()
42                    .map(|value| value.get_value(cx).to_string_local(cx));
43
44                let Some(text_data) = text_data else {
45                    return;
46                };
47
48                // cx.text_context.set_text(entity, &text_data);
49                cx.style.set_text(cx.tree, entity, text_data);
50
51                cx.style.needs_text_update(entity);
52                cx.needs_relayout();
53                cx.needs_redraw();
54            });
55        });
56
57        self
58    }
59
60    modifier!(
61        /// Sets the font that should be used by the view.
62        ///
63        /// The font name refers to the name assigned when the font is added to context.
64        font_family,
65        Vec<FamilyOwned>,
66        SystemFlags::REFLOW
67    );
68
69    /// Sets the font weight that should be used by the view.
70    fn font_weight<U: Into<FontWeight>>(mut self, value: impl Res<U>) -> Self {
71        let entity = self.entity();
72        value.set_or_bind(self.context(), move |cx, v| {
73            cx.style.font_weight.insert(
74                entity,
75                crate::style::SpecifiedFontWeight::Absolute(v.get_value(cx).into()),
76            );
77            cx.style.system_flags |= SystemFlags::REFLOW;
78            cx.set_system_flags(entity, SystemFlags::REFLOW);
79        });
80        self
81    }
82
83    modifier!(
84        /// Sets the font style that should be used by the view.
85        font_slant,
86        FontSlant,
87        SystemFlags::REFLOW
88    );
89
90    modifier!(
91        /// Sets the font stretch that should be used by the view if the font supports it.
92        font_width,
93        FontWidth,
94        SystemFlags::REFLOW
95    );
96
97    modifier!(
98        /// Sets the font variation settings that should be used by the view.
99        font_variation_settings,
100        Vec<FontVariation>,
101        SystemFlags::REFLOW
102    );
103
104    /// Sets the text color of the view.
105    fn color<U: Clone + Into<Color>>(mut self, value: impl Res<U>) -> Self {
106        let entity = self.entity();
107        let current = self.current();
108        self.context().with_current(current, move |cx| {
109            value.set_or_bind(cx, move |cx, v| {
110                cx.style.font_color.insert(entity, v.get_value(cx).into());
111                cx.style.needs_text_update(entity);
112                cx.needs_redraw(entity);
113            });
114        });
115        self
116    }
117
118    /// Sets the font size of the view.
119    fn font_size<U: Into<FontSize>>(mut self, value: impl Res<U>) -> Self {
120        let entity = self.entity();
121        let current = self.current();
122        self.context().with_current(current, move |cx| {
123            value.set_or_bind(cx, move |cx, v| {
124                cx.style.font_size.insert(entity, v.get_value(cx).into());
125                cx.style.needs_text_update(entity);
126            });
127        });
128        self
129    }
130
131    /// Sets the letter spacing of the view text.
132    fn letter_spacing<U: Into<LetterSpacing>>(mut self, value: impl Res<U>) -> Self {
133        let entity = self.entity();
134        let current = self.current();
135        self.context().with_current(current, move |cx| {
136            value.set_or_bind(cx, move |cx, v| {
137                cx.style.letter_spacing.insert(entity, v.get_value(cx).into());
138                cx.style.needs_text_update(entity);
139            });
140        });
141        self
142    }
143
144    modifier!(
145        /// Sets the ext caret color of the view.
146        caret_color,
147        Color,
148        SystemFlags::REDRAW
149    );
150
151    modifier!(
152        /// Sets the color used to highlight selected text within the view.
153        selection_color,
154        Color,
155        SystemFlags::REDRAW
156    );
157
158    modifier!(
159        /// Sets whether the text of the view should be allowed to wrap.
160        text_wrap,
161        bool,
162        SystemFlags::REFLOW
163    );
164
165    modifier!(
166        /// Sets the horizontal alignment of text within the view.
167        text_align,
168        TextAlign,
169        SystemFlags::REFLOW
170    );
171
172    modifier!(
173        /// Sets the text overflow.
174        text_overflow,
175        TextOverflow,
176        SystemFlags::REFLOW
177    );
178
179    modifier!(
180        /// Sets the line height of the view text.
181        line_height,
182        LineHeight,
183        SystemFlags::REFLOW
184    );
185
186    modifier!(
187        /// Sets the max number of .
188        line_clamp,
189        LineClamp,
190        SystemFlags::REFLOW
191    );
192
193    modifier!(
194        /// Sets the max number of .
195        text_decoration_line,
196        TextDecorationLine,
197        SystemFlags::REFLOW
198    );
199
200    modifier!(
201        /// Sets the width of the text stroke.
202        /// This sets Skia's [`skia_safe::textlayout::TextStyle`]'s foreground [`skia_safe::Paint`] to
203        /// draw a stroke on the text.
204        ///
205        /// See also [`Self::text_stroke_style`].
206        text_stroke_width,
207        Length,
208        SystemFlags::REFLOW
209    );
210
211    modifier!(
212        /// Sets the paint style of the text stroke.
213        /// You can either draw text with a stroke, or just the stroke outline.
214        ///
215        /// If you desire to paint the text stroke in a separate colour to the
216        /// fill property, Skia does not seem to support this (see the
217        /// [discussion on the PR to add text stroke](https://github.com/vizia/vizia/pull/528)).
218        ///
219        /// A workaround would involve a [`ZStack`] to overlay two copies of the
220        /// same text, one with a stroke and one without. These can have two
221        /// different colours. See also the [Flutter documentation](
222        /// https://api.flutter.dev/flutter/painting/TextStyle-class.html#painting.TextStyle.6)
223        /// on achieving this.
224        text_stroke_style,
225        TextStrokeStyle,
226        SystemFlags::REFLOW
227    );
228}
229
230impl<V> TextModifiers for Handle<'_, V> {}