feat: Adding toggle (#8)
* feat: Adding toggle * Update src/lib.rs Return unneded Co-authored-by: EdJoPaTo <github@edjopato.de> Co-authored-by: EdJoPaTo <github@edjopato.de>
This commit is contained in:
@@ -82,6 +82,9 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
Key::Right => {
|
||||
app.tree.open();
|
||||
}
|
||||
Key::Char('\n') => {
|
||||
app.tree.toggle();
|
||||
}
|
||||
Key::Down => {
|
||||
app.tree.next();
|
||||
}
|
||||
|
||||
@@ -60,4 +60,8 @@ impl<'a> StatefulTree<'a> {
|
||||
pub fn open(&mut self) {
|
||||
self.state.open(self.state.selected());
|
||||
}
|
||||
|
||||
pub fn toggle(&mut self) {
|
||||
self.state.toggle();
|
||||
}
|
||||
}
|
||||
|
||||
+10
@@ -58,6 +58,16 @@ impl TreeState {
|
||||
self.opened.remove(identifier)
|
||||
}
|
||||
|
||||
/// Toggles a tree node.
|
||||
/// If the node is in opened then it calls `close()`. Otherwise it calls `open()`.
|
||||
pub fn toggle(&mut self) {
|
||||
if self.opened.contains(&self.selected()) {
|
||||
self.close(&self.selected());
|
||||
} else {
|
||||
self.open(self.selected());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn close_all(&mut self) {
|
||||
self.opened.clear();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user