nativeview-layout 0.1.0-rc.1

Shared NodeStore layout layer: StoreLayoutEngine / TextMeasurer traits and the default direct Taffy engine
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//! Change-set synchronization, invalidation, and box-tree inheritance propagation for
//! [`TaffyStoreLayout`].

use nativeview_abi::table::effect;
use nativeview_engine::InteractionTracker;
use nativeview_store::{ChangeSet, NodeId, NodeStore, NvTable};
use nativeview_tables::tables::{
    Direction as NvDirection, Interaction, Layout as NvLayout, Overlay, Responsive,
    Transform as TransformTable, Typography, is_display_contents,
};
use taffy::tree::ClearState;

use crate::{InheritedText, ResolvedBoxEdges, ResolvedTextStyle};

use super::container::scope::committed_container_env;
use super::responsive::text::ResponsiveTextEnv;
use super::transform_cb::{has_store_transform, subtree_has_out_of_flow};
use crate::scene::state_rules_patch_box;
use super::responsive::sync::NodeStyleFlags;
use super::{TaffyStoreLayout, UnroundedBox};

impl TaffyStoreLayout {
    /// Invalidates cached Taffy entries for all layout/measure/structural changes in `changes`.
    pub fn sync_changes(&mut self, store: &NodeStore, changes: &ChangeSet) {
        self.sync_changes_with_interaction(store, changes, None);
    }

