freya-core 0.5.0-beta.9

Reactivity runtime, tree management, accessibility integration, rendering pipeline and more, for Freya
Documentation
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Draw text with [label()]. Its a simplified version of [crate::elements::paragraph].

use std::{
    any::Any,
    borrow::Cow,
    rc::Rc,
};

use freya_engine::prelude::{
    ClipOp,
    ParagraphBuilder,
    ParagraphStyle,
    SkParagraph,
    SkRect,
};
use torin::prelude::{
    Area,
    Size2D,
    SizeModel,
};

use crate::{
    data::{
        AccessibilityData,
        EffectData,
        LayoutData,
        StyleState,
        TextStyleData,
    },
    diff_key::DiffKey,
    element::{
        ClipContext,
        Element,
        ElementExt,
        EventHandlers,
        LayoutContext,
        RenderContext,
    },
    elements::paragraph::ParagraphPaintExt,
    layers::Layer,
    prelude::{
        AccessibilityExt,
        ContainerExt,
        EffectExt,
        EventHandlersExt,
        KeyExt,
        LayerExt,
        LayoutExt,
        MaybeExt,
        Span,
        TextAlign,
        TextStyleExt,
    },
    style::render_callback::RenderContext as FillRenderContext,
    text_cache::CachedParagraph,
    tree::DiffModifies,
};

/// Draw text with [label()]. Its a simplified version of [crate::elements::paragraph].
///
/// See the available methods in [Label].
///
/// ```rust
/// # use freya::prelude::*;
/// fn app() -> impl IntoElement {
///     label().text("Hello, world!").font_size(16.0)
/// }
/// ```
pub fn label() -> Label {
    Label::default()
}

impl From<&str> for Element {
    fn from(value: &str) -> Self {
        label().text(value.to_string()).into()
    }
}

impl From<String> for Element {
    fn from(value: String) -> Self {
        label().text(value).into()
    }
}

/// Whether text sizes itself to its content or expands to the available width.
pub enum TextWidth {
    /// Shrink to fit the text content.
    Fit,
    /// Expand to the maximum available width.
    Max,
}

#[derive(PartialEq, Clone)]
pub struct LabelElement {
    pub text: Cow<'static, str>,
    pub accessibility: AccessibilityData,
    pub text_style_data: TextStyleData,
    pub layout: LayoutData,
    pub event_handlers: EventHandlers,
    pub max_lines: Option<usize>,
    pub line_height: Option<f32>,
    pub relative_layer: Layer,
    pub effect: Option<EffectData>,
}

impl Default for LabelElement {
    fn default() -> Self {
        let mut accessibility = AccessibilityData::default();
        accessibility.builder.set_role(accesskit::Role::Label);
        Self {
            text: Default::default(),
            accessibility,
            text_style_data: Default::default(),
            layout: Default::default(),
            event_handlers: Default::default(),
            max_lines: None,
            line_height: None,
            relative_layer: Layer::default(),
            effect: None,
        }
    }
}

impl ElementExt for LabelElement {
    fn changed(&self, other: &Rc<dyn ElementExt>) -> bool {
        let Some(label) = (other.as_ref() as &dyn Any).downcast_ref::<LabelElement>() else {
            return false;
        };
        self != label
    }

    fn diff(&self, other: &Rc<dyn ElementExt>) -> DiffModifies {
        let Some(label) = (other.as_ref() as &dyn Any).downcast_ref::<LabelElement>() else {
            return DiffModifies::all();
        };

        let mut diff = DiffModifies::empty();

        if self.text != label.text {
            diff.insert(DiffModifies::STYLE);
            diff.insert(DiffModifies::LAYOUT);
            diff.insert(DiffModifies::ACCESSIBILITY);
        }

        if self.accessibility != label.accessibility {
            diff.insert(DiffModifies::ACCESSIBILITY);
        }

        if self.relative_layer != label.relative_layer {
            diff.insert(DiffModifies::LAYER);
        }

        if self.effect != label.effect {
            diff.insert(DiffModifies::EFFECT);
        }

        if self.text_style_data != label.text_style_data
            || self.line_height != label.line_height
            || self.max_lines != label.max_lines
        {
            diff.insert(DiffModifies::TEXT_STYLE);
            diff.insert(DiffModifies::LAYOUT);
        }
        if self.layout != label.layout {
            diff.insert(DiffModifies::LAYOUT);
        }

        if self.event_handlers != label.event_handlers {
            diff.insert(DiffModifies::EVENT_HANDLERS);
        }

        diff
    }

