wip: implement focus_next and focus_prev

This commit is contained in:
🪞👃🪞 2024-10-09 21:39:52 +03:00
parent a694903bdb
commit cd9244ec88
4 changed files with 37 additions and 60 deletions

View file

@ -11,7 +11,7 @@ pub trait FocusGrid<T> {
fn focus_up (&mut self) {
let layout = self.layout();
let (x, y) = self.cursor();
let next_y = if y == 0 { layout.len() - 1 } else { y - 1 };
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
};
@ -20,7 +20,7 @@ pub trait FocusGrid<T> {
fn focus_down (&mut self) {
let layout = self.layout();
let (x, y) = self.cursor();
let next_y = if y >= layout.len() - 1 { 0 } else { y + 1 };
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
};
@ -29,20 +29,34 @@ pub trait FocusGrid<T> {
fn focus_left (&mut self) {
let layout = self.layout();
let (x, y) = self.cursor();
let next_x = if x == 0 { layout[y].len() - 1 } else { x - 1 };
let next_x = if x == 0 { layout[y].len().saturating_sub(1) } else { x - 1 };
*self.cursor_mut() = (next_x, y);
}
fn focus_right (&mut self) {
let layout = self.layout();
let (x, y) = self.cursor();
let next_x = if x >= layout[y].len() - 1 { 0 } else { x - 1 };
let next_x = if x >= layout[y].len().saturating_sub(1) { 0 } else { x + 1 };
*self.cursor_mut() = (next_x, y);
}
fn focus_next (&mut self) {
todo!();
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;
}
}
fn focus_prev (&mut self) {
todo!();
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;
}
}
}