refactor: remove deprecated drag-and-drop code (#34615)
This commit is contained in:
parent
d341610d64
commit
8e45f43f18
1 changed files with 56 additions and 31 deletions
|
@ -10,53 +10,78 @@
|
||||||
#include "base/strings/sys_string_conversions.h"
|
#include "base/strings/sys_string_conversions.h"
|
||||||
#include "shell/browser/ui/drag_util.h"
|
#include "shell/browser/ui/drag_util.h"
|
||||||
|
|
||||||
namespace electron {
|
// Contents largely copied from
|
||||||
|
// chrome/browser/download/drag_download_item_mac.mm.
|
||||||
|
|
||||||
|
@interface DragDownloadItemSource : NSObject <NSDraggingSource>
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation DragDownloadItemSource
|
||||||
|
|
||||||
|
- (NSDragOperation)draggingSession:(NSDraggingSession*)session
|
||||||
|
sourceOperationMaskForDraggingContext:(NSDraggingContext)context {
|
||||||
|
return NSDragOperationEvery;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// Write information about the file being dragged to the pasteboard.
|
id<NSDraggingSource> GetDraggingSource() {
|
||||||
void AddFilesToPasteboard(NSPasteboard* pasteboard,
|
static id<NSDraggingSource> source = [[DragDownloadItemSource alloc] init];
|
||||||
const std::vector<base::FilePath>& files) {
|
return source;
|
||||||
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];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
namespace electron {
|
||||||
|
|
||||||
void DragFileItems(const std::vector<base::FilePath>& files,
|
void DragFileItems(const std::vector<base::FilePath>& files,
|
||||||
const gfx::Image& icon,
|
const gfx::Image& icon,
|
||||||
gfx::NativeView view) {
|
gfx::NativeView view) {
|
||||||
NSPasteboard* pasteboard =
|
auto* native_view = view.GetNativeNSView();
|
||||||
[NSPasteboard pasteboardWithName:NSPasteboardNameDrag];
|
NSPoint current_position =
|
||||||
AddFilesToPasteboard(pasteboard, files);
|
[[native_view window] mouseLocationOutsideOfEventStream];
|
||||||
|
current_position =
|
||||||
|
[native_view backingAlignedRect:NSMakeRect(current_position.x,
|
||||||
|
current_position.y, 0, 0)
|
||||||
|
options:NSAlignAllEdgesOutward]
|
||||||
|
.origin;
|
||||||
|
|
||||||
|
NSMutableArray* file_items = [NSMutableArray array];
|
||||||
|
for (auto const& file : files) {
|
||||||
|
NSURL* file_url =
|
||||||
|
[NSURL fileURLWithPath:base::SysUTF8ToNSString(file.value())];
|
||||||
|
NSDraggingItem* file_item = [[[NSDraggingItem alloc]
|
||||||
|
initWithPasteboardWriter:file_url] autorelease];
|
||||||
|
NSImage* file_image = icon.ToNSImage();
|
||||||
|
NSSize image_size = file_image.size;
|
||||||
|
NSRect image_rect = NSMakeRect(current_position.x - image_size.width / 2,
|
||||||
|
current_position.y - image_size.height / 2,
|
||||||
|
image_size.width, image_size.height);
|
||||||
|
[file_item setDraggingFrame:image_rect contents:file_image];
|
||||||
|
[file_items addObject:file_item];
|
||||||
|
}
|
||||||
|
|
||||||
// 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).
|
||||||
NSWindow* window = [view.GetNativeNSView() window];
|
NSPoint position = [[native_view window] mouseLocationOutsideOfEventStream];
|
||||||
NSPoint position = [window mouseLocationOutsideOfEventStream];
|
|
||||||
NSTimeInterval eventTime = [[NSApp currentEvent] timestamp];
|
NSTimeInterval eventTime = [[NSApp currentEvent] timestamp];
|
||||||
NSEvent* dragEvent = [NSEvent mouseEventWithType:NSEventTypeLeftMouseDragged
|
NSEvent* dragEvent =
|
||||||
|
[NSEvent mouseEventWithType:NSEventTypeLeftMouseDragged
|
||||||
location:position
|
location:position
|
||||||
modifierFlags:NSEventMaskLeftMouseDragged
|
modifierFlags:NSEventMaskLeftMouseDragged
|
||||||
timestamp:eventTime
|
timestamp:eventTime
|
||||||
windowNumber:[window windowNumber]
|
windowNumber:[[native_view window] windowNumber]
|
||||||
context:nil
|
context:nil
|
||||||
eventNumber:0
|
eventNumber:0
|
||||||
clickCount:1
|
clickCount:1
|
||||||
pressure:1.0];
|
pressure:1.0];
|
||||||
|
|
||||||
// Run the drag operation.
|
// Run the drag operation.
|
||||||
[window dragImage:icon.ToNSImage()
|
[native_view beginDraggingSessionWithItems:file_items
|
||||||
at:position
|
|
||||||
offset:NSZeroSize
|
|
||||||
event:dragEvent
|
event:dragEvent
|
||||||
pasteboard:pasteboard
|
source:GetDraggingSource()];
|
||||||
source:view.GetNativeNSView()
|
|
||||||
slideBack:YES];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace electron
|
} // namespace electron
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue