diff --git a/CHANGELOG.md b/CHANGELOG.md index f72cc25131..d559902246 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Added `restrict`, `type`, `max_length`, and `valid_empty` to Input https://github.com/Textualize/textual/pull/3657 - Added `Pilot.mouse_down` to simulate `MouseDown` events https://github.com/Textualize/textual/pull/3495 - Added `Pilot.mouse_up` to simulate `MouseUp` events https://github.com/Textualize/textual/pull/3495 +- Added `Widget.is_mounted` property https://github.com/Textualize/textual/pull/3709 ### Changed diff --git a/src/textual/widget.py b/src/textual/widget.py index 98c605fe50..ac5da1e04d 100644 --- a/src/textual/widget.py +++ b/src/textual/widget.py @@ -393,6 +393,11 @@ def __init__( border_subtitle: str | Text | None = _BorderTitle() # type: ignore """A title to show in the bottom border (if there is one).""" + @property + def is_mounted(self) -> bool: + """Check if this widget is mounted.""" + return self._mounted_event.is_set() + @property def siblings(self) -> list[Widget]: """Get the widget's siblings (self is removed from the return list). diff --git a/tests/test_widget.py b/tests/test_widget.py index 4b0058aafe..43f844c430 100644 --- a/tests/test_widget.py +++ b/tests/test_widget.py @@ -383,3 +383,14 @@ def compose(self) -> ComposeResult: label.loading = False # Setting to same value is a null-op await pilot.pause() assert len(label.query(LoadingIndicator)) == 0 + + +async def test_is_mounted_property(): + class TestWidgetIsMountedApp(App): + pass + + async with TestWidgetIsMountedApp().run_test() as pilot: + widget = Widget() + assert widget.is_mounted is False + await pilot.app.mount(widget) + assert widget.is_mounted is True