Skip to content

Commit

Permalink
Add a toggle action to Tree, along with a new binding
Browse files Browse the repository at this point in the history
See Textualize#1433. The idea here is that the user has an option of
expanding/collapsing a non-leaf node without causing a selected event,
or (with auto_expand turned off) cause a selected event without an
expand/collapse event.

As this will need a new binding, I've chosen the space bar as the key to
toggle the expanded state.
  • Loading branch information
davep committed Jan 31, 2023
1 parent dc1c4da commit 5c17bc3
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/textual/widgets/_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,15 @@ class Tree(Generic[TreeDataType], ScrollView, can_focus=True):

BINDINGS: ClassVar[list[BindingType]] = [
Binding("enter", "select_cursor", "Select", show=False),
Binding("space", "toggle_node", "Toggle", show=False),
Binding("up", "cursor_up", "Cursor Up", show=False),
Binding("down", "cursor_down", "Cursor Down", show=False),
]
"""
| Key(s) | Description |
| :- | :- |
| enter | Select the current item. |
| space | Toggle the expand/collapsed space of the current item. |
| up | Move the cursor up. |
| down | Move the cursor down. |
"""
Expand Down Expand Up @@ -1004,6 +1006,14 @@ def action_scroll_end(self) -> None:
self.cursor_line = self.last_line
self.scroll_to_line(self.cursor_line)

def action_toggle_node(self) -> None:
try:
line = self._tree_lines[self.cursor_line]
except IndexError:
pass
else:
self._toggle_node(line.path[-1])

def action_select_cursor(self) -> None:
try:
line = self._tree_lines[self.cursor_line]
Expand Down

0 comments on commit 5c17bc3

Please sign in to comment.