nativeview_engine/
focus_query.rs1use nativeview_abi::NodeId;
4use nativeview_store::NodeStore;
5use nativeview_tables::semantics::{SemanticStyleOracle, resolve_effective_visibility_hidden};
6pub use nativeview_tables::semantics::{
7 is_open_modal, is_subtree_closed_overlay, is_within_subtree, topmost_open_modal,
8};
9use nativeview_tables::tables::{Accessibility, AriaRole, Interaction, Overlay, OverlayFlags};
10
11pub(crate) use nativeview_store::MAX_TREE_DEPTH;
12
13#[inline]
16pub fn is_text_entry_node(store: &NodeStore, id: NodeId) -> bool {
17 store.is_live(id)
18 && store
19 .try_row::<Interaction>(id)
20 .is_some_and(|i| i.captures_text_keys())
21}
22
23#[inline]
25pub fn is_subtree_disabled(store: &NodeStore, id: NodeId) -> bool {
26 let mut cur = Some(id);
27 for _ in 0..MAX_TREE_DEPTH {
28 let Some(nid) = cur else {
29 break;
30 };
31 if store
32 .try_row::<Interaction>(nid)
33 .is_some_and(|i| i.disabled())
34 {
35 return true;
36 }
37 cur = store.parent(nid).filter(|&p| p != nid);
38 }
39 false
40}
41
42pub fn press_focus_target<O: SemanticStyleOracle + ?Sized>(
46 store: &NodeStore,
47 oracle: &O,
48 hit: Option<NodeId>,
49 current: Option<NodeId>,
50) -> Option<NodeId> {
51 let mut cur = hit?;
52 for _ in 0..MAX_TREE_DEPTH {
53 if is_focusable_node(store, oracle, cur) {
54 return Some(cur);
55 }
56 if store
57 .try_row::<Interaction>(cur)
58 .is_some_and(|i| i.keep_focus_on_press())
59 {
60 return current;
61 }
62 match store.parent(cur) {
63 Some(p) if p != cur => cur = p,
64 _ => return None,
65 }
66 }
67 None
68}
69
70#[inline]
77pub fn is_focusable_node<O: SemanticStyleOracle + ?Sized>(
78 store: &NodeStore,
79 oracle: &O,
80 id: NodeId,
81) -> bool {
82 store.is_connected(id)
83 && is_focusable_self(store, id)
84 && !oracle.is_subtree_display_none(store, id)
85 && !is_subtree_closed_overlay(store, id)
86 && !is_subtree_disabled(store, id)
87 && !resolve_effective_visibility_hidden(store, oracle, id)
88 && !oracle.is_inert(store, id)
89}
90
91#[inline]
96pub fn is_focusable_self(store: &NodeStore, id: NodeId) -> bool {
97 if !store.is_live(id) {
98 return false;
99 }
100 if is_text_entry_node(store, id) {
101 return true;
102 }
103 store.try_row::<Interaction>(id).is_some_and(|i| {
104 let l = i.listeners();
105 i.focusable()
106 || i.tab_index().is_some()
107 || l.contains(nativeview_abi::InteractionEvent::Focus)
108 || l.contains(nativeview_abi::InteractionEvent::Blur)
109 })
110}
111
112#[inline]
115pub fn is_composite_aria_group(store: &NodeStore, id: NodeId) -> bool {
116 store
117 .try_row::<Interaction>(id)
118 .is_some_and(|i| i.roving_focus_group())
119 || store
120 .try_row::<Accessibility>(id)
121 .and_then(|a| a.role())
122 .is_some_and(|r| {
123 matches!(
124 r,
125 AriaRole::TabList
126 | AriaRole::Menu
127 | AriaRole::RadioGroup
128 | AriaRole::ListBox
129 | AriaRole::Grid
130 )
131 })
132}
133
134pub fn first_matching_in_subtree<O: SemanticStyleOracle + ?Sized>(
138 store: &NodeStore,
139 oracle: &O,
140 root: NodeId,
141 dfs_scratch: &mut Vec<NodeId>,
142 mut predicate: impl FnMut(&NodeStore, NodeId) -> bool,
143) -> Option<NodeId> {
144 if !store.is_live(root)
145 || oracle.is_subtree_display_none(store, root)
146 || is_subtree_closed_overlay(store, root)
147 || is_subtree_disabled(store, root)
148 {
149 return None;
150 }
151 dfs_scratch.clear();
152 dfs_scratch.push(root);
153 while let Some(cur) = dfs_scratch.pop() {
154 if !store.is_live(cur) {
155 continue;
156 }
157 if cur != root
158 && (oracle.is_display_none(store, cur)
159 || store
160 .try_row::<Overlay>(cur)
161 .is_some_and(|ov| !ov.options().contains(OverlayFlags::OPEN))
162 || store
163 .try_row::<Interaction>(cur)
164 .is_some_and(|i| i.disabled()))
165 {
166 continue;
167 }
168 if predicate(store, cur) {
169 return Some(cur);
170 }
171 for &child in store.children(cur).iter().rev() {
172 dfs_scratch.push(child);
173 }
174 }
175 None
176}
177
178#[inline]
179pub fn first_focusable_in_subtree<O: SemanticStyleOracle + ?Sized>(
180 store: &NodeStore,
181 oracle: &O,
182 root: NodeId,
183 dfs_scratch: &mut Vec<NodeId>,
184) -> Option<NodeId> {
185 first_matching_in_subtree(store, oracle, root, dfs_scratch, is_focusable_self)
186}
187
188#[inline]
190pub fn is_tabbable_self(store: &NodeStore, id: NodeId) -> bool {
191 is_focusable_self(store, id)
192 && store
193 .try_row::<Interaction>(id)
194 .and_then(|i| i.tab_index())
195 .is_none_or(|ti| ti >= 0)
196}
197
198pub(crate) fn pick_modal_initial_focus<O: SemanticStyleOracle + ?Sized>(
199 store: &NodeStore,
200 oracle: &O,
201 modal_root: NodeId,
202 dfs_scratch: &mut Vec<NodeId>,
203) -> Option<NodeId> {
204 first_matching_in_subtree(store, oracle, modal_root, dfs_scratch, |s, id| {
205 is_focusable_self(s, id) && s.try_row::<Interaction>(id).is_some_and(|i| i.autofocus())
206 })
207 .or_else(|| first_matching_in_subtree(store, oracle, modal_root, dfs_scratch, is_tabbable_self))
208 .or_else(|| first_focusable_in_subtree(store, oracle, modal_root, dfs_scratch))
209}
210
211#[cfg(test)]
212mod tests {
213 use super::*;
214
215 #[test]
216 fn interaction_flags_drive_text_entry_and_roving_group_without_capability_tables() {
217 let mut store = NodeStore::new();
218 let mut changes = nativeview_store::ChangeSet::default();
219 let mut tx = store.tx();
220 let plain = tx.create();
221 let text_host = tx.create();
222 let roving = tx.create();
223 tx.edit::<Interaction>(text_host).captures_text_keys(true);
224 tx.edit::<Interaction>(roving).roving_focus_group(true);
225 tx.commit_into(&mut changes);
226
227 assert!(store.try_row::<Interaction>(text_host).is_some_and(|i| i.captures_text_keys()));
228 assert!(!store.has::<Accessibility>(roving));
229
230 assert!(!is_text_entry_node(&store, plain));
231 assert!(is_text_entry_node(&store, text_host));
232 assert!(is_focusable_self(&store, text_host));
233
234 assert!(!is_composite_aria_group(&store, plain));
235 assert!(is_composite_aria_group(&store, roving));
236
237 let mut tx = store.tx();
239 tx.remove(text_host);
240 tx.remove(roving);
241 tx.commit_into(&mut changes);
242 assert!(!is_text_entry_node(&store, text_host));
243 assert!(!is_composite_aria_group(&store, roving));
244 }
245
246 #[test]
247 fn detached_live_node_is_not_focusable_until_connected() {
248 use nativeview_tables::semantics::StaticSemanticOracle;
249
250 let mut store = NodeStore::new();
251 let mut changes = nativeview_store::ChangeSet::default();
252 let btn = {
253 let mut tx = store.tx();
254 let b = tx.create();
255 tx.edit::<Interaction>(b).focusable(true).tab_index(Some(0));
256 tx.commit_into(&mut changes);
257 b
258 };
259 assert!(store.is_live(btn) && !store.is_connected(btn));
260 assert!(!is_focusable_node(&store, &StaticSemanticOracle, btn));
261
262 {
263 let mut tx = store.tx();
264 tx.set_children(NodeId::ROOT, &[btn]);
265 tx.commit_into(&mut changes);
266 }
267 assert!(is_focusable_node(&store, &StaticSemanticOracle, btn));
268
269 {
270 let mut tx = store.tx();
271 tx.detach_child(btn);
272 tx.commit_into(&mut changes);
273 }
274 assert!(store.is_live(btn) && !store.is_connected(btn));
275 assert!(!is_focusable_node(&store, &StaticSemanticOracle, btn));
276 }
277}