nativeview_layout/
interaction.rs1use nativeview_abi::NodeId;
4use nativeview_store::NodeStore;
5use nativeview_tables::tables::Interaction;
6
7#[inline]
15pub fn has_visual_interaction_effect(
16 store: &NodeStore,
17 id: NodeId,
18 dirty_paint_paras: &[NodeId],
19) -> bool {
20 if !store.is_live(id) {
21 return false;
22 }
23 if dirty_paint_paras.contains(&id) {
24 return true;
25 }
26 if store.is_inline(id) {
27 return !dirty_paint_paras.is_empty();
28 }
29 store.try_row::<Interaction>(id).is_some_and(|i| {
30 !i.state_rules().is_none()
31 || i.focusable()
32 || i.captures_text_keys()
33 || i.tab_index().is_some()
34 })
35}
36
37pub enum VisualInteractionDirty {
40 Inline { buf: [NodeId; 32], len: usize },
41 Heap(Vec<NodeId>),
42}
43
44impl VisualInteractionDirty {
45 #[inline]
46 pub fn as_slice(&self) -> &[NodeId] {
47 match self {
48 Self::Inline { buf, len } => &buf[..*len],
49 Self::Heap(v) => v.as_slice(),
50 }
51 }
52
53 #[inline]
54 pub fn is_empty(&self) -> bool {
55 self.as_slice().is_empty()
56 }
57}
58
59#[inline]
62pub fn filter_visual_interaction_dirty(
63 store: &NodeStore,
64 dirty: &[NodeId],
65 dirty_paint_paras: &[NodeId],
66) -> VisualInteractionDirty {
67 if dirty.len() <= 32 {
68 let mut buf = [NodeId::ROOT; 32];
69 let mut len = 0usize;
70 for &id in dirty {
71 if has_visual_interaction_effect(store, id, dirty_paint_paras) {
72 buf[len] = id;
73 len += 1;
74 }
75 }
76 VisualInteractionDirty::Inline { buf, len }
77 } else {
78 let paint_nodes: Vec<NodeId> = dirty
79 .iter()
80 .copied()
81 .filter(|&id| has_visual_interaction_effect(store, id, dirty_paint_paras))
82 .collect();
83 VisualInteractionDirty::Heap(paint_nodes)
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90 use nativeview_store::ChangeSet;
91
92 #[test]
93 fn plain_container_has_no_visual_interaction_effect_while_focusable_does() {
94 let mut store = NodeStore::new();
95 let mut changes = ChangeSet::default();
96 let (plain, focusable) = {
97 let mut tx = store.tx();
98 let plain = tx.create();
99 let focusable = tx.create();
100 tx.edit::<Interaction>(focusable).focusable(true);
101 tx.set_children(NodeId::ROOT, &[plain, focusable]);
102 tx.commit_into(&mut changes);
103 (plain, focusable)
104 };
105 assert!(!has_visual_interaction_effect(&store, plain, &[]));
106 assert!(has_visual_interaction_effect(&store, focusable, &[]));
107 assert!(has_visual_interaction_effect(&store, plain, &[plain]));
108
109 assert!(filter_visual_interaction_dirty(&store, &[], &[]).is_empty());
110 let inline = filter_visual_interaction_dirty(&store, &[plain, focusable], &[]);
111 assert_eq!(inline.as_slice(), &[focusable]);
112 let many = vec![focusable; 40];
113 let heap = filter_visual_interaction_dirty(&store, &many, &[]);
114 assert_eq!(heap.as_slice().len(), 40);
115 }
116}