    pub fn sync_changes_with_interaction(
        &mut self,
        store: &NodeStore,
        changes: &ChangeSet,
        interaction: Option<&InteractionTracker>,
    ) {
        self.ensure_capacity(store.capacity());
        // Refresh `style_flags` and responsive/container counters BEFORE `top_layer.sync`,
        // `enqueue_inherit_root`, and `paragraphs.sync_changes` so `0 -> 1` transitions enable
        // `display_env!` media/container resolution on the very same commit.
        for &id in &changes.removed {
            let idx = id.index();
            if idx < self.style_flags.len() {
                self.set_style_flags(idx, NodeStyleFlags::empty());
            }
        }
        for &id in &changes.created {
            if store.is_live(id) {
                self.sync_node_responsive_counters(store, id);
            }
        }
        if let Some(resp_td) = changes.table(Responsive::slot()) {
            for &id in resp_td.nodes.iter().chain(&resp_td.detached) {
                if store.is_live(id) {
                    self.sync_node_responsive_counters(store, id);
                }
            }
            self.responsive_changed_this_pass = true;
            // `@media { display: none }` or `show_on` on open overlays/ancestors may flip hoisting.
            self.top_layer.invalidate();
        }
        let flag_words =
            (1u32 << NvLayout::CONTAINER_TYPE.word()) | (1u32 << NvLayout::POSITION.word());
        if let Some(layout_td) = changes.table(NvLayout::slot()) {
            for (&id, &m) in layout_td.nodes.iter().zip(&layout_td.masks) {
                if (m & flag_words) != 0 && store.is_live(id) {
                    self.sync_node_responsive_counters(store, id);
                }
            }
            for &id in &layout_td.detached {
                if store.is_live(id) {
                    self.sync_node_responsive_counters(store, id);
                }
            }
        }
        // A state rule may patch `position` (OOF candidacy), `container_type`, or `display`.
        if let Some(td) = changes.table(Interaction::slot()) {
            for &id in td.nodes.iter().chain(&td.detached) {
                if store.is_live(id) {
                    self.sync_node_responsive_counters(store, id);
                }
            }
        }

        // Hoisting changes reshape only the affected DOM parents and the root (below), never the
        // whole box tree: a `display` / `OPEN` change triggers the inheritance
        // walk only when it flips `display: contents` (or direction).
        let top_layer_changed =
            self.top_layer
                .sync(store, changes, &display_env!(self, interaction));

        let root_id = self.last_viewport.map_or(NodeId::ROOT, |(r, _, _)| r);
        let dir_bit = 1u32 << NvLayout::DIRECTION.word();
        let disp_bit = 1u32 << NvLayout::DISPLAY.word();
        let layout_bits = dir_bit | disp_bit;
        if let Some(td) = changes.table(NvLayout::slot()) {
            for &id in &td.detached {
                if !store.is_inline(id) {
                    self.enqueue_inherit_root(store, id, true, interaction);
                }
            }
            for (&id, &m) in td.nodes.iter().zip(&td.masks) {
                if !store.is_inline(id) && m & layout_bits != 0 {
                    if m & disp_bit != 0
                        && self.display_change_reshapes_boxes(store, id, interaction)
                    {
                        self.enqueue_inherit_root(store, id, true, interaction);
                    } else if m & dir_bit != 0 && self.direction_changed(store, id) {
                        self.enqueue_inherit_root(store, id, false, interaction);
                    }
                }
            }
        }
        for (slot, bit) in [(Overlay::slot(), 1u32 << Overlay::OPTIONS.word())] {
            if let Some(td) = changes.table(slot) {
                for (&id, &m) in td.nodes.iter().zip(&td.masks) {
                    if m & bit != 0
                        && !store.is_inline(id)
                        && self.display_change_reshapes_boxes(store, id, interaction)
                    {
                        self.enqueue_inherit_root(store, id, true, interaction);
                    }
                }
                for &id in &td.detached {
                    if !store.is_inline(id)
                        && self.display_change_reshapes_boxes(store, id, interaction)
                    {
                        self.enqueue_inherit_root(store, id, true, interaction);
                    }
                }
            }
        }
        // Non-inline Typography edits require subtree inheritance propagation; pure inline edits
        // are handled O(1) directly inside `ParagraphStore::sync_changes`.
        if let Some(td) = changes.table(Typography::slot()) {
            for &id in td.nodes.iter().chain(&td.detached) {
                if !store.is_inline(id) {
                    self.enqueue_inherit_root(store, id, false, interaction);
                }
            }
        }
        let responsive_changed = if let Some(td) = changes.table(Responsive::slot()) {
            for &id in td.nodes.iter().chain(&td.detached) {
                if !store.is_inline(id) {
                    self.enqueue_inherit_root(store, id, true, interaction);
                }
            }
            true
        } else {
            false
        };
        let box_topology_changed = changes.created.iter().any(|&id| !store.is_inline(id))
            || changes
                .removed
                .iter()
                .any(|&id| self.paragraphs.owner_paragraph(id).is_none())
            || changes
                .children
                .iter()
                .any(|&id| !store.is_inline(id) && !store.is_inline_context_root(id));
        if box_topology_changed {
            for &id in &changes.children {
                if !store.is_inline(id) && !store.is_inline_context_root(id) {
                    self.enqueue_inherit_root(store, id, true, interaction);
                }
            }
            for &id in &changes.created {
                if store.is_live(id) && !store.is_inline(id) {
                    if id == root_id {
                        self.mark_inherit_full();
                    } else if store
                        .parent(id)
                        .is_none_or(|p| !changes.children.contains(&p))
                    {
                        self.enqueue_inherit_root(store, id, true, interaction);
                    }
                }
            }
            if !changes.removed.is_empty() && !self.inherit_dirty {
                // Removed node without a parent in `changes.children` (e.g. root or orphan).
                self.committed_store_rev = None;
            }
        }
        if changes
            .children
            .iter()
            .any(|&id| !store.is_inline(id) && !store.is_inline_context_root(id))
        {
            // Non-inline `set_children` may detach a live box without removing it; a full-root
            // pass bumps `rect_valid_gen` so `layout_of(detached)` becomes `None`.
            self.full_layout_needed = true;
        }
        if box_topology_changed || responsive_changed {
            self.responsive_tree_dirty = true;
        }

        // Sync compiled paragraphs (lifting inline edits to owning paragraph roots). Defer
        // compilation only when a full-tree inheritance walk will visit every paragraph anyway;
        // localized subtree walks leave untouched branches to this pass.
        let cenvs = &self.resolved_container_env;
        let lookup = |n: NodeId| committed_container_env(cenvs, n);
        self.paragraphs.sync_changes(
            store,
            changes,
            InheritedText {
                text: &self.resolved_text,
                rtl: &self.resolved_rtl,
                responsive: (self.responsive_node_count > 0).then_some(ResponsiveTextEnv {
                    media: &self.media_env,
                    container_env_of: &lookup,
                }),
            },
            interaction,
            self.inherit_full,
        );
        if !self.paragraphs.box_to_inline_transitions().is_empty()
            || !self.paragraphs.inline_to_box_transitions().is_empty()
        {
            self.full_layout_needed = true;
            if self.responsive_node_count > 0 {
                self.responsive_tree_dirty = true;
            }
        }

        for &id in &changes.removed {
            let idx = id.index();
            if idx < self.caches.len() {
                if self.rect_gen[idx] != 0 || self.was_contents[idx] {
                    self.layout_dirty = true;
                    self.full_layout_needed = true;
                }
                self.caches[idx].clear();
                self.boxes[idx] = UnroundedBox::default();
                if self.capture_box_edges && idx < self.box_edges.len() {
                    self.box_edges[idx] = ResolvedBoxEdges::default();
                }
                self.rects[idx] = nativeview_engine::LayoutRect::default();
                self.rect_gen[idx] = 0;
                self.node_gens[idx] = 0;
                self.resolved_rtl[idx] = false;
                self.was_contents[idx] = false;
                self.was_transformed[idx] = false;
                if idx < self.cached_env_hash.len() {
                    self.cached_env_hash[idx] = 0;
                    self.para_last_env_hash[idx] = 0;
                }
                self.container_records.clear_slot(idx);
                self.boundary_records.clear_slot(idx);
                self.resolved_container_env.clear_slot(idx);
                self.resolved_text[idx] = ResolvedTextStyle::default();
                self.hoisted.release(idx);
                self.box_children.release(idx);
            }
        }
        for &id in &changes.created {
            if !store.is_live(id) || store.is_inline(id) {
                continue;
            }
            let idx = id.index();
            self.caches[idx].clear();
            self.boxes[idx] = UnroundedBox::default();
            if self.capture_box_edges && idx < self.box_edges.len() {
                self.box_edges[idx] = ResolvedBoxEdges::default();
            }
            self.rects[idx] = nativeview_engine::LayoutRect::default();
            self.rect_gen[idx] = 0;
            // Leave `node_gens[idx] = 0` (unvisited) until `propagate_inheritance` /
            // `commit_round_walk` resolves this node, so `resolve_inherit_roots` detects unvisited
            // parents accurately and slot-reused nodes always report in `changed_rects`.
            self.node_gens[idx] = 0;
            self.was_contents[idx] = false;
            self.was_transformed[idx] = has_store_transform(store, id);
            if idx < self.cached_env_hash.len() {
                self.cached_env_hash[idx] = 0;
                self.para_last_env_hash[idx] = 0;
            }
            self.container_records.clear_slot(idx);
            self.boundary_records.clear_slot(idx);
            self.resolved_container_env.clear_slot(idx);
            self.hoisted.release(idx);
            self.box_children.release(idx);
            self.mark_dirty(store, id);
        }
        if let Some(resp_td) = changes.table(Responsive::slot()) {
            for &id in resp_td.nodes.iter().chain(&resp_td.detached) {
                if store.is_live(id) {
                    if id.index() < self.para_last_env_hash.len() {
                        self.para_last_env_hash[id.index()] = 0;
                    }
                    if store.is_inline(id) {
                        if let Some(owner) = self.paragraphs.owner_paragraph(id)
                            && store.is_live(owner)
                        {
                            if owner.index() < self.para_last_env_hash.len() {
                                self.para_last_env_hash[owner.index()] = 0;
                            }
                            self.mark_dirty(store, owner);
                        }
                    } else {
                        self.mark_dirty(store, id);
                    }
                }
            }
        }
        for &id in &changes.children {
            if !store.is_inline(id) && !store.is_inline_context_root(id) {
                self.mark_dirty(store, id);
            }
        }
        for (&id, &fx) in changes.touched.iter().zip(changes.effects.iter()) {
            if store.is_inline(id) {
                continue;
            }
            if fx & effect::LAYOUT != 0
                || (fx & effect::MEASURE != 0
                    && (!store.is_inline_context_root(id)
                        || (!self.measure_hooks.is_empty()
                            && self
                                .paragraphs
                                .paragraph_view(store, id)
                                .is_none_or(|p| p.text.len == 0 || p.run_count == 0))))
            {
                self.mark_dirty(store, id);
            }
        }
        if let Some(tf_td) = changes.table(TransformTable::slot()) {
            for &id in tf_td.nodes.iter().chain(&tf_td.detached) {
                if store.is_live(id) && !store.is_inline(id) {
                    let now_tf = has_store_transform(store, id);
                    if self.was_transformed[id.index()] != now_tf {
                        self.was_transformed[id.index()] = now_tf;
                        if subtree_has_out_of_flow(store, id) {
                            self.mark_dirty(store, id);
                        }
                    }
                }
            }
        }
        for i in 0..self.paragraphs.box_to_inline_transitions().len() {
            let id = self.paragraphs.box_to_inline_transitions()[i];
            let idx = id.index();
            self.sync_node_responsive_counters(store, id);
            if idx < self.caches.len() {
                self.caches[idx].clear();
                self.boxes[idx] = UnroundedBox::default();
                self.rects[idx] = nativeview_engine::LayoutRect::default();
                self.rect_gen[idx] = 0;
                self.container_records.clear_slot(idx);
                self.boundary_records.clear_slot(idx);
                self.hoisted.release(idx);
                self.box_children.release(idx);
            }
            if let Some(p) = store.parent(id) {
                self.enqueue_inherit_root(store, p, true, interaction);
                self.mark_dirty(store, p);
            } else {
                self.mark_inherit_full();
            }
        }
        for i in 0..self.paragraphs.inline_to_box_transitions().len() {
            let id = self.paragraphs.inline_to_box_transitions()[i];
            self.sync_node_responsive_counters(store, id);
            if let Some(p) = store.parent(id) {
                self.enqueue_inherit_root(store, p, true, interaction);
            } else {
                self.mark_inherit_full();
            }
            self.mark_dirty(store, id);
        }
        for i in 0..self.paragraphs.dirty_layout_paragraphs().len() {
            let pid = self.paragraphs.dirty_layout_paragraphs()[i];
            self.mark_dirty(store, pid);
        }
        if top_layer_changed {
            self.reshape_top_layer(store, root_id, interaction);
        }
        if self.inherit_dirty || self.layout_dirty {
            self.dry.invalidate();
        }
        if !self.inherit_dirty && self.committed_store_rev.is_some() {
            // Inherited state is untouched by this commit: dry runs may keep reusing it.
            self.committed_store_rev = Some((store.instance_id(), store.frame()));
        }
    }

