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.
This commit is contained in:
Dave Pearson
2023-01-24 11:07:40 +00:00
parent 6743ec6a30
commit a36583612c

View File

@@ -201,26 +201,16 @@ class TreeNode(Generic[TreeDataType]):
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: