Skip to content

Commit

Permalink
LibGUI: Navigate to parent when FileSystemModel directory is deleted
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AtkinsSJ authored and alimpfard committed Jul 1, 2021
1 parent 0fa141d commit 1761564
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions Userland/Libraries/LibGUI/FileSystemModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

Expand Down

0 comments on commit 1761564

Please sign in to comment.