freya-core 0.4.0-rc.7

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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! [paragraph()] makes it possible to render rich text with different styles. Its a more customizable API than [crate::elements::label].

use std::{
    any::Any,
    borrow::Cow,
    cell::RefCell,
    fmt::{
        Debug,
        Display,
    },
    rc::Rc,
};

use freya_engine::prelude::{
    FontStyle,
    Paint,
    PaintStyle,
    ParagraphBuilder,
    ParagraphStyle,
    RectHeightStyle,
    RectWidthStyle,
    SkParagraph,
    SkRect,
    TextStyle,
};
use rustc_hash::FxHashMap;
use torin::prelude::Size2D;

use crate::{
    data::{
        AccessibilityData,
        CursorStyleData,
        EffectData,
        LayoutData,
        StyleState,
        TextStyleData,
        TextStyleState,
    },
    diff_key::DiffKey,
    element::{
        Element,
        ElementExt,
        EventHandlerType,
        LayoutContext,
        RenderContext,
    },
    events::name::EventName,
    layers::Layer,
    prelude::{
        AccessibilityExt,
        Color,
        ContainerExt,
        EventHandlersExt,
        KeyExt,
        LayerExt,
        LayoutExt,
        MaybeExt,
        TextAlign,
        TextStyleExt,
    },
    style::cursor::CursorStyle,
    text_cache::CachedParagraph,
    tree::DiffModifies,
};

/// [paragraph()] makes it possible to render rich text with different styles. Its a more customizable API than [crate::elements::label].
///
/// See the available methods in [Paragraph].
///
/// ```rust
/// # use freya::prelude::*;
/// fn app() -> impl IntoElement {
///     paragraph()
///         .span(Span::new("Hello").font_size(24.0))
///         .span(Span::new("World").font_size(16.0))
/// }
/// ```
pub fn paragraph() -> Paragraph {
    Paragraph {
        key: DiffKey::None,
        element: ParagraphElement::default(),
    }
}

pub struct ParagraphHolderInner {
    pub paragraph: Rc<SkParagraph>,
    pub scale_factor: f64,
}

#[derive(Clone)]
pub struct ParagraphHolder(pub Rc<RefCell<Option<ParagraphHolderInner>>>);

impl PartialEq for ParagraphHolder {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.0, &other.0)
    }
}

impl Debug for ParagraphHolder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("ParagraphHolder")
    }
}

impl Default for ParagraphHolder {
    fn default() -> Self {
        Self(Rc::new(RefCell::new(None)))
    }
}

#[derive(PartialEq, Clone)]
pub struct ParagraphElement {
    pub layout: LayoutData,
    pub spans: Vec<Span<'static>>,
    pub accessibility: AccessibilityData,
    pub text_style_data: TextStyleData,
    pub cursor_style_data: CursorStyleData,
    pub event_handlers: FxHashMap<EventName, EventHandlerType>,
    pub sk_paragraph: ParagraphHolder,
    pub cursor_index: Option<usize>,
    pub highlights: Vec<(usize, usize)>,
    pub max_lines: Option<usize>,
    pub line_height: Option<f32>,
    pub relative_layer: Layer,
    pub cursor_style: CursorStyle,
}

impl Default for ParagraphElement {
    fn default() -> Self {
        let mut accessibility = AccessibilityData::default();
        accessibility.builder.set_role(accesskit::Role::Paragraph);
        Self {
            layout: Default::default(),
            spans: Default::default(),
            accessibility,
            text_style_data: Default::default(),
            cursor_style_data: Default::default(),
            event_handlers: Default::default(),
            sk_paragraph: Default::default(),
            cursor_index: Default::default(),
            highlights: Default::default(),
            max_lines: Default::default(),
            line_height: Default::default(),
            relative_layer: Default::default(),
            cursor_style: CursorStyle::default(),
        }
    }
}

impl Display for ParagraphElement {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(
            &self
                .spans
                .iter()
                .map(|s| s.text.clone())
                .collect::<Vec<_>>()
                .join("\n"),
        )
    }
}

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

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

        let mut diff = DiffModifies::empty();

        if self.spans != paragraph.spans {
            diff.insert(DiffModifies::STYLE);
            diff.insert(DiffModifies::LAYOUT);
        }

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

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

        if self.text_style_data != paragraph.text_style_data {
            diff.insert(DiffModifies::STYLE);
        }

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

        if self.cursor_index != paragraph.cursor_index || self.highlights != paragraph.highlights {
            diff.insert(DiffModifies::STYLE);
        }

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

        if self.layout != paragraph.layout {
            diff.insert(DiffModifies::STYLE);
            diff.insert(DiffModifies::LAYOUT);
        }

        diff
    }

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

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

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

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

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

    fn measure(&self, context: LayoutContext) -> Option<(Size2D, Rc<dyn Any>)> {
        let cached_paragraph = CachedParagraph {
            text_style_state: context.text_style_state,
            spans: &self.spans,
            max_lines: self.max_lines,
            line_height: self.line_height,
            width: context.area_size.width,
        };
        let paragraph = context
            .text_cache
            .utilize(context.node_id, &cached_paragraph)
            .unwrap_or_else(|| {
                let mut paragraph_style = ParagraphStyle::default();
                let mut text_style = TextStyle::default();

                let mut font_families = context.text_style_state.font_families.clone();
                font_families.extend_from_slice(context.fallback_fonts);

                text_style.set_color(context.text_style_state.color);
                text_style.set_font_size(
                    f32::from(context.text_style_state.font_size) * context.scale_factor as f32,
                );
                text_style.set_font_families(&font_families);
                text_style.set_font_style(FontStyle::new(
                    context.text_style_state.font_weight.into(),
                    context.text_style_state.font_width.into(),
                    context.text_style_state.font_slant.into(),
                ));

                if context.text_style_state.text_height.needs_custom_height() {
                    text_style.set_height_override(true);
                    text_style.set_half_leading(true);
                }

                if let Some(line_height) = self.line_height {
                    text_style.set_height_override(true).set_height(line_height);
                }

                for text_shadow in context.text_style_state.text_shadows.iter() {
                    text_style.add_shadow((*text_shadow).into());
                }

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

                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);

                for span in &self.spans {
                    let text_style_state =
                        TextStyleState::from_data(context.text_style_state, &span.text_style_data);
                    let mut text_style = TextStyle::new();
                    let mut font_families = context.text_style_state.font_families.clone();
                    font_families.extend_from_slice(context.fallback_fonts);

                    for text_shadow in text_style_state.text_shadows.iter() {
                        text_style.add_shadow((*text_shadow).into());
                    }

                    text_style.set_color(text_style_state.color);
                    text_style.set_font_size(
                        f32::from(text_style_state.font_size) * context.scale_factor as f32,
                    );
                    text_style.set_font_families(&font_families);
                    paragraph_builder.push_style(&text_style);
                    paragraph_builder.add_text(&span.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 {
                        context.area_size.width + 1.0
                    },
                );
                context
                    .text_cache
                    .insert(context.node_id, &cached_paragraph, paragraph)
            });

        let size = Size2D::new(paragraph.longest_line(), paragraph.height());

        self.sk_paragraph
            .0
            .borrow_mut()
            .replace(ParagraphHolderInner {
                paragraph,
                scale_factor: context.scale_factor,
            });

        Some((size, Rc::new(())))
    }

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

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

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

    fn render(&self, context: RenderContext) {
        let paragraph = self.sk_paragraph.0.borrow();
        let ParagraphHolderInner { paragraph, .. } = paragraph.as_ref().unwrap();
        let area = context.layout_node.visible_area();

        // Draw highlights
        for (from, to) in self.highlights.iter() {
            let (from, to) = { if from < to { (from, to) } else { (to, from) } };
            let rects = paragraph.get_rects_for_range(
                *from..*to,
                RectHeightStyle::Tight,
                RectWidthStyle::Tight,
            );

            let mut highlights_paint = Paint::default();
            highlights_paint.set_anti_alias(true);
            highlights_paint.set_style(PaintStyle::Fill);
            highlights_paint.set_color(self.cursor_style_data.highlight_color);

            // TODO: Add a expanded option for highlights and cursor

            for rect in rects {
                let rect = SkRect::new(
                    area.min_x() + rect.rect.left,
                    area.min_y() + rect.rect.top,
                    area.min_x() + rect.rect.right,
                    area.min_y() + rect.rect.bottom,
                );
                context.canvas.draw_rect(rect, &highlights_paint);
            }
        }

        // We exclude those highlights that on the same start and end (e.g the user just started dragging)
        let visible_highlights = self
            .highlights
            .iter()
            .filter(|highlight| highlight.0 != highlight.1)
            .count()
            > 0;

        // Draw block cursor behind text if needed
        if let Some(cursor_index) = self.cursor_index
            && self.cursor_style == CursorStyle::Block
            && let Some(cursor_rect) = paragraph
                .get_rects_for_range(
                    cursor_index..cursor_index + 1,
                    RectHeightStyle::Tight,
                    RectWidthStyle::Tight,
                )
                .first()
                .map(|text| text.rect)
                .or_else(|| {
                    // Show the cursor at the end of the text if possible
                    let text_len = paragraph
                        .get_glyph_position_at_coordinate((f32::MAX, f32::MAX))
                        .position as usize;
                    let last_rects = paragraph.get_rects_for_range(
                        (text_len - 1)..text_len,
                        RectHeightStyle::Tight,
                        RectWidthStyle::Tight,
                    );

                    if let Some(last_rect) = last_rects.first() {
                        let mut caret = last_rect.rect;
                        caret.left = caret.right;
                        Some(caret)
                    } else {
                        None
                    }
                })
        {
            let width = (cursor_rect.right - cursor_rect.left).max(6.0);
            let cursor_rect = SkRect::new(
                area.min_x() + cursor_rect.left,
                area.min_y() + cursor_rect.top,
                area.min_x() + cursor_rect.left + width,
                area.min_y() + cursor_rect.bottom,
            );

            let mut paint = Paint::default();
            paint.set_anti_alias(true);
            paint.set_style(PaintStyle::Fill);
            paint.set_color(self.cursor_style_data.color);

            context.canvas.draw_rect(cursor_rect, &paint);
        }

        // Draw text
        paragraph.paint(context.canvas, area.origin.to_tuple());

        // Draw cursor
        if let Some(cursor_index) = self.cursor_index
            && !visible_highlights
        {
            let cursor_rects = paragraph.get_rects_for_range(
                cursor_index..cursor_index + 1,
                RectHeightStyle::Tight,
                RectWidthStyle::Tight,
            );
            if let Some(cursor_rect) = cursor_rects.first().map(|text| text.rect).or_else(|| {
                // Show the cursor at the end of the text if possible
                let text_len = paragraph
                    .get_glyph_position_at_coordinate((f32::MAX, f32::MAX))
                    .position as usize;
                let last_rects = paragraph.get_rects_for_range(
                    (text_len - 1)..text_len,
                    RectHeightStyle::Tight,
                    RectWidthStyle::Tight,
                );

                if let Some(last_rect) = last_rects.first() {
                    let mut caret = last_rect.rect;
                    caret.left = caret.right;
                    Some(caret)
                } else {
                    None
                }
            }) {
                let paint_color = self.cursor_style_data.color;
                match self.cursor_style {
                    CursorStyle::Underline => {
                        let thickness = 2.0_f32;
                        let underline_rect = SkRect::new(
                            area.min_x() + cursor_rect.left,
                            area.min_y() + cursor_rect.bottom - thickness,
                            area.min_x() + cursor_rect.right,
                            area.min_y() + cursor_rect.bottom,
                        );

                        let mut paint = Paint::default();
                        paint.set_anti_alias(true);
                        paint.set_style(PaintStyle::Fill);
                        paint.set_color(paint_color);

                        context.canvas.draw_rect(underline_rect, &paint);
                    }
                    CursorStyle::Line => {
                        let cursor_rect = SkRect::new(
                            area.min_x() + cursor_rect.left,
                            area.min_y() + cursor_rect.top,
                            area.min_x() + cursor_rect.left + 2.,
                            area.min_y() + cursor_rect.bottom,
                        );

                        let mut paint = Paint::default();
                        paint.set_anti_alias(true);
                        paint.set_style(PaintStyle::Fill);
                        paint.set_color(paint_color);

                        context.canvas.draw_rect(cursor_rect, &paint);
                    }
                    _ => {}
                }
            }
        }
    }
}

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

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

