Skip to content

Commit

Permalink
Add a message for when a node is highlighted
Browse files Browse the repository at this point in the history
This is sort of different from selected. Selected is when someone mashes the
enter button or clicks on a node. Highlighted is when the cursor moves into
a new node.

See Textualize#1400.
  • Loading branch information
davep committed Jan 9, 2023
1 parent 6f10c63 commit 0cf2995
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/textual/widgets/_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,13 @@ class NodeCollapsed(NodeMessage[EventTreeDataType]):
TreeNode[EventTreeDataType]: The node that was collapsed.
"""

class NodeHighlighted(NodeMessage[EventTreeDataType]):
"""Event sent when a node is highlighted.
Attributes:
TreeNode[EventTreeDataType]: The node that was collapsed.
"""

def __init__(
self,
label: TextType,
Expand Down Expand Up @@ -573,6 +580,8 @@ def watch_cursor_line(self, previous_line: int, line: int) -> None:
self._refresh_node(node)
node._selected = True
self._cursor_node = node
if previous_node != node:
self.post_message_no_wait(self.NodeHighlighted(self, node))

def watch_guide_depth(self, guide_depth: int) -> None:
self._invalidate()
Expand Down
10 changes: 10 additions & 0 deletions tests/tree/test_tree_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def on_tree_node_expanded(self, event: Tree.NodeExpanded[None]) -> None:
def on_tree_node_collapsed(self, event: Tree.NodeCollapsed[None]) -> None:
self.record(event)

def on_tree_node_highlighted(self, event: Tree.NodeHighlighted[None]) -> None:
self.record(event)


async def test_tree_node_selected_message() -> None:
"""Selecting a node should result in a selected message being emitted."""
Expand All @@ -57,3 +60,10 @@ async def test_tree_node_collapsed_message() -> None:
"NodeCollapsed",
"NodeSelected",
]


async def test_tree_node_highlighted_message() -> None:
"""Highlighting a node should result in a highlighted message being emitted."""
async with TreeApp().run_test() as pilot:
await pilot.press("enter", "down")
assert pilot.app.messages == ["NodeExpanded", "NodeSelected", "NodeHighlighted"]

0 comments on commit 0cf2995

Please sign in to comment.