freya_core/elements/
utils.rs

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
use freya_engine::prelude::{
    Canvas,
    FontCollection,
    FontMgr,
};
use freya_native_core::{
    tags::TagName,
    NodeId,
};
use torin::{
    prelude::{
        Area,
        AreaModel,
        CursorPoint,
        LayoutNode,
    },
    torin::Torin,
};

use super::*;
use crate::{
    dom::{
        DioxusNode,
        ImagesCache,
    },
    states::{
        StyleState,
        TransformState,
        ViewportState,
    },
};

pub trait ElementUtils {
    fn is_point_inside_area(
        &self,
        point: &CursorPoint,
        _node_ref: &DioxusNode,
        layout_node: &LayoutNode,
        _scale_factor: f32,
    ) -> bool {
        layout_node.area.contains(point.to_f32())
    }

    fn clip(
        &self,
        _layout_node: &LayoutNode,
        _node_ref: &DioxusNode,
        _canvas: &Canvas,
        _scale_factor: f32,
    ) {
    }

    #[allow(clippy::too_many_arguments)]
    fn render(
        self,
        layout_node: &LayoutNode,
        node_ref: &DioxusNode,
        canvas: &Canvas,
        font_collection: &mut FontCollection,
        font_manager: &FontMgr,
        fallback_fonts: &[String],
        images_cache: &mut ImagesCache,
        scale_factor: f32,
    );

    fn element_drawing_area(
        &self,
        layout_node: &LayoutNode,
        _node_ref: &DioxusNode,
        _scale_factor: f32,
        _node_style: &StyleState,
    ) -> Area {
        // Images neither SVG elements have support for shadows or borders, so its fine so simply return the visible area.
        layout_node.visible_area()
    }

    /// Check if this element requires any kind of special caching.
    /// Mainly used for text-like elements with shadows.
    /// See [crate::render::CompositorCache].
    /// Default to `false`.
    #[inline]
    fn element_needs_cached_area(&self, _node_ref: &DioxusNode, _style_state: &StyleState) -> bool {
        false
    }
}

pub trait ElementUtilsResolver {
    fn utils(&self) -> Option<ElementWithUtils>;
}

impl ElementUtilsResolver for TagName {
    #[inline]
    fn utils(&self) -> Option<ElementWithUtils> {
        match self {
            TagName::Rect => Some(ElementWithUtils::Rect(RectElement)),
            TagName::Svg => Some(ElementWithUtils::Svg(SvgElement)),
            TagName::Paragraph => Some(ElementWithUtils::Paragraph(ParagraphElement)),
            TagName::Image => Some(ElementWithUtils::Image(ImageElement)),
            TagName::Label => Some(ElementWithUtils::Label(LabelElement)),
            _ => None,
        }
    }
}

pub enum ElementWithUtils {
    Rect(RectElement),
    Svg(SvgElement),
    Paragraph(ParagraphElement),
    Image(ImageElement),
    Label(LabelElement),
}

impl ElementWithUtils {
    /// Measure the area that this element once rendered will use.
    /// This takes into consideration things like borders and shadows.
    pub fn get_drawing_area(
        &self,
        layout_node: &LayoutNode,
        node_ref: &DioxusNode,
        layout: &Torin<NodeId>,
        scale_factor: f32,
        node_style: &StyleState,
        transform_state: &TransformState,
    ) -> Area {
        let mut drawing_area =
            self.element_drawing_area(layout_node, node_ref, scale_factor, node_style);

        // Apply scale effect
        for (id, scale_x, scale_y) in &transform_state.scales {
            let layout_node = layout.get(*id).unwrap();
            let center = layout_node.area.center();
            drawing_area = drawing_area.translate(-center.to_vector());
            drawing_area = drawing_area.scale(*scale_x, *scale_y);
            drawing_area = drawing_area.translate(center.to_vector());
            drawing_area = drawing_area.inflate(1.0, 1.0);
        }

        if !transform_state.rotations.is_empty() {
            // Apply rotation effect
            let area = layout_node.visible_area();
            drawing_area.max_area_when_rotated(area.center())
        } else {
            drawing_area
        }
    }

    /// Just like [Self::get_drawing_area] but only if all the viewports allow the element to be visible.
    #[allow(clippy::too_many_arguments)]
    pub fn get_drawing_area_if_viewports_allow(
        &self,
        layout_node: &LayoutNode,
        node_ref: &DioxusNode,
        layout: &Torin<NodeId>,
        scale_factor: f32,
        node_style: &StyleState,
        node_viewports: &ViewportState,
        transform_state: &TransformState,
    ) -> Option<Area> {
        let mut drawing_area = self.get_drawing_area(
            layout_node,
            node_ref,
            layout,
            scale_factor,
            node_style,
            transform_state,
        );

        for viewport_id in &node_viewports.viewports {
            let viewport = layout.get(*viewport_id).unwrap().visible_area();
            drawing_area.clip(&viewport);
            if !viewport.intersects(&drawing_area) {
                return None;
            }
        }

        // Inflate the area by 1px in each side to cover potential off-bounds rendering caused by antialising
        Some(drawing_area.inflate(1.0, 1.0))
    }

