chore: cleanup drag_util (#36806)

chore: cleanup drag_util
This commit is contained in:
Shelley Vohr 2023-01-09 10:00:47 +01:00 committed by GitHub
parent 0d05273024
commit 1a9c338c92
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 15 deletions

View file

@ -2,13 +2,14 @@
// Use of this source code is governed by the MIT license that can be // Use of this source code is governed by the MIT license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "shell/browser/ui/drag_util.h"
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#include <vector> #include <vector>
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/strings/sys_string_conversions.h" #include "base/mac/foundation_util.h"
#include "shell/browser/ui/drag_util.h"
// Contents largely copied from // Contents largely copied from
// chrome/browser/download/drag_download_item_mac.mm. // chrome/browser/download/drag_download_item_mac.mm.
@ -40,9 +41,10 @@ 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) {
DCHECK(view);
auto* native_view = view.GetNativeNSView(); auto* native_view = view.GetNativeNSView();
NSPoint current_position = NSPoint current_position =
[[native_view window] mouseLocationOutsideOfEventStream]; native_view.window.mouseLocationOutsideOfEventStream;
current_position = current_position =
[native_view backingAlignedRect:NSMakeRect(current_position.x, [native_view backingAlignedRect:NSMakeRect(current_position.x,
current_position.y, 0, 0) current_position.y, 0, 0)
@ -51,8 +53,7 @@ void DragFileItems(const std::vector<base::FilePath>& files,
NSMutableArray* file_items = [NSMutableArray array]; NSMutableArray* file_items = [NSMutableArray array];
for (auto const& file : files) { for (auto const& file : files) {
NSURL* file_url = NSURL* file_url = base::mac::FilePathToNSURL(file);
[NSURL fileURLWithPath:base::SysUTF8ToNSString(file.value())];
NSDraggingItem* file_item = [[[NSDraggingItem alloc] NSDraggingItem* file_item = [[[NSDraggingItem alloc]
initWithPasteboardWriter:file_url] autorelease]; initWithPasteboardWriter:file_url] autorelease];
NSImage* file_image = icon.ToNSImage(); NSImage* file_image = icon.ToNSImage();
@ -66,14 +67,12 @@ void DragFileItems(const std::vector<base::FilePath>& 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).
NSPoint position = [[native_view window] mouseLocationOutsideOfEventStream];
NSTimeInterval eventTime = [[NSApp currentEvent] timestamp];
NSEvent* dragEvent = NSEvent* dragEvent =
[NSEvent mouseEventWithType:NSEventTypeLeftMouseDragged [NSEvent mouseEventWithType:NSEventTypeLeftMouseDragged
location:position location:current_position
modifierFlags:0 modifierFlags:0
timestamp:eventTime timestamp:NSApp.currentEvent.timestamp
windowNumber:[[native_view window] windowNumber] windowNumber:native_view.window.windowNumber
context:nil context:nil
eventNumber:0 eventNumber:0
clickCount:1 clickCount:1

View file

@ -21,7 +21,11 @@ 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) {
// Set up our OLE machinery aura::Window* root_window = view->GetRootWindow();
if (!root_window || !aura::client::GetDragDropClient(root_window))
return;
// Set up our OLE machinery.
auto data = std::make_unique<ui::OSExchangeData>(); auto data = std::make_unique<ui::OSExchangeData>();
button_drag_utils::SetDragImage(GURL(), files[0].LossyDisplayName(), button_drag_utils::SetDragImage(GURL(), files[0].LossyDisplayName(),
@ -34,10 +38,6 @@ void DragFileItems(const std::vector<base::FilePath>& files,
} }
data->SetFilenames(file_infos); data->SetFilenames(file_infos);
aura::Window* root_window = view->GetRootWindow();
if (!root_window || !aura::client::GetDragDropClient(root_window))
return;
gfx::Point location = display::Screen::GetScreen()->GetCursorScreenPoint(); gfx::Point location = display::Screen::GetScreen()->GetCursorScreenPoint();
// TODO(varunjain): Properly determine and send DragEventSource below. // TODO(varunjain): Properly determine and send DragEventSource below.
aura::client::GetDragDropClient(root_window) aura::client::GetDragDropClient(root_window)