use nativeview_abi::TimestampUs;
use nativeview_engine::InteractionTracker;
use nativeview_engine::tracker::PointerKey;
use nativeview_store::{NodeId, NodeStore, ResolvedRow, ResourceId, Rgba, RowPatchView};
use nativeview_tables::interaction::{
CursorKind, FocusModality, InteractionState,
};
use nativeview_tables::snapshots::{
LayoutSnapshot, VisualSnapshot, apply_patch_view_to_layout, apply_patch_view_to_visual,
};
use nativeview_tables::tables::{
DragDrop, Interaction, PointerCaptureMode, TextAlign, Typography, Visual, paint_of_box,
};
use crate::{MAX_TREE_DEPTH, StoreLayoutEngine};
use super::style_cascade::StyleCascade;
use super::{LaidOutScene, layout_snapshot_of, visual_snapshot_of};
#[must_use]
#[derive(Debug, Clone, Copy)]
pub struct InteractionChange<'a> {
pub dirty: &'a [NodeId],
pub relayout: bool,
}
impl<L: StoreLayoutEngine> LaidOutScene<L> {
pub(crate) fn sync_interaction_dirty(&mut self, store: &NodeStore) -> bool {
let dirty = self.interaction.last_dirty();
if dirty.is_empty() {
return false;
}
let _ = self
.layout
.sync_interaction_paragraphs(store, &self.interaction, dirty);
let relayout = self.recompute_layout(store);
if relayout {
self.reconcile_overlays_with_transform(store, |_, base| base);
}
relayout
}
#[inline]
fn interaction_change(&mut self, store: &NodeStore) -> InteractionChange<'_> {
let relayout = self.sync_interaction_dirty(store);
InteractionChange {
dirty: self.interaction.last_dirty(),
relayout,
}
}
#[inline]
pub fn interaction_tracker(&self) -> &InteractionTracker {
&self.interaction
}
#[inline]
pub fn interaction_epoch(&self) -> u64 {
self.interaction_epoch
}
#[inline]
pub fn responsive_epoch(&self) -> u64 {
self.layout.responsive_epoch()
}
#[inline]
pub(crate) fn bump_interaction_epoch(&mut self) {
self.interaction_epoch = self.interaction_epoch.wrapping_add(1).max(1);
}
#[inline]
pub fn interaction_tracker_mut(&mut self) -> &mut InteractionTracker {
self.bump_interaction_epoch();
&mut self.interaction
}
#[inline]
pub fn interaction_state(&self, node_id: NodeId) -> InteractionState {
self.interaction.state(node_id)
}
#[inline]
pub fn is_subtree_disabled(&self, store: &NodeStore, id: NodeId) -> bool {
let mut cur = Some(id);
for _ in 0..MAX_TREE_DEPTH {
let Some(nid) = cur else {
break;
};
if self.interaction.is_disabled(nid)
|| store
.try_row::<Interaction>(nid)
.is_some_and(|i| i.disabled())
{
return true;
}
cur = store.parent(nid).filter(|&p| p != nid);
}
false
}
pub fn set_node_disabled(&mut self, id: NodeId, disabled: bool) -> &[NodeId] {
self.bump_interaction_epoch();
self.interaction.set_disabled_slice(id, disabled)
}
#[inline]
pub fn is_interaction_target(&self, store: &NodeStore, id: NodeId) -> bool {
if !self.is_displayed(store, id) {
return false;
}
let has_inter = store.try_row::<Interaction>(id).is_some_and(|i| {
!i.listeners().is_empty()
|| i.cursor() != CursorKind::Auto
|| i.disabled()
|| i.focusable()
|| i.capture_mode() != PointerCaptureMode::None
|| !i.state_rules().is_none()
|| i.captures_text_keys()
|| i.tab_index().is_some()
});
let has_drag = store.try_row::<DragDrop>(id).is_some_and(|d| {
d.draggable() || !d.accept().is_empty() || !d.listeners().is_empty()
});
has_inter
|| has_drag
|| self.interaction.is_disabled(id)
}
#[inline]
pub fn is_drop_target(store: &NodeStore, id: NodeId) -> bool {
store.is_live(id)
&& store.try_row::<DragDrop>(id).is_some_and(|d| !d.accept().is_empty())
}
#[inline]
pub fn is_draggable_target(store: &NodeStore, id: NodeId) -> bool {
store.is_live(id)
&& store.try_row::<DragDrop>(id).is_some_and(|d| d.draggable())
}
pub fn hit_test_interaction(&self, store: &NodeStore, x: f32, y: f32) -> Option<NodeId> {
self.hit_test_where(store, x, y, |id| self.is_interaction_target(store, id))
}
pub fn hit_test_drop_zone(&self, store: &NodeStore, x: f32, y: f32) -> Option<NodeId> {
self.hit_test_where(store, x, y, |id| Self::is_drop_target(store, id))
}
pub fn hit_test_draggable(&self, store: &NodeStore, x: f32, y: f32) -> Option<NodeId> {
self.hit_test_where(store, x, y, |id| Self::is_draggable_target(store, id))
}
pub fn visit_effective_patches<'a>(
&self,
store: &'a NodeStore,
id: NodeId,
mut f: impl FnMut(RowPatchView<'a>),
) {
self.style_cascade().visit(store, id, |_, pv| f(pv));
}
#[inline]
pub fn is_displayed(&self, store: &NodeStore, id: NodeId) -> bool {
store.is_live(id) && !self.style_cascade().effective_box(store, id).is_none()
}
#[inline]
pub fn style_cascade(&self) -> StyleCascade<'_, L> {
StyleCascade {
responsive: &self.layout,
interaction: Some(&self.interaction),
inert: Some(&self.overlays),
}
}
pub fn effective_visual(&self, store: &NodeStore, id: NodeId) -> VisualSnapshot {
let mut visual = visual_snapshot_of(store, id);
self.visit_effective_patches(store, id, |pv| apply_patch_view_to_visual(pv, &mut visual));
paint_of_box(store, id, visual)
}
pub fn effective_layout_and_visual(
&self,
store: &NodeStore,
id: NodeId,
) -> (LayoutSnapshot, VisualSnapshot) {
let mut layout = layout_snapshot_of(store, id);
let mut visual = visual_snapshot_of(store, id);
self.visit_effective_patches(store, id, |pv| {
apply_patch_view_to_layout(pv, &mut layout);
apply_patch_view_to_visual(pv, &mut visual);
});
(layout, paint_of_box(store, id, visual))
}
#[inline]
pub fn effective_background_fill_res(&self, store: &NodeStore, id: NodeId) -> Option<ResourceId> {
let mut patched: Option<ResourceId> = None;
self.visit_effective_patches(store, id, |pv| {
if let Some(rid) = pv.get_res_id(Visual::BACKGROUND_FILL) {
patched = Some(rid);
} else if pv.get_prop(Visual::BACKGROUND).is_some() {
patched = Some(ResourceId::NONE);
}
});
match patched {
Some(rid) => (!rid.is_none()).then_some(rid),
None => store
.try_row::<Visual>(id)
.map(|v| v.background_fill())
.filter(|rid| !rid.is_none()),
}
}
#[inline]
pub fn resolve_text_style_fields(
&self,
store: &NodeStore,
id: NodeId,
style: &VisualSnapshot,
parent_text: Option<(u16, TextAlign, Rgba, f32)>,
color_hooked: bool,
) -> (u16, TextAlign, Rgba, f32) {
let mut parent_row = ResolvedRow::<Typography>::root(store);
if let Some((w, align, color, size)) = parent_text {
parent_row.override_prop(Typography::COLOR, color);
parent_row.override_prop(Typography::FONT_SIZE, size);
parent_row.override_prop(Typography::FONT_WEIGHT, w);
parent_row.override_prop(Typography::TEXT_ALIGN, align);
}
let resolved_typo = ResolvedRow::<Typography>::resolve(store, id, &parent_row);
let typo = resolved_typo.get();
let (mut color_ov, mut size_ov, mut weight_ov, mut align_ov) =
(color_hooked, false, None, None);
self.visit_effective_patches(store, id, |pv| {
if pv.get_prop(Typography::COLOR).is_some() {
color_ov = true;
}
if pv.get_prop(Typography::FONT_SIZE).is_some() {
size_ov = true;
}
if let Some(w) = pv.get_prop(Typography::FONT_WEIGHT) {
weight_ov = Some(w);
}
if let Some(a) = pv.get_prop(Typography::TEXT_ALIGN) {
align_ov = Some(a);
}
});
(
weight_ov.unwrap_or_else(|| typo.font_weight()),
align_ov.unwrap_or_else(|| typo.text_align()),
if color_ov { Rgba::from(style.text_color) } else { typo.color() },
if size_ov { style.font_size } else { typo.font_size() },
)
}
fn local_cursor_of(&self, store: &NodeStore, id: NodeId) -> CursorKind {
let inter = store.try_row::<Interaction>(id);
let base_cursor = inter.map(|i| i.cursor()).unwrap_or(CursorKind::Auto);
let captures_text = inter.is_some_and(|i| i.captures_text_keys());
let base = if base_cursor == CursorKind::Auto && captures_text {
CursorKind::Text
} else {
base_cursor
};
self.style_cascade()
.patched(store, id, Interaction::CURSOR)
.unwrap_or(base)
}
pub fn effective_cursor(&self, store: &NodeStore, id: NodeId) -> CursorKind {
if !self.is_displayed(store, id) {
return CursorKind::Auto;
}
let mut cur = Some(id);
let mut found: Option<CursorKind> = None;
let mut disabled_seen = false;
for _ in 0..MAX_TREE_DEPTH {
let Some(nid) = cur else {
break;
};
if self.interaction.is_disabled(nid)
|| store.try_row::<Interaction>(nid).is_some_and(|i| i.disabled())
{
disabled_seen = true;
if found == Some(CursorKind::Pointer) {
return CursorKind::NotAllowed;
}
}
if found.is_none() {
let resolved = self.local_cursor_of(store, nid);
if resolved != CursorKind::Auto {
if resolved != CursorKind::Pointer {
return resolved;
}
if disabled_seen {
return CursorKind::NotAllowed;
}
found = Some(resolved);
}
}
cur = store.parent(nid).filter(|&p| p != nid);
}
found.unwrap_or(CursorKind::Auto)
}
pub fn current_cursor(&self, store: &NodeStore) -> CursorKind {
if let Some(cap) = self.interaction.captured_node() {
let c = self.effective_cursor(store, cap);
return if c == CursorKind::Auto {
CursorKind::Default
} else {
c
};
}
if let Some(&leaf) = self
.interaction
.hovered_chain()
.iter()
.find(|&&id| self.is_displayed(store, id))
{
let c = self.effective_cursor(store, leaf);
if c != CursorKind::Auto {
return c;
}
}
if let Some(&leaf) = self
.interaction
.active_chain()
.iter()
.find(|&&id| self.is_displayed(store, id))
{
let c = self.effective_cursor(store, leaf);
if c != CursorKind::Auto {
return c;
}
}
CursorKind::Default
}
#[inline]
pub fn pointer_move_in_store(
&mut self,
store: &NodeStore,
target: Option<NodeId>,
) -> InteractionChange<'_> {
self.bump_interaction_epoch();
let _ = self
.interaction
.pointer_move_with(PointerKey::MOUSE, target, |id| store.parent(id));
self.interaction_change(store)
}
#[inline]
pub fn pointer_down_in_store(
&mut self,
store: &NodeStore,
target: Option<NodeId>,
) -> InteractionChange<'_> {
self.bump_interaction_epoch();
let _ = self.interaction.pointer_down_with(
PointerKey::MOUSE,
[0.0, 0.0],
TimestampUs::ZERO,
target,
|id| store.parent(id),
);
self.interaction_change(store)
}
#[inline]
pub fn pointer_up_in_store(
&mut self,
store: &NodeStore,
target: Option<NodeId>,
) -> (Option<NodeId>, InteractionChange<'_>) {
self.bump_interaction_epoch();
let end = self
.interaction
.pointer_up_with(PointerKey::MOUSE, target, |id| store.parent(id));
(end.clicked, self.interaction_change(store))
}
#[inline]
pub fn pointer_leave_in_store(&mut self, store: &NodeStore) -> InteractionChange<'_> {
self.bump_interaction_epoch();
let _ = self.interaction.pointer_leave_slice();
self.interaction_change(store)
}
#[inline]
pub fn pointer_cancel_in_store(&mut self, store: &NodeStore) -> InteractionChange<'_> {
self.bump_interaction_epoch();
let _ = self.interaction.pointer_cancel_all();
self.interaction_change(store)
}
#[inline]
pub fn set_drag_over_in_store(
&mut self,
store: &NodeStore,
target: Option<NodeId>,
) -> InteractionChange<'_> {
self.bump_interaction_epoch();
let _ = self
.interaction
.set_drag_over_with(target, |id| store.parent(id));
self.interaction_change(store)
}
#[inline]
pub fn clear_active_in_store(&mut self, store: &NodeStore) -> InteractionChange<'_> {
self.bump_interaction_epoch();
let _ = self.interaction.clear_active_slice();
self.interaction_change(store)
}
#[inline]
pub fn set_focus_in_store(
&mut self,
store: &NodeStore,
node_id: Option<NodeId>,
modality: FocusModality,
) -> InteractionChange<'_> {
self.last_focus_transition = None;
self.bump_interaction_epoch();
let is_text = node_id.is_some_and(|id| nativeview_engine::is_text_entry_node(store, id));
let _ = self.interaction.set_focus_ex(node_id, modality, is_text);
if self.interaction.focused() == node_id {
let mut anchor = core::mem::take(self.interaction.focus_anchor_mut());
nativeview_engine::snapshot_focus_anchor(
store,
&super::cascade_of(&self.layout, &self.interaction),
&mut anchor,
node_id,
modality,
&mut self.focus_dfs_scratch,
);
*self.interaction.focus_anchor_mut() = anchor;
}
self.interaction_change(store)
}
}