Fix crash when trying to scroll on empty results

This commit is contained in:
afonsofrancof 2024-07-30 22:49:51 +01:00
parent e85cc943b3
commit b7c2ecedd9
No known key found for this signature in database

View File

@ -242,6 +242,8 @@ impl CustomList {
pub fn reset_selection(&mut self) { pub fn reset_selection(&mut self) {
if !self.filtered_items.is_empty() { if !self.filtered_items.is_empty() {
self.list_state.select(Some(0)); self.list_state.select(Some(0));
} else {
self.list_state.select(None);
} }
} }
@ -317,8 +319,11 @@ impl CustomList {
} }
fn try_scroll_up(&mut self) { fn try_scroll_up(&mut self) {
self.list_state if let Some(selected) = self.list_state.selected() {
.select(Some(self.list_state.selected().unwrap().saturating_sub(1))); if selected > 0 {
self.list_state.select(Some(selected.saturating_sub(1)));
}
}
} }
fn try_scroll_down(&mut self) { fn try_scroll_down(&mut self) {
@ -332,7 +337,7 @@ impl CustomList {
self.filtered_items.len() self.filtered_items.len()
}; };
let curr_selection = self.list_state.selected().unwrap(); if let Some(curr_selection) = self.list_state.selected() {
if self.at_root() { if self.at_root() {
self.list_state self.list_state
.select(Some((curr_selection + 1).min(count - 1))); .select(Some((curr_selection + 1).min(count - 1)));
@ -343,6 +348,7 @@ impl CustomList {
.select(Some((curr_selection + 1).min(count))); .select(Some((curr_selection + 1).min(count)));
} }
} }
}
/// Scroll the preview window down /// Scroll the preview window down
fn scroll_preview_window_down(&mut self) { fn scroll_preview_window_down(&mut self) {