Skip to content

Commit 60979e0

Browse files
authored
Merge pull request #5159 from TomJGooding/feat-events-add-control-property-to-enter-and-leave
feat(events): add control property to Enter and Leave
2 parents b9be24f + 9a72e06 commit 60979e0

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8-
98
## Unreleased
109

1110
### Changed
@@ -29,6 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2928
- Added new demo `python -m textual`, not *quite* finished but better than the old one https://github.com/Textualize/textual/pull/5174
3029
- Added `Screen.can_view_partial` and `Widget.can_view_partial` https://github.com/Textualize/textual/pull/5174
3130
- Added `App.is_web` property to indicate if the app is running via a web browser https://github.com/Textualize/textual/pull/5128
31+
- `Enter` and `Leave` events can now be used with the `on` decorator https://github.com/Textualize/textual/pull/5159
3232

3333
### Fixed
3434

src/textual/events.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,11 @@ def __init__(self, node: DOMNode) -> None:
572572
"""The node directly under the mouse."""
573573
super().__init__()
574574

575+
@property
576+
def control(self) -> DOMNode:
577+
"""Alias for the `node` under the mouse."""
578+
return self.node
579+
575580

576581
class Leave(Event, bubble=True, verbose=True):
577582
"""Sent when the mouse is moved away from a widget, or if a widget is
@@ -592,6 +597,11 @@ def __init__(self, node: DOMNode) -> None:
592597
"""The node that was previously directly under the mouse."""
593598
super().__init__()
594599

600+
@property
601+
def control(self) -> DOMNode:
602+
"""Alias for the `node` that was previously under the mouse."""
603+
return self.node
604+
595605

596606
class Focus(Event, bubble=False):
597607
"""Sent when a widget is focussed.

tests/test_on.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from textual import on
88
from textual._on import OnDecoratorError
99
from textual.app import App, ComposeResult
10+
from textual.events import Enter, Leave
1011
from textual.message import Message
1112
from textual.widget import Widget
1213
from textual.widgets import Button, TabbedContent, TabPane
@@ -352,3 +353,28 @@ def on_mount(self) -> None:
352353
pass
353354

354355
assert posted == ["parent", "child", "parent"]
356+
357+
358+
async def test_on_with_enter_and_leave_events():
359+
class EnterLeaveApp(App):
360+
messages = []
361+
362+
def compose(self) -> ComposeResult:
363+
yield Button("OK")
364+
365+
@on(Enter, "Button")
366+
@on(Leave, "Button")
367+
def record(self, event: Enter | Leave) -> None:
368+
self.messages.append(event.__class__.__name__)
369+
370+
app = EnterLeaveApp()
371+
async with app.run_test() as pilot:
372+
expected_messages = []
373+
374+
await pilot.hover(Button)
375+
expected_messages.append("Enter")
376+
assert app.messages == expected_messages
377+
378+
await pilot.hover(Button, offset=(0, 20))
379+
expected_messages.append("Leave")
380+
assert app.messages == expected_messages

0 commit comments

Comments
 (0)