2016-07-03 03:26:43 +00:00
|
|
|
// Copyright (c) 2016 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2023-01-09 09:00:47 +00:00
|
|
|
#include "shell/browser/ui/drag_util.h"
|
|
|
|
|
2016-07-03 03:26:43 +00:00
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
2019-05-02 12:05:37 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2016-07-03 03:26:43 +00:00
|
|
|
#include "base/files/file_path.h"
|
2023-01-09 09:00:47 +00:00
|
|
|
#include "base/mac/foundation_util.h"
|
2016-07-03 03:26:43 +00:00
|
|
|
|
2022-06-20 13:17:53 +00:00
|
|
|
// 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 {
|
2022-10-11 16:19:59 +00:00
|
|
|
return context == NSDraggingContextOutsideApplication ? NSDragOperationCopy
|
|
|
|
: NSDragOperationEvery;
|
2022-06-20 13:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2016-07-03 03:26:43 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-06-20 13:17:53 +00:00
|
|
|
id<NSDraggingSource> GetDraggingSource() {
|
|
|
|
static id<NSDraggingSource> source = [[DragDownloadItemSource alloc] init];
|
|
|
|
return source;
|
2016-07-03 03:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2022-06-20 13:17:53 +00:00
|
|
|
namespace electron {
|
|
|
|
|
2016-07-03 04:58:31 +00:00
|
|
|
void DragFileItems(const std::vector<base::FilePath>& files,
|
|
|
|
const gfx::Image& icon,
|
|
|
|
gfx::NativeView view) {
|
2023-01-09 09:00:47 +00:00
|
|
|
DCHECK(view);
|
2022-06-20 13:17:53 +00:00
|
|
|
auto* native_view = view.GetNativeNSView();
|
|
|
|
NSPoint current_position =
|
2023-01-09 09:00:47 +00:00
|
|
|
native_view.window.mouseLocationOutsideOfEventStream;
|
2022-06-20 13:17:53 +00:00
|
|
|
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) {
|
2023-01-09 09:00:47 +00:00
|
|
|
NSURL* file_url = base::mac::FilePathToNSURL(file);
|
2023-08-04 08:47:29 +00:00
|
|
|
NSDraggingItem* file_item =
|
|
|
|
[[NSDraggingItem alloc] initWithPasteboardWriter:file_url];
|
2022-06-20 13:17:53 +00:00
|
|
|
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];
|
|
|
|
}
|
2016-07-03 03:26:43 +00:00
|
|
|
|
|
|
|
// 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).
|
2022-06-20 13:17:53 +00:00
|
|
|
NSEvent* dragEvent =
|
|
|
|
[NSEvent mouseEventWithType:NSEventTypeLeftMouseDragged
|
2023-01-09 09:00:47 +00:00
|
|
|
location:current_position
|
2022-10-11 16:19:59 +00:00
|
|
|
modifierFlags:0
|
2023-01-09 09:00:47 +00:00
|
|
|
timestamp:NSApp.currentEvent.timestamp
|
|
|
|
windowNumber:native_view.window.windowNumber
|
2022-06-20 13:17:53 +00:00
|
|
|
context:nil
|
|
|
|
eventNumber:0
|
|
|
|
clickCount:1
|
|
|
|
pressure:1.0];
|
2016-07-03 03:26:43 +00:00
|
|
|
|
|
|
|
// Run the drag operation.
|
2022-06-20 13:17:53 +00:00
|
|
|
[native_view beginDraggingSessionWithItems:file_items
|
|
|
|
event:dragEvent
|
|
|
|
source:GetDraggingSource()];
|
2016-07-03 03:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace electron
|