fret-ui 0.1.0

Mechanism-layer UI engine for Fret with tree, layout, focus, routing, and interaction contracts.
Documentation
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use super::*;
use slotmap::SecondaryMap;
use std::collections::HashSet;

impl<H: UiHost> UiTree<H> {
    pub(in crate::tree) fn subtree_layout_dirty_aggregation_enabled(&self) -> bool {
        crate::runtime_config::ui_runtime_config().layout_subtree_dirty_aggregation
    }

    pub(crate) fn node_subtree_layout_dirty(&self, node: NodeId) -> bool {
        self.subtree_layout_dirty_aggregation_enabled()
            && self
                .nodes
                .get(node)
                .is_some_and(|n| n.subtree_layout_dirty_count > 0)
    }

    #[allow(dead_code)]
    pub(crate) fn node_subtree_layout_dirty_count(&self, node: NodeId) -> u32 {
        if !self.subtree_layout_dirty_aggregation_enabled() {
            return 0;
        }
        self.nodes
            .get(node)
            .map(|n| n.subtree_layout_dirty_count)
            .unwrap_or(0)
    }

    pub(in crate::tree) fn note_layout_invalidation_transition_for_subtree_aggregation(
        &mut self,
        node: NodeId,
        before: bool,
        after: bool,
    ) {
        if before == after {
            return;
        }

        // Always keep view-cache dirty roots discoverable, even if subtree aggregation is
        // disabled. Contained view-cache relayouts use `dirty_cache_roots` as their entry set.
        if after
            && self.view_cache_active()
            && let Some(n) = self.nodes.get(node)
            && n.view_cache.enabled
            && n.view_cache.contained_layout
        {
            self.mark_cache_root_dirty(
                node,
                UiDebugInvalidationSource::Other,
                UiDebugInvalidationDetail::Unknown,
            );
        }

        if !self.subtree_layout_dirty_aggregation_enabled() {
            return;
        }

        let delta: i32 = if after { 1 } else { -1 };
        self.apply_subtree_layout_dirty_delta_to_node_and_ancestors(node, delta);
    }

    pub(in crate::tree) fn recompute_node_subtree_layout_dirty_count_and_propagate(
        &mut self,
        node: NodeId,
    ) {
        // Always keep view-cache dirty roots discoverable, even if subtree aggregation is
        // disabled. This mirrors the older retained-tree behavior and is relied on by mutation
        // paths that toggle invalidations without a full invalidation walk.
        if self.view_cache_active()
            && let Some(root) = self.nearest_view_cache_root(node)
            && let Some(n) = self.nodes.get(root)
            && n.view_cache.enabled
            && n.view_cache.contained_layout
            && n.invalidation.layout
        {
            self.mark_cache_root_dirty(
                root,
                UiDebugInvalidationSource::Other,
                UiDebugInvalidationDetail::Unknown,
            );
        }

        if !self.subtree_layout_dirty_aggregation_enabled() {
            return;
        }

        let (parent, old_count, new_count) = {
            let Some(n) = self.nodes.get(node) else {
                return;
            };
            let mut sum: u32 = if n.invalidation.layout { 1 } else { 0 };
            for &child in &n.children {
                sum = sum.saturating_add(
                    self.nodes
                        .get(child)
                        .map(|c| c.subtree_layout_dirty_count)
                        .unwrap_or(0),
                );
            }
            (n.parent, n.subtree_layout_dirty_count, sum)
        };

        if old_count == new_count {
            return;
        }

        if let Some(n) = self.nodes.get_mut(node) {
            n.subtree_layout_dirty_count = new_count;
        }

        let delta_i64: i64 = new_count as i64 - old_count as i64;
        debug_assert!(delta_i64 >= i32::MIN as i64 && delta_i64 <= i32::MAX as i64);
        let delta: i32 = delta_i64.clamp(i32::MIN as i64, i32::MAX as i64) as i32;
        self.apply_subtree_layout_dirty_delta_to_ancestors(parent, delta);
    }

