use nativeview_abi::{NodeId, NV_PARAGRAPH_NONE};
use nativeview_engine::InteractionTracker;
use nativeview_store::{ChangeSet, NodeStore};
use crate::measure::ResolvedTextStyle;
use crate::taffy_store::responsive::text::ResponsiveTextEnv;
use super::InheritedText;
use crate::MAX_TREE_DEPTH;
use super::ParagraphStore;
impl ParagraphStore {
fn unlink_paragraph_span(&mut self, id: NodeId) -> bool {
let idx = id.index();
if idx >= self.node_to_para.len() {
return false;
}
let slot = self.node_to_para[idx];
if slot == NV_PARAGRAPH_NONE {
return false;
}
let slot_usize = slot as usize;
self.node_to_para[idx] = NV_PARAGRAPH_NONE;
let old_meta = self.meta[slot_usize];
let old_run_span = old_meta.run_span;
let old_text_span = old_meta.text_span;
let run_start = old_run_span.off() as usize;
let run_len = old_run_span.len() as usize;
for k in 0..run_len {
let rid = self.runs.data()[run_start + k].node_id;
if rid != id
&& !rid.is_null()
&& rid.index() < self.owner_paragraph.len()
&& self.owner_paragraph[rid.index()] == id
{
self.owner_paragraph[rid.index()] = NodeId::NULL;
}
}
self.runs.free(old_run_span);
self.text.free(old_text_span);
let last = self.spans.len() - 1;
if slot_usize != last {
let moved_owner = self.spans[last].owner_node_id;
self.spans.swap_remove(slot_usize);
self.meta.swap_remove(slot_usize);
if moved_owner.index() < self.node_to_para.len() {
self.node_to_para[moved_owner.index()] = slot;
}
} else {
self.spans.pop();
self.meta.pop();
}
true
}
pub(super) fn remove_paragraph_span(&mut self, store: &NodeStore, id: NodeId, push_list: bool) {
if !self.unlink_paragraph_span(id) {
return;
}
if store.is_live(id) {
self.sync_subtree_inline_transitions(store, id, push_list);
}
}
fn sync_subtree_inline_transitions(&mut self, store: &NodeStore, root: NodeId, push_list: bool) {
let base = self.subtree_stack.len();
self.subtree_stack.push(root);
let mut visited = 0usize;
let cap = store.capacity().max(1);
while self.subtree_stack.len() > base {
let Some(nid) = self.subtree_stack.pop() else {
break;
};
if visited > cap || !store.is_live(nid) {
continue;
}
visited += 1;
let idx = nid.index();
let now_inline = store.is_inline(nid);
let prev_inline = self.was_inline[idx];
if prev_inline != now_inline {
self.was_inline[idx] = now_inline;
if now_inline {
if !self.is_created_this_frame(nid) {
self.box_to_inline.push(nid);
}
if self.node_to_para[idx] != NV_PARAGRAPH_NONE {
self.unlink_paragraph_span(nid);
}
} else {
self.owner_paragraph[idx] = NodeId::NULL;
if !self.is_created_this_frame(nid) {
self.inline_to_box.push(nid);
}
if store.is_inline_context_root(nid) {
self.queue_dirty(nid, push_list);
}
}
for &child in store.children(nid) {
self.subtree_stack.push(child);
}
} else if !now_inline && nid == root {
for &child in store.children(nid) {
let cidx = child.index();
if cidx < self.was_inline.len()
&& store.is_live(child)
&& self.was_inline[cidx] != store.is_inline(child)
{
self.subtree_stack.push(child);
}
}
}
}
self.subtree_stack.truncate(base);
}
fn find_paragraph_root(store: &NodeStore, mut id: NodeId) -> Option<NodeId> {
for _ in 0..MAX_TREE_DEPTH {
let p = store.parent(id)?;
if store.is_inline_context_root(p) {
return Some(p);
}
if !store.is_inline(p) {
return None;
}
id = p;
}
None
}
fn lift_to_paragraph(&mut self, store: &NodeStore, id: NodeId, push_list: bool) {
let idx = id.index();
if idx < self.owner_paragraph.len() {
let prev_owner = self.owner_paragraph[idx];
if !prev_owner.is_null() {
if store.is_live(prev_owner) && store.is_inline_context_root(prev_owner) {
self.queue_dirty(prev_owner, push_list);
}
if !store.is_live(id) || !store.is_inline(id) {
self.owner_paragraph[idx] = NodeId::NULL;
}
}
}
if store.is_live(id) {
if store.is_inline(id) {
if let Some(root) = Self::find_paragraph_root(store, id) {
self.queue_dirty(root, push_list);
}
} else if store.is_inline_context_root(id) {
self.queue_dirty(id, push_list);
} else if idx < self.node_to_para.len() && self.node_to_para[idx] != NV_PARAGRAPH_NONE
{
self.remove_paragraph_span(store, id, push_list);
}
}
}
pub fn sync_changes(
&mut self,
store: &NodeStore,
changes: &ChangeSet,
inherited: InheritedText<'_>,
interaction: Option<&InteractionTracker>,
defer_compile: bool,
) {
self.ensure_capacity(store.capacity());
self.reset_frame_deltas();
let push_list = !defer_compile;
if !changes.created.is_empty() {
let mut new_paras = 0usize;
for &id in &changes.created {
if store.is_live(id) && store.is_inline_context_root(id) {
new_paras += 1;
}
}
if new_paras > 0 {
self.spans.reserve(new_paras);
self.meta.reserve(new_paras);
self.dirty_layout_paras.reserve(new_paras);
self.dirty_paint_paras.reserve(new_paras);
let _ = self
.runs
.try_reserve_data(self.runs.data().len().saturating_add(new_paras));
if push_list {
self.dirty_paras.reserve(new_paras);
}
}
}
Self::bump_epoch(&mut self.created_epoch, &mut self.created_stamp);
for &id in &changes.created {
let idx = id.index();
if idx < self.created_stamp.len() {
self.created_stamp[idx] = self.created_epoch;
}
}
for &id in &changes.removed {
let idx = id.index();
if idx < self.owner_paragraph.len() {
let owner = self.owner_paragraph[idx];
if !owner.is_null() && store.is_live(owner) && store.is_inline_context_root(owner) {
self.queue_dirty(owner, push_list);
}
self.owner_paragraph[idx] = NodeId::NULL;
self.was_inline[idx] = false;
}
if idx < self.node_to_para.len() && self.node_to_para[idx] != NV_PARAGRAPH_NONE {
self.remove_paragraph_span(store, id, push_list);
}
}
for &id in &changes.created {
if !store.is_live(id) {
continue;
}
let idx = id.index();
self.was_inline[idx] = store.is_inline(id);
self.owner_paragraph[idx] = NodeId::NULL;
self.lift_to_paragraph(store, id, push_list);
}
for &parent in &changes.children {
if !store.is_live(parent) {
continue;
}
self.sync_subtree_inline_transitions(store, parent, push_list);
self.lift_to_paragraph(store, parent, push_list);
if store.is_inline_context_root(parent) || store.is_inline(parent) {
for &child in store.children(parent) {
if store.is_live(child) {
self.sync_subtree_inline_transitions(store, child, push_list);
}
}
}
}
for &id in &changes.touched {
if !store.is_live(id) {
continue;
}
let idx = id.index();
if self.was_inline[idx] != store.is_inline(id) {
self.sync_subtree_inline_transitions(store, id, push_list);
} else if idx < self.node_to_para.len()
&& self.node_to_para[idx] != NV_PARAGRAPH_NONE
&& !store.is_inline_context_root(id)
{
self.remove_paragraph_span(store, id, push_list);
}
self.lift_to_paragraph(store, id, push_list);
}
if push_list {
let count = self.dirty_paras.len();
for i in 0..count {
let pid = self.dirty_paras[i];
if !store.is_live(pid) || !store.is_inline_context_root(pid) {
continue;
}
let (base_text, base_rtl) = self.cached_or_parent_base(
store,
pid,
inherited.text,
inherited.rtl,
);
let (layout_changed, paint_changed) = self.compile_paragraph(
store,
pid,
base_text,
base_rtl,
interaction,
inherited.responsive,
);
self.dirty_stamp[pid.index()] = 0;
if layout_changed {
self.record_layout_dirty(pid);
}
if paint_changed {
self.record_paint_dirty(pid);
}
}
self.dirty_paras.clear();
}
}
#[inline]
fn cached_or_parent_base(
&self,
store: &NodeStore,
pid: NodeId,
resolved_text: &[ResolvedTextStyle],
resolved_rtl: &[bool],
) -> (ResolvedTextStyle, bool) {
if !resolved_text.is_empty() && !resolved_rtl.is_empty() {
return match store.parent(pid) {
Some(p) if p.index() < resolved_text.len() && p.index() < resolved_rtl.len() => {
(resolved_text[p.index()], resolved_rtl[p.index()])
}
_ => (ResolvedTextStyle::default(), false),
};
}
if let Some(slot) = self.paragraph_slot(pid) {
let m = self.meta[slot as usize];
return (m.base_style, m.base_rtl);
}
(ResolvedTextStyle::default(), false)
}
pub fn sync_inherited_paragraph(
&mut self,
store: &NodeStore,
pid: NodeId,
parent_text: ResolvedTextStyle,
parent_rtl: bool,
interaction: Option<&InteractionTracker>,
responsive: Option<ResponsiveTextEnv<'_>>,
) -> (bool, bool) {
self.ensure_capacity(store.capacity());
if !store.is_live(pid) || !store.is_inline_context_root(pid) {
return (false, false);
}
let idx = pid.index();
let queued = self.dirty_stamp[idx] == self.dirty_epoch;
let existing_slot = self.paragraph_slot(pid);
let base_changed = existing_slot.is_none_or(|slot| {
let m = self.meta[slot as usize];
m.base_rtl != parent_rtl || !m.base_style.style_eq(&parent_text)
});
if !queued && !base_changed {
return (false, false);
}
self.dirty_stamp[idx] = 0;
self.recompile_paragraph(store, pid, parent_text, parent_rtl, interaction, responsive)
}
pub fn recompile_paragraph(
&mut self,
store: &NodeStore,
pid: NodeId,
parent_text: ResolvedTextStyle,
parent_rtl: bool,
interaction: Option<&InteractionTracker>,
responsive: Option<ResponsiveTextEnv<'_>>,
) -> (bool, bool) {
self.ensure_capacity(store.capacity());
if !store.is_live(pid) || !store.is_inline_context_root(pid) {
return (false, false);
}
let (layout_changed, paint_changed) =
self.compile_paragraph(store, pid, parent_text, parent_rtl, interaction, responsive);
if layout_changed {
self.record_layout_dirty(pid);
}
if paint_changed {
self.record_paint_dirty(pid);
}
(layout_changed, paint_changed)
}
pub fn sync_interaction_dirty(
&mut self,
store: &NodeStore,
interaction: &InteractionTracker,
dirty_nodes: &[NodeId],
responsive: Option<ResponsiveTextEnv<'_>>,
) -> (&[NodeId], &[NodeId]) {
self.ensure_capacity(store.capacity());
self.dirty_layout_paras.clear();
self.dirty_paint_paras.clear();
self.dirty_paras.clear();
Self::bump_epoch(&mut self.dirty_epoch, &mut self.dirty_stamp);
Self::bump_epoch(&mut self.paint_dirty_epoch, &mut self.paint_dirty_stamp);
Self::bump_epoch(&mut self.layout_dirty_epoch, &mut self.layout_dirty_stamp);
for &nid in dirty_nodes {
if !store.is_live(nid) {
continue;
}
if let Some(owner) = self.owner_paragraph(nid) {
if store.is_live(owner) && store.is_inline_context_root(owner) {
self.queue_dirty(owner, true);
}
} else if store.is_inline_context_root(nid) {
self.queue_dirty(nid, true);
}
}
let count = self.dirty_paras.len();
for i in 0..count {
let pid = self.dirty_paras[i];
let Some(slot) = self.paragraph_slot(pid) else {
continue;
};
let m = self.meta[slot as usize];
let (layout_changed, paint_changed) = self.compile_paragraph(
store,
pid,
m.base_style,
m.base_rtl,
Some(interaction),
responsive,
);
if layout_changed {
self.record_layout_dirty(pid);
}
if paint_changed {
self.record_paint_dirty(pid);
}
}
self.dirty_paras.clear();
(&self.dirty_layout_paras, &self.dirty_paint_paras)
}
}