FileManager: Copy item(s) when dragging and dropping them :^)

This patch implements basic drag & drop file management in a narrow set
of cases. You can now drag & drop a file onto a folder in the same
directory, and the dropped file will be copied into the directory.

We'll need to support a lot more variations of this, but this is nice!
This commit is contained in:
Andreas Kling 2020-02-13 21:55:05 +01:00
parent 95fe78667f
commit 4dc15fc063
Notes: sideshowbarker 2024-07-19 09:20:44 +09:00
3 changed files with 49 additions and 0 deletions

View file

@ -200,6 +200,19 @@ DirectoryView::DirectoryView(GUI::Widget* parent)
on_context_menu_request(*m_columns_view, index, event);
};
m_table_view->on_drop = [this](auto& index, auto& event) {
if (on_drop)
on_drop(*m_table_view, index, event);
};
m_item_view->on_drop = [this](auto& index, auto& event) {
if (on_drop)
on_drop(*m_item_view, index, event);
};
m_columns_view->on_drop = [this](auto& index, auto& event) {
if (on_drop)
on_drop(*m_columns_view, index, event);
};
set_view_mode(ViewMode::Icon);
}

View file

@ -52,6 +52,7 @@ public:
Function<void(const StringView&)> on_path_change;
Function<void(GUI::AbstractView&)> on_selection_change;
Function<void(const GUI::AbstractView&, const GUI::ModelIndex&, const GUI::ContextMenuEvent&)> on_context_menu_request;
Function<void(const GUI::AbstractView&, const GUI::ModelIndex&, const GUI::DropEvent&)> on_drop;
Function<void(const StringView&)> on_status_message;
Function<void(int done, int total)> on_thumbnail_progress;

View file

@ -29,6 +29,7 @@
#include "PropertiesDialog.h"
#include <AK/FileSystemPath.h>
#include <AK/StringBuilder.h>
#include <AK/URL.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/UserInfo.h>
#include <LibGUI/AboutDialog.h>
@ -571,6 +572,40 @@ int main(int argc, char** argv)
}
};
directory_view->on_drop = [&](const GUI::AbstractView&, const GUI::ModelIndex& index, const GUI::DropEvent& event) {
if (!index.is_valid())
return;
if (event.data_type() != "url-list")
return;
auto paths_to_copy = event.data().split('\n');
if (paths_to_copy.is_empty()) {
dbg() << "No files to drop";
return;
}
auto& target_node = directory_view->model().node(index);
if (!target_node.is_directory())
return;
for (auto& path_to_copy : paths_to_copy) {
auto url_to_copy = URL(path_to_copy);
if (!url_to_copy.is_valid())
continue;
auto new_path = String::format("%s/%s",
target_node.full_path(directory_view->model()).characters(),
FileSystemPath(url_to_copy.path()).basename().characters());
if (!FileUtils::copy_file_or_directory(url_to_copy.path(), new_path)) {
auto error_message = String::format("Could not copy %s into %s.",
path_to_copy.characters(),
new_path.characters());
GUI::MessageBox::show(error_message, "File Manager", GUI::MessageBox::Type::Error);
} else {
refresh_tree_view();
}
}
};
tree_view->on_selection_change = [&] {
auto path = directories_model->full_path(tree_view->selection().first());
if (directory_view->path() == path)