    fn layout(&'_ self) -> Cow<'_, LayoutData> {
        Cow::Borrowed(&self.layout)
    }

    fn effect(&'_ self) -> Option<Cow<'_, EffectData>> {
        self.effect.as_ref().map(Cow::Borrowed)
    }

    fn style(&'_ self) -> Cow<'_, StyleState> {
        Cow::Owned(StyleState::default())
    }

    fn is_transparent(&self) -> bool {
        false
    }

    fn text_style(&'_ self) -> Cow<'_, TextStyleData> {
        Cow::Borrowed(&self.text_style_data)
    }

    fn accessibility(&'_ self) -> Cow<'_, AccessibilityData> {
        Cow::Borrowed(&self.accessibility)
    }

    fn finish_accessibility(&self, builder: &mut accesskit::Node) {
        builder.set_value(self.text.clone());
    }

    fn layer(&self) -> Layer {
        self.relative_layer
    }

    fn events_handlers(&'_ self) -> Option<Cow<'_, EventHandlers>> {
        Some(Cow::Borrowed(&self.event_handlers))
    }

    fn measure(&self, context: LayoutContext) -> Option<(Size2D, Rc<dyn Any>)> {
        let content_area_size =
            (*context.area_size - context.torin_node.padding.into()).max(Size2D::zero());
        let cached_paragraph = CachedParagraph {
            text_style_state: context.text_style_state,
            spans: &[Span::new(&*self.text)],
            max_lines: self.max_lines,
            line_height: self.line_height,
            width: content_area_size.width,
        };
        let paragraph = context
            .text_cache
            .utilize(context.node_id, &cached_paragraph)
            .unwrap_or_else(|| {
                let build = |fill_area: Area| {
                    let mut paragraph_style = ParagraphStyle::default();

                    if let Some(ellipsis) = context.text_style_state.text_overflow.get_ellipsis() {
                        paragraph_style.set_ellipsis(ellipsis);
                    }

                    let text_style = context.text_style_state.to_text_style(
                        context.fallback_fonts,
                        context.scale_factor,
                        self.line_height,
                        fill_area,
                    );
                    paragraph_style.set_text_style(&text_style);
                    paragraph_style.set_max_lines(self.max_lines);
                    paragraph_style.set_text_align(context.text_style_state.text_align.into());

                    let mut paragraph_builder =
                        ParagraphBuilder::new(&paragraph_style, &*context.font_collection);

                    paragraph_builder.push_style(&text_style);
                    paragraph_builder.add_text(&self.text);

                    let mut paragraph = paragraph_builder.build();
                    paragraph.layout(
                        if self.max_lines == Some(1)
                            && context.text_style_state.text_align == TextAlign::default()
                            && !paragraph_style.ellipsized()
                        {
                            f32::MAX
                        } else {
                            content_area_size.width + 1.0
                        },
                    );
                    paragraph
                };

                let mut paragraph = build(Area::default());

                if context.text_style_state.color.as_color().is_none() {
                    paragraph = build(paragraph.fill_area());
                }

                context
                    .text_cache
                    .insert(context.node_id, &cached_paragraph, paragraph)
            });

        let size = Size2D::new(paragraph.longest_line(), paragraph.height())
            .max(Size2D::zero())
            .with_gaps(&context.torin_node.padding);

        Some((size, paragraph))
    }

    fn should_hook_measurement(&self) -> bool {
        true
    }

    fn should_measure_inner_children(&self) -> bool {
        false
    }

    fn clip(&self, context: ClipContext) {
        let area = context.visible_area;
        context.canvas.clip_rect(
            SkRect::new(area.min_x(), area.min_y(), area.max_x(), area.max_y()),
            ClipOp::Intersect,
            true,
        );
    }

    fn render(&self, context: RenderContext) {
        let layout_data = context.layout_node.data.as_ref().unwrap();
        let paragraph = layout_data.downcast_ref::<SkParagraph>().unwrap();

        let origin = context.layout_node.inner_area.origin;
        paragraph.paint_at(context.canvas, origin.cast_unit());
        if let Some(callback) = context.text_style_state.color.render_callback() {
            let layer = context.canvas.save();
            context.canvas.translate(origin.to_tuple());
            context
                .canvas
                .scale((context.scale_factor as f32, context.scale_factor as f32));
            callback.call(&mut FillRenderContext {
                canvas: context.canvas,
                font_collection: context.font_collection,
                origin: (origin / context.scale_factor as f32).cast_unit(),
                size: paragraph.fill_area().size / context.scale_factor as f32,
                text_style_state: context.text_style_state,
            });
            context.canvas.restore_to_count(layer);
        }
    }
}

impl From<Label> for Element {
    fn from(value: Label) -> Self {
        Element::Element {
            key: value.key,
            element: Rc::new(value.element),
            elements: vec![],
        }
    }
}

impl KeyExt for Label {
    fn write_key(&mut self) -> &mut DiffKey {
        &mut self.key
    }
}

impl EventHandlersExt for Label {
    fn get_event_handlers(&mut self) -> &mut EventHandlers {
        &mut self.element.event_handlers
    }
}

impl AccessibilityExt for Label {
    fn get_accessibility_data(&mut self) -> &mut AccessibilityData {
        &mut self.element.accessibility
    }
}

impl TextStyleExt for Label {
    fn get_text_style_data(&mut self) -> &mut TextStyleData {
        &mut self.element.text_style_data
    }
}

impl EffectExt for Label {
    fn get_effect(&mut self) -> &mut EffectData {
        self.element.effect.get_or_insert_with(EffectData::default)
    }
}

impl LayerExt for Label {
    fn get_layer(&mut self) -> &mut Layer {
        &mut self.element.relative_layer
    }
}

impl MaybeExt for Label {}

#[derive(Default, Clone)]
pub struct Label {
    key: DiffKey,
    element: LabelElement,
}

impl Label {
    pub fn try_downcast(element: &dyn ElementExt) -> Option<LabelElement> {
        (element as &dyn Any)
            .downcast_ref::<LabelElement>()
            .cloned()
    }

    /// Set the text content of the label.
    pub fn text(mut self, text: impl Into<Cow<'static, str>>) -> Self {
        let text = text.into();
        self.element.text = text;
        self
    }

    /// Limit the label to at most this many lines, truncating the rest. Pass `None` for no limit.
    pub fn max_lines(mut self, max_lines: impl Into<Option<usize>>) -> Self {
        self.element.max_lines = max_lines.into();
        self
    }

    /// Override the height of each line as a multiple of the font size. Pass `None` for the default.
    pub fn line_height(mut self, line_height: impl Into<Option<f32>>) -> Self {
        self.element.line_height = line_height.into();
        self
    }
}

impl LayoutExt for Label {
    fn get_layout(&mut self) -> &mut LayoutData {
        &mut self.element.layout
    }
}

impl ContainerExt for Label {}