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
use std::{collections::HashMap, mem};

pub use euclid::Rect;
use rustc_hash::{FxHashMap, FxHashSet};
use tracing::info;

use crate::{
    custom_measurer::LayoutMeasurer,
    dom_adapter::{DOMAdapter, NodeAreas, NodeKey},
    geometry::{Area, Size2D},
    measure::measure_node,
    prelude::Gaps,
};

/// Contains the best Root node candidate from where to start measuring
#[derive(PartialEq, Debug, Clone)]
pub enum RootNodeCandidate<Key: NodeKey> {
    /// A valid Node ID
    Valid(Key),

    /// None
    None,
}

impl<Key: NodeKey> RootNodeCandidate<Key> {
    pub fn take(&mut self) -> Self {
        mem::replace(self, Self::None)
    }
}

pub struct Torin<Key: NodeKey> {
    /// Layout results of the registered Nodes
    pub results: FxHashMap<Key, NodeAreas>,

    /// Invalid registered nodes since previous layout measurement
    pub dirty: FxHashSet<Key>,

    /// Best Root node candidate from where to start measuring
    pub root_node_candidate: RootNodeCandidate<Key>,
}

impl<Key: NodeKey> Default for Torin<Key> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "dioxus")]
use dioxus_core::Mutations;
#[cfg(feature = "dioxus")]
use dioxus_native_core::prelude::*;

#[cfg(feature = "dioxus")]
impl Torin<NodeId> {
    pub fn apply_mutations(
        &mut self,
        mutations: &Mutations,
        dioxus_integration_state: &DioxusState,
        dom_adapter: &mut impl DOMAdapter<NodeId>,
    ) {
        use dioxus_core::Mutation;

        for mutation in &mutations.edits {
            match mutation {
                Mutation::SetText { id, .. } => {
                    self.invalidate(dioxus_integration_state.element_to_node_id(*id));
                }
                Mutation::InsertAfter { id, m } => {
                    if *m > 0 {
                        self.invalidate(dioxus_integration_state.element_to_node_id(*id));
                    }
                }
                Mutation::InsertBefore { id, m } => {
                    if *m > 0 {
                        self.invalidate(dioxus_integration_state.element_to_node_id(*id));
                    }
                }
                Mutation::Remove { id } => {
                    self.remove(
                        dioxus_integration_state.element_to_node_id(*id),
                        dom_adapter,
                        true,
                    );
                }
                Mutation::ReplaceWith { id, m } => {
                    if *m > 0 {
                        self.remove(
                            dioxus_integration_state.element_to_node_id(*id),
                            dom_adapter,
                            true,
                        );
                    }
                }
                _ => {}
            }
        }
    }
}

impl<Key: NodeKey> Torin<Key> {
    /// Create a new Layout
    pub fn new() -> Self {
        Self {
            results: HashMap::default(),
            dirty: FxHashSet::default(),
            root_node_candidate: RootNodeCandidate::None,
        }
    }

    /// Reset the layout
    pub fn reset(&mut self) {
        self.root_node_candidate = RootNodeCandidate::None;
        self.results.clear();
        self.dirty.clear();
    }

    /// Read the HashSet of dirty nodes
    pub fn get_dirty_nodes(&self) -> &FxHashSet<Key> {
        &self.dirty
    }

    /// Remove a Node's result and data
    pub fn raw_remove(&mut self, node_id: Key) {
        self.results.remove(&node_id);
        self.dirty.remove(&node_id);
        if let RootNodeCandidate::Valid(id) = self.root_node_candidate {
            if id == node_id {
                self.root_node_candidate = RootNodeCandidate::None
            }
        }
    }

    /// Remove a Node from the layout
    pub fn remove(
        &mut self,
        node_id: Key,
        dom_adapter: &mut impl DOMAdapter<Key>,
        invalidate_parent: bool,
    ) {
        // Remove itself
        self.raw_remove(node_id);

        // Mark as dirty the Node's parent
        if invalidate_parent {
            self.invalidate(dom_adapter.parent_of(&node_id).unwrap());
        }

        // Remove all it's children
        for child_id in dom_adapter.children_of(&node_id) {
            self.remove(child_id, dom_adapter, false);
        }
    }

    /// Mark as dirty a Node
    pub fn invalidate(&mut self, node_id: Key) {
        self.dirty.insert(node_id);
    }

    pub fn safe_invalidate(&mut self, node_id: Key, dom_adapter: &mut impl DOMAdapter<Key>) {
        if dom_adapter.is_node_valid(&node_id) {
            self.invalidate(node_id)
        }
    }

