mac: Implement webContents.startDrag
authorCheng Zhao <zcbenz@gmail.com>
Sun, 3 Jul 2016 03:26:43 +0000 (12:26 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Sun, 3 Jul 2016 03:26:43 +0000 (12:26 +0900)
atom/browser/api/atom_api_web_contents.cc
atom/browser/api/atom_api_web_contents.h
atom/browser/ui/drag_util.h [new file with mode: 0644]
atom/browser/ui/drag_util_mac.mm [new file with mode: 0644]
atom/browser/ui/drag_util_views.cc [new file with mode: 0644]
filenames.gypi

index e6959c887111e1751ea5af02ccc05696f6b2916f..370cac33fcb392226f1a2825a2e8257e4e5eaff9 100644 (file)
@@ -17,6 +17,7 @@
 #include "atom/browser/lib/bluetooth_chooser.h"
 #include "atom/browser/native_window.h"
 #include "atom/browser/net/atom_network_delegate.h"
+#include "atom/browser/ui/drag_util.h"
 #include "atom/browser/web_contents_permission_helper.h"
 #include "atom/browser/web_contents_preferences.h"
 #include "atom/browser/web_view_guest_delegate.h"
@@ -1205,6 +1206,13 @@ void WebContents::EndFrameSubscription() {
     view->EndFrameSubscription();
 }
 
+void WebContents::StartDrag(const base::FilePath& file,
+                            mate::Handle<NativeImage> image) {
+  base::MessageLoop::ScopedNestableTaskAllower allow(
+      base::MessageLoop::current());
+  DragItem(file, image->image(), web_contents()->GetNativeView());
+}
+
 void WebContents::OnCursorChange(const content::WebCursor& cursor) {
   content::WebCursor::CursorInfo info;
   cursor.GetCursorInfo(&info);
@@ -1324,6 +1332,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
       .SetMethod("beginFrameSubscription",
                  &WebContents::BeginFrameSubscription)
       .SetMethod("endFrameSubscription", &WebContents::EndFrameSubscription)
+      .SetMethod("startDrag", &WebContents::StartDrag)
       .SetMethod("setSize", &WebContents::SetSize)
       .SetMethod("isGuest", &WebContents::IsGuest)
       .SetMethod("getType", &WebContents::GetType)
index 5279812be427e18a0b7a469fe690a644e063937e..88eec80d33701ad8113727419e00620f955399d5 100644 (file)
@@ -39,6 +39,8 @@ class WebViewGuestDelegate;
 
 namespace api {
 
+class NativeImage;
+
 class WebContents : public mate::TrackableObject<WebContents>,
                     public CommonWebContentsDelegate,
                     public content::WebContentsObserver {
@@ -142,6 +144,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
   void BeginFrameSubscription(mate::Arguments* args);
   void EndFrameSubscription();
 
+  // Dragging native items.
+  void StartDrag(const base::FilePath& file, mate::Handle<NativeImage> image);
+
   // Methods for creating <webview>.
   void SetSize(const SetSizeParams& params);
   bool IsGuest() const;
diff --git a/atom/browser/ui/drag_util.h b/atom/browser/ui/drag_util.h
new file mode 100644 (file)
index 0000000..50f87c4
--- /dev/null
@@ -0,0 +1,22 @@
+// Copyright (c) 2016 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#ifndef ATOM_BROWSER_UI_DRAG_UTIL_H_
+#define ATOM_BROWSER_UI_DRAG_UTIL_H_
+
+#include "ui/gfx/image/image.h"
+
+namespace base {
+class FilePath;
+}
+
+namespace atom {
+
+void DragItem(const base::FilePath& path,
+              const gfx::Image& icon,
+              gfx::NativeView view);
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_UI_DRAG_UTIL_H_
diff --git a/atom/browser/ui/drag_util_mac.mm b/atom/browser/ui/drag_util_mac.mm
new file mode 100644 (file)
index 0000000..fe4254b
--- /dev/null
@@ -0,0 +1,56 @@
+// Copyright (c) 2016 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#import <Cocoa/Cocoa.h>
+
+#include "atom/browser/ui/drag_util.h"
+#include "base/files/file_path.h"
+#include "base/strings/sys_string_conversions.h"
+
+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];
+  [pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType]
+                     owner:nil];
+  [pasteboard setPropertyList:fileList forType:NSFilenamesPboardType];
+}
+
+}  // namespace
+
+void DragItem(const base::FilePath& path,
+              const gfx::Image& icon,
+              gfx::NativeView view) {
+  NSPasteboard* pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
+  AddFileToPasteboard(pasteboard, path);
+
+  // 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).
+  NSPoint position = [[view window] mouseLocationOutsideOfEventStream];
+  NSTimeInterval eventTime = [[NSApp currentEvent] timestamp];
+  NSEvent* dragEvent = [NSEvent mouseEventWithType:NSLeftMouseDragged
+                                          location:position
+                                     modifierFlags:NSLeftMouseDraggedMask
+                                         timestamp:eventTime
+                                      windowNumber:[[view window] windowNumber]
+                                           context:nil
+                                       eventNumber:0
+                                        clickCount:1
+                                          pressure:1.0];
+
+  // Run the drag operation.
+  [[view window] dragImage:icon.ToNSImage()
+                        at:position
+                    offset:NSZeroSize
+                     event:dragEvent
+                pasteboard:pasteboard
+                    source:view
+                 slideBack:YES];
+}
+
+}  // namespace atom
diff --git a/atom/browser/ui/drag_util_views.cc b/atom/browser/ui/drag_util_views.cc
new file mode 100644 (file)
index 0000000..1db7e54
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright (c) 2016 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#include "atom/browser/ui/drag_util.h"
+
+namespace atom {
+
+
+
+}  // namespace atom
index 6b086e1f8b9866468f22661bfb0a869927277303..a86c53ba4fd06f326cd2ecd142b50f3c8a73c638 100644 (file)
       'atom/browser/ui/atom_menu_model.h',
       'atom/browser/ui/cocoa/atom_menu_controller.h',
       'atom/browser/ui/cocoa/atom_menu_controller.mm',
+      'atom/browser/ui/drag_util_mac.mm',
+      'atom/browser/ui/drag_util_views.cc',
+      'atom/browser/ui/drag_util.h',
       'atom/browser/ui/file_dialog.h',
       'atom/browser/ui/file_dialog_gtk.cc',
       'atom/browser/ui/file_dialog_mac.mm',