electron/shell/browser/ui/drag_util_mac.mm

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
3 KiB
Text
Raw Normal View History

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.
#include "shell/browser/ui/drag_util.h"
2016-07-03 03:26:43 +00:00
#import <Cocoa/Cocoa.h>
#include <vector>
2016-07-03 03:26:43 +00:00
#include "base/files/file_path.h"
#include "base/mac/foundation_util.h"
2016-07-03 03:26:43 +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 {
return context == NSDraggingContextOutsideApplication ? NSDragOperationCopy
: NSDragOperationEvery;
}
@end
2016-07-03 03:26:43 +00:00
namespace {
id<NSDraggingSource> GetDraggingSource() {
static id<NSDraggingSource> source = [[DragDownloadItemSource alloc] init];
return source;
2016-07-03 03:26:43 +00:00
}
} // namespace
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) {
DCHECK(view);
auto* native_view = view.GetNativeNSView();
NSPoint current_position =
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 = base::mac::FilePathToNSURL(file);
chore: bump chromium to 117.0.5923.0 (main) (#39304) * chore: bump chromium in DEPS to 117.0.5921.0 * chore: update chromium patches * 4721409: Remove redundant ARC configuration in /components | https://chromium-review.googlesource.com/c/chromium/src/+/4721409 * 4643750: Add V8_LOW_PRIORITY_TQ for main thread | https://chromium-review.googlesource.com/c/chromium/src/+/4643750 * 4022621: Re-register status item when owner of status watcher is changed | https://chromium-review.googlesource.com/c/chromium/src/+/4022621 * chore: update V8/boringssl patches * fixup! 4643750: Add V8_LOW_PRIORITY_TQ for main thread | https://chromium-review.googlesource.com/c/chromium/src/+/4643750 * chore: bump chromium in DEPS to 117.0.5923.0 * build [debug]: remove assert 4722125: Update enterprise content analysis buildflags usage | https://chromium-review.googlesource.com/c/chromium/src/+/4722125 * chore: manually rollback to 117.0.5921.0 * build [arc]: ARC conversion in auto_updater * build [arc]: ARC conversion in browser/api * build [arc]: ARC conversion in notifications/mac * build [arc]: ARC conversion in in_app_purchase * build [arc]: ARC conversion in browser/ui * build [arc]: ARC conversion in ui/cocoa * build [arc]: ARC conversion in shell/common * build [arc]: ARC conversion in OSR * build [arc]: ARC conversion in login_helper * build [arc]: ARC conversion in app_mas * build [arc]: fix up ARC syntax (thanks @codebytere!) * 4726946: [Extensions] Work around dangling BrowserContext pointer. | https://chromium-review.googlesource.com/c/chromium/src/+/4726946 --------- Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> Co-authored-by: Keeley Hammond <vertedinde@electronjs.org> Co-authored-by: VerteDinde <keeleymhammond@gmail.com>
2023-08-04 08:47:29 +00:00
NSDraggingItem* file_item =
[[NSDraggingItem alloc] initWithPasteboardWriter:file_url];
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).
NSEvent* dragEvent =
[NSEvent mouseEventWithType:NSEventTypeLeftMouseDragged
location:current_position
modifierFlags:0
timestamp:NSApp.currentEvent.timestamp
windowNumber:native_view.window.windowNumber
context:nil
eventNumber:0
clickCount:1
pressure:1.0];
2016-07-03 03:26:43 +00:00
// Run the drag operation.
[native_view beginDraggingSessionWithItems:file_items
event:dragEvent
source:GetDraggingSource()];
2016-07-03 03:26:43 +00:00
}
} // namespace electron