use nativeview_store::{NodeId, NodeStore};
use super::node::NodeLists;
#[cfg(test)]
mod tests;
use crate::{DisplayEnv, TopLayerIndex};
#[derive(Debug, Default)]
pub(crate) struct BoxChildren {
lists: NodeLists,
flattened: Vec<bool>,
extras: Option<(NodeId, usize)>,
stack: Vec<NodeId>,
out: Vec<NodeId>,
}
impl BoxChildren {
pub(crate) fn ensure_len(&mut self, len: usize) {
self.lists.ensure_len(len);
if self.flattened.len() < len {
self.flattened.resize(len, false);
}
}
pub(crate) fn reset(&mut self) {
self.lists.reset();
self.flattened.clear();
self.extras = None;
self.stack.clear();
self.out.clear();
}
#[cfg(test)]
pub(crate) fn list_capacity(&self, parent: NodeId) -> usize {
self.lists.capacity_of(parent.index())
}
#[cfg(test)]
pub(crate) fn dead_slots(&self) -> usize {
self.lists.dead_slots()
}
#[cfg(test)]
pub(crate) fn assert_consistent(&self) {
self.lists.assert_consistent();
for (idx, &flat) in self.flattened.iter().enumerate() {
assert!(
flat || self.lists.capacity_of(idx) == 0,
"unflattened parent {idx} pins list capacity"
);
}
}
pub(crate) fn release(&mut self, idx: usize) {
self.lists.release(idx);
if let Some(f) = self.flattened.get_mut(idx) {
*f = false;
}
if self.extras.is_some_and(|(p, _)| p.index() == idx) {
self.extras = None;
}
}
pub(crate) fn rebuild(
&mut self,
store: &NodeStore,
top_layer: &TopLayerIndex,
parent: NodeId,
extra_children: &[NodeId],
env: &DisplayEnv<'_>,
) {
let idx = parent.index();
if self.extras.is_some_and(|(p, _)| p == parent) {
self.extras = None;
}
if store.is_inline_context_root(parent) {
if self.flattened.get(idx).copied().unwrap_or(false) {
self.release(idx);
}
return;
}
let children = store.children(parent);
let flatten = !extra_children.is_empty()
|| children.iter().any(|&c| {
env.is_display_contents(store, c)
|| store.is_inline(c)
|| top_layer.is_hoisted(c)
});
if !flatten {
if self.flattened.get(idx).copied().unwrap_or(false) {
self.release(idx);
}
return;
}
let Some(flag) = self.flattened.get_mut(idx) else {
return;
};
*flag = true;
self.out.clear();
self.stack.clear();
self.stack.extend(children.iter().rev());
while let Some(child) = self.stack.pop() {
if store.is_inline(child) {
continue;
}
if env.is_display_contents(store, child) {
if !store.is_inline_context_root(child) {
self.stack.extend(store.children(child).iter().rev());
}
} else if !top_layer.is_hoisted(child) {
self.out.push(child);
}
}
self.out.extend_from_slice(extra_children);
if !extra_children.is_empty() {
self.extras = Some((parent, extra_children.len()));
}
self.lists.clear(idx);
self.lists.extend(idx, self.out.iter().copied());
}
#[inline]
pub(crate) fn is_flattened(&self, parent: NodeId) -> bool {
self.flattened.get(parent.index()).copied().unwrap_or(false)
}
pub(crate) fn compact_if_fragmented(&mut self) {
self.lists.compact_if_fragmented();
}
#[inline]
pub(crate) fn get<'a>(&'a self, store: &'a NodeStore, parent: NodeId) -> &'a [NodeId] {
if store.is_inline_context_root(parent) {
&[]
} else if self.is_flattened(parent) {
self.lists.get(parent.index())
} else {
store.children(parent)
}
}
#[inline]
pub(crate) fn get_in_flow<'a>(&'a self, store: &'a NodeStore, parent: NodeId) -> &'a [NodeId] {
let all = self.get(store, parent);
match self.extras {
Some((p, n)) if p == parent && self.is_flattened(parent) => {
&all[..all.len().saturating_sub(n)]
}
_ => all,
}
}
}