From 32b7308ac83c20c49ca422726be149fdc5b8fc2d Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Mon, 19 Dec 2022 12:54:06 +0000 Subject: [PATCH] fox for nested heights --- src/textual/widget.py | 10 ++- .../snapshot_apps/nested_auto_heights.py | 64 +++++++++++++++++++ tests/snapshot_tests/test_snapshots.py | 9 ++- 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 tests/snapshot_tests/snapshot_apps/nested_auto_heights.py diff --git a/src/textual/widget.py b/src/textual/widget.py index 1d5f4365b..2d9ed4e8f 100644 --- a/src/textual/widget.py +++ b/src/textual/widget.py @@ -2175,8 +2175,14 @@ class Widget(DOMNode): if layout: self._layout_required = True - if isinstance(self._parent, Widget): - self._parent._clear_arrangement_cache() + for ancestor in self.ancestors: + if not isinstance(ancestor, Widget): + break + if ancestor.styles.auto_dimensions: + for ancestor in self.ancestors_with_self: + if isinstance(ancestor, Widget): + ancestor._clear_arrangement_cache() + break if repaint: self._set_dirty(*regions) diff --git a/tests/snapshot_tests/snapshot_apps/nested_auto_heights.py b/tests/snapshot_tests/snapshot_apps/nested_auto_heights.py new file mode 100644 index 000000000..5df6c2960 --- /dev/null +++ b/tests/snapshot_tests/snapshot_apps/nested_auto_heights.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 + +from __future__ import annotations + +from textual.app import App, ComposeResult +from textual.containers import Vertical +from textual.widgets import Static + + +class NestedAutoApp(App[None]): + CSS = """ + Screen { + background: red; + } + + #my-static-container { + border: heavy lightgreen; + background: green; + height: auto; + max-height: 10; + } + + #my-static-wrapper { + border: heavy lightblue; + background: blue; + width: auto; + height: auto; + } + + #my-static { + border: heavy gray; + background: black; + width: auto; + height: auto; + } + """ + BINDINGS = [ + ("1", "1", "1"), + ("2", "2", "2"), + ("q", "quit", "Quit"), + ] + + def compose(self) -> ComposeResult: + self._static = Static("", id="my-static") + yield Vertical( + Vertical( + self._static, + id="my-static-wrapper", + ), + id="my-static-container", + ) + + def action_1(self) -> None: + self._static.update( + "\n".join(f"Lorem {i} Ipsum {i} Sit {i}" for i in range(1, 21)) + ) + + def action_2(self) -> None: + self._static.update("JUST ONE LINE") + + +if __name__ == "__main__": + app = NestedAutoApp() + app.run() diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index 88fccaaf5..c3fb1d19d 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -101,7 +101,9 @@ def test_header_render(snap_compare): def test_list_view(snap_compare): - assert snap_compare(WIDGET_EXAMPLES_DIR / "list_view.py", press=["tab", "down", "down", "up"]) + assert snap_compare( + WIDGET_EXAMPLES_DIR / "list_view.py", press=["tab", "down", "down", "up"] + ) def test_textlog_max_lines(snap_compare): @@ -160,6 +162,11 @@ def test_offsets(snap_compare): assert snap_compare("snapshot_apps/offsets.py") +def test_nested_auto_heights(snap_compare): + """Test refreshing widget within a auto sized container""" + assert snap_compare("snapshot_apps/nested_auto_heights.py", press=["1", "2"]) + + # --- Other ---