drag_util_mac.mm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2016 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/ui/drag_util.h"
  5. #import <Cocoa/Cocoa.h>
  6. #include <vector>
  7. #include "base/apple/foundation_util.h"
  8. #include "base/files/file_path.h"
  9. // Contents largely copied from
  10. // chrome/browser/download/drag_download_item_mac.mm.
  11. @interface DragDownloadItemSource : NSObject <NSDraggingSource>
  12. @end
  13. @implementation DragDownloadItemSource
  14. - (NSDragOperation)draggingSession:(NSDraggingSession*)session
  15. sourceOperationMaskForDraggingContext:(NSDraggingContext)context {
  16. return context == NSDraggingContextOutsideApplication ? NSDragOperationCopy
  17. : NSDragOperationEvery;
  18. }
  19. @end
  20. namespace {
  21. id<NSDraggingSource> GetDraggingSource() {
  22. static id<NSDraggingSource> source = [[DragDownloadItemSource alloc] init];
  23. return source;
  24. }
  25. } // namespace
  26. namespace electron {
  27. void DragFileItems(const std::vector<base::FilePath>& files,
  28. const gfx::Image& icon,
  29. gfx::NativeView view) {
  30. DCHECK(view);
  31. auto* native_view = view.GetNativeNSView();
  32. NSPoint current_position =
  33. native_view.window.mouseLocationOutsideOfEventStream;
  34. current_position =
  35. [native_view backingAlignedRect:NSMakeRect(current_position.x,
  36. current_position.y, 0, 0)
  37. options:NSAlignAllEdgesOutward]
  38. .origin;
  39. NSMutableArray* file_items = [NSMutableArray array];
  40. for (auto const& file : files) {
  41. NSURL* file_url = base::apple::FilePathToNSURL(file);
  42. NSDraggingItem* file_item =
  43. [[NSDraggingItem alloc] initWithPasteboardWriter:file_url];
  44. NSImage* file_image = icon.ToNSImage();
  45. NSSize image_size = file_image.size;
  46. NSRect image_rect = NSMakeRect(current_position.x - image_size.width / 2,
  47. current_position.y - image_size.height / 2,
  48. image_size.width, image_size.height);
  49. [file_item setDraggingFrame:image_rect contents:file_image];
  50. [file_items addObject:file_item];
  51. }
  52. // Synthesize a drag event, since we don't have access to the actual event
  53. // that initiated a drag (possibly consumed by the Web UI, for example).
  54. NSEvent* dragEvent =
  55. [NSEvent mouseEventWithType:NSEventTypeLeftMouseDragged
  56. location:current_position
  57. modifierFlags:0
  58. timestamp:NSApp.currentEvent.timestamp
  59. windowNumber:native_view.window.windowNumber
  60. context:nil
  61. eventNumber:0
  62. clickCount:1
  63. pressure:1.0];
  64. // Run the drag operation.
  65. [native_view beginDraggingSessionWithItems:file_items
  66. event:dragEvent
  67. source:GetDraggingSource()];
  68. }
  69. } // namespace electron