Skip to content

Commit 54ba357

Browse files
authored
Event control (#3099)
* Add control * added control * post to parent
1 parent a0be460 commit 54ba357

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2121
- Added widgets.Digit https://github.com/Textualize/textual/pull/3073
2222
- Added `BORDER_TITLE` and `BORDER_SUBTITLE` classvars to Widget https://github.com/Textualize/textual/pull/3097
2323

24+
### Changed
25+
- DescendantBlur and DescendantFocus can now be used with @on decorator
26+
2427
## [0.32.0] - 2023-08-03
2528

2629
### Added

src/textual/events.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from __future__ import annotations
1515

16+
from dataclasses import dataclass
1617
from typing import TYPE_CHECKING, Type, TypeVar
1718

1819
import rich.repr
@@ -547,21 +548,39 @@ class Blur(Event, bubble=False):
547548
"""
548549

549550

551+
@dataclass
550552
class DescendantFocus(Event, bubble=True, verbose=True):
551553
"""Sent when a child widget is focussed.
552554
553555
- [X] Bubbles
554556
- [X] Verbose
555557
"""
556558

559+
widget: Widget
560+
"""The widget that was focused."""
557561

562+
@property
563+
def control(self) -> Widget:
564+
"""The widget that was focused (alias of `widget`)."""
565+
return self.widget
566+
567+
568+
@dataclass
558569
class DescendantBlur(Event, bubble=True, verbose=True):
559570
"""Sent when a child widget is blurred.
560571
561572
- [X] Bubbles
562573
- [X] Verbose
563574
"""
564575

576+
widget: Widget
577+
"""The widget that was blurred."""
578+
579+
@property
580+
def control(self) -> Widget:
581+
"""The widget that was blurred (alias of `widget`)."""
582+
return self.widget
583+
565584

566585
@rich.repr.auto
567586
class Paste(Event, bubble=True):

src/textual/widget.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3290,12 +3290,14 @@ def _on_enter(self, event: events.Enter) -> None:
32903290
def _on_focus(self, event: events.Focus) -> None:
32913291
self.has_focus = True
32923292
self.refresh()
3293-
self.post_message(events.DescendantFocus())
3293+
if self.parent is not None:
3294+
self.parent.post_message(events.DescendantFocus(self))
32943295

32953296
def _on_blur(self, event: events.Blur) -> None:
32963297
self.has_focus = False
32973298
self.refresh()
3298-
self.post_message(events.DescendantBlur())
3299+
if self.parent is not None:
3300+
self.parent.post_message(events.DescendantBlur(self))
32993301

33003302
def _on_mouse_scroll_down(self, event: events.MouseScrollDown) -> None:
33013303
if event.ctrl or event.shift:

0 commit comments

Comments
 (0)