wip: p.53, e=118, fixed focus trait loop

This commit is contained in:
🪞👃🪞 2024-11-17 19:19:46 +01:00
parent 9b996878c2
commit 76af9d9bac
15 changed files with 737 additions and 727 deletions

View file

@ -12,7 +12,7 @@ pub enum FocusCommand {
Exit
}
impl<F: FocusOrder + FocusGrid + FocusEnter> Command<F> for FocusCommand {
impl<F: HasFocus + FocusGrid + FocusEnter> Command<F> for FocusCommand {
fn execute (self, state: &mut F) -> Perhaps<FocusCommand> {
use FocusCommand::*;
match self {
@ -30,24 +30,25 @@ impl<F: FocusOrder + FocusGrid + FocusEnter> Command<F> for FocusCommand {
}
}
/// Trait for things that have ordered 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;
fn focus (&mut self, target: Self::Item);
}
pub trait FocusOrder {
/// Focus the next item.
fn focus_next (&mut self);
/// Focus the previous item.
fn focus_prev (&mut self);
/// Loop forward until a specific item is focused.
fn focus (&mut self, target: Self::Item) {
while self.focused() != target {
self.focus_next()
}
}
}
pub trait FocusEnter {
type Item: Copy + PartialEq + Debug;
fn focus_enter (&mut self) {}
fn focus_exit (&mut self) {}
fn focus_entered (&self) -> Option<Self::Item>;
}
/// Trait for things that implement directional focus.
pub trait FocusGrid {
type Item: Copy + PartialEq + Debug;
fn focus_layout (&self) -> &[&[Self::Item]];
@ -93,25 +94,13 @@ pub trait FocusGrid {
impl<T, U> HasFocus for U
where
T: Copy + PartialEq + Debug,
U: FocusGrid<Item = T> + FocusOrder,
U: FocusGrid<Item = T> + FocusEnter<Item = T>,
{
type Item = T;
fn focused (&self) -> Self::Item {
let (x, y) = self.focus_cursor();
self.focus_layout()[y][x]
}
fn focus (&mut self, target: Self::Item) {
while self.focused() != target {
self.focus_next()
}
}
}
impl<T, U> FocusOrder for U
where
T: Copy + PartialEq + Debug,
U: HasFocus<Item = T> + FocusGrid<Item = T> + FocusEnter
{
fn focus_next (&mut self) {
let current = self.focused();
let (x, y) = self.focus_cursor();
@ -145,3 +134,11 @@ where
self.focus_update();
}
}
/// Trait for things that can be focused into.
pub trait FocusEnter {
type Item: Copy + PartialEq + Debug;
fn focus_enter (&mut self) {}
fn focus_exit (&mut self) {}
fn focus_entered (&self) -> Option<Self::Item>;
}