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
//! The post-layout commit walk: fused cumulative rounding, absolute origin accumulation, and the
//! committed `@container` env of every box (honoring containing-block hoisting for
//! `position: absolute | fixed`, whose `@container` env still follows the flat-tree ancestor).
use nativeview_engine::{InteractionTracker, LayoutRect};
use nativeview_store::{NodeId, NodeStore};
use nativeview_tables::container::ContainerEnv;
use nativeview_tables::tables::{ContainerType, Display as NvDisplay, Layout as NvLayout};
use crate::ResolvedBoxEdges;
use crate::scene::{EffectiveBox, NoResponsivePatches, StyleCascade};
use super::container::scope::{ContainerRecord, ContainerScope, PaddingBorder, scope_of};
use super::responsive::PassPatches;
use super::responsive::sync::NodeStyleFlags;
use super::{TaffyStoreLayout, UnroundedBox, gen_tag};
#[inline(always)]
fn same_rect_bits(a: LayoutRect, b: LayoutRect) -> bool {
a.x.to_bits() == b.x.to_bits()
&& a.y.to_bits() == b.y.to_bits()
&& a.width.to_bits() == b.width.to_bits()
&& a.height.to_bits() == b.height.to_bits()
}
/// One pending node of the commit walk.
#[derive(Debug, Clone, Copy)]
pub(super) struct RoundFrame {
id: NodeId,
/// Absolute origin of the parent box (unrounded).
px: f32,
py: f32,
ancestor_hidden: bool,
/// Container scope inherited from the in-flow parent.
scope: ContainerScope,
/// Hoisted to a containing block: resolve the env from the flat-tree ancestor instead.
hoisted: bool,
}
impl TaffyStoreLayout {
/// `id`'s [`EffectiveBox`] under its in-pass `@container` env `cenv` (the shared
/// [`StyleCascade`], live pseudo-state included), fast-pathing plain nodes that have neither
/// `Responsive` rows nor box-patching `StateRules`.
#[inline]
fn box_of(
&self,
store: &NodeStore,
id: NodeId,
cenv: ContainerEnv,
interaction: Option<&InteractionTracker>,
) -> EffectiveBox {
let flags = self
.style_flags
.get(id.index())
.copied()
.unwrap_or(NodeStyleFlags::empty());
let needs_resp =
self.responsive_node_count > 0 && flags.contains(NodeStyleFlags::RESPONSIVE);
let needs_state =
interaction.is_some() && flags.contains(NodeStyleFlags::HAS_STATE_BOX_PATCH);
if !needs_resp && !needs_state {
let mut effective =
EffectiveBox::from_layout(store, id, store.try_row::<NvLayout>(id), false);
if effective.display == NvDisplay::None {
effective.container_type = ContainerType::Normal;
}
return effective;
}
let pass = PassPatches {
media: &self.media_env,
container_env: cenv,
};
let responsive: &dyn crate::scene::ResponsivePatches = if needs_resp {
&pass
} else {
&NoResponsivePatches
};
StyleCascade {
responsive,
interaction: needs_state.then_some(interaction).flatten(),
inert: None,
}
.effective_box(store, id)
}
/// Rounds every box of `root`'s tree into `rects`, records exact `changed_rects`, and commits
/// each box's `@container` env and relayout-boundary origin. Returns whether any committed
/// `@container` env changed.
pub(super) fn commit_round_pass(
&mut self,
store: &NodeStore,
root: NodeId,
interaction: Option<&InteractionTracker>,
) -> bool {
self.changed_rects.clear();
self.all_rects_changed = false;
let prev_valid_gen = self.rect_valid_gen;
self.layout_gen = self.layout_gen.wrapping_add(1).max(1);
self.rect_valid_gen = self.layout_gen;
let cur_valid_gen = self.rect_valid_gen;
self.commit_round_walk(
store,
root,
RoundFrame {
id: root,
px: 0.0,
py: 0.0,
ancestor_hidden: false,
scope: ContainerScope::default(),
hoisted: false,
},
prev_valid_gen,
cur_valid_gen,
interaction,
)
}
/// Rounds one relayout containment boundary subtree starting at `boundary` without bumping
/// `rect_valid_gen`, appending exact rect deltas into `self.changed_rects`.
#[allow(clippy::too_many_arguments)]
pub(super) fn commit_round_boundary(
&mut self,
store: &NodeStore,
root: NodeId,
boundary: NodeId,
parent_origin: (f32, f32),
enclosing_scope: ContainerScope,
hoisted: bool,
interaction: Option<&InteractionTracker>,
) -> bool {
let valid_gen = self.rect_valid_gen;
self.commit_round_walk(
store,
root,
RoundFrame {
id: boundary,
px: parent_origin.0,
py: parent_origin.1,
ancestor_hidden: false,
scope: enclosing_scope,
hoisted,
},
valid_gen,
valid_gen,
interaction,
)
}
fn commit_round_walk(
&mut self,
store: &NodeStore,
root: NodeId,
start: RoundFrame,
prev_valid_gen: u64,
cur_valid_gen: u64,
interaction: Option<&InteractionTracker>,
) -> bool {
let mut envs_changed = false;
let any_cont = self.container_node_count > 0;
self.round_stack.clear();
self.round_stack.push(start);
while let Some(f) = self.round_stack.pop() {
let id = f.id;
if !store.is_live(id) || store.is_inline(id) {
continue;
}
let idx = id.index();
let scope = if f.hoisted && any_cont {
scope_of(store, &self.container_records, id)
} else {
f.scope
};
let cenv = scope.env;
if any_cont {
envs_changed |= self.resolved_container_env.get(id) != Some(cenv);
self.resolved_container_env.set(id, cenv);
}
let effective = self.box_of(store, id, cenv, interaction);
let is_contents = id != root && effective.is_contents();
let is_hidden_self = effective.is_none();
let ct = if any_cont {
effective.container_type
} else {
ContainerType::Normal
};
// `display: contents` generates no box: a zero rect at the parent origin, which its
// children (laid out by Taffy as the parent's) inherit unchanged.
let b = if is_contents {
if self.capture_box_edges && idx < self.box_edges.len() {
self.box_edges[idx] = ResolvedBoxEdges::default();
}
UnroundedBox::default()
} else {
self.boxes[idx]
};
let cx = f.px + b.x;
let cy = f.py + b.y;
let rx = cx.round();
let ry = cy.round();
let new_rect = LayoutRect {
x: rx,
y: ry,
width: (cx + b.w).round() - rx,
height: (cy + b.h).round() - ry,
};
let tag = gen_tag(id);
let was_valid = prev_valid_gen != 0
&& self.rect_gen[idx] == prev_valid_gen
&& self.node_gens[idx] == tag;
if !was_valid || !same_rect_bits(self.rects[idx], new_rect) {
self.changed_rects.push(id);
}
self.rects[idx] = new_rect;
self.rect_gen[idx] = cur_valid_gen;
self.node_gens[idx] = tag;
let hidden = f.ancestor_hidden || is_hidden_self;
// A box-generating container's committed env is its final content box: final
// border box minus the padding + border its layout recorded.
let child_scope = if !any_cont {
scope
} else if !hidden && !is_contents && ct != ContainerType::Normal {
let padding_border = self
.container_records
.get(id)
.map_or(PaddingBorder::default(), |r| r.padding_border);
let inner = scope.nested(ct, b.w, b.h, padding_border);
self.container_records.set(
id,
ContainerRecord {
inner,
padding_border,
},
);
if self.size_container_count > 0 && ct == ContainerType::Size && id != root {
if let Some(mut rec) = self.boundary_records.get(id) {
rec.parent_origin = (f.px, f.py);
rec.hoisted = f.hoisted;
rec.round_valid = rec.layout_valid && rec.layout.definite_size;
self.boundary_records.set(id, rec);
}
} else if self.size_container_count > 0 {
self.boundary_records.clear_slot(idx);
}
inner
} else {
self.container_records.clear_slot(idx);
self.boundary_records.clear_slot(idx);
scope
};
if !hidden {
for &child in self.hoisted.get(idx).iter().rev() {
if store.is_live(child) {
self.round_stack.push(RoundFrame {
id: child,
px: cx,
py: cy,
ancestor_hidden: false,
scope: child_scope,
hoisted: true,
});
}
}
// A top-layer root that became `display: none` during this pass (e.g. via an
// `@container` rule on the overlay itself) is omitted from Taffy's `hoisted[root]`
// yet still skipped in its DOM parent until `refresh_top_layer_after_pass` runs.
// Visit it here so its `resolved_container_env` updates and un-hoisting converges.
if id == root && !self.top_layer.hoisted().is_empty() {
let root_hoisted = self.hoisted.get(idx);
for &ov in self.top_layer.hoisted().iter().rev() {
if store.is_live(ov) && !root_hoisted.contains(&ov) {
self.round_stack.push(RoundFrame {
id: ov,
px: cx,
py: cy,
ancestor_hidden: true,
scope: child_scope,
hoisted: true,
});
}
}
}
}
if !store.is_inline_context_root(id) {
for &child in store.children(id).iter().rev() {
// Open overlays hoisted to the root are reached through the root's hoisted
// list (their origin is the viewport, not this parent). Guard the OOF check
// behind `NodeStyleFlags::OUT_OF_FLOW` so ~98% in-flow children skip `box_of`.
let may_oof = self
.style_flags
.get(child.index())
.is_some_and(|fl| fl.contains(NodeStyleFlags::OUT_OF_FLOW));
if self.top_layer.is_hoisted(child)
|| (!hidden
&& may_oof
&& self
.box_of(store, child, child_scope.env, interaction)
.is_out_of_flow())
{
continue;
}
self.round_stack.push(RoundFrame {
id: child,
px: cx,
py: cy,
ancestor_hidden: hidden,
scope: child_scope,
hoisted: false,
});
}
}
}
envs_changed
}
}