From a36583612c78d21fe8bbd2cce2bb6da2caf70fe8 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 24 Jan 2023 11:07:40 +0000 Subject: [PATCH] Change TreeNode.toggle to act only from the source node See https://github.com/Textualize/textual/pull/1644#issuecomment-1401720808 where Darren raises the excellent point that while the "technically correct" approach that I had was... well, technically correct I guess (it toggled all the nodes from the target node down), it didn't have what was likely the desired effect. So this commit does away with the previous logic for doing the toggle and instead simply calls on expand or collapse depending on the state of the source node. --- src/textual/widgets/_tree.py | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/textual/widgets/_tree.py b/src/textual/widgets/_tree.py index aa03fcf3e2..bb94c8c013 100644 --- a/src/textual/widgets/_tree.py +++ b/src/textual/widgets/_tree.py @@ -201,26 +201,16 @@ def collapse(self, *, collapse_all: bool = False) -> None: self._collapse(collapse_all) self._tree._invalidate() - def _toggle(self, toggle_all: bool) -> None: - """Toggle the expanded state of the node. - - Args: - toggle_all: If `True` toggle all offspring at all depths. - """ - self._expanded = not self._expanded - if toggle_all: - for child in self.children: - child._toggle(toggle_all) - self._updates += 1 - def toggle(self, *, toggle_all: bool = False) -> None: """Toggle the expanded state. Args: toggle_all: If `True` toggle all offspring at all depths. """ - self._toggle(toggle_all) - self._tree._invalidate() + if self._expanded: + self.collapse(collapse_all=toggle_all) + else: + self.expand(expand_all=toggle_all) @property def label(self) -> TextType: