use nativeview_store::{NodeId, NodeStore};
use nativeview_tables::tables::{Display, Interaction, Overlay, Position, TextContent};
use crate::MAX_TREE_DEPTH;
use super::super::ResolvedOverlay;
use super::{
CS_CLIPS, CS_HAS_SCROLL, CS_IS_STICKY, CS_IS_TOP_LAYER, CS_IS_TRANSFORM, ClipScrollIdx,
ClipScrollNode, DfsFrame, ItemCount, ItemPaintPhase, SCOPE_HAS_SCROLL, SCOPE_IS_MODAL, SCOPE_IS_STICKY, SCOPE_IS_TOP_LAYER, ScopeIdx, ScopeKind,
StackingBucket, StackingItem, StackingPlan, StackingScope, TopLayerScope, ZOrder,
};
use super::style::{
StackingStyleSource, bucket_for_sc_z, classify_node_sc_with_parent_display,
};
impl StackingPlan {
pub fn build<S: StackingStyleSource>(
&mut self,
store: &NodeStore,
open_overlays: &[ResolvedOverlay],
style: &S,
) {
self.clear();
let cap = store.capacity();
if self.node_to_scope.len() < cap {
self.node_to_scope.resize(cap, ScopeIdx::NONE);
}
if self.node_to_item.len() < cap {
self.node_to_item.resize(cap, u32::MAX);
}
let mut next_tree_order = 0u32;
if store.is_live(NodeId::ROOT) && !style.is_display_none(store, NodeId::ROOT) {
self.root = self.build_subtree(store, style, NodeId::ROOT, None, &mut next_tree_order);
}
for &ro in open_overlays {
if !store.is_live(ro.node) {
continue;
}
if let Some(scope) =
self.build_subtree(store, style, ro.node, Some(ro), &mut next_tree_order)
{
self.top_layers.push(TopLayerScope {
scope,
node_id: ro.node,
is_modal: ro.is_modal,
backdrop: ro.backdrop(),
});
}
}
for s in (0..self.scopes.len()).rev() {
let parent = self.scopes[s].parent_scope;
if parent.is_some() {
let child_min = self.scopes[s].min_descendant_clip;
let cur_min = self.scopes[parent.index()].min_descendant_clip;
if child_min != cur_min {
let lca = self.lca_clip(cur_min, child_min);
self.scopes[parent.index()].min_descendant_clip = lca;
}
}
}
self.write_cursors.clear();
let mut cursor = 0u32;
for s in 0..self.scopes.len() {
let count = self.scope_counts[s];
self.scopes[s].items_start = cursor;
self.scopes[s].items_len = ItemCount(count);
self.scopes[s].neg_end = 0;
self.write_cursors.push(cursor);
cursor += count;
}
self.ordered_items
.resize(cursor as usize, StackingItem::default());
for i in 0..self.staging.len() {
let (owner, item) = self.staging[i];
let oi = owner.index();
let dst = self.write_cursors[oi] as usize;
self.write_cursors[oi] += 1;
self.ordered_items[dst] = item;
if item.paint_phase != ItemPaintPhase::FgOnly {
let nidx = item.node_id.index();
if nidx < self.node_to_item.len() {
self.node_to_item[nidx] = dst as u32;
}
}
}
for s in 0..self.scopes.len() {
let flags = self.scope_sort_flags[s];
let len = self.scopes[s].items_len.as_usize();
if len == 0 {
continue;
}
if len > 1 && flags.needs_sort() {
self.resort_scope_in_place(ScopeIdx(s as u32));
} else if flags.has_neg() {
self.recompute_scope_neg_end(ScopeIdx(s as u32));
}
}
}
fn build_subtree<S: StackingStyleSource>(
&mut self,
store: &NodeStore,
style: &S,
root_id: NodeId,
top_layer: Option<ResolvedOverlay>,
next_tree_order: &mut u32,
) -> Option<ScopeIdx> {
let mut root_scope: Option<ScopeIdx> = None;
self.dfs_stack.clear();
self.dfs_stack.push(DfsFrame {
id: root_id,
cur_sc: ScopeIdx::NONE,
cur_inflow_owner: ScopeIdx::NONE,
box_parent_display: Display::Block,
box_clip: ClipScrollIdx::NONE,
abs_clip: ClipScrollIdx::NONE,
fixed_clip: ClipScrollIdx::NONE,
inherited_hidden: false,
depth: 0,
is_root_of_pass: true,
is_top_layer_root: top_layer.is_some(),
});
while let Some(frame) = self.dfs_stack.pop() {
let id = frame.id;
if frame.depth as usize > MAX_TREE_DEPTH
|| !store.is_live(id)
|| store.is_inline(id)
|| style.is_display_none(store, id)
{
continue;
}
if !frame.is_root_of_pass && store.has::<Overlay>(id) {
continue;
}
let effective_hidden = style.visibility_hidden(store, id, frame.inherited_hidden);
if !frame.is_root_of_pass && style.is_display_contents(store, id) {
for &cid in store.children(id).iter().rev() {
self.dfs_stack.push(DfsFrame {
id: cid,
inherited_hidden: effective_hidden,
depth: frame.depth + 1,
is_root_of_pass: false,
is_top_layer_root: false,
..frame
});
}
continue;
}
let tree_order = *next_tree_order;
*next_tree_order = next_tree_order.wrapping_add(1);
let (creates_sc, sc_z, has_transform, pos, display) =
classify_node_sc_with_parent_display(
store,
style,
id,
frame.box_parent_display,
frame.is_top_layer_root,
);
let cb_clip = if frame.is_root_of_pass {
ClipScrollIdx::NONE
} else {
match pos {
Position::Absolute => frame.abs_clip,
Position::Fixed => frame.fixed_clip,
Position::Static | Position::Relative | Position::Sticky => frame.box_clip,
}
};
let clips = style.clips_overflow(store, id);
let has_scroll = style.is_scrollable(store, id);
let is_sticky = !frame.is_top_layer_root && pos == Position::Sticky;
let mut cs_flags = 0u8;
if clips {
cs_flags |= CS_CLIPS;
}
if has_scroll {
cs_flags |= CS_HAS_SCROLL;
}
if is_sticky {
cs_flags |= CS_IS_STICKY;
}
if frame.is_top_layer_root {
cs_flags |= CS_IS_TOP_LAYER;
}
if has_transform {
cs_flags |= CS_IS_TRANSFORM;
}
let child_cs = if cs_flags != 0 {
let idx = ClipScrollIdx(self.clips.len() as u32);
let depth = self.clip_depth(cb_clip).saturating_add(1);
self.clips.push(ClipScrollNode {
parent: cb_clip,
owner: id,
depth,
flags: cs_flags,
});
idx
} else {
cb_clip
};
let next_box_clip = child_cs;
let next_abs_clip =
if frame.is_top_layer_root || has_transform || pos != Position::Static {
child_cs
} else {
frame.abs_clip
};
let next_fixed_clip = if frame.is_top_layer_root || has_transform {
child_cs
} else {
frame.fixed_clip
};
let mut scope_flags = 0u8;
if frame.is_top_layer_root {
scope_flags |= SCOPE_IS_TOP_LAYER;
if top_layer.is_some_and(|ro| ro.is_modal) {
scope_flags |= SCOPE_IS_MODAL;
}
}
if has_scroll {
scope_flags |= SCOPE_HAS_SCROLL;
}
if is_sticky {
scope_flags |= SCOPE_IS_STICKY;
}
let children = if store.is_inline_context_root(id) {
&[][..]
} else {
store.children(id)
};
let (next_sc, next_inflow) = if frame.is_root_of_pass {
let s = self.alloc_scope(StackingScope {
node_id: id,
parent_scope: ScopeIdx::NONE,
clip_scroll: ClipScrollIdx::NONE,
min_descendant_clip: ClipScrollIdx::NONE,
items_start: 0,
items_len: ItemCount::ZERO,
neg_end: 0,
kind: ScopeKind::RealStackingContext,
hidden: effective_hidden,
has_transform,
flags: scope_flags,
});
root_scope = Some(s);
(s, s)
} else if creates_sc {
let s = self.alloc_scope(StackingScope {
node_id: id,
parent_scope: frame.cur_sc,
clip_scroll: cb_clip,
min_descendant_clip: cb_clip,
items_start: 0,
items_len: ItemCount::ZERO,
neg_end: 0,
kind: ScopeKind::RealStackingContext,
hidden: effective_hidden,
has_transform,
flags: scope_flags,
});
self.push_item(
frame.cur_sc,
StackingItem {
node_id: id,
child_scope: s,
clip_scroll: cb_clip,
z_index: ZOrder(sc_z),
tree_order,
bucket: bucket_for_sc_z(sc_z),
paint_phase: ItemPaintPhase::Full,
hidden: effective_hidden,
},
);
(s, s)
} else if pos != Position::Static {
let child_scope = if children.is_empty() && !has_scroll {
ScopeIdx::NONE
} else {
self.alloc_scope(StackingScope {
node_id: id,
parent_scope: frame.cur_sc,
clip_scroll: cb_clip,
min_descendant_clip: cb_clip,
items_start: 0,
items_len: ItemCount::ZERO,
neg_end: 0,
kind: ScopeKind::PseudoStackingContext,
hidden: effective_hidden,
has_transform: false,
flags: scope_flags,
})
};
self.push_item(
frame.cur_sc,
StackingItem {
node_id: id,
child_scope,
clip_scroll: cb_clip,
z_index: ZOrder(0),
tree_order,
bucket: StackingBucket::Step8,
paint_phase: ItemPaintPhase::Full,
hidden: effective_hidden,
},
);
(frame.cur_sc, child_scope)
} else {
let parent_is_flex_or_grid = matches!(
frame.box_parent_display,
Display::Flex | Display::InlineFlex | Display::Grid
);
let is_atomic_inline = parent_is_flex_or_grid
|| matches!(
display,
Display::Inline | Display::InlineBlock | Display::InlineFlex
);
if is_atomic_inline || has_scroll {
let bucket = if is_atomic_inline {
StackingBucket::InFlowInline
} else {
StackingBucket::InFlowBlock
};
let child_scope = if children.is_empty() && !has_scroll {
ScopeIdx::NONE
} else {
self.alloc_scope(StackingScope {
node_id: id,
parent_scope: frame.cur_inflow_owner,
clip_scroll: cb_clip,
min_descendant_clip: cb_clip,
items_start: 0,
items_len: ItemCount::ZERO,
neg_end: 0,
kind: ScopeKind::PseudoStackingContext,
hidden: effective_hidden,
has_transform: false,
flags: scope_flags,
})
};
self.push_item(
frame.cur_inflow_owner,
StackingItem {
node_id: id,
child_scope,
clip_scroll: cb_clip,
z_index: ZOrder(0),
tree_order,
bucket,
paint_phase: ItemPaintPhase::Full,
hidden: effective_hidden,
},
);
(frame.cur_sc, child_scope)
} else if store.has::<TextContent>(id)
|| store
.try_row::<Interaction>(id)
.is_some_and(|i| i.captures_text_keys())
{
self.push_item(
frame.cur_inflow_owner,
StackingItem {
node_id: id,
child_scope: ScopeIdx::NONE,
clip_scroll: cb_clip,
z_index: ZOrder(0),
tree_order,
bucket: StackingBucket::InFlowBlock,
paint_phase: ItemPaintPhase::BgOnly,
hidden: effective_hidden,
},
);
self.push_item(
frame.cur_inflow_owner,
StackingItem {
node_id: id,
child_scope: ScopeIdx::NONE,
clip_scroll: cb_clip,
z_index: ZOrder(0),
tree_order,
bucket: StackingBucket::InFlowInline,
paint_phase: ItemPaintPhase::FgOnly,
hidden: effective_hidden,
},
);
(frame.cur_sc, frame.cur_inflow_owner)
} else {
self.push_item(
frame.cur_inflow_owner,
StackingItem {
node_id: id,
child_scope: ScopeIdx::NONE,
clip_scroll: cb_clip,
z_index: ZOrder(0),
tree_order,
bucket: StackingBucket::InFlowBlock,
paint_phase: ItemPaintPhase::Full,
hidden: effective_hidden,
},
);
(frame.cur_sc, frame.cur_inflow_owner)
}
};
if !children.is_empty() {
for &cid in children.iter().rev() {
self.dfs_stack.push(DfsFrame {
id: cid,
cur_sc: next_sc,
cur_inflow_owner: next_inflow,
box_parent_display: display,
box_clip: next_box_clip,
abs_clip: next_abs_clip,
fixed_clip: next_fixed_clip,
inherited_hidden: effective_hidden,
depth: frame.depth + 1,
is_root_of_pass: false,
is_top_layer_root: false,
});
}
}
}
root_scope
}
}