mac: Implement webContents.startDrag
This commit is contained in:
parent
74ebbf9c78
commit
13c668f22b
6 changed files with 106 additions and 0 deletions
22
atom/browser/ui/drag_util.h
Normal file
22
atom/browser/ui/drag_util.h
Normal file
|
@ -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_
|
56
atom/browser/ui/drag_util_mac.mm
Normal file
56
atom/browser/ui/drag_util_mac.mm
Normal file
|
@ -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
|
11
atom/browser/ui/drag_util_views.cc
Normal file
11
atom/browser/ui/drag_util_views.cc
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue