use nativeview_abi::NodeId;
use nativeview_store::NodeStore;
use nativeview_tables::tables::Interaction;
#[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()
})
}
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()
}
}
#[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);
}
}