From 6de3f101d64a7e3e3c6a751f33ba6f1b49a65760 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Tue, 6 Aug 2024 10:45:07 +0100 Subject: [PATCH 1/2] don't refresh if not visible --- src/textual/dom.py | 15 +++++++++++---- tests/test_auto_refresh.py | 4 ++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/textual/dom.py b/src/textual/dom.py index f3f3b39566..f9d490f075 100644 --- a/src/textual/dom.py +++ b/src/textual/dom.py @@ -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 @@ -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, diff --git a/tests/test_auto_refresh.py b/tests/test_auto_refresh.py index c9eacf4690..fa6eaee956 100644 --- a/tests/test_auto_refresh.py +++ b/tests/test_auto_refresh.py @@ -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(): From 81667a4b5ff85d55184232f1d659459817cbce15 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Tue, 6 Aug 2024 10:48:15 +0100 Subject: [PATCH 2/2] readme --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 579ad68fd7..ca94f519c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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