Skip to main content

freya_core/elements/
label.rs

1//! Draw text with [label()]. Its a simplified version of [crate::elements::paragraph].
2
3use std::{
4    any::Any,
5    borrow::Cow,
6    rc::Rc,
7};
8
9use freya_engine::prelude::{
10    ClipOp,
11    FontStyle,
12    ParagraphBuilder,
13    ParagraphStyle,
14    SkParagraph,
15    SkRect,
16    TextStyle,
17};
18use rustc_hash::FxHashMap;
19use torin::prelude::Size2D;
20
21use crate::{
22    data::{
23        AccessibilityData,
24        EffectData,
25        LayoutData,
26        StyleState,
27        TextStyleData,
28    },
29    diff_key::DiffKey,
30    element::{
31        ClipContext,
32        Element,
33        ElementExt,
34        EventHandlerType,
35        LayoutContext,
36        RenderContext,
37    },
38    elements::paragraph::paint_paragraph_with_fill,
39    events::name::EventName,
40    layers::Layer,
41    prelude::{
42        AccessibilityExt,
43        Color,
44        ContainerExt,
45        EventHandlersExt,
46        KeyExt,
47        LayerExt,
48        LayoutExt,
49        MaybeExt,
50        Span,
51        TextAlign,
52        TextStyleExt,
53    },
54    text_cache::CachedParagraph,
55    tree::DiffModifies,
56};
57
58/// Draw text with [label()]. Its a simplified version of [crate::elements::paragraph].
59///
60/// See the available methods in [Label].
61///
62/// ```rust
63/// # use freya::prelude::*;
64/// fn app() -> impl IntoElement {
65///     label().text("Hello, world!").font_size(16.0)
66/// }
67/// ```
68pub fn label() -> Label {
69    Label {
70        key: DiffKey::None,
71        element: LabelElement::default(),
72    }
73}
74
75impl From<&str> for Element {
76    fn from(value: &str) -> Self {
77        label().text(value.to_string()).into()
78    }
79}
80
81impl From<String> for Element {
82    fn from(value: String) -> Self {
83        label().text(value).into()
84    }
85}
86
87/// Whether text sizes itself to its content or expands to the available width.
88pub enum TextWidth {
89    /// Shrink to fit the text content.
90    Fit,
91    /// Expand to the maximum available width.
92    Max,
93}
94
95#[derive(PartialEq, Clone)]
96pub struct LabelElement {
97    pub text: Cow<'static, str>,
98    pub accessibility: AccessibilityData,
99    pub text_style_data: TextStyleData,
100    pub layout: LayoutData,
101    pub event_handlers: FxHashMap<EventName, EventHandlerType>,
102    pub max_lines: Option<usize>,
103    pub line_height: Option<f32>,
104    pub relative_layer: Layer,
105}
106
107impl Default for LabelElement {
108    fn default() -> Self {
109        let mut accessibility = AccessibilityData::default();
110        accessibility.builder.set_role(accesskit::Role::Label);
111        Self {
112            text: Default::default(),
113            accessibility,
114            text_style_data: Default::default(),
115            layout: Default::default(),
116            event_handlers: Default::default(),
117            max_lines: None,
118            line_height: None,
119            relative_layer: Layer::default(),
120        }
121    }
122}
123
124impl ElementExt for LabelElement {
125    fn changed(&self, other: &Rc<dyn ElementExt>) -> bool {
126        let Some(label) = (other.as_ref() as &dyn Any).downcast_ref::<LabelElement>() else {
127            return false;
128        };
129        self != label
130    }
131
132    fn diff(&self, other: &Rc<dyn ElementExt>) -> DiffModifies {
133        let Some(label) = (other.as_ref() as &dyn Any).downcast_ref::<LabelElement>() else {
134            return DiffModifies::all();
135        };
136
137        let mut diff = DiffModifies::empty();
138
139        if self.text != label.text {
140            diff.insert(DiffModifies::STYLE);
141            diff.insert(DiffModifies::LAYOUT);
142        }
143
144        if self.accessibility != label.accessibility {
145            diff.insert(DiffModifies::ACCESSIBILITY);
146        }
147
148        if self.relative_layer != label.relative_layer {
149            diff.insert(DiffModifies::LAYER);
150        }
151
152        if self.text_style_data != label.text_style_data
153            || self.line_height != label.line_height
154            || self.max_lines != label.max_lines
155        {
156            diff.insert(DiffModifies::TEXT_STYLE);
157            diff.insert(DiffModifies::LAYOUT);
158        }
159        if self.layout != label.layout {
160            diff.insert(DiffModifies::LAYOUT);
161        }
162
163        if self.event_handlers != label.event_handlers {
164            diff.insert(DiffModifies::EVENT_HANDLERS);
165        }
166
167        diff
168    }
169
170    fn layout(&'_ self) -> Cow<'_, LayoutData> {
171        Cow::Borrowed(&self.layout)
172    }
173
174    fn effect(&'_ self) -> Option<Cow<'_, EffectData>> {
175        None
176    }
177
178    fn style(&'_ self) -> Cow<'_, StyleState> {
179        Cow::Owned(StyleState::default())
180    }
181
182    fn text_style(&'_ self) -> Cow<'_, TextStyleData> {
183        Cow::Borrowed(&self.text_style_data)
184    }
185
186    fn accessibility(&'_ self) -> Cow<'_, AccessibilityData> {
187        Cow::Borrowed(&self.accessibility)
188    }
189
190    fn layer(&self) -> Layer {
191        self.relative_layer
192    }
193
194    fn events_handlers(&'_ self) -> Option<Cow<'_, FxHashMap<EventName, EventHandlerType>>> {
195        Some(Cow::Borrowed(&self.event_handlers))
196    }
197
198    fn measure(&self, context: LayoutContext) -> Option<(Size2D, Rc<dyn Any>)> {
199        let cached_paragraph = CachedParagraph {
200            text_style_state: context.text_style_state,
201            spans: &[Span::new(&*self.text)],
202            max_lines: None,
203            line_height: None,
204            width: context.area_size.width,
205        };
206        let paragraph = context
207            .text_cache
208            .utilize(context.node_id, &cached_paragraph)
209            .unwrap_or_else(|| {
210                let mut paragraph_style = ParagraphStyle::default();
211                let mut text_style = TextStyle::default();
212
213                let mut font_families = context.text_style_state.font_families.clone();
214                font_families.extend_from_slice(context.fallback_fonts);
215
216                text_style.set_color(
217                    context
218                        .text_style_state
219                        .color
220                        .as_color()
221                        .unwrap_or(Color::WHITE),
222                );
223                text_style.set_font_size(
224                    f32::from(context.text_style_state.font_size) * context.scale_factor as f32,
225                );
226                text_style.set_font_families(&font_families);
227                text_style.set_font_style(FontStyle::new(
228                    context.text_style_state.font_weight.into(),
229                    context.text_style_state.font_width.into(),
230                    context.text_style_state.font_slant.into(),
231                ));
232
233                if context.text_style_state.text_height.needs_custom_height() {
234                    text_style.set_height_override(true);
235                    text_style.set_half_leading(true);
236                }
237
238                if let Some(line_height) = self.line_height {
239                    text_style.set_height_override(true).set_height(line_height);
240                }
241
242                for text_shadow in context.text_style_state.text_shadows.iter() {
243                    text_style.add_shadow((*text_shadow).into());
244                }
245
246                if let Some(ellipsis) = context.text_style_state.text_overflow.get_ellipsis() {
247                    paragraph_style.set_ellipsis(ellipsis);
248                }
249
250                paragraph_style.set_text_style(&text_style);
251                paragraph_style.set_max_lines(self.max_lines);
252                paragraph_style.set_text_align(context.text_style_state.text_align.into());
253
254                let mut paragraph_builder =
255                    ParagraphBuilder::new(&paragraph_style, &*context.font_collection);
256
257                paragraph_builder.add_text(&self.text);
258
259                let mut paragraph = paragraph_builder.build();
260                paragraph.layout(
261                    if self.max_lines == Some(1)
262                        && context.text_style_state.text_align == TextAlign::default()
263                        && !paragraph_style.ellipsized()
264                    {
265                        f32::MAX
266                    } else {
267                        context.area_size.width + 1.0
268                    },
269                );
270
271                context
272                    .text_cache
273                    .insert(context.node_id, &cached_paragraph, paragraph)
274            });
275
276        let size = Size2D::new(paragraph.longest_line(), paragraph.height());
277
278        Some((size, paragraph))
279    }
280
281    fn should_hook_measurement(&self) -> bool {
282        true
283    }
284
285    fn should_measure_inner_children(&self) -> bool {
286        false
287    }
288
289    fn clip(&self, context: ClipContext) {
290        let area = context.visible_area;
291        context.canvas.clip_rect(
292            SkRect::new(area.min_x(), area.min_y(), area.max_x(), area.max_y()),
293            ClipOp::Intersect,
294            true,
295        );
296    }
297
298    fn render(&self, context: RenderContext) {
299        let layout_data = context.layout_node.data.as_ref().unwrap();
300        let paragraph = layout_data.downcast_ref::<SkParagraph>().unwrap();
301
302        paint_paragraph_with_fill(
303            paragraph,
304            context.canvas,
305            context.layout_node.visible_area().origin,
306            &context.text_style_state.color,
307        );
308    }
309}
310
311impl From<Label> for Element {
312    fn from(value: Label) -> Self {
313        Element::Element {
314            key: value.key,
315            element: Rc::new(value.element),
316            elements: vec![],
317        }
318    }
319}
320
321impl KeyExt for Label {
322    fn write_key(&mut self) -> &mut DiffKey {
323        &mut self.key
324    }
325}
326
327impl EventHandlersExt for Label {
328    fn get_event_handlers(&mut self) -> &mut FxHashMap<EventName, EventHandlerType> {
329        &mut self.element.event_handlers
330    }
331}
332
333impl AccessibilityExt for Label {
334    fn get_accessibility_data(&mut self) -> &mut AccessibilityData {
335        &mut self.element.accessibility
336    }
337}
338
339impl TextStyleExt for Label {
340    fn get_text_style_data(&mut self) -> &mut TextStyleData {
341        &mut self.element.text_style_data
342    }
343}
344
345impl LayerExt for Label {
346    fn get_layer(&mut self) -> &mut Layer {
347        &mut self.element.relative_layer
348    }
349}
350
351impl MaybeExt for Label {}
352
353pub struct Label {
354    key: DiffKey,
355    element: LabelElement,
356}
357
358impl Label {
359    pub fn try_downcast(element: &dyn ElementExt) -> Option<LabelElement> {
360        (element as &dyn Any)
361            .downcast_ref::<LabelElement>()
362            .cloned()
363    }
364
365    /// Set the text content of the label.
366    pub fn text(mut self, text: impl Into<Cow<'static, str>>) -> Self {
367        let text = text.into();
368        self.element.text = text;
369        self
370    }
371
372    /// Limit the label to at most this many lines, truncating the rest. Pass `None` for no limit.
373    pub fn max_lines(mut self, max_lines: impl Into<Option<usize>>) -> Self {
374        self.element.max_lines = max_lines.into();
375        self
376    }
377
378    /// Override the height of each line as a multiple of the font size. Pass `None` for the default.
379    pub fn line_height(mut self, line_height: impl Into<Option<f32>>) -> Self {
380        self.element.line_height = line_height.into();
381        self
382    }
383}
384
385impl LayoutExt for Label {
386    fn get_layout(&mut self) -> &mut LayoutData {
387        &mut self.element.layout
388    }
389}
390
391impl ContainerExt for Label {}