No future for this approach...

This commit is contained in:
Isaiah Odhner 2023-06-27 13:21:54 -04:00
parent e1111a24c6
commit 6db2b53830

View File

@ -1,3 +1,4 @@
from asyncio import Future
from pathlib import Path
from typing import Iterable
@ -10,16 +11,18 @@ class EnhancedDirectoryTree(DirectoryTree):
def filter_paths(self, paths: Iterable[Path]) -> Iterable[Path]:
return [path for path in paths if not (path.name.startswith(".") or path.name.endswith("~") or path.name.startswith("~"))]
def _go_to_node(self, node: TreeNode[DirEntry]) -> None:
def _go_to_node(self, node: TreeNode[DirEntry], future: Future[None]) -> None:
"""Scroll to the node, and select it."""
def _go_to_node_now():
self.select_node(node)
region = self._get_label_region(node._line) # type: ignore
assert region, "Node not found in tree"
self.scroll_to_region(region, animate=False, top=True)
future.set_result(None)
# Is there a way to avoid this delay?
self.set_timer(0.01, _go_to_node_now)
def _expand_matching_child(self, node: TreeNode[DirEntry], remaining_parts: tuple[str]) -> None:
def _expand_matching_child(self, node: TreeNode[DirEntry], remaining_parts: tuple[str], future: Future[None]) -> None:
"""Hooks into DirectoryTree's add method, and expands the child node matching the next path part, recursively.
Once the last part of the path is reached, it scrolls to and selects the node.
@ -37,13 +40,13 @@ class EnhancedDirectoryTree(DirectoryTree):
if node.data.path.is_dir():
sliced_parts = remaining_parts[1:]
# print("recursing with sliced_parts", sliced_parts)
_expand_matching_child(node, sliced_parts) # type: ignore
_expand_matching_child(node, sliced_parts, future) # type: ignore
_add_to_load_queue(node)
# else:
# print("Found a file, not as last part of path:", node.data.path, "remaining_parts:", remaining_parts)
else:
# print("scrolling", node)
_go_to_node(node)
_go_to_node(node, future)
# If the target path is a directory, expand it.
# This might not always be desired, for a general API,
# but for File > New, File > Open, it should expand the current directory.
@ -62,7 +65,7 @@ class EnhancedDirectoryTree(DirectoryTree):
expand_if_match(node)
# Note: The hook is left indefinitely.
# It may be worth some consideration as to whether this could pose any problems.
# However, we can't just reset to orig_add here since it would only handle the first added node.
# However, we can't just reset to orig_add here since that would make this only handle the first added node.
return node
# Hook the add method to handle new nodes.
node.add = add.__get__(node, type(node))
@ -70,7 +73,7 @@ class EnhancedDirectoryTree(DirectoryTree):
for child in node.children:
expand_if_match(child)
def expand_to_path(self, target_path: str | Path) -> None:
async def expand_to_path(self, target_path: str | Path) -> None:
"""Expand the directory tree to the target path, loading any directories as needed."""
target_path = Path(target_path)
@ -81,7 +84,8 @@ class EnhancedDirectoryTree(DirectoryTree):
# - I should test it with symbolic links, and UNC paths on Windows.
# - There should be an attribute to expand to a path initially.
# - Maybe this method should return the node if it was found.
# - Definitely want to figure out how to avoid the timers.
self._expand_matching_child(self.root, target_path.parts[1:])
future: Future[None] = Future()
self._expand_matching_child(self.root, target_path.parts[1:], future)
await future