From c0e4967b88e20f1ccae3713de8ee196ceb989df7 Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Wed, 31 Aug 2022 14:40:10 +0200 Subject: [PATCH] feat: implement TreeState arrow key methods --- examples/example.rs | 8 ++++---- examples/util/mod.rs | 40 +++++++++----------------------------- src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 35 deletions(-) diff --git a/examples/example.rs b/examples/example.rs index 1e45fbf..20c58c1 100644 --- a/examples/example.rs +++ b/examples/example.rs @@ -91,11 +91,11 @@ fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<( if let Event::Key(key) = event::read()? { match key.code { KeyCode::Char('q') => return Ok(()), - KeyCode::Left => app.tree.close(), - KeyCode::Right => app.tree.open(), + KeyCode::Left => app.tree.left(), + KeyCode::Right => app.tree.right(), KeyCode::Char('\n') => app.tree.toggle(), - KeyCode::Down => app.tree.next(), - KeyCode::Up => app.tree.previous(), + KeyCode::Down => app.tree.down(), + KeyCode::Up => app.tree.up(), _ => {} } } diff --git a/examples/util/mod.rs b/examples/util/mod.rs index 1d943c5..69bf9f8 100644 --- a/examples/util/mod.rs +++ b/examples/util/mod.rs @@ -1,4 +1,4 @@ -use tui_tree_widget::{flatten, get_identifier_without_leaf, TreeItem, TreeState}; +use tui_tree_widget::{TreeItem, TreeState}; pub struct StatefulTree<'a> { pub state: TreeState, @@ -21,42 +21,20 @@ impl<'a> StatefulTree<'a> { } } - fn move_up_down(&mut self, down: bool) { - let visible = flatten(&self.state.get_all_opened(), &self.items); - let current_identifier = self.state.selected(); - let current_index = visible - .iter() - .position(|o| o.identifier == current_identifier); - let new_index = current_index.map_or(0, |current_index| { - if down { - current_index.saturating_add(1) - } else { - current_index.saturating_sub(1) - } - .min(visible.len() - 1) - }); - let new_identifier = visible.get(new_index).unwrap().identifier.clone(); - self.state.select(new_identifier); + pub fn down(&mut self) { + self.state.key_down(&self.items); } - pub fn next(&mut self) { - self.move_up_down(true); + pub fn up(&mut self) { + self.state.key_up(&self.items); } - pub fn previous(&mut self) { - self.move_up_down(false); + pub fn left(&mut self) { + self.state.key_left(); } - pub fn close(&mut self) { - let selected = self.state.selected(); - if !self.state.close(&selected) { - let (head, _) = get_identifier_without_leaf(&selected); - self.state.select(head); - } - } - - pub fn open(&mut self) { - self.state.open(self.state.selected()); + pub fn right(&mut self) { + self.state.key_right(); } pub fn toggle(&mut self) { diff --git a/src/lib.rs b/src/lib.rs index 4257de8..224c227 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -85,6 +85,52 @@ impl TreeState { pub fn close_all(&mut self) { self.opened.clear(); } + + /// Handles the up arrow key. + /// Moves up in the current depth or to its parent. + pub fn key_up(&mut self, items: &[TreeItem]) { + let visible = flatten(&self.get_all_opened(), items); + let current_identifier = self.selected(); + let current_index = visible + .iter() + .position(|o| o.identifier == current_identifier); + let new_index = current_index.map_or(0, |current_index| { + current_index.saturating_sub(1).min(visible.len() - 1) + }); + let new_identifier = visible[new_index].identifier.clone(); + self.select(new_identifier); + } + + /// Handles the down arrow key. + /// Moves down in the current depth or into a child node. + pub fn key_down(&mut self, items: &[TreeItem]) { + let visible = flatten(&self.get_all_opened(), items); + let current_identifier = self.selected(); + let current_index = visible + .iter() + .position(|o| o.identifier == current_identifier); + let new_index = current_index.map_or(0, |current_index| { + current_index.saturating_add(1).min(visible.len() - 1) + }); + let new_identifier = visible[new_index].identifier.clone(); + self.select(new_identifier); + } + + /// Handles the left arrow key. + /// Closes the currently selected or moves to its parent. + pub fn key_left(&mut self) { + let selected = self.selected(); + if !self.close(&selected) { + let (head, _) = get_identifier_without_leaf(&selected); + self.select(head); + } + } + + /// Handles the right arrow key. + /// Openes the currently selected. + pub fn key_right(&mut self) { + self.open(self.selected()); + } } #[derive(Debug, Clone)]