mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Simplify _load_directory
Move the node population code into its own method, the idea here being that the update happens in one call to call_from_thread rather than spawning lots of calls to it.
This commit is contained in:
@@ -250,6 +250,23 @@ class DirectoryTree(Tree[DirEntry]):
|
|||||||
yield entry
|
yield entry
|
||||||
time.sleep(0.05)
|
time.sleep(0.05)
|
||||||
|
|
||||||
|
def _populate_node(
|
||||||
|
self, node: TreeNode[DirEntry], directory: Iterable[Path]
|
||||||
|
) -> None:
|
||||||
|
"""Populate the given node with the contents of a directory.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
node: The node to populate.
|
||||||
|
directory: The directory contents to populate it with.
|
||||||
|
"""
|
||||||
|
for path in directory:
|
||||||
|
node.add(
|
||||||
|
path.name,
|
||||||
|
data=DirEntry(path),
|
||||||
|
allow_expand=path.is_dir(),
|
||||||
|
)
|
||||||
|
node.expand()
|
||||||
|
|
||||||
@work
|
@work
|
||||||
def _load_directory(self, node: TreeNode[DirEntry]) -> None:
|
def _load_directory(self, node: TreeNode[DirEntry]) -> None:
|
||||||
"""Load the directory contents for a given node.
|
"""Load the directory contents for a given node.
|
||||||
@@ -259,18 +276,14 @@ class DirectoryTree(Tree[DirEntry]):
|
|||||||
"""
|
"""
|
||||||
assert node.data is not None
|
assert node.data is not None
|
||||||
node.data.loaded = True
|
node.data.loaded = True
|
||||||
directory = sorted(
|
self.app.call_from_thread(
|
||||||
|
self._populate_node,
|
||||||
|
node,
|
||||||
|
sorted(
|
||||||
self.filter_paths(self._directory_content(node.data.path)),
|
self.filter_paths(self._directory_content(node.data.path)),
|
||||||
key=lambda path: (not path.is_dir(), path.name.lower()),
|
key=lambda path: (not path.is_dir(), path.name.lower()),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
for path in directory:
|
|
||||||
self.app.call_from_thread(
|
|
||||||
node.add,
|
|
||||||
path.name,
|
|
||||||
data=DirEntry(path),
|
|
||||||
allow_expand=path.is_dir(),
|
|
||||||
)
|
|
||||||
self.app.call_from_thread(node.expand)
|
|
||||||
|
|
||||||
def _on_mount(self, _: Mount) -> None:
|
def _on_mount(self, _: Mount) -> None:
|
||||||
self._load_directory(self.root)
|
self._load_directory(self.root)
|
||||||
|
|||||||
Reference in New Issue
Block a user