    pub(in crate::tree) fn rebuild_subtree_layout_dirty_counts_and_propagate(
        &mut self,
        root: NodeId,
    ) {
        if !self.subtree_layout_dirty_aggregation_enabled() {
            return;
        }

        let root_parent = self.nodes.get(root).and_then(|n| n.parent);
        let old_root_count = self
            .nodes
            .get(root)
            .map(|n| n.subtree_layout_dirty_count)
            .unwrap_or(0);

        let mut stack: Vec<(NodeId, bool)> = Vec::new();
        stack.push((root, false));
        let mut rebuilt_nodes: u32 = 0;
        while let Some((id, children_pushed)) = stack.pop() {
            let Some(n) = self.nodes.get(id) else {
                continue;
            };
            if !children_pushed {
                stack.push((id, true));
                for &child in &n.children {
                    stack.push((child, false));
                }
                continue;
            }

            let mut sum: u32 = if n.invalidation.layout { 1 } else { 0 };
            for &child in &n.children {
                sum = sum.saturating_add(
                    self.nodes
                        .get(child)
                        .map(|c| c.subtree_layout_dirty_count)
                        .unwrap_or(0),
                );
            }
            if let Some(n) = self.nodes.get_mut(id) {
                n.subtree_layout_dirty_count = sum;
            }
            rebuilt_nodes = rebuilt_nodes.saturating_add(1);
        }

        if self.debug_enabled {
            self.debug_stats.layout_subtree_dirty_agg_rebuild_nodes = self
                .debug_stats
                .layout_subtree_dirty_agg_rebuild_nodes
                .saturating_add(rebuilt_nodes);
        }

        let new_root_count = self
            .nodes
            .get(root)
            .map(|n| n.subtree_layout_dirty_count)
            .unwrap_or(0);
        let delta_i64: i64 = new_root_count as i64 - old_root_count as i64;
        debug_assert!(delta_i64 >= i32::MIN as i64 && delta_i64 <= i32::MAX as i64);
        let delta: i32 = delta_i64.clamp(i32::MIN as i64, i32::MAX as i64) as i32;
        self.apply_subtree_layout_dirty_delta_to_ancestors(root_parent, delta);
    }

    pub(in crate::tree) fn repair_subtree_layout_dirty_counts_from(&mut self, root: NodeId) {
        if !self.subtree_layout_dirty_aggregation_enabled() {
            return;
        }

        // Step 1: rebuild the subtree rooted at `root` (post-order) so descendants become
        // internally consistent with their invalidation flags and child lists.
        let mut stack: Vec<(NodeId, bool)> = Vec::new();
        stack.push((root, false));
        let mut rebuilt_nodes: u32 = 0;
        while let Some((id, children_pushed)) = stack.pop() {
            let Some(n) = self.nodes.get(id) else {
                continue;
            };
            if !children_pushed {
                stack.push((id, true));
                for &child in &n.children {
                    stack.push((child, false));
                }
                continue;
            }

            let mut sum: u32 = if n.invalidation.layout { 1 } else { 0 };
            for &child in &n.children {
                sum = sum.saturating_add(
                    self.nodes
                        .get(child)
                        .map(|c| c.subtree_layout_dirty_count)
                        .unwrap_or(0),
                );
            }
            if let Some(n) = self.nodes.get_mut(id) {
                n.subtree_layout_dirty_count = sum;
            }
            rebuilt_nodes = rebuilt_nodes.saturating_add(1);
        }

        // Step 2: recompute exact counts on ancestors so drift cannot linger above `root` even if
        // the previously stored `root` count (used by delta propagation) was already incorrect.
        let mut walked_nodes: u32 = 0;
        let mut current = self.nodes.get(root).and_then(|n| n.parent);
        while let Some(id) = current {
            let (next_parent, expected) = {
                let Some(n) = self.nodes.get(id) else {
                    break;
                };
                let mut sum: u32 = if n.invalidation.layout { 1 } else { 0 };
                for &child in &n.children {
                    sum = sum.saturating_add(
                        self.nodes
                            .get(child)
                            .map(|c| c.subtree_layout_dirty_count)
                            .unwrap_or(0),
                    );
                }
                (n.parent, sum)
            };

            if let Some(n) = self.nodes.get_mut(id) {
                n.subtree_layout_dirty_count = expected;
            }

            walked_nodes = walked_nodes.saturating_add(1);
            if walked_nodes > 4096 {
                tracing::warn!(
                    node = ?id,
                    "repair_subtree_layout_dirty_counts_from: aborting ancestor walk (cycle or corrupt parent pointers?)"
                );
                break;
            }
            current = next_parent;
        }

        if self.debug_enabled {
            self.debug_stats.layout_subtree_dirty_agg_rebuild_nodes = self
                .debug_stats
                .layout_subtree_dirty_agg_rebuild_nodes
                .saturating_add(rebuilt_nodes);
        }
    }

