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
425
426
use super::super::*;

impl<H: UiHost> UiTree<H> {
    /// Convenience helper for single-window/single-tree setups.
    ///
    /// This drains pending model changes from the host and immediately propagates them into the
    /// tree. Multi-window runtimes should drain `take_changed_models()` once per frame and fan the
    /// resulting list out to each window's [`UiTree`] instead.
    pub fn propagate_pending_model_changes(&mut self, app: &mut H) -> bool {
        let changed = app.take_changed_models();
        self.propagate_model_changes(app, &changed)
    }

    fn propagate_observation_masks(
        &mut self,
        app: &mut H,
        masks: impl IntoIterator<Item = (NodeId, ObservationMask)>,
        source: UiDebugInvalidationSource,
    ) -> bool {
        self.propagation_depth_generation = self.propagation_depth_generation.wrapping_add(1);
        if self.propagation_depth_generation == 0 {
            self.propagation_depth_generation = 1;
            self.propagation_depth_cache.clear();
        }
        self.propagation_chain.clear();
        self.propagation_entries.clear();

        for (node, mask) in masks {
            if mask.is_empty() || !self.nodes.contains_key(node) {
                continue;
            }

            let (strength, inv) = if mask.hit_test {
                (3, Invalidation::HitTest)
            } else if mask.layout {
                (2, Invalidation::Layout)
            } else if mask.paint {
                (1, Invalidation::Paint)
            } else {
                continue;
            };

            let depth = propagation_depth::propagation_depth_for(self, node);
            let key = node.data().as_ffi();
            self.propagation_entries
                .push((strength, depth, key, node, inv));
        }

        if self.propagation_entries.is_empty() {
            return false;
        }

        self.propagation_entries.sort_by(|a, b| {
            // Higher-strength invalidations first to maximize reuse via `visited`.
            b.0.cmp(&a.0)
                // Within the same strength, prefer ancestors first to reduce redundant walks.
                .then(a.1.cmp(&b.1))
                // Stabilize order for determinism in stats/perf.
                .then(a.2.cmp(&b.2))
        });

        let mut did_invalidate = false;
        let mut visited = std::mem::take(&mut self.invalidation_dedup);
        visited.begin();
        let mut entries = std::mem::take(&mut self.propagation_entries);
        for (_, _, _, node, inv) in entries.drain(..) {
            self.mark_invalidation_dedup_with_source(node, inv, &mut visited, source);
            did_invalidate = true;
        }
        self.invalidation_dedup = visited;
        self.propagation_entries = entries;

        if did_invalidate {
            self.request_redraw_coalesced(app);
        }

        did_invalidate
    }

    fn propagate_model_changes_from_elements(&mut self, app: &mut H, changed: &[ModelId]) -> bool {
        let Some(window) = self.window else {
            return false;
        };
        if changed.is_empty() {
            return false;
        }

        let changed: std::collections::HashSet<ModelId> = changed.iter().copied().collect();
        let frame_id = app.frame_id();
        let mut combined: HashMap<NodeId, ObservationMask> = HashMap::new();

        app.with_global_mut_untracked(crate::elements::ElementRuntime::new, |runtime, _app| {
            let Some(window_state) = runtime.for_window(window) else {
                return;
            };
            window_state.for_each_observed_model_for_invalidation(
                frame_id,
                |element, observations| {
                    let mut mask = ObservationMask::default();
                    for (model, inv) in observations {
                        if changed.contains(model) {
                            mask.add(*inv);
                        }
                    }
                    if mask.is_empty() {
                        return;
                    }
                    let seeded = window_state.node_entry(element).map(|e| e.node);
                    let Some(node) =
                        self.resolve_live_attached_node_for_element_seeded(element, seeded)
                    else {
                        return;
                    };
                    combined
                        .entry(node)
                        .and_modify(|m| *m = m.union(mask))
                        .or_insert(mask);
                },
            );
        });

        if combined.is_empty() {
            return false;
        }
        self.propagate_observation_masks(app, combined, UiDebugInvalidationSource::ModelChange)
    }

