From 24d341fadd14016288fe227de593fe1987a173c8 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Mon, 26 Feb 2024 11:08:10 +0000 Subject: [PATCH] call clear cached dimensions --- src/textual/widget.py | 16 ++++++++++++++-- src/textual/widgets/_pretty.py | 1 + src/textual/widgets/_static.py | 3 +-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/textual/widget.py b/src/textual/widget.py index 034f73d191..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 e26fb13897..3ccd43c3df 100644 --- a/src/textual/widgets/_static.py +++ b/src/textual/widgets/_static.py @@ -78,8 +78,7 @@ def renderable(self, renderable: RenderableType) -> None: self._renderable = Text(renderable) else: self._renderable = renderable - self._content_width_cache = (None, 0) - self._content_height_cache = (None, 0) + self.clear_cached_dimensions() def render(self) -> RenderableType: """Get a rich renderable for the widget's content.