mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Add a toggle action to Tree, along with a new binding
See #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.
This commit is contained in:
@@ -284,6 +284,7 @@ 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),
|
||||
]
|
||||
@@ -291,6 +292,7 @@ class Tree(Generic[TreeDataType], ScrollView, can_focus=True):
|
||||
| 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. |
|
||||
"""
|
||||
@@ -1004,6 +1006,14 @@ class Tree(Generic[TreeDataType], ScrollView, can_focus=True):
|
||||
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]
|
||||
|
||||
Reference in New Issue
Block a user