Selaa lähdekoodia

FileManager: Handle drop events in DirectoryView

This makes it possible to drag & drop files to/from the desktop! :^)
Andreas Kling 4 vuotta sitten
vanhempi
commit
4e8c50d92d

+ 37 - 6
Applications/FileManager/DirectoryView.cpp

@@ -29,6 +29,7 @@
 #include <AK/LexicalPath.h>
 #include <AK/NumberFormat.h>
 #include <AK/StringBuilder.h>
+#include <LibCore/MimeData.h>
 #include <LibCore/StandardPaths.h>
 #include <LibGUI/InputBox.h>
 #include <LibGUI/MessageBox.h>
@@ -211,8 +212,7 @@ void DirectoryView::setup_icon_view()
             on_context_menu_request(index, event);
     };
     m_icon_view->on_drop = [this](auto& index, auto& event) {
-        if (on_drop)
-            on_drop(index, event);
+        handle_drop(index, event);
     };
 }
 
@@ -236,8 +236,7 @@ void DirectoryView::setup_columns_view()
     };
 
     m_columns_view->on_drop = [this](auto& index, auto& event) {
-        if (on_drop)
-            on_drop(index, event);
+        handle_drop(index, event);
     };
 }
 
@@ -262,8 +261,7 @@ void DirectoryView::setup_table_view()
     };
 
     m_table_view->on_drop = [this](auto& index, auto& event) {
-        if (on_drop)
-            on_drop(index, event);
+        handle_drop(index, event);
     };
 }
 
@@ -521,3 +519,36 @@ void DirectoryView::setup_actions()
         [this](auto&) { do_delete(false); },
         window());
 }
+
+void DirectoryView::handle_drop(const GUI::ModelIndex& index, const GUI::DropEvent& event)
+{
+    if (!event.mime_data().has_urls())
+        return;
+    auto urls = event.mime_data().urls();
+    if (urls.is_empty()) {
+        dbg() << "No files to drop";
+        return;
+    }
+
+    auto& target_node = node(index);
+    if (!target_node.is_directory())
+        return;
+
+    for (auto& url_to_copy : urls) {
+        if (!url_to_copy.is_valid() || url_to_copy.path() == target_node.full_path())
+            continue;
+        auto new_path = String::format("%s/%s",
+            target_node.full_path().characters(),
+            LexicalPath(url_to_copy.path()).basename().characters());
+
+        if (url_to_copy.path() == new_path)
+            continue;
+
+        if (!FileUtils::copy_file_or_directory(url_to_copy.path(), new_path)) {
+            auto error_message = String::format("Could not copy %s into %s.",
+                url_to_copy.to_string().characters(),
+                new_path.characters());
+            GUI::MessageBox::show(window(), error_message, "File Manager", GUI::MessageBox::Type::Error);
+        }
+    }
+};

+ 1 - 1
Applications/FileManager/DirectoryView.h

@@ -82,7 +82,6 @@ public:
     Function<void(const StringView& path, bool can_write_in_path)> on_path_change;
     Function<void(GUI::AbstractView&)> on_selection_change;
     Function<void(const GUI::ModelIndex&, const GUI::ContextMenuEvent&)> on_context_menu_request;
-    Function<void(const GUI::ModelIndex&, const GUI::DropEvent&)> on_drop;
     Function<void(const StringView&)> on_status_message;
     Function<void(int done, int total)> on_thumbnail_progress;
     Function<void(int error, const char* error_string, bool quit)> on_error;
@@ -147,6 +146,7 @@ private:
     GUI::FileSystemModel& model() { return *m_model; }
 
     void handle_selection_change();
+    void handle_drop(const GUI::ModelIndex&, const GUI::DropEvent&);
     void do_delete(bool should_confirm);
 
     // ^GUI::ModelClient

+ 0 - 34
Applications/FileManager/main.cpp

@@ -667,40 +667,6 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
         }
     };
 
-    directory_view.on_drop = [&](const GUI::ModelIndex& index, const GUI::DropEvent& event) {
-        if (!event.mime_data().has_urls())
-            return;
-        auto urls = event.mime_data().urls();
-        if (urls.is_empty()) {
-            dbg() << "No files to drop";
-            return;
-        }
-
-        auto& target_node = directory_view.node(index);
-        if (!target_node.is_directory())
-            return;
-
-        for (auto& url_to_copy : urls) {
-            if (!url_to_copy.is_valid() || url_to_copy.path() == target_node.full_path())
-                continue;
-            auto new_path = String::format("%s/%s",
-                target_node.full_path().characters(),
-                LexicalPath(url_to_copy.path()).basename().characters());
-
-            if (url_to_copy.path() == new_path)
-                continue;
-
-            if (!FileUtils::copy_file_or_directory(url_to_copy.path(), new_path)) {
-                auto error_message = String::format("Could not copy %s into %s.",
-                    url_to_copy.to_string().characters(),
-                    new_path.characters());
-                GUI::MessageBox::show(window, error_message, "File Manager", GUI::MessageBox::Type::Error);
-            } else {
-                refresh_tree_view();
-            }
-        }
-    };
-
     tree_view.on_selection = [&](const GUI::ModelIndex& index) {
         if (directories_model->m_previously_selected_index.is_valid())
             directories_model->update_node_on_selection(directories_model->m_previously_selected_index, false);