mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
76 lines
2.6 KiB
Rust
76 lines
2.6 KiB
Rust
use crate::*;
|
|
|
|
pub trait FocusGrid<T: Copy + PartialEq> {
|
|
fn layout (&self) -> &[&[T]];
|
|
fn cursor (&self) -> (usize, usize);
|
|
fn cursor_mut (&mut self) -> &mut (usize, usize);
|
|
fn update_focus (&mut self) {}
|
|
fn focused (&self) -> &T {
|
|
let (x, y) = self.cursor();
|
|
&self.layout()[y][x]
|
|
}
|
|
fn focus_up (&mut self) {
|
|
let layout = self.layout();
|
|
let (x, y) = self.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.cursor_mut() = (next_x, next_y);
|
|
self.update_focus();
|
|
}
|
|
fn focus_down (&mut self) {
|
|
let layout = self.layout();
|
|
let (x, y) = self.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.cursor_mut() = (next_x, next_y);
|
|
self.update_focus();
|
|
}
|
|
fn focus_left (&mut self) {
|
|
let layout = self.layout();
|
|
let (x, y) = self.cursor();
|
|
let next_x = if x == 0 { layout[y].len().saturating_sub(1) } else { x - 1 };
|
|
*self.cursor_mut() = (next_x, y);
|
|
self.update_focus();
|
|
}
|
|
fn focus_right (&mut self) {
|
|
let layout = self.layout();
|
|
let (x, y) = self.cursor();
|
|
let next_x = if x >= layout[y].len().saturating_sub(1) { 0 } else { x + 1 };
|
|
*self.cursor_mut() = (next_x, y);
|
|
self.update_focus();
|
|
}
|
|
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();
|
|
} else {
|
|
self.focus_down();
|
|
self.cursor_mut().0 = 0;
|
|
}
|
|
if *self.focused() == current { // FIXME: prevent infinite loop
|
|
self.focus_next()
|
|
}
|
|
self.update_focus();
|
|
}
|
|
fn focus_prev (&mut self) {
|
|
let current = *self.focused();
|
|
let (x, _) = self.cursor();
|
|
if x > 0 {
|
|
self.focus_left();
|
|
} else {
|
|
self.focus_up();
|
|
let (_, y) = self.cursor();
|
|
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()
|
|
}
|
|
self.update_focus();
|
|
}
|
|
}
|