    fn propagate_global_changes_from_elements(&mut self, app: &mut H, changed: &[TypeId]) -> bool {
        let Some(window) = self.window else {
            return false;
        };
        if changed.is_empty() {
            return false;
        }

        let changed: std::collections::HashSet<TypeId> = changed.iter().copied().collect();
        let frame_id = app.frame_id();
        let mut combined: HashMap<NodeId, ObservationMask> = HashMap::new();

        app.with_global_mut_untracked(crate::elements::ElementRuntime::new, |runtime, _app| {
            let Some(window_state) = runtime.for_window(window) else {
                return;
            };
            window_state.for_each_observed_global_for_invalidation(
                frame_id,
                |element, observations| {
                    let mut mask = ObservationMask::default();
                    for (global, inv) in observations {
                        if changed.contains(global) {
                            mask.add(*inv);
                        }
                    }
                    if mask.is_empty() {
                        return;
                    }
                    let seeded = window_state.node_entry(element).map(|e| e.node);
                    let Some(node) =
                        self.resolve_live_attached_node_for_element_seeded(element, seeded)
                    else {
                        return;
                    };
                    combined
                        .entry(node)
                        .and_modify(|m| *m = m.union(mask))
                        .or_insert(mask);
                },
            );
        });

        if combined.is_empty() {
            return false;
        }
        self.propagate_observation_masks(app, combined, UiDebugInvalidationSource::GlobalChange)
    }

