Support dragging multiple files

This commit is contained in:
Cheng Zhao 2016-07-03 13:58:31 +09:00
parent 13c668f22b
commit fa468a529b
4 changed files with 41 additions and 16 deletions

View file

@ -1206,11 +1206,34 @@ void WebContents::EndFrameSubscription() {
view->EndFrameSubscription(); view->EndFrameSubscription();
} }
void WebContents::StartDrag(const base::FilePath& file, void WebContents::StartDrag(const mate::Dictionary& item,
mate::Handle<NativeImage> image) { mate::Arguments* args) {
base::MessageLoop::ScopedNestableTaskAllower allow( base::MessageLoop::ScopedNestableTaskAllower allow(
base::MessageLoop::current()); 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) { void WebContents::OnCursorChange(const content::WebCursor& cursor) {

View file

@ -39,8 +39,6 @@ class WebViewGuestDelegate;
namespace api { namespace api {
class NativeImage;
class WebContents : public mate::TrackableObject<WebContents>, class WebContents : public mate::TrackableObject<WebContents>,
public CommonWebContentsDelegate, public CommonWebContentsDelegate,
public content::WebContentsObserver { public content::WebContentsObserver {
@ -145,7 +143,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
void EndFrameSubscription(); void EndFrameSubscription();
// Dragging native items. // 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>. // Methods for creating <webview>.
void SetSize(const SetSizeParams& params); void SetSize(const SetSizeParams& params);

View file

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

View file

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