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
use dioxus_native_core::node::NodeType;
use dioxus_native_core::real_dom::NodeImmutable;

use freya_dom::prelude::FreyaDOM;
use freya_layout::Layers;

use freya_node_state::{OverflowMode, Style};
use rustc_hash::FxHashMap;

use crate::ViewportsCollection;

// Calculate all the applicable viewports for the given nodes
pub fn calculate_viewports(
    layers_nums: &[&i16],
    layers: &Layers,
    fdom: &FreyaDOM,
) -> ViewportsCollection {
    let mut viewports_collection = FxHashMap::default();
    let layout = fdom.layout();
    let rdom = fdom.rdom();

    for layer_num in layers_nums {
        let layer = layers.layers.get(layer_num).unwrap();
        for node_id in layer {
            let node = rdom.get(*node_id);
            let node_areas = layout.get(*node_id);

            if let Some((node, node_areas)) = node.zip(node_areas) {
                let node_type = &*node.node_type();

                if let NodeType::Element(..) = node_type {
                    let style = node.get::<Style>().unwrap();

                    // Clip any overflow from it's children
                    if style.overflow == OverflowMode::Clip {
                        let viewport = viewports_collection
                            .entry(*node_id)
                            .or_insert_with(|| (None, Vec::new()));
                        viewport.0 = Some(node_areas.visible_area());
                    }

                    // Pass viewports to the children
                    if let Some((_, mut inherited_viewports)) =
                        viewports_collection.get(node_id).cloned()
                    {
                        // Only pass the inherited viewports if they are not empty
                        // or this same element has a clipped overflow
                        if !inherited_viewports.is_empty() || style.overflow == OverflowMode::Clip {
                            // Add itself
                            inherited_viewports.push(*node_id);

                            for child in node.children() {
                                if let NodeType::Element(..) = *child.node_type() {
                                    viewports_collection
                                        .entry(child.id())
                                        .or_insert_with(|| (None, Vec::new()))
                                        .1 = inherited_viewports.clone();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    viewports_collection
}