    #[track_caller]
    pub(in crate::tree) fn apply_subtree_layout_dirty_delta_to_node_and_ancestors(
        &mut self,
        node: NodeId,
        delta: i32,
    ) {
        if delta == 0 || !self.subtree_layout_dirty_aggregation_enabled() {
            return;
        }

        let mut walked_nodes: u32 = 0;
        let mut current = Some(node);
        while let Some(id) = current {
            let (parent, element, stored, underflow) = {
                let Some(n) = self.nodes.get_mut(id) else {
                    break;
                };
                let underflow = apply_i32_delta_to_u32(&mut n.subtree_layout_dirty_count, delta);
                (n.parent, n.element, n.subtree_layout_dirty_count, underflow)
            };
            if underflow {
                let caller = std::panic::Location::caller();
                tracing::error!(
                    node = ?id,
                    element = ?element,
                    stored,
                    delta,
                    caller = %caller,
                    "subtree layout dirty count underflow"
                );
                // Parent pointers participate in both delta propagation and repair walks. When an
                // underflow is observed, aggressively repair reachable parent pointers first so
                // the subsequent subtree-count rebuild can propagate along the most plausible
                // ancestry chain.
                let repaired_parents = self.repair_parent_pointers_from_layer_roots();
                if repaired_parents > 0 {
                    tracing::warn!(
                        node = ?id,
                        repaired_parents,
                        caller = %caller,
                        "repaired parent pointers after subtree layout dirty underflow"
                    );
                }
                self.repair_subtree_layout_dirty_counts_from(id);
                break;
            }
            walked_nodes = walked_nodes.saturating_add(1);
            current = parent;
        }

        if self.debug_enabled {
            self.debug_stats.layout_subtree_dirty_agg_updates = self
                .debug_stats
                .layout_subtree_dirty_agg_updates
                .saturating_add(1);
            self.debug_stats.layout_subtree_dirty_agg_nodes_touched = self
                .debug_stats
                .layout_subtree_dirty_agg_nodes_touched
                .saturating_add(walked_nodes);
            self.debug_stats.layout_subtree_dirty_agg_max_parent_walk = self
                .debug_stats
                .layout_subtree_dirty_agg_max_parent_walk
                .max(walked_nodes);
        }
    }

    fn apply_subtree_layout_dirty_delta_to_ancestors(&mut self, start: Option<NodeId>, delta: i32) {
        let Some(node) = start else {
            return;
        };
        self.apply_subtree_layout_dirty_delta_to_node_and_ancestors(node, delta);
    }

    pub(in crate::tree) fn validate_subtree_layout_dirty_counts_if_enabled(&mut self) {
        let cfg = crate::runtime_config::ui_runtime_config();
        if !cfg.layout_subtree_dirty_aggregation_validate {
            return;
        }
        if !cfg.layout_subtree_dirty_aggregation {
            return;
        }

        let mut expected: SecondaryMap<NodeId, u32> = SecondaryMap::new();
        let mut visited: HashSet<NodeId> = HashSet::new();
        let mut stack: Vec<(NodeId, bool)> = Vec::new();

        for (root, _) in self.nodes.iter() {
            if !visited.insert(root) {
                continue;
            }
            stack.push((root, false));
            while let Some((id, children_pushed)) = stack.pop() {
                let Some(n) = self.nodes.get(id) else {
                    continue;
                };
                if !children_pushed {
                    stack.push((id, true));
                    for &child in &n.children {
                        if visited.insert(child) {
                            stack.push((child, false));
                        }
                    }
                    continue;
                }

                let mut sum: u32 = if n.invalidation.layout { 1 } else { 0 };
                for &child in &n.children {
                    sum = sum.saturating_add(expected.get(child).copied().unwrap_or(0));
                }
                expected.insert(id, sum);
            }
        }

        let mut failures: u32 = 0;
        const MAX_REPORTS: usize = 16;
        for (id, n) in self.nodes.iter() {
            let exp = expected.get(id).copied().unwrap_or(0);
            if n.subtree_layout_dirty_count == exp {
                continue;
            }
            failures = failures.saturating_add(1);
            if (failures as usize) <= MAX_REPORTS {
                tracing::error!(
                    node = ?id,
                    element = ?n.element,
                    stored = n.subtree_layout_dirty_count,
                    expected = exp,
                    "subtree layout dirty count drift"
                );
            }
        }

        if failures == 0 {
            return;
        }

        if self.debug_enabled {
            self.debug_stats
                .layout_subtree_dirty_agg_validation_failures = self
                .debug_stats
                .layout_subtree_dirty_agg_validation_failures
                .saturating_add(failures);
        }

        if cfg.layout_subtree_dirty_aggregation_validate_panic {
            panic!("subtree layout dirty count drift: failures={failures}");
        }
    }
}

pub(in crate::tree) fn apply_i32_delta_to_u32(value: &mut u32, delta: i32) -> bool {
    if delta > 0 {
        *value = value.saturating_add(delta as u32);
        return false;
    }
    if delta < 0 {
        let dec = (-delta) as u32;
        if *value < dec {
            return true;
        }
        *value -= dec;
    }
    false
}