1use std::cell::Cell;
5
6use nativeview_store::{NodeId, NodeStore};
7use nativeview_tables::tables::{
8 Display, Interaction, Isolation, Overflow, Position, Scroll, TextContent, is_closed_overlay,
9 is_translucent_opacity,
10};
11use nativeview_tables::transform::Transform2D;
12
13use crate::MAX_TREE_DEPTH;
14
15use super::StackingBucket;
16use super::super::style_cascade::{ResponsivePatches, StackingStyle, StyleCascade};
17
18pub trait StackingStyleSource {
21 fn is_display_none(&self, store: &NodeStore, id: NodeId) -> bool;
22 fn is_display_contents(&self, store: &NodeStore, id: NodeId) -> bool;
23 fn display_of(&self, store: &NodeStore, id: NodeId) -> Display;
24 fn position_of(&self, store: &NodeStore, id: NodeId) -> Position;
25 fn z_index_of(&self, store: &NodeStore, id: NodeId) -> Option<i32>;
26 fn opacity_of(&self, store: &NodeStore, id: NodeId) -> f32;
27 fn isolation_of(&self, store: &NodeStore, id: NodeId) -> Isolation;
28 fn has_transform(&self, store: &NodeStore, id: NodeId) -> bool;
29 fn visibility_hidden(&self, store: &NodeStore, id: NodeId, inherited_hidden: bool) -> bool;
30 fn clips_overflow(&self, store: &NodeStore, id: NodeId) -> bool;
31 fn is_scrollable(&self, store: &NodeStore, id: NodeId) -> bool;
32}
33
34pub trait StackingOverrides {
38 fn resolve_transform(&self, id: NodeId, base: Option<Transform2D>) -> Option<Transform2D>;
39
40 #[inline]
41 fn resolve_opacity(&self, _id: NodeId, base: f32) -> f32 {
42 base
43 }
44}
45
46impl<F: Fn(NodeId, Option<Transform2D>) -> Option<Transform2D>> StackingOverrides for F {
47 #[inline]
48 fn resolve_transform(&self, id: NodeId, base: Option<Transform2D>) -> Option<Transform2D> {
49 (self)(id, base)
50 }
51}
52
53pub struct SceneStackingStyleSource<'a, F, R: ResponsivePatches + ?Sized = dyn ResponsivePatches> {
57 cascade: StyleCascade<'a, R>,
58 overrides: F,
59 last: Cell<Option<(NodeId, StackingStyle)>>,
60}
61
62impl<'a, F, R: ResponsivePatches + ?Sized> SceneStackingStyleSource<'a, F, R> {
63 #[inline]
64 pub fn new(cascade: StyleCascade<'a, R>, overrides: F) -> Self {
65 Self {
66 cascade,
67 overrides,
68 last: Cell::new(None),
69 }
70 }
71
72 #[inline]
73 fn style(&self, store: &NodeStore, id: NodeId) -> StackingStyle {
74 if let Some((last_id, style)) = self.last.get()
75 && last_id == id
76 {
77 return style;
78 }
79 let style = self.cascade.stacking_style(store, id);
80 self.last.set(Some((id, style)));
81 style
82 }
83}
84
85impl<F: StackingOverrides, R: ResponsivePatches + ?Sized> StackingStyleSource
86 for SceneStackingStyleSource<'_, F, R>
87{
88 #[inline]
89 fn is_display_none(&self, store: &NodeStore, id: NodeId) -> bool {
90 !store.is_live(id) || self.display_of(store, id) == Display::None
91 }
92
93 #[inline]
94 fn is_display_contents(&self, store: &NodeStore, id: NodeId) -> bool {
95 id != NodeId::ROOT && store.is_live(id) && self.display_of(store, id) == Display::Contents
96 }
97
98 #[inline]
99 fn display_of(&self, store: &NodeStore, id: NodeId) -> Display {
100 self.style(store, id).display
101 }
102
103 #[inline]
104 fn position_of(&self, store: &NodeStore, id: NodeId) -> Position {
105 self.style(store, id).position
106 }
107
108 #[inline]
109 fn z_index_of(&self, store: &NodeStore, id: NodeId) -> Option<i32> {
110 self.style(store, id).z_index
111 }
112
113 #[inline]
114 fn opacity_of(&self, store: &NodeStore, id: NodeId) -> f32 {
115 self.overrides.resolve_opacity(id, self.style(store, id).opacity)
116 }
117
118 #[inline]
119 fn isolation_of(&self, store: &NodeStore, id: NodeId) -> Isolation {
120 self.style(store, id).isolation
121 }
122
123 #[inline]
124 fn has_transform(&self, store: &NodeStore, id: NodeId) -> bool {
125 self.overrides
126 .resolve_transform(id, self.style(store, id).transform)
127 .is_some_and(|t| !t.is_identity())
128 }
129
130 #[inline]
131 fn visibility_hidden(&self, store: &NodeStore, id: NodeId, inherited_hidden: bool) -> bool {
132 self.style(store, id)
133 .visibility
134 .map_or(inherited_hidden, |v| v.resolve(inherited_hidden))
135 }
136
137 #[inline]
138 fn clips_overflow(&self, store: &NodeStore, id: NodeId) -> bool {
139 if store.has::<Scroll>(id) {
140 return true;
141 }
142 let s = self.style(store, id);
143 let clips = |o: Overflow| matches!(o, Overflow::Hidden | Overflow::Scroll | Overflow::Auto);
144 s.overflow_x.is_some_and(clips) || s.overflow_y.is_some_and(clips)
145 }
146
147 #[inline]
148 fn is_scrollable(&self, store: &NodeStore, id: NodeId) -> bool {
149 if store.has::<Scroll>(id) {
150 return true;
151 }
152 let s = self.style(store, id);
153 let scrollable = |o: Overflow| matches!(o, Overflow::Scroll | Overflow::Auto);
154 s.overflow_x.is_some_and(scrollable) || s.overflow_y.is_some_and(scrollable)
155 }
156}
157
158#[inline]
160pub const fn bucket_for_sc_z(z: i32) -> StackingBucket {
161 if z < 0 {
162 StackingBucket::NegSc
163 } else if z == 0 {
164 StackingBucket::Step8
165 } else {
166 StackingBucket::PosSc
167 }
168}
169
170#[inline]
174pub fn classify_node_sc_with_parent_display<S: StackingStyleSource>(
175 store: &NodeStore,
176 style: &S,
177 id: NodeId,
178 box_parent_display: Display,
179 is_top_layer_root: bool,
180) -> (bool, i32, bool, Position, Display) {
181 let pos = style.position_of(store, id);
182 let display = style.display_of(store, id);
183 let is_positioned = pos != Position::Static;
184 let parent_is_flex_or_grid = matches!(
185 box_parent_display,
186 Display::Flex | Display::InlineFlex | Display::Grid
187 );
188 let z_applies = is_positioned || parent_is_flex_or_grid;
189 let eff_z = if z_applies {
190 style.z_index_of(store, id)
191 } else {
192 None
193 };
194 let has_transform = style.has_transform(store, id);
195 let creates_sc = id == NodeId::ROOT
196 || is_top_layer_root
197 || eff_z.is_some()
198 || matches!(pos, Position::Fixed | Position::Sticky)
199 || is_translucent_opacity(style.opacity_of(store, id))
200 || has_transform
201 || style.isolation_of(store, id) == Isolation::Isolate
202 || store
203 .node_tables(id)
204 .intersects(store.stacking_context_tables());
205 (creates_sc, eff_z.unwrap_or(0), has_transform, pos, display)
206}
207
208pub fn resolve_node_sc_state<S: StackingStyleSource>(
211 store: &NodeStore,
212 style: &S,
213 id: NodeId,
214 is_top_layer_root: bool,
215) -> (bool, i32, bool) {
216 let box_parent_display = if id == NodeId::ROOT || is_top_layer_root {
217 Display::Block
218 } else {
219 let mut cur = store.parent(id);
220 let mut parent_display = Display::Block;
221 for _ in 0..MAX_TREE_DEPTH {
222 let Some(p) = cur else {
223 break;
224 };
225 if !style.is_display_contents(store, p) {
226 parent_display = style.display_of(store, p);
227 break;
228 }
229 cur = store.parent(p).filter(|&gp| gp != p);
230 }
231 parent_display
232 };
233 let (creates_sc, sc_z, has_transform, _, _) = classify_node_sc_with_parent_display(
234 store,
235 style,
236 id,
237 box_parent_display,
238 is_top_layer_root,
239 );
240 (creates_sc, sc_z, has_transform)
241}
242
243#[derive(Debug, Clone, Copy, PartialEq, Eq)]
246pub struct StackingNodeRole {
247 pub absent: bool,
248 pub position: Position,
249 pub display: Display,
250 pub is_real_sc: bool,
251 pub sc_z: i32,
252 pub has_transform: bool,
253 pub clips_children: bool,
254 pub has_scroll_table: bool,
255 pub is_scrollable: bool,
256 pub effective_hidden: bool,
257 pub has_text_split: bool,
258}
259
260impl StackingNodeRole {
261 pub fn resolve<S: StackingStyleSource>(
266 store: &NodeStore,
267 style: &S,
268 id: NodeId,
269 is_top_layer: bool,
270 parent_hidden: bool,
271 ) -> Self {
272 let (is_real_sc, sc_z, has_transform) =
273 resolve_node_sc_state(store, style, id, is_top_layer);
274 let is_contents = style.is_display_contents(store, id);
275 let absent = style.is_display_none(store, id)
276 || is_closed_overlay(store, id)
277 || (!is_top_layer && is_contents);
278 let position = style.position_of(store, id);
279 let display = style.display_of(store, id);
280 let has_scroll_table = store.has::<Scroll>(id);
281 Self {
282 absent,
283 position,
284 display,
285 is_real_sc,
286 sc_z,
287 has_transform,
288 clips_children: !is_contents && style.clips_overflow(store, id),
289 has_scroll_table,
290 is_scrollable: has_scroll_table || style.is_scrollable(store, id),
291 effective_hidden: style.visibility_hidden(store, id, parent_hidden),
292 has_text_split: store.has::<TextContent>(id)
293 || store
294 .try_row::<Interaction>(id)
295 .is_some_and(|i| i.captures_text_keys()),
296 }
297 }
298}