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