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
use dioxus_native_core::{prelude::NodeType, real_dom::NodeImmutable, tree::TreeRef, NodeId};
use freya_node_state::LayoutState;
use rustc_hash::FxHashMap;
use torin::prelude::*;

use crate::dom::DioxusDOM;

/// RealDOM adapter for Torin.
pub struct DioxusDOMAdapter<'a> {
    pub rdom: &'a DioxusDOM,

    valid_nodes_cache: Option<FxHashMap<NodeId, bool>>,
}

impl<'a> DioxusDOMAdapter<'a> {
    pub fn new(rdom: &'a DioxusDOM) -> Self {
        Self {
            rdom,
            valid_nodes_cache: None,
        }
    }

    pub fn new_with_cache(rdom: &'a DioxusDOM) -> Self {
        Self {
            rdom,
            valid_nodes_cache: Some(FxHashMap::default()),
        }
    }
}

impl DOMAdapter<NodeId> for DioxusDOMAdapter<'_> {
    fn get_node(&self, node_id: &NodeId) -> Option<Node> {
        let node = self.rdom.get(*node_id)?;
        let mut size = node.get::<LayoutState>().unwrap().clone();

        // The root node expands by default
        if *node_id == self.rdom.root_id() {
            size.width = Size::Percentage(Length::new(100.0));
            size.height = Size::Percentage(Length::new(100.0));
        }

        Some(Node {
            width: size.width,
            height: size.height,
            minimum_width: size.minimum_width,
            minimum_height: size.minimum_height,
            maximum_width: size.maximum_width,
            maximum_height: size.maximum_height,
            direction: size.direction,
            padding: size.padding,
            margin: size.margin,
            main_alignment: size.main_alignment,
            cross_alignment: size.cross_alignment,
            offset_x: Length::new(size.offset_x),
            offset_y: Length::new(size.offset_y),
            has_layout_references: size.node_ref.is_some(),
        })
    }

    fn height(&self, node_id: &NodeId) -> Option<u16> {
        self.rdom.tree_ref().height(*node_id)
    }

    fn parent_of(&self, node_id: &NodeId) -> Option<NodeId> {
        self.rdom.tree_ref().parent_id(*node_id)
    }

    fn children_of(&mut self, node_id: &NodeId) -> Vec<NodeId> {
        self.rdom
            .tree_ref()
            .children_ids(*node_id)
            .into_iter()
            .filter(|id| is_node_valid(self.rdom, &mut self.valid_nodes_cache, id))
            .collect::<Vec<NodeId>>()
    }

    fn is_node_valid(&mut self, node_id: &NodeId) -> bool {
        is_node_valid(self.rdom, &mut self.valid_nodes_cache, node_id)
    }

    fn closest_common_parent(&self, node_id_a: &NodeId, node_id_b: &NodeId) -> Option<NodeId> {
        find_common_parent(self.rdom, *node_id_a, *node_id_b)
    }
}

/// Walk to the ancestor of `base` with the same height of `target`
fn balance_heights(rdom: &DioxusDOM, base: NodeId, target: NodeId) -> Option<NodeId> {
    let tree = rdom.tree_ref();
    let target_height = tree.height(target)?;
    let mut current = base;
    loop {
        if tree.height(current)? == target_height {
            break;
        }

        let parent_current = tree.parent_id(current);
        if let Some(parent_current) = parent_current {
            current = parent_current;
        }
    }
    Some(current)
}

/// Return the closest common ancestor of both Nodes
fn find_common_parent(rdom: &DioxusDOM, node_a: NodeId, node_b: NodeId) -> Option<NodeId> {
    let tree = rdom.tree_ref();
    let height_a = tree.height(node_a)?;
    let height_b = tree.height(node_b)?;

    let (node_a, node_b) = match height_a.cmp(&height_b) {
        std::cmp::Ordering::Less => (
            node_a,
            balance_heights(rdom, node_b, node_a).unwrap_or(node_b),
        ),
        std::cmp::Ordering::Equal => (node_a, node_b),
        std::cmp::Ordering::Greater => (
            balance_heights(rdom, node_a, node_b).unwrap_or(node_a),
            node_b,
        ),
    };

    let mut currents = (node_a, node_b);

    loop {
        // Common parent of node_a and node_b
        if currents.0 == currents.1 {
            return Some(currents.0);
        }

        let parent_a = tree.parent_id(currents.0);
        if let Some(parent_a) = parent_a {
            currents.0 = parent_a;
        } else if rdom.root_id() != currents.0 {
            // Skip unconected nodes
            break;
        }

        let parent_b = tree.parent_id(currents.1);
        if let Some(parent_b) = parent_b {
            currents.1 = parent_b;
        } else if rdom.root_id() != currents.1 {
            // Skip unconected nodes
            break;
        }
    }

    None
}

/// Check is the given Node is valid or not, this means not being a placeholder or an unconnected Node.
fn is_node_valid(
    rdom: &DioxusDOM,
    valid_nodes_cache: &mut Option<FxHashMap<NodeId, bool>>,
    node_id: &NodeId,
) -> bool {
    // Check if Node was valid from cache
    if let Some(valid_nodes_cache) = valid_nodes_cache {
        if let Some(is_valid) = valid_nodes_cache.get(node_id) {
            return *is_valid;
        }
    }

    let node = rdom.get(*node_id);

    let is_valid = 'validation: {
        if let Some(node) = node {
            let is_placeholder = matches!(*node.node_type(), NodeType::Placeholder);

            // Placeholders can't be measured
            if is_placeholder {
                break 'validation false;
            }

            // Make sure this Node isn't part of an unconnected Node
            // This walkes up to the ancestor that has a height of 0 and checks if it has the same ID as the root Node
            // If it has the same ID, it means that is not an unconnected ID, otherwise, it is and should be skipped.
            let tree = rdom.tree_ref();
            let mut current = *node_id;
            loop {
                let height = tree.height(current);
                if let Some(height) = height {
                    if height == 0 {
                        break;
                    }
                }

                let parent_current = tree.parent_id(current);
                if let Some(parent_current) = parent_current {
                    current = parent_current;
                }
            }

            current == rdom.root_id()
        } else {
            false
        }
    };

    // Save the validation result in the cache
    if let Some(valid_nodes_cache) = valid_nodes_cache {
        valid_nodes_cache.insert(*node_id, is_valid);
    }

    is_valid
}