Fix some bugs

This commit is contained in:
afonsofrancof 2024-07-30 22:12:03 +01:00
parent 71c81b2302
commit 847e09b637
No known key found for this signature in database

View File

@ -325,15 +325,18 @@ 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)));
} else { } else {
// When we are not at the root, we have to account for 1 more "virtual" node, `..`. So // When we are not at the root, we have to account for 1 more "virtual" node, `..`. So
// the count is 1 bigger (select is 0 based, because it's an index) // the count is 1 bigger (select is 0 based, because it's an index)
self.list_state self.list_state
.select(Some((curr_selection + 1).min(count))); .select(Some((curr_selection + 1).min(count)));
}
} else if count > 0 {
self.list_state.select(Some(0));
} }
} }
@ -355,29 +358,38 @@ impl CustomList {
} }
} }
/// This method return the currently selected command, or None if no command is selected. /// This method returns the currently selected command, or None if no command is selected.
/// It was extracted from the 'handle_enter()' /// It was extracted from the 'handle_enter()'
/// ///
/// This could probably be integrated into the 'handle_enter()' method as to avoid code /// This could probably be integrated into the 'handle_enter()' method to avoid code
/// duplication, but I don't want to make too major changes to the codebase. /// duplication, but I don't want to make too major changes to the codebase.
fn get_selected_command(&self) -> Option<Command> { fn get_selected_command(&self) -> Option<Command> {
let curr = self
.inner_tree
.get(*self.visit_stack.last().unwrap())
.unwrap();
let selected = self.list_state.selected().unwrap(); let selected = self.list_state.selected().unwrap();
// If we are not at the root and the first item is selected, it's the `..` item if self.filter_query.is_empty() {
if !self.at_root() && selected == 0 { // No filter query, use the regular tree navigation
return None; let curr = self
} .inner_tree
.get(*self.visit_stack.last().unwrap())
.unwrap();
for (mut idx, node) in curr.children().enumerate() { // If we are not at the root and the first item is selected, it's the `..` item
if !self.at_root() { if !self.at_root() && selected == 0 {
idx += 1; return None;
} }
if idx == selected {
return Some(node.value().command.clone()); for (mut idx, node) in curr.children().enumerate() {
if !self.at_root() {
idx += 1;
}
if idx == selected {
return Some(node.value().command.clone());
}
}
} else {
// Filter query is active, use the filtered items
if let Some(filtered_node) = self.filtered_items.get(selected) {
return Some(filtered_node.command.clone());
} }
} }
None None
@ -390,6 +402,11 @@ impl CustomList {
/// ///
/// Returns `Some(command)` when command is selected, othervise we returns `None` /// Returns `Some(command)` when command is selected, othervise we returns `None`
fn handle_enter(&mut self) -> Option<Command> { fn handle_enter(&mut self) -> Option<Command> {
// Ensure an item is selected if none is selected
if self.list_state.selected().is_none() {
self.list_state.select(Some(0));
}
// Get the selected index // Get the selected index
let selected = self.list_state.selected().unwrap(); let selected = self.list_state.selected().unwrap();