mod tab_order;
use std::ops::{BitAnd, BitOr, BitOrAssign};
use nativeview_abi::{
FocusMove, NV_FOCUS_TRAVERSE_MASK, NV_FOCUS_TRAVERSE_NONE, NV_FOCUS_TRAVERSE_NO_WRAP,
NV_FOCUS_TRAVERSE_OUT_SCROLLED, NV_FOCUS_TRAVERSE_OUT_TRAPPED, NV_FOCUS_TRAVERSE_OUT_WRAPPED,
NV_FOCUS_TRAVERSE_SCROLL_INTO_VIEW,
};
use nativeview_engine::{
LayoutRect, Viewport, is_focusable_node, is_focusable_self, is_subtree_closed_overlay,
is_tabbable_self,
};
use nativeview_store::{NodeId, NodeStore};
use nativeview_tables::semantics::{
SemanticStyleOracle, resolve_effective_disabled, resolve_effective_visibility_hidden,
};
use nativeview_tables::tables::{Overlay, OverlayFlags, Visibility};
use crate::StoreLayoutEngine;
use super::overlay::resolved::ResolvedOverlay;
use super::overlay::scene::OverlayState;
use super::style_cascade::{ResponsivePatches, StyleCascade};
use super::LaidOutScene;
pub use tab_order::TabOrderPolicy;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct FocusTraverseFlags(u32);
impl FocusTraverseFlags {
pub const NONE: Self = Self(NV_FOCUS_TRAVERSE_NONE);
pub const NO_WRAP: Self = Self(NV_FOCUS_TRAVERSE_NO_WRAP);
pub const SCROLL_INTO_VIEW: Self = Self(NV_FOCUS_TRAVERSE_SCROLL_INTO_VIEW);
pub const ALL: Self = Self(NV_FOCUS_TRAVERSE_MASK);
#[inline]
pub const fn from_bits(raw: u32) -> Option<Self> {
if (raw & !NV_FOCUS_TRAVERSE_MASK) != 0 {
None
} else {
Some(Self(raw))
}
}
#[inline]
pub const fn bits(self) -> u32 {
self.0
}
#[inline]
pub const fn is_empty(self) -> bool {
self.0 == 0
}
#[inline]
pub const fn contains(self, other: Self) -> bool {
(self.0 & other.0) == other.0
}
#[inline]
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
#[inline]
pub const fn without(self, other: Self) -> Self {
Self(self.0 & !other.0)
}
}
impl BitOr for FocusTraverseFlags {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self::Output {
Self(self.0 | rhs.0)
}
}
impl BitOrAssign for FocusTraverseFlags {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}
impl BitAnd for FocusTraverseFlags {
type Output = Self;
#[inline]
fn bitand(self, rhs: Self) -> Self::Output {
Self(self.0 & rhs.0)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct FocusTraverseOutFlags(u32);
impl FocusTraverseOutFlags {
pub const NONE: Self = Self(0);
pub const TRAPPED: Self = Self(NV_FOCUS_TRAVERSE_OUT_TRAPPED);
pub const SCROLLED: Self = Self(NV_FOCUS_TRAVERSE_OUT_SCROLLED);
pub const WRAPPED: Self = Self(NV_FOCUS_TRAVERSE_OUT_WRAPPED);
#[inline]
pub const fn bits(self) -> u32 {
self.0
}
#[inline]
pub const fn contains(self, other: Self) -> bool {
(self.0 & other.0) == other.0
}
#[inline]
pub const fn from_outcome(outcome: FocusTraversalOutcome) -> Self {
let mut bits = 0u32;
if outcome.trapped {
bits |= NV_FOCUS_TRAVERSE_OUT_TRAPPED;
}
if outcome.scrolled {
bits |= NV_FOCUS_TRAVERSE_OUT_SCROLLED;
}
if outcome.wrapped {
bits |= NV_FOCUS_TRAVERSE_OUT_WRAPPED;
}
Self(bits)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct FocusTraversalOutcome {
pub target: Option<NodeId>,
pub trapped: bool,
pub scrolled: bool,
pub wrapped: bool,
}
impl FocusTraversalOutcome {
#[inline]
pub const fn out_flags(self) -> FocusTraverseOutFlags {
FocusTraverseOutFlags::from_outcome(self)
}
}
#[derive(Debug, Default)]
pub struct FocusTraversalScratch {
pub dfs_stack: Vec<(NodeId, bool)>,
pub positive_buf: Vec<(i16, NodeId)>,
pub positive_sort_buf: Vec<(i16, NodeId)>,
pub natural_buf: Vec<NodeId>,
pub scroll_dirty: Vec<NodeId>,
}
impl FocusTraversalScratch {
#[inline]
pub fn clear(&mut self) {
self.dfs_stack.clear();
self.positive_buf.clear();
self.positive_sort_buf.clear();
self.natural_buf.clear();
self.scroll_dirty.clear();
}
}
pub(crate) trait FocusLayoutLookup {
fn layout_of(&self, id: NodeId) -> Option<LayoutRect>;
fn visual_rect_of(&self, store: &NodeStore, id: NodeId) -> Option<LayoutRect>;
}
impl<L: StoreLayoutEngine> FocusLayoutLookup for LaidOutScene<L> {
#[inline]
fn layout_of(&self, id: NodeId) -> Option<LayoutRect> {
LaidOutScene::layout_of(self, id)
}
#[inline]
fn visual_rect_of(&self, store: &NodeStore, id: NodeId) -> Option<LayoutRect> {
LaidOutScene::visual_rect_of(self, store, id)
}
}
pub struct FocusTraversalCtx<'a> {
pub(super) store: &'a NodeStore,
pub(super) cascade: StyleCascade<'a, dyn ResponsivePatches + 'a>,
pub(super) overlays: &'a OverlayState,
layout: &'a (dyn FocusLayoutLookup + 'a),
viewport: Viewport,
pub(super) scratch: &'a mut FocusTraversalScratch,
}
impl<'a> FocusTraversalCtx<'a> {
#[inline]
pub fn store(&self) -> &'a NodeStore {
self.store
}
#[inline]
pub fn style_cascade(&self) -> &StyleCascade<'a, dyn ResponsivePatches + 'a> {
&self.cascade
}
#[inline]
pub fn open_overlays(&self) -> &'a [ResolvedOverlay] {
self.overlays.open_overlays()
}
#[inline]
pub fn resolved_overlay(&self, id: NodeId) -> Option<ResolvedOverlay> {
self.overlays.get(id)
}
#[inline]
pub fn is_open_overlay(&self, id: NodeId) -> bool {
self.overlays.is_open_overlay(id)
}
#[inline]
pub fn topmost_modal(&self) -> Option<NodeId> {
self.overlays.topmost_modal()
}
#[inline]
pub fn topmost_modal_index(&self) -> Option<usize> {
self.overlays.topmost_modal_index()
}
#[inline]
pub fn layout_of(&self, id: NodeId) -> Option<LayoutRect> {
self.layout.layout_of(id)
}
#[inline]
pub fn visual_rect_of(&self, id: NodeId) -> Option<LayoutRect> {
self.layout.visual_rect_of(self.store, id)
}
#[inline]
pub fn viewport(&self) -> (f32, f32) {
(self.viewport.width, self.viewport.height)
}
#[inline]
pub fn is_focusable(&self, id: NodeId) -> bool {
is_focusable_node(self.store, &self.cascade, id)
}
#[inline]
pub fn is_tabbable(&self, id: NodeId) -> bool {
self.is_focusable(id) && is_tabbable_self(self.store, id)
}
#[inline]
pub fn is_in_active_scope(&self, id: NodeId) -> bool {
self.store.is_connected(id) && !self.cascade.is_inert(self.store, id)
}
#[inline]
pub fn scratch(&mut self) -> &mut FocusTraversalScratch {
self.scratch
}
pub fn visit_active_scope(
&mut self,
mut visit: impl FnMut(&NodeStore, NodeId, bool, Option<ResolvedOverlay>),
) {
walk_active_scope(
self.store,
&self.cascade,
self.overlays,
&mut self.scratch.dfs_stack,
&mut visit,
);
}
pub fn visit_active_scope_focusables(
&mut self,
mut visit: impl FnMut(NodeId, Option<LayoutRect>),
) {
let store = self.store;
let cascade = &self.cascade;
let overlays = self.overlays;
let layout = self.layout;
walk_active_scope(
store,
cascade,
overlays,
&mut self.scratch.dfs_stack,
&mut |s, id, eff_hidden, _active_ov| {
if eff_hidden || !is_focusable_self(s, id) {
return;
}
visit(id, layout.visual_rect_of(s, id));
},
);
}
}
pub trait FocusTraversalPolicy: Send {
fn traverse(
&mut self,
ctx: &mut FocusTraversalCtx<'_>,
current: Option<NodeId>,
direction: FocusMove,
flags: FocusTraverseFlags,
) -> FocusTraversalOutcome;
fn on_scene_commit(&mut self, _store: &NodeStore) {}
}
pub(super) fn walk_active_scope<'a>(
store: &NodeStore,
cascade: &StyleCascade<'a, dyn ResponsivePatches + 'a>,
overlays: &OverlayState,
dfs_stack: &mut Vec<(NodeId, bool)>,
visit: &mut impl FnMut(&NodeStore, NodeId, bool, Option<ResolvedOverlay>),
) {
let open = overlays.open_overlays();
if let Some(modal_idx) = overlays.topmost_modal_index() {
for &ov in &open[modal_idx..] {
walk_root(store, cascade, overlays, dfs_stack, ov.node, Some(ov), visit);
}
} else {
walk_root(store, cascade, overlays, dfs_stack, NodeId::ROOT, None, visit);
for &ov in open {
walk_root(store, cascade, overlays, dfs_stack, ov.node, Some(ov), visit);
}
}
}
fn walk_root<'a>(
store: &NodeStore,
cascade: &StyleCascade<'a, dyn ResponsivePatches + 'a>,
overlays: &OverlayState,
dfs_stack: &mut Vec<(NodeId, bool)>,
root: NodeId,
active_overlay: Option<ResolvedOverlay>,
visit: &mut impl FnMut(&NodeStore, NodeId, bool, Option<ResolvedOverlay>),
) {
if !store.is_connected(root)
|| cascade.is_subtree_display_none(store, root)
|| is_subtree_closed_overlay(store, root)
|| resolve_effective_disabled(store, cascade, root)
{
return;
}
let root_parent_hidden = store
.parent(root)
.filter(|&p| p != root)
.is_some_and(|p| resolve_effective_visibility_hidden(store, cascade, p));
dfs_stack.clear();
dfs_stack.push((root, root_parent_hidden));
while let Some((id, parent_hidden)) = dfs_stack.pop() {
if !store.is_live(id) {
continue;
}
if id != root
&& (overlays.is_open_overlay(id)
|| cascade.is_display_none(store, id)
|| cascade.is_node_disabled(store, id)
|| store
.try_row::<Overlay>(id)
.is_some_and(|ov| !ov.options().contains(OverlayFlags::OPEN)))
{
continue;
}
let eff_hidden = match cascade.visibility(store, id) {
Visibility::Hidden => true,
Visibility::Visible => false,
Visibility::Inherit => parent_hidden,
};
visit(store, id, eff_hidden, active_overlay);
for &child in store.children(id).iter().rev() {
if !overlays.is_open_overlay(child) {
dfs_stack.push((child, eff_hidden));
}
}
}
}
impl<L: StoreLayoutEngine> LaidOutScene<L> {
#[inline]
pub fn set_focus_traversal_policy(&mut self, policy: Box<dyn FocusTraversalPolicy>) {
self.focus_policy = policy;
}
pub fn traverse_focus(
&mut self,
store: &NodeStore,
current: Option<NodeId>,
direction: FocusMove,
flags: FocusTraverseFlags,
) -> FocusTraversalOutcome {
self.focus_scratch.scroll_dirty.clear();
let mut policy = std::mem::replace(&mut self.focus_policy, Box::new(TabOrderPolicy));
let mut scratch = std::mem::take(&mut self.focus_scratch);
let mut outcome = {
let cascade: StyleCascade<'_, dyn ResponsivePatches + '_> = StyleCascade {
responsive: &self.layout,
interaction: Some(&self.interaction),
inert: Some(&self.overlays),
};
let mut ctx = FocusTraversalCtx {
store,
cascade,
overlays: &self.overlays,
layout: &*self,
viewport: self.viewport,
scratch: &mut scratch,
};
policy.traverse(&mut ctx, current, direction, flags)
};
self.focus_policy = policy;
self.focus_scratch = scratch;
if flags.contains(FocusTraverseFlags::SCROLL_INTO_VIEW)
&& let Some(target) = outcome.target
&& let Some(rect) = self.layout_of(target)
{
let mut dirty = std::mem::take(&mut self.focus_scratch.scroll_dirty);
let scrolled = self.scroll_into_view_box_with_transform(
store,
target,
rect,
rect,
|_, tf| tf,
|scene, sid, tx, ty| {
let (cx, cy, _) = scene.set_scroll_offset_clamped(store, sid, tx, ty);
(cx, cy)
},
Some(&mut dirty),
);
outcome.scrolled = scrolled || !dirty.is_empty();
self.focus_scratch.scroll_dirty = dirty;
}
outcome
}
#[inline]
pub fn last_focus_scroll_dirty(&self) -> &[NodeId] {
&self.focus_scratch.scroll_dirty
}
}