From 4c5defe778b1a45d7706eaab2ec3e18d4a0bccca Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 2 Jan 2024 11:19:39 +0000 Subject: [PATCH 1/3] Remember Tree.root's expanded state when performing Tree.clear See #3557. --- src/textual/widgets/_tree.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/textual/widgets/_tree.py b/src/textual/widgets/_tree.py index 5a4ca1f9b7..501efefd66 100644 --- a/src/textual/widgets/_tree.py +++ b/src/textual/widgets/_tree.py @@ -717,13 +717,14 @@ def clear(self) -> Self: self._current_id = 0 root_label = self.root._label root_data = self.root.data + root_expanded = self.root.is_expanded self.root = TreeNode( self, None, self._new_id(), root_label, root_data, - expanded=True, + expanded=root_expanded, ) self._updates += 1 self.refresh() From d5765de863ec109c77e70c282dd06e51f76d5249 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 2 Jan 2024 12:11:09 +0000 Subject: [PATCH 2/3] :sparkles: Add snapshot tests for Tree.node/clear expansion --- .../__snapshots__/test_snapshots.ambr | 157 ++++++++++++++++++ .../snapshot_apps/tree_clearing.py | 30 ++++ tests/snapshot_tests/test_snapshots.py | 7 + 3 files changed, 194 insertions(+) create mode 100644 tests/snapshot_tests/snapshot_apps/tree_clearing.py diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr index 246fc4c553..a6be20b3bb 100644 --- a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr +++ b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr @@ -37109,6 +37109,163 @@ ''' # --- +# name: test_tree_clearing_and_expansion + ''' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + TreeClearingSnapshotApp + + + + + + + + + + ▼ Left▶ Right + + + + + + + + + + + + + + + + + + + + + + + + + + + + ''' +# --- # name: test_tree_example ''' diff --git a/tests/snapshot_tests/snapshot_apps/tree_clearing.py b/tests/snapshot_tests/snapshot_apps/tree_clearing.py new file mode 100644 index 0000000000..0d867fdf74 --- /dev/null +++ b/tests/snapshot_tests/snapshot_apps/tree_clearing.py @@ -0,0 +1,30 @@ +from textual.app import App, ComposeResult +from textual.widgets import Tree + +class TreeClearingSnapshotApp(App[None]): + + CSS = """ + Screen { + layout: horizontal; + } + """ + + @staticmethod + def _populate(tree: Tree) -> Tree: + for n in range(5): + branch = tree.root.add(str(n)) + for m in range(5): + branch.add_leaf(f"{n}-{m}") + return tree + + def compose(self) -> ComposeResult: + yield self._populate(Tree("Left", id="left")) + yield self._populate(Tree("Right", id="right")) + + def on_mount(self) -> None: + self.query_one("#left", Tree).root.expand() + self.query_one("#left", Tree).clear() + self.query_one("#right", Tree).clear() + +if __name__ == "__main__": + TreeClearingSnapshotApp().run() diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index 15bdd015a9..0b9e309474 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -967,3 +967,10 @@ def test_zero_scrollbar_size(snap_compare): """Regression test for missing content with 0 sized scrollbars""" # https://github.com/Textualize/textual/issues/3886 assert snap_compare(SNAPSHOT_APPS_DIR / "zero_scrollbar_size.py") + + +def test_tree_clearing_and_expansion(snap_compare): + """Test the Tree.root.is_expanded state after a Tree.clear""" + # https://github.com/Textualize/textual/issues/3557 + assert snap_compare(SNAPSHOT_APPS_DIR / "tree_clearing.py") + From 5f5758c51f6611a93ecbbce64d2d9eb65c54f407 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 2 Jan 2024 12:13:47 +0000 Subject: [PATCH 3/3] Update the ChangeLog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16298bde83..c872b70c9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - `Widget.move_child` would break if `before`/`after` is set to the index of the widget in `child` https://github.com/Textualize/textual/issues/1743 - Fixed auto width text not processing markup https://github.com/Textualize/textual/issues/3918 +- Fixed `Tree.clear` not retaining the root's expanded state https://github.com/Textualize/textual/issues/3557 ### Changed