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
use freya_engine::prelude::*;
use freya_native_core::prelude::NodeImmutable;
use itertools::Itertools;
use torin::geometry::Area;

use crate::{
    dom::*,
    prelude::{
        Compositor,
        NodeAccessibility,
    },
    render::SkiaMeasurer,
};

/// Process the layout of the DOM
pub fn process_layout(
    fdom: &FreyaDOM,
    area: Area,
    font_collection: &mut FontCollection,
    scale_factor: f32,
    default_fonts: &[String],
) {
    {
        let rdom = fdom.rdom();
        let mut dom_adapter = DioxusDOMAdapter::new(rdom, scale_factor);
        let skia_measurer = SkiaMeasurer::new(rdom, font_collection, default_fonts, scale_factor);

        let mut layout = fdom.layout();

        // Finds the best Node from where to start measuring
        layout.find_best_root(&mut dom_adapter);

        let mut dirty_accessibility_tree = fdom.accessibility_dirty_nodes();
        let mut compositor_dirty_nodes = fdom.compositor_dirty_nodes();
        let mut compositor_dirty_area = fdom.compositor_dirty_area();
        let mut buffer = layout.dirty.iter().copied().collect_vec();
        while let Some(node_id) = buffer.pop() {
            if let Some(area) = Compositor::get_drawing_area(node_id, &layout, rdom, scale_factor) {
                // Unite the invalidated area with the dirty area
                compositor_dirty_area.unite_or_insert(&area);

                // Mark these elements as dirty for the compositor
                compositor_dirty_nodes.insert(node_id);

                // Continue iterating in the children of this node
                if let Some(node) = rdom.get(node_id) {
                    // Mark as invalidated this node as its layout has changed
                    if node.get_accessibility_id().is_some() {
                        dirty_accessibility_tree.add_or_update(node_id);
                    }

                    buffer.extend(node.child_ids());
                }
            }
        }
        let root_id = fdom.rdom().root_id();

        // Measure the layout
        layout.measure(root_id, area, &mut Some(skia_measurer), &mut dom_adapter);
    }
}