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.
This commit is contained in:
Sam Atkins 2021-06-23 16:59:36 +01:00 committed by Ali Mohammad Pur
commit 17615641db
Notes: sideshowbarker 2024-07-18 11:10:25 +09:00

View file

@ -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();
};