Skip to content

Commit

Permalink
feat(widget): add is_mounted property (#3709)
Browse files Browse the repository at this point in the history
* feat(widget): add is_mounted property

Co-authored-by: Darren Burns <[email protected]>

* fix property docstring

* add simple test

* update changelog

---------

Co-authored-by: Darren Burns <[email protected]>
  • Loading branch information
TomJGooding and darrenburns authored Nov 20, 2023
1 parent ca534a0 commit ca68b86
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
11 changes: 11 additions & 0 deletions tests/test_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ca68b86

Please sign in to comment.