#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FocusAxis {
Horizontal,
Vertical,
Both,
None,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FocusGroup {
pub(crate) axis: FocusAxis,
pub(crate) wrap: bool,
pub(crate) activate: bool,
pub(crate) one_stop: bool,
pub(crate) follow_pointer: bool,
pub(crate) typeahead: bool,
pub(crate) nested: bool,
pub(crate) menu_row: bool,
pub(crate) pages: bool,
pub(crate) shift_extends: bool,
pub(crate) panel_row: bool,
}
impl FocusGroup {
fn new(axis: FocusAxis) -> Self {
Self {
axis,
wrap: true,
activate: false,
one_stop: false,
follow_pointer: false,
typeahead: false,
nested: false,
menu_row: false,
pages: false,
shift_extends: false,
panel_row: false,
}
}
pub fn horizontal() -> Self {
Self::new(FocusAxis::Horizontal)
}
pub fn vertical() -> Self {
Self::new(FocusAxis::Vertical)
}
pub fn both() -> Self {
Self::new(FocusAxis::Both)
}
pub fn none() -> Self {
Self::new(FocusAxis::None)
}
pub(crate) fn is_none(&self) -> bool {
self.axis == FocusAxis::None
}
pub fn wrap(mut self, wrap: bool) -> Self {
self.wrap = wrap;
self
}
pub fn activate(mut self, activate: bool) -> Self {
self.activate = activate;
self
}
pub fn follow_pointer(mut self, follow: bool) -> Self {
self.follow_pointer = follow;
self
}
pub fn typeahead(mut self, typeahead: bool) -> Self {
self.typeahead = typeahead;
self
}
pub fn nested(mut self, nested: bool) -> Self {
self.nested = nested;
self
}
pub fn menu_row(mut self, menu_row: bool) -> Self {
self.menu_row = menu_row;
self
}
pub fn pages(mut self, pages: bool) -> Self {
self.pages = pages;
self
}
pub fn shift_extends(mut self, extends: bool) -> Self {
self.shift_extends = extends;
self
}
pub fn panel_row(mut self, panel_row: bool) -> Self {
self.menu_row = panel_row;
self.panel_row = panel_row;
self
}
pub fn one_stop(mut self, one_stop: bool) -> Self {
self.one_stop = one_stop;
self
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum FocusStep {
Back,
On,
First,
Last,
Open,
Close,
OpenSiblings,
PageBack,
PageOn,
}
impl FocusStep {
pub(crate) fn is_nested(self) -> bool {
matches!(self, Self::Open | Self::Close | Self::OpenSiblings)
}
pub(crate) fn is_page(self) -> bool {
matches!(self, Self::PageBack | Self::PageOn)
}
}
impl FocusGroup {
pub(crate) fn step(&self, key: &str) -> Option<FocusStep> {
let (horizontal, vertical) = match self.axis {
FocusAxis::Horizontal => (true, false),
FocusAxis::Vertical => (false, true),
FocusAxis::Both => (true, true),
FocusAxis::None => return None,
};
let nested = self.nested && self.axis == FocusAxis::Vertical;
match key {
"ArrowRight" if nested => Some(FocusStep::Open),
"ArrowLeft" if nested => Some(FocusStep::Close),
"*" if nested => Some(FocusStep::OpenSiblings),
"ArrowLeft" if horizontal => Some(FocusStep::Back),
"ArrowRight" if horizontal => Some(FocusStep::On),
"ArrowUp" if vertical => Some(FocusStep::Back),
"ArrowDown" if vertical => Some(FocusStep::On),
"PageUp" if vertical => Some(FocusStep::PageBack),
"PageDown" if vertical => Some(FocusStep::PageOn),
"Home" => Some(FocusStep::First),
"End" => Some(FocusStep::Last),
_ => None,
}
}
}
pub trait FocusRows: Send + Sync {
fn count(&self) -> usize;
fn label(&self, row: usize) -> Option<String>;
fn enabled(&self, _row: usize) -> bool {
true
}
fn level(&self, _row: usize) -> u32 {
1
}
fn expanded(&self, _row: usize) -> Option<bool> {
None
}
fn set_expanded(&self, _row: usize, _open: bool) {}
fn page(&self) -> usize {
10
}
fn reveal(&self, row: usize);
}
pub(crate) fn step_rows(
step: FocusStep,
current: usize,
count: usize,
page: usize,
wrap: bool,
enabled: impl Fn(usize) -> bool,
) -> Option<usize> {
if count == 0 {
return None;
}
let last = count - 1;
let forward = |from: usize| (from..count).find(|&row| enabled(row));
let backward = |from: usize| (0..=from.min(last)).rev().find(|&row| enabled(row));
let target = match step {
FocusStep::First => forward(0),
FocusStep::Last => backward(last),
FocusStep::On => forward(current + 1).or_else(|| wrap.then(|| forward(0)).flatten()),
FocusStep::Back => current
.checked_sub(1)
.and_then(backward)
.or_else(|| wrap.then(|| backward(last)).flatten()),
FocusStep::PageOn => {
let to = (current + page.max(1)).min(last);
backward(to).filter(|&row| row > current).or_else(|| forward(to))
}
FocusStep::PageBack => {
let to = current.saturating_sub(page.max(1));
forward(to).filter(|&row| row < current).or_else(|| backward(to))
}
FocusStep::Open | FocusStep::Close | FocusStep::OpenSiblings => None,
};
target.filter(|&row| row != current)
}
#[derive(Clone, Default)]
pub(crate) struct FocusBranch {
pub(crate) level: Option<u32>,
pub(crate) expanded: Option<bool>,
pub(crate) toggle: Option<std::sync::Arc<dyn Fn(bool) + Send + Sync>>,
pub(crate) row: Option<usize>,
pub(crate) column: Option<usize>,
pub(crate) rows: Option<std::sync::Arc<dyn FocusRows>>,
}
impl FocusBranch {
pub(crate) fn level(&self) -> u32 {
self.level.unwrap_or(1).max(1)
}
}