use nativeview_abi::FocusMove;
use nativeview_engine::is_focusable_self;
use nativeview_store::NodeId;
use nativeview_tables::tables::Interaction;
use super::{
FocusTraversalCtx, FocusTraversalOutcome, FocusTraversalPolicy, FocusTraverseFlags,
walk_active_scope,
};
#[derive(Debug, Clone, Copy, Default)]
pub struct TabOrderPolicy;
impl FocusTraversalPolicy for TabOrderPolicy {
fn traverse(
&mut self,
ctx: &mut FocusTraversalCtx<'_>,
current: Option<NodeId>,
direction: FocusMove,
flags: FocusTraverseFlags,
) -> FocusTraversalOutcome {
let trapped = ctx.topmost_modal().is_some();
if matches!(
direction,
FocusMove::Up | FocusMove::Down | FocusMove::Left | FocusMove::Right
) {
return FocusTraversalOutcome {
target: None,
trapped,
scrolled: false,
wrapped: false,
};
}
let store = ctx.store;
let cascade = &ctx.cascade;
let overlays = ctx.overlays;
let scratch = &mut *ctx.scratch;
scratch.positive_buf.clear();
scratch.natural_buf.clear();
let mut cur_in_dfs = false;
let mut nat_pos_at_cur = 0usize;
walk_active_scope(
store,
cascade,
overlays,
&mut scratch.dfs_stack,
&mut |s, id, eff_hidden, _ov| {
if current == Some(id) {
cur_in_dfs = true;
nat_pos_at_cur = scratch.natural_buf.len();
}
if eff_hidden || !is_focusable_self(s, id) {
return;
}
let tab_index = s.try_row::<Interaction>(id).and_then(|i| i.tab_index());
match tab_index {
Some(ti) if ti < 0 => {}
Some(ti) if ti > 0 => scratch.positive_buf.push((ti, id)),
_ => scratch.natural_buf.push(id),
}
},
);
stable_sort_positive(&mut scratch.positive_buf, &mut scratch.positive_sort_buf);
let pos_len = scratch.positive_buf.len();
let nat_len = scratch.natural_buf.len();
let total = pos_len + nat_len;
if total == 0 {
return FocusTraversalOutcome {
target: None,
trapped,
scrolled: false,
wrapped: false,
};
}
let combined_at = |idx: usize| -> NodeId {
if idx < pos_len {
scratch.positive_buf[idx].1
} else {
scratch.natural_buf[idx - pos_len]
}
};
let wrap_allowed = trapped || !flags.contains(FocusTraverseFlags::NO_WRAP);
let (target, wrapped) = match direction {
FocusMove::First => (Some(combined_at(0)), false),
FocusMove::Last => (Some(combined_at(total - 1)), false),
FocusMove::Next | FocusMove::Prev => {
let forward = direction == FocusMove::Next;
let current_idx = current.and_then(|cur| {
scratch
.positive_buf
.iter()
.position(|&(_, id)| id == cur)
.or_else(|| {
scratch
.natural_buf
.iter()
.position(|&id| id == cur)
.map(|i| pos_len + i)
})
});
if let Some(idx) = current_idx {
if forward {
if idx + 1 < total {
(Some(combined_at(idx + 1)), false)
} else if wrap_allowed {
(Some(combined_at(0)), true)
} else {
(None, false)
}
} else if idx > 0 {
(Some(combined_at(idx - 1)), false)
} else if wrap_allowed {
(Some(combined_at(total - 1)), true)
} else {
(None, false)
}
} else if cur_in_dfs {
if forward {
if nat_pos_at_cur < nat_len {
(Some(scratch.natural_buf[nat_pos_at_cur]), false)
} else if wrap_allowed {
(Some(combined_at(0)), true)
} else {
(None, false)
}
} else if nat_pos_at_cur > 0 {
(Some(scratch.natural_buf[nat_pos_at_cur - 1]), false)
} else if pos_len > 0 {
(Some(scratch.positive_buf[pos_len - 1].1), false)
} else if wrap_allowed {
(Some(combined_at(total - 1)), true)
} else {
(None, false)
}
} else if forward {
(Some(combined_at(0)), false)
} else {
(Some(combined_at(total - 1)), false)
}
}
FocusMove::Up | FocusMove::Down | FocusMove::Left | FocusMove::Right => unreachable!(),
};
FocusTraversalOutcome {
target,
trapped,
scrolled: false,
wrapped,
}
}
}
fn stable_sort_positive(buf: &mut [(i16, NodeId)], scratch: &mut Vec<(i16, NodeId)>) {
let n = buf.len();
if n <= 1 {
return;
}
if n <= 32 {
for i in 1..n {
let key = buf[i];
let mut j = i;
while j > 0 && buf[j - 1].0 > key.0 {
buf[j] = buf[j - 1];
j -= 1;
}
buf[j] = key;
}
return;
}
scratch.clear();
scratch.resize(n, (0, NodeId::NULL));
let mut width = 1usize;
let mut src_is_buf = true;
type TabEntry = (i16, NodeId);
while width < n {
let (src, dst): (&[TabEntry], &mut [TabEntry]) = if src_is_buf {
(buf, scratch.as_mut_slice())
} else {
(scratch.as_slice(), buf)
};
let mut left = 0usize;
while left < n {
let mid = (left + width).min(n);
let right = (left + 2 * width).min(n);
let (mut i, mut j, mut k) = (left, mid, left);
while i < mid && j < right {
if src[i].0 <= src[j].0 {
dst[k] = src[i];
i += 1;
} else {
dst[k] = src[j];
j += 1;
}
k += 1;
}
if i < mid {
dst[k..k + (mid - i)].copy_from_slice(&src[i..mid]);
} else if j < right {
dst[k..k + (right - j)].copy_from_slice(&src[j..right]);
}
left = right;
}
src_is_buf = !src_is_buf;
width *= 2;
}
if !src_is_buf {
buf.copy_from_slice(&scratch[..n]);
}
}