    /// Check if this element needs its drawing area to be cached in case it needs to be
    /// invalidated in the next frame due to potential rotation or scale changes.
    #[inline]
    pub fn needs_cached_drawing_area(
        &self,
        node_ref: &DioxusNode,
        transform_state: &TransformState,
        style_state: &StyleState,
    ) -> bool {
        let element_check = self.element_needs_cached_area(node_ref, style_state);

        let rotate_effect = !transform_state.rotations.is_empty();
        let scales_effect = !transform_state.scales.is_empty();

        element_check || rotate_effect || scales_effect
    }

    /// Some elements such as `rect` might always need to rerender as Skia doesnt work well with clipped canvases with applied blur.
    pub fn needs_explicit_render(
        &self,
        node_transform: &TransformState,
        node_style: &StyleState,
    ) -> bool {
        match self {
            Self::Rect(el) => el.has_blur(node_transform, node_style),
            _ => false,
        }
    }
}

impl ElementUtils for ElementWithUtils {
    fn clip(
        &self,
        layout_node: &LayoutNode,
        node_ref: &DioxusNode,
        canvas: &Canvas,
        scale_factor: f32,
    ) {
        match self {
            Self::Rect(el) => el.clip(layout_node, node_ref, canvas, scale_factor),
            Self::Svg(el) => el.clip(layout_node, node_ref, canvas, scale_factor),
            Self::Paragraph(el) => el.clip(layout_node, node_ref, canvas, scale_factor),
            Self::Image(el) => el.clip(layout_node, node_ref, canvas, scale_factor),
            Self::Label(el) => el.clip(layout_node, node_ref, canvas, scale_factor),
        }
    }

    fn is_point_inside_area(
        &self,
        point: &CursorPoint,
        node_ref: &DioxusNode,
        layout_node: &LayoutNode,
        scale_factor: f32,
    ) -> bool {
        match self {
            Self::Rect(el) => el.is_point_inside_area(point, node_ref, layout_node, scale_factor),
            Self::Svg(el) => el.is_point_inside_area(point, node_ref, layout_node, scale_factor),
            Self::Paragraph(el) => {
                el.is_point_inside_area(point, node_ref, layout_node, scale_factor)
            }
            Self::Image(el) => el.is_point_inside_area(point, node_ref, layout_node, scale_factor),
            Self::Label(el) => el.is_point_inside_area(point, node_ref, layout_node, scale_factor),
        }
    }

    fn render(
        self,
        layout_node: &LayoutNode,
        node_ref: &DioxusNode,
        canvas: &Canvas,
        font_collection: &mut FontCollection,
        font_manager: &FontMgr,
        fallback_fonts: &[String],
        images_cache: &mut ImagesCache,
        scale_factor: f32,
    ) {
        match self {
            Self::Rect(el) => el.render(
                layout_node,
                node_ref,
                canvas,
                font_collection,
                font_manager,
                fallback_fonts,
                images_cache,
                scale_factor,
            ),
            Self::Svg(el) => el.render(
                layout_node,
                node_ref,
                canvas,
                font_collection,
                font_manager,
                fallback_fonts,
                images_cache,
                scale_factor,
            ),
            Self::Paragraph(el) => el.render(
                layout_node,
                node_ref,
                canvas,
                font_collection,
                font_manager,
                fallback_fonts,
                images_cache,
                scale_factor,
            ),
            Self::Image(el) => el.render(
                layout_node,
                node_ref,
                canvas,
                font_collection,
                font_manager,
                fallback_fonts,
                images_cache,
                scale_factor,
            ),
            Self::Label(el) => el.render(
                layout_node,
                node_ref,
                canvas,
                font_collection,
                font_manager,
                fallback_fonts,
                images_cache,
                scale_factor,
            ),
        }
    }

    fn element_drawing_area(
        &self,
        layout_node: &LayoutNode,
        node_ref: &DioxusNode,
        scale_factor: f32,
        node_style: &StyleState,
    ) -> Area {
        match self {
            Self::Rect(el) => {
                el.element_drawing_area(layout_node, node_ref, scale_factor, node_style)
            }
            Self::Svg(el) => {
                el.element_drawing_area(layout_node, node_ref, scale_factor, node_style)
            }
            Self::Paragraph(el) => {
                el.element_drawing_area(layout_node, node_ref, scale_factor, node_style)
            }
            Self::Image(el) => {
                el.element_drawing_area(layout_node, node_ref, scale_factor, node_style)
            }
            Self::Label(el) => {
                el.element_drawing_area(layout_node, node_ref, scale_factor, node_style)
            }
        }
    }

    fn element_needs_cached_area(&self, node_ref: &DioxusNode, style_state: &StyleState) -> bool {
        match self {
            Self::Rect(el) => el.element_needs_cached_area(node_ref, style_state),
            Self::Svg(el) => el.element_needs_cached_area(node_ref, style_state),
            Self::Paragraph(el) => el.element_needs_cached_area(node_ref, style_state),
            Self::Image(el) => el.element_needs_cached_area(node_ref, style_state),
            Self::Label(el) => el.element_needs_cached_area(node_ref, style_state),
        }
    }
}