nativeview-layout 0.1.0-rc.1

Shared NodeStore layout layer: StoreLayoutEngine / TextMeasurer traits and the default direct Taffy engine
Documentation
//! Platform-agnostic interaction visual-effect filtering shared by retained scene backends.

use nativeview_abi::NodeId;
use nativeview_store::NodeStore;
use nativeview_tables::tables::Interaction;

/// Returns `true` when an interaction state change (`:hover`/`:active`/`:focus`/`:disabled`) on
/// `id` can actually alter painted output (either `id` is an inline node lifted to a paragraph,
/// a dirty paragraph root, carries `StateRules`, or can draw a focus ring / text caret).
///
/// Filtering out plain ancestor containers (e.g. `NodeId::ROOT` or wrapper `view {}` nodes with no
/// `StateRules`) keeps inline `:hover` toggles strictly on the O(1) zero-allocation
/// `try_update_paragraphs_in_place` fast path.
#[inline]
pub fn has_visual_interaction_effect(
    store: &NodeStore,
    id: NodeId,
    dirty_paint_paras: &[NodeId],
) -> bool {
    if !store.is_live(id) {
        return false;
    }
    if dirty_paint_paras.contains(&id) {
        return true;
    }
    if store.is_inline(id) {
        return !dirty_paint_paras.is_empty();
    }
    store.try_row::<Interaction>(id).is_some_and(|i| {
        !i.state_rules().is_none()
            || i.focusable()
            || i.captures_text_keys()
            || i.tab_index().is_some()
    })
}

/// Stack-first inline-32 buffer of nodes whose paint can change under interaction pseudo-state
/// transitions (0 heap allocations for `<= 32` dirty nodes; spills to `Vec` only above 32).
pub enum VisualInteractionDirty {
    Inline { buf: [NodeId; 32], len: usize },
    Heap(Vec<NodeId>),
}

impl VisualInteractionDirty {
    #[inline]
    pub fn as_slice(&self) -> &[NodeId] {
        match self {
            Self::Inline { buf, len } => &buf[..*len],
            Self::Heap(v) => v.as_slice(),
        }
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.as_slice().is_empty()
    }
}

/// Filters `dirty` to nodes that can visually change under interaction pseudo-state transitions
/// with zero heap allocations for `<= 32` dirty nodes.
#[inline]
pub fn filter_visual_interaction_dirty(
    store: &NodeStore,
    dirty: &[NodeId],
    dirty_paint_paras: &[NodeId],
) -> VisualInteractionDirty {
    if dirty.len() <= 32 {
        let mut buf = [NodeId::ROOT; 32];
        let mut len = 0usize;
        for &id in dirty {
            if has_visual_interaction_effect(store, id, dirty_paint_paras) {
                buf[len] = id;
                len += 1;
            }
        }
        VisualInteractionDirty::Inline { buf, len }
    } else {
        let paint_nodes: Vec<NodeId> = dirty
            .iter()
            .copied()
            .filter(|&id| has_visual_interaction_effect(store, id, dirty_paint_paras))
            .collect();
        VisualInteractionDirty::Heap(paint_nodes)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use nativeview_store::ChangeSet;

    #[test]
    fn plain_container_has_no_visual_interaction_effect_while_focusable_does() {
        let mut store = NodeStore::new();
        let mut changes = ChangeSet::default();
        let (plain, focusable) = {
            let mut tx = store.tx();
            let plain = tx.create();
            let focusable = tx.create();
            tx.edit::<Interaction>(focusable).focusable(true);
            tx.set_children(NodeId::ROOT, &[plain, focusable]);
            tx.commit_into(&mut changes);
            (plain, focusable)
        };
        assert!(!has_visual_interaction_effect(&store, plain, &[]));
        assert!(has_visual_interaction_effect(&store, focusable, &[]));
        assert!(has_visual_interaction_effect(&store, plain, &[plain]));

        assert!(filter_visual_interaction_dirty(&store, &[], &[]).is_empty());
        let inline = filter_visual_interaction_dirty(&store, &[plain, focusable], &[]);
        assert_eq!(inline.as_slice(), &[focusable]);
        let many = vec![focusable; 40];
        let heap = filter_visual_interaction_dirty(&store, &many, &[]);
        assert_eq!(heap.as_slice().len(), 40);
    }
}