Skip to content

Commit

Permalink
fix(TreeComponent): bring back NavigationAction::Up if is_filtering :D
Browse files Browse the repository at this point in the history
  • Loading branch information
lautarodragan committed Feb 19, 2025
1 parent 08682e5 commit 2024b2b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
30 changes: 23 additions & 7 deletions src/components/tree/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,29 @@ where
}
}
}
// NavigationAction::Up if is_filtering => {
// let items = self.items.borrow();
// let Some(n) = items.iter().take(initial_i).rposition(|item| item.is_match) else {
// return;
// };
// n
// }
NavigationAction::Up if is_filtering => {
let mut new_path = initial_i.clone();

'outer: for (root_node_index, root_node) in nodes.iter().enumerate().rev() {
for (path, node) in root_node.iter().rev() {
let path = path.with_parent(root_node_index);
if path < *initial_i && node.is_match {
new_path = path;
break 'outer;
}
log::debug!("{path} not < {initial_i} for {}", node.inner);
}

let path = TreeNodePath::from_vec(vec![root_node_index]);
if root_node.is_match && path < *initial_i {
new_path = path;
break 'outer;
}
log::debug!("root_path {path} not < {initial_i} for root_node {}", root_node.inner);
}

new_path
}
NavigationAction::Down if is_filtering => {
let mut new_path = initial_i.clone();

Expand Down
10 changes: 5 additions & 5 deletions src/components/tree/tree_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl<'a, T> Iterator for TreeNodeIterator<'a, T> {
}
}

impl<'a, T> DoubleEndedIterator for TreeNodeIterator<'a, T> {
impl<T> DoubleEndedIterator for TreeNodeIterator<'_, T> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.root.children.is_empty() {
None
Expand All @@ -227,10 +227,10 @@ impl<'a, T> DoubleEndedIterator for TreeNodeIterator<'a, T> {
self.current_path = self.current_path.parent();
self.current_node = self.root.get_child(&self.current_path);
self.current_node.as_ref().map(|n| (self.current_path.clone(), *n))
} else { // self.current_path.last() > 0
// We have at least one sibling before us. Move onto it.
self.current_path = self.current_path.with_value(self.current_path.len() - 1, self.current_path.last() - 1);
let mut node = self.root.get_child(&self.current_path).unwrap();
} else {
// We have at least one sibling before us. Move onto it. (self.current_path.last() > 0)
self.current_path = self.current_path.prev();
let mut node = self.root.get_child(&self.current_path).unwrap();

// This sibling may have children. We now must find its last child.
loop {
Expand Down
8 changes: 8 additions & 0 deletions src/components/tree/tree_node_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ impl TreeNodePath {
}
}

pub fn prev(&self) -> Self {
if self.is_empty() {
self.clone()
} else {
self.with_value(self.len() - 1, self.last() - 1)
}
}

pub fn parent(&self) -> Self {
let mut parent = self.clone();
let new_len = parent.len().saturating_sub(1);
Expand Down

0 comments on commit 2024b2b

Please sign in to comment.