    /// After a pass committed new `@container` environments, re-resolves hoisting against them.
    /// Returns `true` (with the box lists reshaped) when the hoisted set changed.
    pub(super) fn refresh_top_layer_after_pass(
        &mut self,
        store: &NodeStore,
        root: NodeId,
        interaction: Option<&InteractionTracker>,
    ) -> bool {
        if self.container_node_count == 0 {
            return false;
        }
        self.top_layer.invalidate();
        let changed = self
            .top_layer
            .refresh(store, root, &display_env!(self, interaction));
        if changed {
            self.reshape_top_layer(store, root, interaction);
        }
        changed
    }

    /// Re-attaches top-layer roots whose state changed without an inheritance walk: each one's
    /// nearest box-generating DOM parent rebuilds its box children (dropping or regaining it)
    /// and the layout root rebuilds with the new hoisted set appended.
    pub(super) fn reshape_top_layer(
        &mut self,
        store: &NodeStore,
        root: NodeId,
        interaction: Option<&InteractionTracker>,
    ) {
        self.layout_dirty = true;
        self.full_layout_needed = true;
        if let Some(c) = self.caches.get_mut(root.index()) {
            c.clear();
        }
        if self.inherit_full {
            // The pending full inheritance walk rebuilds every list.
            return;
        }
        for i in 0..self.top_layer.transitions().len() {
            let id = self.top_layer.transitions()[i];
            if !store.is_live(id) || store.is_inline(id) {
                continue;
            }
            if let Some(c) = self.caches.get_mut(id.index()) {
                c.clear();
            }
            let mut parent = store.parent(id);
            for _ in 0..crate::MAX_TREE_DEPTH {
                match parent {
                    Some(p)
                        if p != root
                            && display_env!(self, interaction).is_display_contents(store, p) =>
                    {
                        parent = store.parent(p).filter(|&gp| gp != p);
                    }
                    _ => break,
                }
            }
            if let Some(p) = parent
                && p != root
                && store.is_live(p)
            {
                let env = display_env!(self, interaction);
                self.box_children
                    .rebuild(store, &self.top_layer, p, &[], &env);
                self.mark_dirty(store, p);
            }
        }
        if store.is_live(root) && !store.is_inline_context_root(root) {
            let env = display_env!(self, interaction);
            self.box_children
                .rebuild(store, &self.top_layer, root, self.top_layer.hoisted(), &env);
        }
        self.box_children.compact_if_fragmented();
    }

