Partial matrix implementation

This commit is contained in:
2023-08-08 10:50:04 -06:00
parent 3dce6c7517
commit ae6e19a414
3 changed files with 65 additions and 2 deletions
+55
View File
@@ -113,6 +113,61 @@ impl<T> Scrollable for StatefulTable<T> {
}
}
#[derive(Default)]
pub struct StatefulMatrix<T> {
pub selection: (usize, usize),
pub items: Vec<Vec<T>>,
}
impl<T> Scrollable for StatefulMatrix<T> {
fn scroll_down(&mut self) {
if self.selection.0 >= self.items.len() - 1 {
self.selection.0 = 0;
} else {
self.selection.0 += 1;
}
}
fn scroll_up(&mut self) {
if self.selection.0 == 0 {
self.selection.0 = self.items.len() - 1;
} else {
self.selection.0 -= 1;
}
}
fn scroll_to_top(&mut self) {
self.selection.0 = 0;
}
fn scroll_to_bottom(&mut self) {
self.selection.0 = self.items.len() - 1;
}
}
impl<T> StatefulMatrix<T> {
pub fn current_selection(&self) -> &T {
let (x, y) = self.selection;
&self.items[x][y]
}
pub fn scroll_left(&mut self) {
if self.selection.1 == 0 {
self.selection.1 = self.items[0].len() - 1;
} else {
self.selection.1 -= 1;
}
}
pub fn scroll_right(&mut self) {
if self.selection.1 >= self.items[0].len() - 1 {
self.selection.1 = 0;
} else {
self.selection.1 += 1;
}
}
}
#[derive(Default)]
pub struct ScrollableText {
pub items: Vec<String>,