Refactor expand_directory_tree and make it a method

This commit is contained in:
Isaiah Odhner 2023-04-15 20:39:00 -04:00
parent ce2fbe44bb
commit 7d433cc27b

View File

@ -718,14 +718,16 @@ class PaintApp(App):
Button("Cancel", id="save_as_cancel_button"), Button("Cancel", id="save_as_cancel_button"),
) )
self.mount(window) self.mount(window)
def expand_directory_tree(): self.expand_directory_tree(window.content.query_one("#save_as_directory_tree"))
def expand_directory_tree(self, tree: DirectoryTree) -> None:
"""Expand the directory tree to the target directory, either the folder of the open file or the current working directory.""" """Expand the directory tree to the target directory, either the folder of the open file or the current working directory."""
target_dirs = (self.filename or os.getcwd()).split(os.path.sep) # TODO: os.path.normcase, and maybe os.path.samefile check
tree = window.content.query_one("#save_as_directory_tree") target_dir = (self.filename or os.getcwd()).rstrip(os.path.sep)
node = tree.root node = tree.root
def get_node_name(node): def get_node_name(node):
return os.path.basename(node.data.path.rstrip(os.path.sep)) return os.path.basename(node.data.path.rstrip(os.path.sep))
for dir_name in target_dirs: for dir_name in target_dir.split(os.path.sep):
# Find the child node with the right name. # Find the child node with the right name.
for child in node.children: for child in node.children:
if get_node_name(child) == dir_name: if get_node_name(child) == dir_name:
@ -737,7 +739,7 @@ class PaintApp(App):
# load_directory also calls node.expand() # load_directory also calls node.expand()
tree.load_directory(node) tree.load_directory(node)
else: else:
# Found file. # Found a file.
break break
else: else:
# Directory or file not found. # Directory or file not found.
@ -761,8 +763,6 @@ class PaintApp(App):
# Timer is needed to wait for the new nodes to mount, I think. # Timer is needed to wait for the new nodes to mount, I think.
self.set_timer(0.01, lambda: tree.scroll_to_region(tree._get_label_region(node._line), animate=False, top=True)) self.set_timer(0.01, lambda: tree.scroll_to_region(tree._get_label_region(node._line), animate=False, top=True))
expand_directory_tree()
def confirm_overwrite(self, filename: str, callback) -> None: def confirm_overwrite(self, filename: str, callback) -> None:
for old_window in self.query("#overwrite_dialog").nodes: for old_window in self.query("#overwrite_dialog").nodes:
old_window.close() old_window.close()