FileManager: Navigate to parent dir when current location is removed

When the location currently displayed in FileManager is removed, find
the nearest existing parent path and open it in the window.
Without the fix, the FileManager window remained in the deleted
directory.

Changing the path in 'DirectoryView' object will automatically update
other components in the FileManager (breadcrumb bar, directory tree
view).
This commit is contained in:
Adam Jakubek 2022-08-30 21:57:30 +02:00 committed by Sam Atkins
parent f7e6593910
commit aa466723eb
Notes: sideshowbarker 2024-07-17 07:37:03 +09:00
3 changed files with 15 additions and 0 deletions

View file

@ -196,6 +196,19 @@ void DirectoryView::setup_model()
on_path_change(model().root_path(), true, can_write_in_path);
};
m_model->on_root_path_removed = [this] {
// Change model root to the first existing parent directory.
LexicalPath model_root(model().root_path());
while (model_root.string() != "/") {
model_root = model_root.parent();
if (Core::File::is_directory(model_root.string()))
break;
}
open(model_root.string());
};
m_model->register_client(*this);
m_model->on_thumbnail_progress = [this](int done, int total) {

View file

@ -421,6 +421,7 @@ void FileSystemModel::handle_file_event(Core::FileWatcherEvent const& event)
if (&child.value() == m_root) {
// Root directory of the filesystem model has been removed. All items became invalid.
invalidate();
on_root_path_removed();
break;
}

View file

@ -120,6 +120,7 @@ public:
Function<void(int error, char const* error_string)> on_directory_change_error;
Function<void(int error, char const* error_string)> on_rename_error;
Function<void(String const& old_name, String const& new_name)> on_rename_successful;
Function<void()> on_root_path_removed;
virtual int tree_column() const override { return Column::Name; }
virtual int row_count(ModelIndex const& = ModelIndex()) const override;