mirror of
https://github.com/RGBCube/serenity
synced 2025-05-22 19:45:08 +00:00
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:
parent
95fe78667f
commit
4dc15fc063
3 changed files with 49 additions and 0 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue