From 1a9c338c92a50ad2891953a7fd2cecc2e7414bc0 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Mon, 9 Jan 2023 10:00:47 +0100 Subject: [PATCH] chore: cleanup `drag_util` (#36806) chore: cleanup drag_util --- shell/browser/ui/drag_util_mac.mm | 19 +++++++++---------- shell/browser/ui/drag_util_views.cc | 10 +++++----- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/shell/browser/ui/drag_util_mac.mm b/shell/browser/ui/drag_util_mac.mm index 36656ea36294..3410aecdd77d 100644 --- a/shell/browser/ui/drag_util_mac.mm +++ b/shell/browser/ui/drag_util_mac.mm @@ -2,13 +2,14 @@ // 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" + #import #include #include "base/files/file_path.h" -#include "base/strings/sys_string_conversions.h" -#include "shell/browser/ui/drag_util.h" +#include "base/mac/foundation_util.h" // Contents largely copied from // chrome/browser/download/drag_download_item_mac.mm. @@ -40,9 +41,10 @@ namespace electron { void DragFileItems(const std::vector& files, const gfx::Image& icon, gfx::NativeView view) { + DCHECK(view); auto* native_view = view.GetNativeNSView(); NSPoint current_position = - [[native_view window] mouseLocationOutsideOfEventStream]; + native_view.window.mouseLocationOutsideOfEventStream; current_position = [native_view backingAlignedRect:NSMakeRect(current_position.x, current_position.y, 0, 0) @@ -51,8 +53,7 @@ void DragFileItems(const std::vector& files, NSMutableArray* file_items = [NSMutableArray array]; for (auto const& file : files) { - NSURL* file_url = - [NSURL fileURLWithPath:base::SysUTF8ToNSString(file.value())]; + NSURL* file_url = base::mac::FilePathToNSURL(file); NSDraggingItem* file_item = [[[NSDraggingItem alloc] initWithPasteboardWriter:file_url] autorelease]; NSImage* file_image = icon.ToNSImage(); @@ -66,14 +67,12 @@ void DragFileItems(const std::vector& files, // 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 = [[native_view window] mouseLocationOutsideOfEventStream]; - NSTimeInterval eventTime = [[NSApp currentEvent] timestamp]; NSEvent* dragEvent = [NSEvent mouseEventWithType:NSEventTypeLeftMouseDragged - location:position + location:current_position modifierFlags:0 - timestamp:eventTime - windowNumber:[[native_view window] windowNumber] + timestamp:NSApp.currentEvent.timestamp + windowNumber:native_view.window.windowNumber context:nil eventNumber:0 clickCount:1 diff --git a/shell/browser/ui/drag_util_views.cc b/shell/browser/ui/drag_util_views.cc index d8e96eea3d85..f64f3ec54dab 100644 --- a/shell/browser/ui/drag_util_views.cc +++ b/shell/browser/ui/drag_util_views.cc @@ -21,7 +21,11 @@ namespace electron { void DragFileItems(const std::vector& files, const gfx::Image& icon, 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(); button_drag_utils::SetDragImage(GURL(), files[0].LossyDisplayName(), @@ -34,10 +38,6 @@ void DragFileItems(const std::vector& files, } 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(); // TODO(varunjain): Properly determine and send DragEventSource below. aura::client::GetDragDropClient(root_window)