tab/backtab focus works pretty ok

This commit is contained in:
🪞👃🪞 2024-10-09 22:13:32 +03:00
parent cd9244ec88
commit 9f15f8fff9
3 changed files with 33 additions and 19 deletions

View file

@ -1,6 +1,6 @@
use crate::*;
pub trait FocusGrid<T> {
pub trait FocusGrid<T: Copy + PartialEq> {
fn layout (&self) -> &[&[T]];
fn cursor (&self) -> (usize, usize);
fn cursor_mut (&mut self) -> &mut (usize, usize);
@ -39,6 +39,7 @@ pub trait FocusGrid<T> {
*self.cursor_mut() = (next_x, y);
}
fn focus_next (&mut self) {
let current = *self.focused();
let (x, y) = self.cursor();
if x < self.layout()[y].len().saturating_sub(1) {
self.focus_right();
@ -46,8 +47,12 @@ pub trait FocusGrid<T> {
self.focus_down();
self.cursor_mut().0 = 0;
}
if *self.focused() == current { // FIXME: prevent infinite loop
self.focus_next()
}
}
fn focus_prev (&mut self) {
let current = *self.focused();
let (x, _) = self.cursor();
if x > 0 {
self.focus_left();
@ -57,6 +62,9 @@ pub trait FocusGrid<T> {
let next_x = self.layout()[y].len().saturating_sub(1);
self.cursor_mut().0 = next_x;
}
if *self.focused() == current { // FIXME: prevent infinite loop
self.focus_prev()
}
}
}