Skip to content

Commit

Permalink
Merge pull request #4847 from Textualize/auto-refresh
Browse files Browse the repository at this point in the history
don't refresh if not visible
  • Loading branch information
willmcgugan authored Aug 6, 2024
2 parents c1fb5df + 81667a4 commit beb4461
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Input cursor will no longer jump to the end on focus https://github.com/Textualize/textual/pull/4773
- Removed `Size.cip_size`, which was a clone of `crop_size`
- Widgets with auto dimensions will now grow if there is a scrollbar https://github.com/Textualize/textual/pull/4844
- Don't do automatic refresh when widget is not visible https://github.com/Textualize/textual/pull/4847
- Renamed `DOMNode._automatic_refresh` to `DOMNode.automatic_refresh` to allow for customization https://github.com/Textualize/textual/pull/4847

### Fixed

Expand Down
15 changes: 11 additions & 4 deletions src/textual/dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def auto_refresh(self, interval: float | None) -> None:
self._auto_refresh_timer = None
if interval is not None:
self._auto_refresh_timer = self.set_interval(
interval, self._automatic_refresh, name=f"auto refresh {self!r}"
interval, self.automatic_refresh, name=f"auto refresh {self!r}"
)
self._auto_refresh = interval

Expand Down Expand Up @@ -476,9 +476,16 @@ def is_modal(self) -> bool:
"""Is the node a modal?"""
return False

def _automatic_refresh(self) -> None:
"""Perform an automatic refresh (set with auto_refresh property)."""
self.refresh()
def automatic_refresh(self) -> None:
"""Perform an automatic refresh.
This method is called when you set the `auto_refresh` attribute.
You could implement this method if you want to perform additional work
during an automatic refresh.
"""
if self.display and self.visible:
self.refresh()

def __init_subclass__(
cls,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_auto_refresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def on_mount(self):
self.start = time()
self.auto_refresh = 0.1

def _automatic_refresh(self):
def automatic_refresh(self):
self.count += 1
if self.count == 3:
self.exit(time() - self.start)
super()._automatic_refresh()
super().automatic_refresh()


def test_auto_refresh():
Expand Down

0 comments on commit beb4461

Please sign in to comment.