impl EventHandlersExt for Paragraph {
    fn get_event_handlers(&mut self) -> &mut FxHashMap<EventName, EventHandlerType> {
        &mut self.element.event_handlers
    }
}

impl MaybeExt for Paragraph {}

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

pub struct Paragraph {
    key: DiffKey,
    element: ParagraphElement,
}

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

impl ContainerExt for Paragraph {}

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

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

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

    pub fn spans_iter(mut self, spans: impl Iterator<Item = Span<'static>>) -> Self {
        let spans = spans.collect::<Vec<Span>>();
        // TODO: Accessible paragraphs
        // self.element.accessibility.builder.set_value(text.clone());
        self.element.spans.extend(spans);
        self
    }

    pub fn span(mut self, span: impl Into<Span<'static>>) -> Self {
        let span = span.into();
        // TODO: Accessible paragraphs
        // self.element.accessibility.builder.set_value(text.clone());
        self.element.spans.push(span);
        self
    }

    pub fn cursor_color(mut self, cursor_color: impl Into<Color>) -> Self {
        self.element.cursor_style_data.color = cursor_color.into();
        self
    }

    pub fn highlight_color(mut self, highlight_color: impl Into<Color>) -> Self {
        self.element.cursor_style_data.highlight_color = highlight_color.into();
        self
    }

    pub fn cursor_style(mut self, cursor_style: impl Into<CursorStyle>) -> Self {
        self.element.cursor_style = cursor_style.into();
        self
    }

    pub fn holder(mut self, holder: ParagraphHolder) -> Self {
        self.element.sk_paragraph = holder;
        self
    }

    pub fn cursor_index(mut self, cursor_index: impl Into<Option<usize>>) -> Self {
        self.element.cursor_index = cursor_index.into();
        self
    }

    pub fn highlights(mut self, highlights: impl Into<Option<Vec<(usize, usize)>>>) -> Self {
        if let Some(highlights) = highlights.into() {
            self.element.highlights = highlights;
        }
        self
    }

    pub fn max_lines(mut self, max_lines: impl Into<Option<usize>>) -> Self {
        self.element.max_lines = max_lines.into();
        self
    }

    pub fn line_height(mut self, line_height: impl Into<Option<f32>>) -> Self {
        self.element.line_height = line_height.into();
        self
    }
}

#[derive(Clone, PartialEq, Hash)]
pub struct Span<'a> {
    pub text_style_data: TextStyleData,
    pub text: Cow<'a, str>,
}

impl From<&'static str> for Span<'static> {
    fn from(text: &'static str) -> Self {
        Span {
            text_style_data: TextStyleData::default(),
            text: text.into(),
        }
    }
}

impl From<String> for Span<'static> {
    fn from(text: String) -> Self {
        Span {
            text_style_data: TextStyleData::default(),
            text: text.into(),
        }
    }
}

impl<'a> Span<'a> {
    pub fn new(text: impl Into<Cow<'a, str>>) -> Self {
        Self {
            text: text.into(),
            text_style_data: TextStyleData::default(),
        }
    }
}

impl<'a> TextStyleExt for Span<'a> {
    fn get_text_style_data(&mut self) -> &mut TextStyleData {
        &mut self.text_style_data
    }
}