diff --git a/CHANGELOG.md b/CHANGELOG.md index 2df2b1993d..8c86abcb13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Tooltips are now cleared when the related widget is no longer under them https://github.com/Textualize/textual/issues/3045 - Simplified tree-sitter highlight queries for HTML, which also seems to fix segfault issue https://github.com/Textualize/textual/pull/4195 - Fixed `DirectoryTree.path` no longer reacting to new values https://github.com/Textualize/textual/issues/4208 +- Fixed content size cache with Pretty widget https://github.com/Textualize/textual/pull/4211 ## [0.52.1] - 2024-02-20 diff --git a/src/textual/widget.py b/src/textual/widget.py index 157e0cb930..665e50ab01 100644 --- a/src/textual/widget.py +++ b/src/textual/widget.py @@ -577,6 +577,19 @@ def __exit__( else: self.app._composed[-1].append(composed) + def clear_cached_dimensions(self) -> None: + """Clear cached results of `get_content_width` and `get_content_height`. + + Call if the widget's renderable changes size after the widget has been created. + + !!! note + + This is not required if you are extending [`Static`][textual.widgets.Static]. + + """ + self._content_width_cache = (None, 0) + self._content_height_cache = (None, 0) + def get_loading_widget(self) -> Widget: """Get a widget to display a loading indicator. @@ -3278,8 +3291,7 @@ def refresh( if repaint: self._set_dirty(*regions) - self._content_width_cache = (None, 0) - self._content_height_cache = (None, 0) + self.clear_cached_dimensions() self._rich_style_cache.clear() self._repaint_required = True diff --git a/src/textual/widgets/_pretty.py b/src/textual/widgets/_pretty.py index 34d88b3964..ffa89819d8 100644 --- a/src/textual/widgets/_pretty.py +++ b/src/textual/widgets/_pretty.py @@ -59,4 +59,5 @@ def update(self, object: Any) -> None: object: The object to pretty-print. """ self._renderable = PrettyRenderable(object) + self.clear_cached_dimensions() self.refresh(layout=True) diff --git a/src/textual/widgets/_static.py b/src/textual/widgets/_static.py index a9f485345b..3ccd43c3df 100644 --- a/src/textual/widgets/_static.py +++ b/src/textual/widgets/_static.py @@ -78,6 +78,7 @@ def renderable(self, renderable: RenderableType) -> None: self._renderable = Text(renderable) else: self._renderable = renderable + self.clear_cached_dimensions() def render(self) -> RenderableType: """Get a rich renderable for the widget's content.