tek/crates/tek_core/src/focus.rs

144 lines
4.6 KiB
Rust

use crate::*;
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum FocusCommand {
Next,
Prev,
Up,
Down,
Left,
Right,
Enter,
Exit
}
impl<F: HasFocus + FocusGrid + FocusEnter> Command<F> for FocusCommand {
fn execute (self, state: &mut F) -> Perhaps<FocusCommand> {
use FocusCommand::*;
match self {
Next => { state.focus_next(); },
Prev => { state.focus_prev(); },
Up => { state.focus_up(); },
Down => { state.focus_down(); },
Left => { state.focus_left(); },
Right => { state.focus_right(); },
Enter => { state.focus_enter(); },
Exit => { state.focus_exit(); },
_ => {}
}
Ok(None)
}
}
/// 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;
/// 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()
}
}
}
/// Trait for things that implement directional focus.
pub trait FocusGrid {
type Item: Copy + PartialEq + Debug;
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_up (&mut self) {
let layout = self.focus_layout();
let (x, y) = self.focus_cursor();
let next_y = if y == 0 { layout.len().saturating_sub(1) } else { y - 1 };
let next_x = if layout[y].len() == layout[next_y].len() { x } else {
((x as f32 / layout[y].len() as f32) * layout[next_y].len() as f32) as usize
};
*self.focus_cursor_mut() = (next_x, next_y);
self.focus_update();
}
fn focus_down (&mut self) {
let layout = self.focus_layout();
let (x, y) = self.focus_cursor();
let next_y = if y >= layout.len().saturating_sub(1) { 0 } else { y + 1 };
let next_x = if layout[y].len() == layout[next_y].len() { x } else {
((x as f32 / layout[y].len() as f32) * layout[next_y].len() as f32) as usize
};
*self.focus_cursor_mut() = (next_x, next_y);
self.focus_update();
}
fn focus_left (&mut self) {
let layout = self.focus_layout();
let (x, y) = self.focus_cursor();
let next_x = if x == 0 { layout[y].len().saturating_sub(1) } else { x - 1 };
*self.focus_cursor_mut() = (next_x, y);
self.focus_update();
}
fn focus_right (&mut self) {
let layout = self.focus_layout();
let (x, y) = self.focus_cursor();
let next_x = if x >= layout[y].len().saturating_sub(1) { 0 } else { x + 1 };
*self.focus_cursor_mut() = (next_x, y);
self.focus_update();
}
}
impl<T, U> HasFocus for U
where
T: Copy + PartialEq + Debug,
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_next (&mut self) {
let current = self.focused();
let (x, y) = self.focus_cursor();
if x < self.focus_layout()[y].len().saturating_sub(1) {
self.focus_right();
} else {
self.focus_down();
self.focus_cursor_mut().0 = 0;
}
if self.focused() == current { // FIXME: prevent infinite loop
self.focus_next()
}
self.focus_exit();
self.focus_update();
}
fn focus_prev (&mut self) {
let current = self.focused();
let (x, _) = self.focus_cursor();
if x > 0 {
self.focus_left();
} else {
self.focus_up();
let (_, y) = self.focus_cursor();
let next_x = self.focus_layout()[y].len().saturating_sub(1);
self.focus_cursor_mut().0 = next_x;
}
if self.focused() == current { // FIXME: prevent infinite loop
self.focus_prev()
}
self.focus_exit();
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>;
}