    pub fn sync_interaction_paragraphs(
        &mut self,
        store: &NodeStore,
        interaction: &InteractionTracker,
        dirty_nodes: &[NodeId],
    ) -> (&[NodeId], &[NodeId]) {
        let cenvs = &self.resolved_container_env;
        let lookup = |n: NodeId| committed_container_env(cenvs, n);
        let responsive = (self.responsive_node_count > 0).then_some(ResponsiveTextEnv {
            media: &self.media_env,
            container_env_of: &lookup,
        });
        let layout_count = self
            .paragraphs
            .sync_interaction_dirty(store, interaction, dirty_nodes, responsive)
            .0
            .len();
        if layout_count > 0 {
            for i in 0..layout_count {
                let pid = self.paragraphs.dirty_layout_paragraphs()[i];
                self.mark_dirty(store, pid);
            }
            self.dry.invalidate();
        }
        // Pseudo-state `StateRules` that patch `display` / `position` / `container_type` reflow
        // their node (Web parity): a `display: contents` flip reshapes box lists, any other change
        // re-lays out.
        for &id in dirty_nodes {
            if store.is_live(id) && !store.is_inline(id) && state_rules_patch_box(store, id) {
                if self.display_change_reshapes_boxes(store, id, Some(interaction)) {
                    self.enqueue_inherit_root(store, id, true, Some(interaction));
                }
                self.mark_dirty(store, id);
                if let Some(parent) = store.parent(id) {
                    self.mark_dirty(store, parent);
                }
                self.dry.invalidate();
            }
        }
        // The same patches on an overlay's chain re-hoist it.
        if self
            .top_layer
            .sync_nodes(store, dirty_nodes, &display_env!(self, Some(interaction)))
        {
            let root = self.last_viewport.map_or(NodeId::ROOT, |(r, _, _)| r);
            self.reshape_top_layer(store, root, Some(interaction));
            self.dry.invalidate();
        }
        (
            self.paragraphs.dirty_layout_paragraphs(),
            self.paragraphs.dirty_paint_paragraphs(),
        )
    }