    // Mark as dirty the given Node and all the nodes that depend on it
    pub fn check_dirty_dependants(
        &mut self,
        node_id: Key,
        dom_adapter: &mut impl DOMAdapter<Key>,
        ignore: bool,
    ) {
        if (self.dirty.contains(&node_id) && ignore) || !dom_adapter.is_node_valid(&node_id) {
            return;
        }

        // Mark this node as dirty
        self.invalidate(node_id);

        if RootNodeCandidate::None == self.root_node_candidate {
            self.root_node_candidate = RootNodeCandidate::Valid(node_id);
        } else if let RootNodeCandidate::Valid(root_candidate) = &mut self.root_node_candidate {
            if node_id != *root_candidate {
                let closest_parent = dom_adapter.closest_common_parent(&node_id, root_candidate);

                if let Some(closest_parent) = closest_parent {
                    *root_candidate = closest_parent;
                }
            }
        }

        // Mark as dirty this Node's children
        for child in dom_adapter.children_of(&node_id) {
            self.check_dirty_dependants(child, dom_adapter, true)
        }

        // Mark this Node's parent if it is affected
        let parent_id = dom_adapter.parent_of(&node_id);

        if let Some(parent_id) = parent_id {
            let parent = dom_adapter.get_node(&parent_id);

            if let Some(parent) = parent {
                // Mark parent if it depeneds on it's inner children
                if parent.does_depend_on_inner() {
                    self.check_dirty_dependants(parent_id, dom_adapter, true);
                }
                // Mark as dirty all the siblings that come after this node
                else {
                    let mut found_node = false;
                    let mut multiple_children = false;
                    for child_id in dom_adapter.children_of(&parent_id) {
                        if found_node {
                            self.check_dirty_dependants(child_id, dom_adapter, true);
                        }
                        if child_id == node_id {
                            found_node = true;
                        } else {
                            multiple_children = true;
                        }
                    }

                    // Try saving using  node's parent as root candidate if it has multiple children
                    if multiple_children {
                        if let RootNodeCandidate::Valid(root_candidate) = self.root_node_candidate {
                            let closest_parent =
                                dom_adapter.closest_common_parent(&parent_id, &root_candidate);

                            if let Some(closest_parent) = closest_parent {
                                self.root_node_candidate = RootNodeCandidate::Valid(closest_parent);
                            }
                        }
                    }
                }
            }
        }
    }

    /// Get the Root Node candidate
    pub fn get_root_candidate(&self) -> RootNodeCandidate<Key> {
        self.root_node_candidate.clone()
    }

    /// Find the best root Node from where to start measuring
    pub fn find_best_root(&mut self, dom_adapter: &mut impl DOMAdapter<Key>) {
        if self.results.is_empty() {
            return;
        }
        for dirty in self.dirty.clone() {
            self.check_dirty_dependants(dirty, dom_adapter, false);
        }
    }

    /// Measure dirty Nodes
    pub fn measure(
        &mut self,
        suggested_root_id: Key,
        suggested_root_area: Area,
        measurer: &mut Option<impl LayoutMeasurer<Key>>,
        dom_adapter: &mut impl DOMAdapter<Key>,
    ) {
        // If there are previosuly cached results
        // But no dirty nodes, we can simply skip the measurement
        // as this means no changes has been made to the layout
        if self.dirty.is_empty() && !self.results.is_empty() {
            return;
        }

        // Try the Root candidate otherwise use the provided Root
        let root_id = if let RootNodeCandidate::Valid(id) = self.root_node_candidate.take() {
            id
        } else {
            suggested_root_id
        };
        let root_parent = dom_adapter.parent_of(&root_id);
        let areas = root_parent
            .and_then(|root_parent| self.get(root_parent).cloned())
            .unwrap_or(NodeAreas {
                area: suggested_root_area,
                inner_area: suggested_root_area,
                inner_sizes: Size2D::default(),
                margin: Gaps::default(),
            });
        let root = dom_adapter.get_node(&root_id).unwrap();
        let root_height = dom_adapter.height(&root_id).unwrap();

        info!(
            "Processing {} dirty nodes and {} cached nodes from a height of {}",
            self.dirty.len(),
            self.results.len(),
            root_height
        );

        let (root_revalidated, root_areas) = measure_node(
            root_id,
            &root,
            self,
            &areas.inner_area,
            &areas.inner_area,
            measurer,
            true,
            dom_adapter,
        );

        // Cache the root Node results if it was modified
        if root_revalidated {
            self.cache_node(root_id, root_areas);
        }

        self.dirty.clear();
        self.root_node_candidate = RootNodeCandidate::None;
    }

    /// Get the areas of a Node
    pub fn get(&self, node_id: Key) -> Option<&NodeAreas> {
        self.results.get(&node_id)
    }

    /// Cache a Node's areas
    pub fn cache_node(&mut self, node_id: Key, areas: NodeAreas) {
        self.results.insert(node_id, areas);
    }
}