From 32790de26a958e104d94f39fc12c146ad0bc0f0f Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Thu, 25 May 2023 20:49:50 +0100 Subject: [PATCH 1/2] Ensure a node has no children before populating it I'm struggling to recreate https://github.com/Textualize/frogmouth/issues/50 in a controlled way, but reviewing the code here makes me think that this is a good idea anyway. While DirectoryTree should not end up in _populate_node if a node has already been populated, it's also the case that it's an all-or-nothing thing; it makes sense to clear out the children of the node before populating it; at least in a belt-and-braces way. --- src/textual/widgets/_directory_tree.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/textual/widgets/_directory_tree.py b/src/textual/widgets/_directory_tree.py index e6b81abee..c0bb2c505 100644 --- a/src/textual/widgets/_directory_tree.py +++ b/src/textual/widgets/_directory_tree.py @@ -271,6 +271,7 @@ class DirectoryTree(Tree[DirEntry]): node: The Tree node to populate. content: The collection of `Path` objects to populate the node with. """ + node.remove_children() for path in content: node.add( path.name, From ec3334e6336d0b58ba2b31f8f38fdb5ea5e12792 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Thu, 25 May 2023 20:55:22 +0100 Subject: [PATCH 2/2] Perform the "is loaded" test in _add_to_load_queue The test if a node was loaded wasn't being performed when loading the root. This ensures that will happen. I suspect this is (no pun...) at the root of the issue with https://github.com/Textualize/frogmouth/issues/50 even though I can't see the route into how this happens, and can't recreate this at will. This feels like a worthwhile change to make anyway as it's a safer approach. --- src/textual/widgets/_directory_tree.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/textual/widgets/_directory_tree.py b/src/textual/widgets/_directory_tree.py index c0bb2c505..8ea138a57 100644 --- a/src/textual/widgets/_directory_tree.py +++ b/src/textual/widgets/_directory_tree.py @@ -131,8 +131,9 @@ class DirectoryTree(Tree[DirEntry]): node: The node to add to the load queue. """ assert node.data is not None - node.data.loaded = True - self._load_queue.put_nowait(node) + if not node.data.loaded: + node.data.loaded = True + self._load_queue.put_nowait(node) def reload(self) -> None: """Reload the `DirectoryTree` contents.""" @@ -351,8 +352,7 @@ class DirectoryTree(Tree[DirEntry]): if dir_entry is None: return if self._safe_is_dir(dir_entry.path): - if not dir_entry.loaded: - self._add_to_load_queue(event.node) + self._add_to_load_queue(event.node) else: self.post_message(self.FileSelected(event.node, dir_entry.path))