    /// Whether `id`'s resolved direction differs from the one recorded by the last inheritance
    /// walk (its parent's recorded direction stands in for `inherit`; a parent whose own
    /// direction changed in the same frame triggers the walk itself).
    fn direction_changed(&self, store: &NodeStore, id: NodeId) -> bool {
        let parent_rtl = store
            .parent(id)
            .is_some_and(|p| self.resolved_rtl.get(p.index()).copied().unwrap_or(false));
        let rtl = match store.try_row::<NvLayout>(id).map(|l| l.direction()) {
            Some(NvDirection::Ltr) => false,
            Some(NvDirection::Rtl) => true,
            _ => parent_rtl,
        };
        self.resolved_rtl.get(id.index()).copied() != Some(rtl)
    }


    /// A `display` / hidden-input / pseudo-state change on `id` reshapes box children only when
    /// it flips whether `id` is `display: contents`.
    pub(super) fn display_change_reshapes_boxes(
        &self,
        store: &NodeStore,
        id: NodeId,
        interaction: Option<&InteractionTracker>,
    ) -> bool {
        display_env!(self, interaction).is_display_contents(store, id)
            != self.was_contents.get(id.index()).copied().unwrap_or(false)
    }

    /// Marks every live node dirty (used after full replica load or measurer swap).
    pub fn invalidate_all(&mut self, store: &NodeStore) {
        self.hoisted.reset();
        self.box_children.reset();
        self.ensure_capacity(store.capacity());
        for c in &mut self.caches {
            c.clear();
        }
        self.resolved_rtl.fill(false);
        self.resolved_text.fill(ResolvedTextStyle::default());
        self.paragraphs.reset();
        self.was_contents.fill(false);
        self.was_transformed.fill(false);
        self.top_layer.invalidate();
        self.style_flags.fill(NodeStyleFlags::empty());
        self.responsive_node_count = 0;
        self.container_node_count = 0;
        self.size_container_count = 0;
        self.responsive_changed_this_pass = true;
        self.counts_dirty = true;
        self.responsive_tree_dirty = true;
        self.cached_env_hash.fill(0);
        self.para_last_env_hash.fill(0);
        self.container_records.reset_all();
        self.boundary_records.reset_all();
        self.dirty_boundaries.clear();
        self.resolved_container_env.reset_all();
        self.pass_root = None;
        self.dry.invalidate();
        self.mark_inherit_full();
        self.layout_dirty = true;
        self.full_layout_needed = true;
    }

    #[inline]
    pub(super) fn mark_dirty(&mut self, store: &NodeStore, mut id: NodeId) {
        self.layout_dirty = true;
        let root = self.last_viewport.map_or(NodeId::ROOT, |(r, _, _)| r);
        let mut steps = 0usize;
        while store.is_live(id) {
            steps += 1;
            debug_assert!(
                steps <= crate::MAX_TREE_DEPTH + 1,
                "parent cycle detected in mark_dirty at node {:?}",
                id
            );
            if steps > crate::MAX_TREE_DEPTH + 1 {
                self.full_layout_needed = true;
                break;
            }
            let idx = id.index();
            if idx >= self.caches.len() {
                self.full_layout_needed = true;
                break;
            }
            let is_hoisted = id != root && self.top_layer.is_hoisted(id);
            let cleared = self.caches[idx].clear();
            if is_hoisted {
                // Hoisted overlays are laid out directly under `root`; jump straight there so
                // already-empty DOM ancestors cannot short-circuit `caches[root]` invalidation.
                let ridx = root.index();
                if ridx < self.caches.len() {
                    self.caches[ridx].clear();
                }
                self.full_layout_needed = true;
                break;
            }
            if steps > 1 && self.try_record_dirty_boundary(id, root) {
                break;
            }
            if matches!(cleared, ClearState::AlreadyEmpty)
                && steps > 1
                && !self.was_contents[idx]
                && !is_display_contents(store, id)
            {
                break;
            }
            if id == root {
                self.full_layout_needed = true;
                break;
            }
            let Some(p) = store.parent(id) else {
                self.full_layout_needed = true;
                break;
            };
            id = p;
        }
    }
}