    pub fn propagate_model_changes(&mut self, app: &mut H, changed: &[ModelId]) -> bool {
        if changed.is_empty() {
            return false;
        }
        let frame_id = app.frame_id();
        #[cfg(debug_assertions)]
        self.debug_forbid_propagate_after_declarative_render_root(frame_id);
        self.begin_debug_frame_if_needed(frame_id);
        if self.debug_enabled {
            self.debug_model_change_hotspots.clear();
            self.debug_model_change_unobserved.clear();
        }

        let mut did_invalidate = false;

        if changed.len() == 1 {
            let model = changed[0];
            let layout_nodes = self.observed_in_layout.by_model.get(&model);
            let paint_nodes = self.observed_in_paint.by_model.get(&model);
            if let (Some(nodes), None) | (None, Some(nodes)) = (layout_nodes, paint_nodes) {
                // Copy out the observations so we don't hold a borrow across the invalidation walk.
                let masks: Vec<(NodeId, ObservationMask)> =
                    nodes.iter().map(|(&n, &m)| (n, m)).collect();
                if self.debug_enabled {
                    self.debug_stats.model_change_invalidation_roots =
                        masks.len().min(u32::MAX as usize) as u32;
                    self.debug_stats.model_change_models = 1;
                    self.debug_stats.model_change_observation_edges =
                        masks.len().min(u32::MAX as usize) as u32;
                    self.debug_stats.model_change_unobserved_models = 0;
                    self.debug_model_change_hotspots = vec![UiDebugModelChangeHotspot {
                        model,
                        observation_edges: masks.len().min(u32::MAX as usize) as u32,
                        changed: app.models().debug_last_changed_info_for_id(model),
                    }];
                }
                did_invalidate |= self.propagate_observation_masks(
                    app,
                    masks,
                    UiDebugInvalidationSource::ModelChange,
                );
                did_invalidate |= self.propagate_model_changes_from_elements(app, changed);
                return did_invalidate;
            }
        }

        // Avoid rehash spikes: `changed` is usually small while each changed model/global can have
        // thousands of observation edges.
        let mut combined_capacity = 0usize;
        for &model in changed {
            if let Some(nodes) = self.observed_in_layout.by_model.get(&model) {
                combined_capacity = combined_capacity.saturating_add(nodes.len());
            }
            if let Some(nodes) = self.observed_in_paint.by_model.get(&model) {
                combined_capacity = combined_capacity.saturating_add(nodes.len());
            }
        }
        combined_capacity = combined_capacity.min(self.nodes.len());

        let mut combined: HashMap<NodeId, ObservationMask> =
            HashMap::with_capacity(combined_capacity.max(changed.len().saturating_mul(8)));
        let mut observation_edges_scanned = 0usize;
        let mut unobserved_models = 0usize;
        for &model in changed {
            let mut edges = 0usize;
            if let Some(nodes) = self.observed_in_layout.by_model.get(&model) {
                observation_edges_scanned = observation_edges_scanned.saturating_add(nodes.len());
                edges = edges.saturating_add(nodes.len());
                for (&node, &mask) in nodes {
                    combined
                        .entry(node)
                        .and_modify(|m| *m = m.union(mask))
                        .or_insert(mask);
                }
            }
            if let Some(nodes) = self.observed_in_paint.by_model.get(&model) {
                observation_edges_scanned = observation_edges_scanned.saturating_add(nodes.len());
                edges = edges.saturating_add(nodes.len());
                for (&node, &mask) in nodes {
                    combined
                        .entry(node)
                        .and_modify(|m| *m = m.union(mask))
                        .or_insert(mask);
                }
            }
            if self.debug_enabled && edges > 0 {
                self.debug_model_change_hotspots
                    .push(UiDebugModelChangeHotspot {
                        model,
                        observation_edges: edges.min(u32::MAX as usize) as u32,
                        changed: app.models().debug_last_changed_info_for_id(model),
                    });
            }
            if edges == 0 {
                unobserved_models = unobserved_models.saturating_add(1);
                if self.debug_enabled {
                    self.debug_model_change_unobserved
                        .push(UiDebugModelChangeUnobserved {
                            model,
                            created: app.models().debug_created_info_for_id(model),
                            changed: app.models().debug_last_changed_info_for_id(model),
                        });
                }
            }
        }

        if self.debug_enabled {
            self.debug_stats.model_change_invalidation_roots =
                combined.len().min(u32::MAX as usize) as u32;
            self.debug_stats.model_change_models = changed.len().min(u32::MAX as usize) as u32;
            self.debug_stats.model_change_observation_edges =
                observation_edges_scanned.min(u32::MAX as usize) as u32;
            self.debug_stats.model_change_unobserved_models =
                unobserved_models.min(u32::MAX as usize) as u32;

            self.debug_model_change_hotspots
                .sort_by(|a, b| b.observation_edges.cmp(&a.observation_edges));
            self.debug_model_change_hotspots.truncate(5);

            self.debug_model_change_unobserved
                .sort_by(|a, b| a.model.data().as_ffi().cmp(&b.model.data().as_ffi()));
            self.debug_model_change_unobserved.truncate(5);
        }
        did_invalidate |=
            self.propagate_observation_masks(app, combined, UiDebugInvalidationSource::ModelChange);
        did_invalidate |= self.propagate_model_changes_from_elements(app, changed);
        did_invalidate
    }

