Browse Source

Support dragging multiple files

Cheng Zhao 8 years ago
parent
commit
fa468a529b

+ 26 - 3
atom/browser/api/atom_api_web_contents.cc

@@ -1206,11 +1206,34 @@ void WebContents::EndFrameSubscription() {
     view->EndFrameSubscription();
 }
 
-void WebContents::StartDrag(const base::FilePath& file,
-                            mate::Handle<NativeImage> image) {
+void WebContents::StartDrag(const mate::Dictionary& item,
+                            mate::Arguments* args) {
   base::MessageLoop::ScopedNestableTaskAllower allow(
       base::MessageLoop::current());
-  DragItem(file, image->image(), web_contents()->GetNativeView());
+
+  base::FilePath file;
+  std::vector<base::FilePath> files;
+  if (!item.Get("files", &files) && item.Get("file", &file)) {
+    files.push_back(file);
+  }
+
+  mate::Handle<NativeImage> icon;
+  if (!item.Get("icon", &icon) && !file.empty()) {
+    // TODO(zcbenz): Set default icon from file.
+  }
+
+  // Error checking.
+  if (icon.IsEmpty()) {
+    args->ThrowError("icon must be set");
+    return;
+  }
+
+  // Start dragging.
+  if (!files.empty()) {
+    DragFileItems(files, icon->image(), web_contents()->GetNativeView());
+  } else {
+    args->ThrowError("There is nothing to drag");
+  }
 }
 
 void WebContents::OnCursorChange(const content::WebCursor& cursor) {

+ 1 - 3
atom/browser/api/atom_api_web_contents.h

@@ -39,8 +39,6 @@ class WebViewGuestDelegate;
 
 namespace api {
 
-class NativeImage;
-
 class WebContents : public mate::TrackableObject<WebContents>,
                     public CommonWebContentsDelegate,
                     public content::WebContentsObserver {
@@ -145,7 +143,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
   void EndFrameSubscription();
 
   // Dragging native items.
-  void StartDrag(const base::FilePath& file, mate::Handle<NativeImage> image);
+  void StartDrag(const mate::Dictionary& item, mate::Arguments* args);
 
   // Methods for creating <webview>.
   void SetSize(const SetSizeParams& params);

+ 5 - 3
atom/browser/ui/drag_util.h

@@ -5,6 +5,8 @@
 #ifndef ATOM_BROWSER_UI_DRAG_UTIL_H_
 #define ATOM_BROWSER_UI_DRAG_UTIL_H_
 
+#include <vector>
+
 #include "ui/gfx/image/image.h"
 
 namespace base {
@@ -13,9 +15,9 @@ class FilePath;
 
 namespace atom {
 
-void DragItem(const base::FilePath& path,
-              const gfx::Image& icon,
-              gfx::NativeView view);
+void DragFileItems(const std::vector<base::FilePath>& files,
+                   const gfx::Image& icon,
+                   gfx::NativeView view);
 
 }  // namespace atom
 

+ 9 - 7
atom/browser/ui/drag_util_mac.mm

@@ -13,9 +13,11 @@ namespace atom {
 namespace {
 
 // Write information about the file being dragged to the pasteboard.
-void AddFileToPasteboard(NSPasteboard* pasteboard, const base::FilePath& path) {
-  NSString* file = base::SysUTF8ToNSString(path.value());
-  NSArray* fileList = [NSArray arrayWithObject:file];
+void AddFilesToPasteboard(NSPasteboard* pasteboard,
+                          const std::vector<base::FilePath>& files) {
+  NSMutableArray* fileList = [NSMutableArray array];
+  for (const base::FilePath& file : files)
+    [fileList addObject:base::SysUTF8ToNSString(file.value())];
   [pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType]
                      owner:nil];
   [pasteboard setPropertyList:fileList forType:NSFilenamesPboardType];
@@ -23,11 +25,11 @@ void AddFileToPasteboard(NSPasteboard* pasteboard, const base::FilePath& path) {
 
 }  // namespace
 
-void DragItem(const base::FilePath& path,
-              const gfx::Image& icon,
-              gfx::NativeView view) {
+void DragFileItems(const std::vector<base::FilePath>& files,
+                   const gfx::Image& icon,
+                   gfx::NativeView view) {
   NSPasteboard* pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
-  AddFileToPasteboard(pasteboard, path);
+  AddFilesToPasteboard(pasteboard, files);
 
   // Synthesize a drag event, since we don't have access to the actual event
   // that initiated a drag (possibly consumed by the Web UI, for example).