wip: focus refactor, e40

This commit is contained in:
🪞👃🪞 2024-11-22 01:36:34 +01:00
parent 94a16b9dbc
commit 364769a2e0
10 changed files with 198 additions and 279 deletions

View file

@ -2,7 +2,6 @@ use crate::*;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum FocusState<T: Copy + Debug + PartialEq> {
Exited(T),
Focused(T),
Entered(T),
}
@ -10,30 +9,22 @@ pub enum FocusState<T: Copy + Debug + PartialEq> {
impl<T: Copy + Debug + PartialEq> FocusState<T> {
pub fn inner (&self) -> T {
match self {
Self::Exited(inner) => *inner,
Self::Focused(inner) => *inner,
Self::Entered(inner) => *inner,
}
}
pub fn set_inner (&mut self, inner: T) {
*self = match self {
Self::Exited(_) => Self::Exited(inner),
Self::Focused(_) => Self::Focused(inner),
Self::Entered(_) => Self::Entered(inner),
}
}
pub fn is_exited (&self) -> bool {
if let Self::Exited(_) = self { true } else { false }
}
pub fn is_focused (&self) -> bool {
if let Self::Focused(_) = self { true } else { false }
}
pub fn is_entered (&self) -> bool {
if let Self::Entered(_) = self { true } else { false }
}
pub fn to_exited (&mut self) {
*self = Self::Exited(self.inner())
}
pub fn to_focused (&mut self) {
*self = Self::Focused(self.inner())
}
@ -44,17 +35,17 @@ impl<T: Copy + Debug + PartialEq> FocusState<T> {
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum FocusCommand {
Next,
Prev,
Up,
Down,
Left,
Right,
Next,
Prev,
Enter,
Exit
Exit,
}
impl<F: HasFocus + FocusGrid> Command<F> for FocusCommand {
impl<F: HasFocus + HasEnter + FocusGrid + FocusOrder> Command<F> for FocusCommand {
fn execute (self, state: &mut F) -> Perhaps<FocusCommand> {
use FocusCommand::*;
match self {
@ -66,43 +57,56 @@ impl<F: HasFocus + FocusGrid> Command<F> for FocusCommand {
Right => { state.focus_right(); },
Enter => { state.focus_enter(); },
Exit => { state.focus_exit(); },
_ => {}
_ => {}
}
Ok(None)
}
}
/// Trait for things that have ordered focusable subparts.
/// Trait for things that have focusable subparts.
pub trait HasFocus {
/// Type that identifies of focused item.
type Item: Copy + PartialEq + Debug;
/// Get the currently focused item.
fn focused (&self) -> Self::Item;
/// Focus the next item.
fn focus_next (&mut self);
/// Focus the previous item.
fn focus_prev (&mut self);
/// Get the currently focused item.
fn set_focused (&mut self, to: Self::Item);
/// Loop forward until a specific item is focused.
fn focus (&mut self, target: Self::Item) {
while self.focused() != target {
self.focus_next()
}
fn focus_to (&mut self, to: Self::Item) {
self.set_focused(to);
self.focus_updated();
}
/// Enter the focused item
fn focus_enter (&mut self) {}
/// Exit the focused item
fn focus_exit (&mut self) {}
/// Return the focused item, if any
fn focus_entered (&self) -> Option<Self::Item> { None }
/// Run this on focus update
fn focus_updated (&mut self) {}
}
/// Trait for things that implement directional focus.
pub trait FocusGrid {
type Item: Copy + PartialEq + Debug;
/// Trait for things that have enterable subparts.
pub trait HasEnter: HasFocus {
/// Get the currently focused item.
fn entered (&self) -> bool;
/// Get the currently focused item.
fn set_entered (&mut self, entered: bool);
/// Enter into the currently focused component
fn focus_enter (&mut self) {
self.set_entered(true);
self.focus_updated();
}
/// Exit the currently entered component
fn focus_exit (&mut self) {
self.set_entered(true);
self.focus_updated();
}
}
/// Trait for things that implement directional navigation between focusable elements.
pub trait FocusGrid: HasFocus {
fn focus_layout (&self) -> &[&[Self::Item]];
fn focus_cursor (&self) -> (usize, usize);
fn focus_cursor_mut (&mut self) -> &mut (usize, usize);
fn focus_update (&mut self) {}
fn focus_update (&mut self) {
let (x, y) = self.focus_cursor();
let item = self.focus_layout()[y][x];
self.focus_to(item)
}
fn focus_up (&mut self) {
let layout = self.focus_layout();
let (x, y) = self.focus_cursor();
@ -139,16 +143,17 @@ pub trait FocusGrid {
}
}
impl<T, U> HasFocus for U
where
T: Copy + PartialEq + Debug,
U: FocusGrid<Item = T>
{
type Item = T;
fn focused (&self) -> Self::Item {
let (x, y) = self.focus_cursor();
self.focus_layout()[y][x]
}
/// Trait for things that implement next/prev navigation between focusable elements.
pub trait FocusOrder {
/// Focus the next item.
fn focus_next (&mut self);
/// Focus the previous item.
fn focus_prev (&mut self);
}
/// Next/prev navigation for directional focusables works in the given way.
impl<T: FocusGrid + HasEnter> FocusOrder for T {
/// Focus the next item.
fn focus_next (&mut self) {
let current = self.focused();
let (x, y) = self.focus_cursor();
@ -164,6 +169,7 @@ where
self.focus_exit();
self.focus_update();
}
/// Focus the previous item.
fn focus_prev (&mut self) {
let current = self.focused();
let (x, _) = self.focus_cursor();