OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | |
5 // Download utility implementation for Mac OS X. | |
6 | 4 |
7 #include "chrome/browser/ui/cocoa/download/download_util_mac.h" | 5 #include "chrome/browser/ui/cocoa/download/download_util_mac.h" |
8 | 6 |
9 #include "base/logging.h" | 7 #include "base/files/file_path.h" |
10 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
11 #include "content/public/browser/download_item.h" | |
12 #include "content/public/browser/download_manager.h" | |
13 #include "ui/gfx/image/image.h" | |
14 #include "ui/gfx/native_widget_types.h" | |
15 | |
16 using content::DownloadItem; | |
17 | 9 |
18 namespace download_util { | 10 namespace download_util { |
19 | 11 |
20 void AddFileToPasteboard(NSPasteboard* pasteboard, const base::FilePath& path) { | 12 void AddFileToPasteboard(NSPasteboard* pasteboard, const base::FilePath& path) { |
21 // Write information about the file being dragged to the pasteboard. | 13 // Write information about the file being dragged to the pasteboard. |
22 NSString* file = base::SysUTF8ToNSString(path.value()); | 14 NSString* file = base::SysUTF8ToNSString(path.value()); |
23 NSArray* fileList = [NSArray arrayWithObject:file]; | 15 NSArray* fileList = [NSArray arrayWithObject:file]; |
24 [pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] | 16 [pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] |
25 owner:nil]; | 17 owner:nil]; |
26 [pasteboard setPropertyList:fileList forType:NSFilenamesPboardType]; | 18 [pasteboard setPropertyList:fileList forType:NSFilenamesPboardType]; |
27 } | 19 } |
28 | 20 |
29 void DragDownload(const DownloadItem* download, | |
30 gfx::Image* icon, | |
31 gfx::NativeView view) { | |
32 DCHECK_EQ(DownloadItem::COMPLETE, download->GetState()); | |
33 NSPasteboard* pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard]; | |
34 AddFileToPasteboard(pasteboard, download->GetTargetFilePath()); | |
35 | |
36 // Synthesize a drag event, since we don't have access to the actual event | |
37 // that initiated a drag (possibly consumed by the Web UI, for example). | |
38 NSPoint position = [[view window] mouseLocationOutsideOfEventStream]; | |
39 NSTimeInterval eventTime = [[NSApp currentEvent] timestamp]; | |
40 NSEvent* dragEvent = [NSEvent mouseEventWithType:NSLeftMouseDragged | |
41 location:position | |
42 modifierFlags:NSLeftMouseDraggedMask | |
43 timestamp:eventTime | |
44 windowNumber:[[view window] windowNumber] | |
45 context:nil | |
46 eventNumber:0 | |
47 clickCount:1 | |
48 pressure:1.0]; | |
49 | |
50 // Run the drag operation. | |
51 [[view window] dragImage:icon->ToNSImage() | |
52 at:position | |
53 offset:NSZeroSize | |
54 event:dragEvent | |
55 pasteboard:pasteboard | |
56 source:view | |
57 slideBack:YES]; | |
58 } | |
59 | |
60 } // namespace download_util | 21 } // namespace download_util |
OLD | NEW |