From 17615641dbc1a857b381fdda3be8b13cb95ecc6e Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 23 Jun 2021 16:59:36 +0100 Subject: [PATCH] LibGUI: Navigate to parent when FileSystemModel directory is deleted Previously, FileSystemModel would not notice if the directory it has open (or a parent one) was deleted. Now, it scans for the closest existing parent directory and opens that. Also, deleted files and directories that are children of the open dir now correctly refresh their parent node instead of their own node. --- Userland/Libraries/LibGUI/FileSystemModel.cpp | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibGUI/FileSystemModel.cpp b/Userland/Libraries/LibGUI/FileSystemModel.cpp index bc0c6f4156..160a090ec4 100644 --- a/Userland/Libraries/LibGUI/FileSystemModel.cpp +++ b/Userland/Libraries/LibGUI/FileSystemModel.cpp @@ -261,10 +261,28 @@ FileSystemModel::FileSystemModel(String root_path, Mode mode) dbgln("Event at \"{}\" on Node {}: {}", node.full_path(), &node, event); // FIXME: Your time is coming, un-granular updates. - node.has_traversed = false; - node.mode = 0; - node.children.clear(); - node.reify_if_needed(); + auto refresh_node = [](Node& node) { + node.has_traversed = false; + node.mode = 0; + node.children.clear(); + node.reify_if_needed(); + }; + + if (event.type == Core::FileWatcherEvent::Type::Deleted) { + auto canonical_event_path = LexicalPath::canonicalized_path(event.event_path); + if (m_root_path.starts_with(canonical_event_path)) { + // Deleted directory contains our root, so navigate to our nearest parent. + auto new_path = LexicalPath(m_root_path).dirname(); + while (!Core::File::is_directory(new_path)) + new_path = LexicalPath(new_path).dirname(); + + set_root_path(new_path); + } else if (node.parent) { + refresh_node(*node.parent); + } + } else { + refresh_node(node); + } did_update(); };