File: drag_download_item_mac.mm

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (91 lines) | stat: -rw-r--r-- 3,792 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/download/drag_download_item.h"

#import <Cocoa/Cocoa.h>

#include "base/apple/foundation_util.h"
#include "components/download/public/common/download_item.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/widget/widget.h"

// Cocoa intends a smart dragging source, while `DragDownloadItem()` is a simple
// "start dragging this" fire-and-forget. This is a generic source just good
// enough to satisfy AppKit.
@interface DragDownloadItemSource : NSObject<NSDraggingSource>
@end

@implementation DragDownloadItemSource

- (NSDragOperation)draggingSession:(NSDraggingSession*)session
    sourceOperationMaskForDraggingContext:(NSDraggingContext)context {
  return NSDragOperationEvery;
}

@end

namespace {
id<NSDraggingSource> GetDraggingSource() {
  static id<NSDraggingSource> source = [[DragDownloadItemSource alloc] init];
  return source;
}
}  // namespace

void DragDownloadItem(const download::DownloadItem* download,
                      const gfx::Image* icon,
                      gfx::NativeView native_view) {
  DCHECK_EQ(download::DownloadItem::COMPLETE, download->GetState());
  DCHECK(native_view);
  NSView* view = native_view.GetNativeNSView();
  NSPoint current_position = view.window.mouseLocationOutsideOfEventStream;
  current_position =
      [view backingAlignedRect:NSMakeRect(current_position.x,
                                          current_position.y, 0, 0)
                       options:NSAlignAllEdgesOutward]
          .origin;

  // If this drag was initiated from a views::Widget, that widget may have
  // mouse capture. Drags via View::DoDrag() usually release it. The code below
  // bypasses that, so release manually. See https://crbug.com/863377.
  views::Widget* widget =
      views::Widget::GetWidgetForNativeView(gfx::NativeView(view));
  if (widget)
    widget->ReleaseCapture();

  NSURL* file_url = base::apple::FilePathToNSURL(download->GetTargetFilePath());
  NSDraggingItem* file_item =
      [[NSDraggingItem alloc] initWithPasteboardWriter:file_url];
  if (icon) {
    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];
  } else {
    // 16x16 placeholder, corresponding to IconLoader::IconSize::SMALL.
    NSRect placeholder_rect =
        NSMakeRect(current_position.x - 8, current_position.y - 8, 16, 16);
    [file_item setDraggingFrame:placeholder_rect contents:nil];
  }

  // 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:view.window.windowNumber
                                           context:nil
                                       eventNumber:0
                                        clickCount:1
                                          pressure:1.0];

  // Run the drag operation.
  [view beginDraggingSessionWithItems:@[ file_item ]
                                event:dragEvent
                               source:GetDraggingSource()];
}