1use nativeview_abi::TimestampUs;
6use nativeview_engine::InteractionTracker;
7use nativeview_engine::tracker::PointerKey;
8use nativeview_store::{NodeId, NodeStore, ResolvedRow, ResourceId, Rgba, RowPatchView};
9use nativeview_tables::interaction::{
10 CursorKind, FocusModality, InteractionState,
11};
12use nativeview_tables::snapshots::{
13 LayoutSnapshot, VisualSnapshot, apply_patch_view_to_layout, apply_patch_view_to_visual,
14};
15use nativeview_tables::tables::{
16 DragDrop, Interaction, PointerCaptureMode, TextAlign, Typography, Visual, paint_of_box,
17};
18
19use crate::{MAX_TREE_DEPTH, StoreLayoutEngine};
20
21use super::style_cascade::StyleCascade;
22use super::{LaidOutScene, layout_snapshot_of, visual_snapshot_of};
23
24#[must_use]
29#[derive(Debug, Clone, Copy)]
30pub struct InteractionChange<'a> {
31 pub dirty: &'a [NodeId],
32 pub relayout: bool,
33}
34
35impl<L: StoreLayoutEngine> LaidOutScene<L> {
36 pub(crate) fn sync_interaction_dirty(&mut self, store: &NodeStore) -> bool {
41 let dirty = self.interaction.last_dirty();
42 if dirty.is_empty() {
43 return false;
44 }
45 let _ = self
46 .layout
47 .sync_interaction_paragraphs(store, &self.interaction, dirty);
48 let relayout = self.recompute_layout(store);
49 if relayout {
50 self.reconcile_overlays_with_transform(store, |_, base| base);
51 }
52 relayout
53 }
54
55 #[inline]
57 fn interaction_change(&mut self, store: &NodeStore) -> InteractionChange<'_> {
58 let relayout = self.sync_interaction_dirty(store);
59 InteractionChange {
60 dirty: self.interaction.last_dirty(),
61 relayout,
62 }
63 }
64
65 #[inline]
66 pub fn interaction_tracker(&self) -> &InteractionTracker {
67 &self.interaction
68 }
69
70 #[inline]
73 pub fn interaction_epoch(&self) -> u64 {
74 self.interaction_epoch
75 }
76
77 #[inline]
80 pub fn responsive_epoch(&self) -> u64 {
81 self.layout.responsive_epoch()
82 }
83
84 #[inline]
85 pub(crate) fn bump_interaction_epoch(&mut self) {
86 self.interaction_epoch = self.interaction_epoch.wrapping_add(1).max(1);
87 }
88
89 #[inline]
90 pub fn interaction_tracker_mut(&mut self) -> &mut InteractionTracker {
91 self.bump_interaction_epoch();
92 &mut self.interaction
93 }
94
95 #[inline]
96 pub fn interaction_state(&self, node_id: NodeId) -> InteractionState {
97 self.interaction.state(node_id)
98 }
99
100 #[inline]
103 pub fn is_subtree_disabled(&self, store: &NodeStore, id: NodeId) -> bool {
104 let mut cur = Some(id);
105 for _ in 0..MAX_TREE_DEPTH {
106 let Some(nid) = cur else {
107 break;
108 };
109 if self.interaction.is_disabled(nid)
110 || store
111 .try_row::<Interaction>(nid)
112 .is_some_and(|i| i.disabled())
113 {
114 return true;
115 }
116 cur = store.parent(nid).filter(|&p| p != nid);
117 }
118 false
119 }
120
121 pub fn set_node_disabled(&mut self, id: NodeId, disabled: bool) -> &[NodeId] {
124 self.bump_interaction_epoch();
125 self.interaction.set_disabled_slice(id, disabled)
126 }
127
128 #[inline]
130 pub fn is_interaction_target(&self, store: &NodeStore, id: NodeId) -> bool {
131 if !self.is_displayed(store, id) {
132 return false;
133 }
134 let has_inter = store.try_row::<Interaction>(id).is_some_and(|i| {
135 !i.listeners().is_empty()
136 || i.cursor() != CursorKind::Auto
137 || i.disabled()
138 || i.focusable()
139 || i.capture_mode() != PointerCaptureMode::None
140 || !i.state_rules().is_none()
141 || i.captures_text_keys()
142 || i.tab_index().is_some()
143 });
144 let has_drag = store.try_row::<DragDrop>(id).is_some_and(|d| {
145 d.draggable() || !d.accept().is_empty() || !d.listeners().is_empty()
146 });
147 has_inter
148 || has_drag
149 || self.interaction.is_disabled(id)
150 }
151
152 #[inline]
154 pub fn is_drop_target(store: &NodeStore, id: NodeId) -> bool {
155 store.is_live(id)
157 && store.try_row::<DragDrop>(id).is_some_and(|d| !d.accept().is_empty())
158 }
159
160 #[inline]
162 pub fn is_draggable_target(store: &NodeStore, id: NodeId) -> bool {
163 store.is_live(id)
165 && store.try_row::<DragDrop>(id).is_some_and(|d| d.draggable())
166 }
167
168 pub fn hit_test_interaction(&self, store: &NodeStore, x: f32, y: f32) -> Option<NodeId> {
170 self.hit_test_where(store, x, y, |id| self.is_interaction_target(store, id))
171 }
172
173 pub fn hit_test_drop_zone(&self, store: &NodeStore, x: f32, y: f32) -> Option<NodeId> {
175 self.hit_test_where(store, x, y, |id| Self::is_drop_target(store, id))
176 }
177
178 pub fn hit_test_draggable(&self, store: &NodeStore, x: f32, y: f32) -> Option<NodeId> {
180 self.hit_test_where(store, x, y, |id| Self::is_draggable_target(store, id))
181 }
182
183 pub fn visit_effective_patches<'a>(
187 &self,
188 store: &'a NodeStore,
189 id: NodeId,
190 mut f: impl FnMut(RowPatchView<'a>),
191 ) {
192 self.style_cascade().visit(store, id, |_, pv| f(pv));
193 }
194
195 #[inline]
197 pub fn is_displayed(&self, store: &NodeStore, id: NodeId) -> bool {
198 store.is_live(id) && !self.style_cascade().effective_box(store, id).is_none()
199 }
200
201 #[inline]
203 pub fn style_cascade(&self) -> StyleCascade<'_, L> {
204 StyleCascade {
205 responsive: &self.layout,
206 interaction: Some(&self.interaction),
207 inert: Some(&self.overlays),
208 }
209 }
210
211 pub fn effective_visual(&self, store: &NodeStore, id: NodeId) -> VisualSnapshot {
214 let mut visual = visual_snapshot_of(store, id);
215 self.visit_effective_patches(store, id, |pv| apply_patch_view_to_visual(pv, &mut visual));
216 paint_of_box(store, id, visual)
217 }
218
219 pub fn effective_layout_and_visual(
222 &self,
223 store: &NodeStore,
224 id: NodeId,
225 ) -> (LayoutSnapshot, VisualSnapshot) {
226 let mut layout = layout_snapshot_of(store, id);
227 let mut visual = visual_snapshot_of(store, id);
228 self.visit_effective_patches(store, id, |pv| {
229 apply_patch_view_to_layout(pv, &mut layout);
230 apply_patch_view_to_visual(pv, &mut visual);
231 });
232 (layout, paint_of_box(store, id, visual))
233 }
234
235 #[inline]
238 pub fn effective_background_fill_res(&self, store: &NodeStore, id: NodeId) -> Option<ResourceId> {
239 let mut patched: Option<ResourceId> = None;
240 self.visit_effective_patches(store, id, |pv| {
241 if let Some(rid) = pv.get_res_id(Visual::BACKGROUND_FILL) {
242 patched = Some(rid);
243 } else if pv.get_prop(Visual::BACKGROUND).is_some() {
244 patched = Some(ResourceId::NONE);
245 }
246 });
247 match patched {
248 Some(rid) => (!rid.is_none()).then_some(rid),
249 None => store
250 .try_row::<Visual>(id)
251 .map(|v| v.background_fill())
252 .filter(|rid| !rid.is_none()),
253 }
254 }
255
256 #[inline]
260 pub fn resolve_text_style_fields(
261 &self,
262 store: &NodeStore,
263 id: NodeId,
264 style: &VisualSnapshot,
265 parent_text: Option<(u16, TextAlign, Rgba, f32)>,
266 color_hooked: bool,
267 ) -> (u16, TextAlign, Rgba, f32) {
268 let mut parent_row = ResolvedRow::<Typography>::root(store);
269 if let Some((w, align, color, size)) = parent_text {
270 parent_row.override_prop(Typography::COLOR, color);
271 parent_row.override_prop(Typography::FONT_SIZE, size);
272 parent_row.override_prop(Typography::FONT_WEIGHT, w);
273 parent_row.override_prop(Typography::TEXT_ALIGN, align);
274 }
275 let resolved_typo = ResolvedRow::<Typography>::resolve(store, id, &parent_row);
276 let typo = resolved_typo.get();
277 let (mut color_ov, mut size_ov, mut weight_ov, mut align_ov) =
278 (color_hooked, false, None, None);
279 self.visit_effective_patches(store, id, |pv| {
280 if pv.get_prop(Typography::COLOR).is_some() {
281 color_ov = true;
282 }
283 if pv.get_prop(Typography::FONT_SIZE).is_some() {
284 size_ov = true;
285 }
286 if let Some(w) = pv.get_prop(Typography::FONT_WEIGHT) {
287 weight_ov = Some(w);
288 }
289 if let Some(a) = pv.get_prop(Typography::TEXT_ALIGN) {
290 align_ov = Some(a);
291 }
292 });
293 (
294 weight_ov.unwrap_or_else(|| typo.font_weight()),
295 align_ov.unwrap_or_else(|| typo.text_align()),
296 if color_ov { Rgba::from(style.text_color) } else { typo.color() },
297 if size_ov { style.font_size } else { typo.font_size() },
298 )
299 }
300
301 fn local_cursor_of(&self, store: &NodeStore, id: NodeId) -> CursorKind {
302 let inter = store.try_row::<Interaction>(id);
303 let base_cursor = inter.map(|i| i.cursor()).unwrap_or(CursorKind::Auto);
304 let captures_text = inter.is_some_and(|i| i.captures_text_keys());
305 let base = if base_cursor == CursorKind::Auto && captures_text {
306 CursorKind::Text
307 } else {
308 base_cursor
309 };
310 self.style_cascade()
311 .patched(store, id, Interaction::CURSOR)
312 .unwrap_or(base)
313 }
314
315 pub fn effective_cursor(&self, store: &NodeStore, id: NodeId) -> CursorKind {
318 if !self.is_displayed(store, id) {
319 return CursorKind::Auto;
320 }
321 let mut cur = Some(id);
322 let mut found: Option<CursorKind> = None;
323 let mut disabled_seen = false;
324 for _ in 0..MAX_TREE_DEPTH {
325 let Some(nid) = cur else {
326 break;
327 };
328 if self.interaction.is_disabled(nid)
329 || store.try_row::<Interaction>(nid).is_some_and(|i| i.disabled())
330 {
331 disabled_seen = true;
332 if found == Some(CursorKind::Pointer) {
333 return CursorKind::NotAllowed;
334 }
335 }
336 if found.is_none() {
337 let resolved = self.local_cursor_of(store, nid);
338 if resolved != CursorKind::Auto {
339 if resolved != CursorKind::Pointer {
340 return resolved;
341 }
342 if disabled_seen {
343 return CursorKind::NotAllowed;
344 }
345 found = Some(resolved);
346 }
347 }
348 cur = store.parent(nid).filter(|&p| p != nid);
349 }
350 found.unwrap_or(CursorKind::Auto)
351 }
352
353 pub fn current_cursor(&self, store: &NodeStore) -> CursorKind {
356 if let Some(cap) = self.interaction.captured_node() {
357 let c = self.effective_cursor(store, cap);
358 return if c == CursorKind::Auto {
359 CursorKind::Default
360 } else {
361 c
362 };
363 }
364 if let Some(&leaf) = self
365 .interaction
366 .hovered_chain()
367 .iter()
368 .find(|&&id| self.is_displayed(store, id))
369 {
370 let c = self.effective_cursor(store, leaf);
371 if c != CursorKind::Auto {
372 return c;
373 }
374 }
375 if let Some(&leaf) = self
376 .interaction
377 .active_chain()
378 .iter()
379 .find(|&&id| self.is_displayed(store, id))
380 {
381 let c = self.effective_cursor(store, leaf);
382 if c != CursorKind::Auto {
383 return c;
384 }
385 }
386 CursorKind::Default
387 }
388
389 #[inline]
393 pub fn pointer_move_in_store(
394 &mut self,
395 store: &NodeStore,
396 target: Option<NodeId>,
397 ) -> InteractionChange<'_> {
398 self.bump_interaction_epoch();
399 let _ = self
400 .interaction
401 .pointer_move_with(PointerKey::MOUSE, target, |id| store.parent(id));
402 self.interaction_change(store)
403 }
404
405 #[inline]
407 pub fn pointer_down_in_store(
408 &mut self,
409 store: &NodeStore,
410 target: Option<NodeId>,
411 ) -> InteractionChange<'_> {
412 self.bump_interaction_epoch();
413 let _ = self.interaction.pointer_down_with(
414 PointerKey::MOUSE,
415 [0.0, 0.0],
416 TimestampUs::ZERO,
417 target,
418 |id| store.parent(id),
419 );
420 self.interaction_change(store)
421 }
422
423 #[inline]
426 pub fn pointer_up_in_store(
427 &mut self,
428 store: &NodeStore,
429 target: Option<NodeId>,
430 ) -> (Option<NodeId>, InteractionChange<'_>) {
431 self.bump_interaction_epoch();
432 let end = self
433 .interaction
434 .pointer_up_with(PointerKey::MOUSE, target, |id| store.parent(id));
435 (end.clicked, self.interaction_change(store))
436 }
437
438 #[inline]
440 pub fn pointer_leave_in_store(&mut self, store: &NodeStore) -> InteractionChange<'_> {
441 self.bump_interaction_epoch();
442 let _ = self.interaction.pointer_leave_slice();
443 self.interaction_change(store)
444 }
445
446 #[inline]
449 pub fn pointer_cancel_in_store(&mut self, store: &NodeStore) -> InteractionChange<'_> {
450 self.bump_interaction_epoch();
451 let _ = self.interaction.pointer_cancel_all();
452 self.interaction_change(store)
453 }
454
455 #[inline]
457 pub fn set_drag_over_in_store(
458 &mut self,
459 store: &NodeStore,
460 target: Option<NodeId>,
461 ) -> InteractionChange<'_> {
462 self.bump_interaction_epoch();
463 let _ = self
464 .interaction
465 .set_drag_over_with(target, |id| store.parent(id));
466 self.interaction_change(store)
467 }
468
469 #[inline]
471 pub fn clear_active_in_store(&mut self, store: &NodeStore) -> InteractionChange<'_> {
472 self.bump_interaction_epoch();
473 let _ = self.interaction.clear_active_slice();
474 self.interaction_change(store)
475 }
476
477 #[inline]
480 pub fn set_focus_in_store(
481 &mut self,
482 store: &NodeStore,
483 node_id: Option<NodeId>,
484 modality: FocusModality,
485 ) -> InteractionChange<'_> {
486 self.last_focus_transition = None;
487 self.bump_interaction_epoch();
488 let is_text = node_id.is_some_and(|id| nativeview_engine::is_text_entry_node(store, id));
489 let _ = self.interaction.set_focus_ex(node_id, modality, is_text);
490 if self.interaction.focused() == node_id {
491 let mut anchor = core::mem::take(self.interaction.focus_anchor_mut());
492 nativeview_engine::snapshot_focus_anchor(
493 store,
494 &super::cascade_of(&self.layout, &self.interaction),
495 &mut anchor,
496 node_id,
497 modality,
498 &mut self.focus_dfs_scratch,
499 );
500 *self.interaction.focus_anchor_mut() = anchor;
501 }
502 self.interaction_change(store)
503 }
504}