vizia_core/modifiers/
text.rs

1use super::internal;
2use crate::prelude::*;
3
4/// Modifiers for changing the text properties of a view.
5pub trait TextModifiers: internal::Modifiable {
6    /// Sets the text content of the view.
7    fn text<T: ToStringLocalized>(mut self, value: impl Res<T>) -> Self {
8        let entity = self.entity();
9        let current = self.current();
10        self.context().with_current(current, |cx| {
11            value.set_or_bind(cx, entity, move |cx, val| {
12                let cx: &mut EventContext<'_> = &mut EventContext::new_with_current(cx, entity);
13                let text_data = val.get(cx).to_string_local(cx);
14                // cx.text_context.set_text(entity, &text_data);
15                cx.style.text.insert(entity, text_data);
16
17                cx.style.needs_text_update(entity);
18                cx.needs_relayout();
19                cx.needs_redraw();
20            });
21        });
22
23        self
24    }
25
26    modifier!(
27        /// Sets the font that should be used by the view.
28        ///
29        /// The font name refers to the name assigned when the font is added to context.
30        font_family,
31        Vec<FamilyOwned>,
32        SystemFlags::REFLOW
33    );
34
35    modifier!(
36        /// Sets the font weight that should be used by the view.
37        font_weight,
38        FontWeight,
39        SystemFlags::REFLOW
40    );
41
42    modifier!(
43        /// Sets the font style that should be used by the view.
44        font_slant,
45        FontSlant,
46        SystemFlags::REFLOW
47    );
48
49    modifier!(
50        /// Sets the font stretch that should be used by the view if the font supports it.
51        font_width,
52        FontWidth,
53        SystemFlags::REFLOW
54    );
55
56    modifier!(
57        /// Sets the font variation settings that should be used by the view.
58        font_variation_settings,
59        Vec<FontVariation>,
60        SystemFlags::REFLOW
61    );
62
63    /// Sets the text color of the view.
64    fn color<U: Clone + Into<Color>>(mut self, value: impl Res<U>) -> Self {
65        let entity = self.entity();
66        let current = self.current();
67        self.context().with_current(current, move |cx| {
68            value.set_or_bind(cx, entity, move |cx, v| {
69                cx.style.font_color.insert(entity, v.get(cx).into());
70                cx.style.needs_text_update(entity);
71                cx.needs_redraw(entity);
72            });
73        });
74        self
75    }
76
77    /// Sets the font size of the view.
78    fn font_size<U: Into<FontSize>>(mut self, value: impl Res<U>) -> Self {
79        let entity = self.entity();
80        let current = self.current();
81        self.context().with_current(current, move |cx| {
82            value.set_or_bind(cx, entity, move |cx, v| {
83                cx.style.font_size.insert(cx.current, v.get(cx).into());
84                cx.style.needs_text_update(entity);
85            });
86        });
87        self
88    }
89
90    modifier!(
91        /// Sets the ext caret color of the view.
92        caret_color,
93        Color,
94        SystemFlags::REDRAW
95    );
96
97    modifier!(
98        /// Sets the color used to highlight selected text within the view.
99        selection_color,
100        Color,
101        SystemFlags::REDRAW
102    );
103
104    modifier!(
105        /// Sets whether the text of the view should be allowed to wrap.
106        text_wrap,
107        bool,
108        SystemFlags::REFLOW
109    );
110
111    modifier!(
112        /// Sets the horizontal alignment of text within the view.
113        text_align,
114        TextAlign,
115        SystemFlags::REFLOW
116    );
117
118    modifier!(
119        /// Sets the text overflow.
120        text_overflow,
121        TextOverflow,
122        SystemFlags::REFLOW
123    );
124
125    modifier!(
126        /// Sets the max number of .
127        line_clamp,
128        LineClamp,
129        SystemFlags::REFLOW
130    );
131
132    modifier!(
133        /// Sets the max number of .
134        text_decoration_line,
135        TextDecorationLine,
136        SystemFlags::REFLOW
137    );
138
139    modifier!(
140        /// Sets the width of the text stroke.
141        /// This sets Skia's [`skia_safe::textlayout::TextStyle`]'s foreground [`skia_safe::Paint`] to
142        /// draw a stroke on the text.
143        ///
144        /// See also [`Self::text_stroke_style`].
145        text_stroke_width,
146        Length,
147        SystemFlags::REFLOW
148    );
149
150    modifier!(
151        /// Sets the paint style of the text stroke.
152        /// You can either draw text with a stroke, or just the stroke outline.
153        ///
154        /// If you desire to paint the text stroke in a separate colour to the
155        /// fill property, Skia does not seem to support this (see the
156        /// [discussion on the PR to add text stroke](https://github.com/vizia/vizia/pull/528)).
157        ///
158        /// A workaround would involve a [`ZStack`] to overlay two copies of the
159        /// same text, one with a stroke and one without. These can have two
160        /// different colours. See also the [Flutter documentation](
161        /// https://api.flutter.dev/flutter/painting/TextStyle-class.html#painting.TextStyle.6)
162        /// on achieving this.
163        text_stroke_style,
164        TextStrokeStyle,
165        SystemFlags::REFLOW
166    );
167}
168
169impl<V> TextModifiers for Handle<'_, V> {}