    pub fn propagate_global_changes(&mut self, app: &mut H, changed: &[TypeId]) -> bool {
        if changed.is_empty() {
            return false;
        }
        let frame_id = app.frame_id();
        #[cfg(debug_assertions)]
        self.debug_forbid_propagate_after_declarative_render_root(frame_id);
        self.begin_debug_frame_if_needed(frame_id);
        if self.debug_enabled {
            self.debug_global_change_hotspots.clear();
            self.debug_global_change_unobserved.clear();
        }

        let mut did_invalidate = false;

        if changed.len() == 1 {
            let global = changed[0];
            let layout_nodes = self.observed_globals_in_layout.by_global.get(&global);
            let paint_nodes = self.observed_globals_in_paint.by_global.get(&global);
            if let (Some(nodes), None) | (None, Some(nodes)) = (layout_nodes, paint_nodes) {
                // Copy out the observations so we don't hold a borrow across the invalidation walk.
                let masks: Vec<(NodeId, ObservationMask)> =
                    nodes.iter().map(|(&n, &m)| (n, m)).collect();
                if self.debug_enabled {
                    self.debug_stats.global_change_invalidation_roots =
                        masks.len().min(u32::MAX as usize) as u32;
                    self.debug_stats.global_change_globals = 1;
                    self.debug_stats.global_change_observation_edges =
                        masks.len().min(u32::MAX as usize) as u32;
                    self.debug_stats.global_change_unobserved_globals = 0;
                }
                did_invalidate |= self.propagate_observation_masks(
                    app,
                    masks,
                    UiDebugInvalidationSource::GlobalChange,
                );
                did_invalidate |= self.propagate_global_changes_from_elements(app, changed);
                return did_invalidate;
            }
        }

        // Avoid rehash spikes: `changed` is usually small while each changed global can have
        // thousands of observation edges.
        let mut combined_capacity = 0usize;
        for &global in changed {
            if let Some(nodes) = self.observed_globals_in_layout.by_global.get(&global) {
                combined_capacity = combined_capacity.saturating_add(nodes.len());
            }
            if let Some(nodes) = self.observed_globals_in_paint.by_global.get(&global) {
                combined_capacity = combined_capacity.saturating_add(nodes.len());
            }
        }
        combined_capacity = combined_capacity.min(self.nodes.len());

        let mut combined: HashMap<NodeId, ObservationMask> =
            HashMap::with_capacity(combined_capacity.max(changed.len().saturating_mul(8)));
        let mut observation_edges_scanned = 0usize;
        let mut unobserved_globals = 0usize;
        for &global in changed {
            let mut edges = 0usize;
            if let Some(nodes) = self.observed_globals_in_layout.by_global.get(&global) {
                observation_edges_scanned = observation_edges_scanned.saturating_add(nodes.len());
                edges = edges.saturating_add(nodes.len());
                for (&node, &mask) in nodes {
                    combined
                        .entry(node)
                        .and_modify(|m| *m = m.union(mask))
                        .or_insert(mask);
                }
            }
            if let Some(nodes) = self.observed_globals_in_paint.by_global.get(&global) {
                observation_edges_scanned = observation_edges_scanned.saturating_add(nodes.len());
                edges = edges.saturating_add(nodes.len());
                for (&node, &mask) in nodes {
                    combined
                        .entry(node)
                        .and_modify(|m| *m = m.union(mask))
                        .or_insert(mask);
                }
            }
            if self.debug_enabled && edges > 0 {
                self.debug_global_change_hotspots
                    .push(UiDebugGlobalChangeHotspot {
                        global,
                        observation_edges: edges.min(u32::MAX as usize) as u32,
                    });
            }
            if edges == 0 {
                unobserved_globals = unobserved_globals.saturating_add(1);
                if self.debug_enabled {
                    self.debug_global_change_unobserved
                        .push(UiDebugGlobalChangeUnobserved { global });
                }
            }
        }

        if self.debug_enabled {
            self.debug_stats.global_change_invalidation_roots =
                combined.len().min(u32::MAX as usize) as u32;
            self.debug_stats.global_change_globals = changed.len().min(u32::MAX as usize) as u32;
            self.debug_stats.global_change_observation_edges =
                observation_edges_scanned.min(u32::MAX as usize) as u32;
            self.debug_stats.global_change_unobserved_globals =
                unobserved_globals.min(u32::MAX as usize) as u32;

            self.debug_global_change_hotspots
                .sort_by(|a, b| b.observation_edges.cmp(&a.observation_edges));
            self.debug_global_change_hotspots.truncate(5);

            self.debug_global_change_unobserved
                .sort_by_key(|u| type_id_sort_key(u.global));
            self.debug_global_change_unobserved.truncate(5);
        }
        did_invalidate |= self.propagate_observation_masks(
            app,
            combined,
            UiDebugInvalidationSource::GlobalChange,
        );
        did_invalidate |= self.propagate_global_changes_from_elements(app, changed);
        did_invalidate
    }
}