OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/views/cocoa/drag_drop_client_mac.h" | |
tapted
2016/05/24 08:06:02
nit: import
spqchan
2016/05/26 01:56:54
Done.
| |
6 | |
7 #include "base/mac/mac_util.h" | |
8 #include "base/strings/sys_string_conversions.h" | |
9 #include "ui/base/dragdrop/os_exchange_data.h" | |
10 #include "ui/base/dragdrop/os_exchange_data_provider_mac.h" | |
tapted
2016/05/24 08:06:02
nit:import
spqchan
2016/05/26 01:56:55
Done.
| |
11 #include "ui/gfx/image/image_skia_util_mac.h" | |
12 #include "ui/views/drag_utils.h" | |
13 #import "ui/views/cocoa/bridged_content_view.h" | |
14 #import "ui/views/cocoa/bridged_native_widget.h" | |
15 #include "ui/views/widget/drop_helper.h" | |
16 | |
17 // This class acts as a bridge between NSPasteboardItem and OSExchangeData by | |
18 // implementing NSPasteboardItemDataProvider and writing data from | |
19 // OSExchangeData into the pasteboard. | |
20 @interface CocoaDragDropDataProvider () { | |
21 std::unique_ptr<ui::OSExchangeData> data_; | |
22 } | |
23 | |
24 @end | |
25 | |
26 @implementation CocoaDragDropDataProvider | |
27 | |
28 - (id)initWithData:(const ui::OSExchangeData&)data { | |
29 if ((self = [super init])) { | |
30 data_.reset(new OSExchangeData(data.provider().Clone())); | |
31 } | |
32 return self; | |
33 } | |
34 | |
35 - (id)initWithPasteboard:(NSPasteboard*)pasteboard { | |
36 if ((self = [super init])) { | |
37 data_ = ui::OSExchangeDataProviderMac::CreateDataFromPasteboard(pasteboard); | |
38 } | |
39 return self; | |
40 } | |
41 | |
42 - (ui::OSExchangeData*)data { | |
43 return data_.get(); | |
44 } | |
45 | |
46 - (void)pasteboard:(NSPasteboard*)sender | |
tapted
2016/05/24 08:06:02
// NSPasteboardItemDataProvider protocol implement
spqchan
2016/05/26 01:56:55
Done.
| |
47 item:(NSPasteboardItem*)item | |
tapted
2016/05/24 08:06:02
nit: align colons
spqchan
2016/05/26 01:56:55
Done.
| |
48 provideDataForType:(NSString*)type { | |
49 const ui::OSExchangeDataProviderMac* provider = | |
50 static_cast<const ui::OSExchangeDataProviderMac*>(&data_->provider()); | |
51 NSData* ns_data = provider->GetNSDataForType(type); | |
52 [sender setData:ns_data forType:type]; | |
53 } | |
54 | |
55 @end | |
56 | |
57 namespace views { | |
58 | |
59 DragDropClientMac::DragDropClientMac(BridgedNativeWidget* bridge) | |
60 : bridge_(bridge) { | |
61 DCHECK(bridge); | |
62 drop_helper_.reset(new DropHelper([bridge->ns_view() hostedView])); | |
63 } | |
64 | |
65 DragDropClientMac::~DragDropClientMac() {} | |
66 | |
67 void DragDropClientMac::StartDragAndDrop( | |
68 const ui::OSExchangeData& data, | |
69 const gfx::Point& location, | |
70 int operation, | |
71 ui::DragDropTypes::DragEventSource source) { | |
72 data_source_.reset([[CocoaDragDropDataProvider alloc] initWithData:data]); | |
73 operation_ = operation; | |
74 | |
75 const ui::OSExchangeDataProviderMac* provider = | |
tapted
2016/05/24 08:06:02
const ui::OSExchangeDataProviderMac&
(i.e. cast t
spqchan
2016/05/26 01:56:54
Done.
| |
76 static_cast<const ui::OSExchangeDataProviderMac*>(&data.provider()); | |
77 const base::NativeEvent event = provider->GetEvent()->native_event(); | |
tapted
2016/05/24 08:06:02
NSEvent* event?
(`const` and NS* objects tend not
spqchan
2016/05/26 01:56:54
Removed
| |
78 | |
79 NSImage* image = gfx::NSImageFromImageSkiaWithColorSpace( | |
80 provider->GetDragImage(), base::mac::GetSRGBColorSpace()); | |
81 | |
82 base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]); | |
83 [item setDataProvider:data_source_.get() | |
84 forTypes:provider->GetPasteboardTypes()]; | |
85 | |
86 base::scoped_nsobject<NSDraggingItem> dragItem( | |
87 [[NSDraggingItem alloc] initWithPasteboardWriter:item.get()]); | |
88 NSRect draggingFrame = | |
89 NSMakeRect([event locationInWindow].x, | |
90 [event locationInWindow].y - [image size].height, | |
91 [image size].width, [image size].height); | |
92 [dragItem setDraggingFrame:draggingFrame contents:image]; | |
93 | |
94 [bridge_->ns_view() beginDraggingSessionWithItems:@[ dragItem.get() ] | |
95 event:event | |
96 source:nil]; | |
tapted
2016/05/24 08:06:02
looks like source: must be non-nil to compile agai
spqchan
2016/05/26 01:56:55
Done.
| |
97 } | |
98 | |
99 NSDragOperation DragDropClientMac::DragUpdate(id<NSDraggingInfo> sender) { | |
100 int drag_operation = ui::DragDropTypes::DRAG_NONE; | |
101 | |
102 // Since dragging from non MacView sources does not generate OSExchangeData, | |
103 // we need to generate one based on the provided pasteboard. | |
104 if (!data_source_.get()) { | |
105 data_source_.reset([[CocoaDragDropDataProvider alloc] | |
106 initWithPasteboard:[sender draggingPasteboard]]); | |
107 } | |
108 | |
109 drag_operation = drop_helper_->OnDragOver( | |
110 *[data_source_ data], LocationInView([sender draggingLocation]), | |
111 operation_); | |
112 return ui::DragDropTypes::DragOperationToNSDragOperation(drag_operation); | |
113 } | |
114 | |
115 NSDragOperation DragDropClientMac::Drop(id<NSDraggingInfo> sender) { | |
116 int drag_operation = drop_helper_->OnDrop( | |
117 *[data_source_ data], LocationInView([sender draggingLocation]), | |
118 operation_); | |
119 return ui::DragDropTypes::DragOperationToNSDragOperation(drag_operation); | |
120 } | |
121 | |
122 void DragDropClientMac::EndDrag() { | |
123 data_source_.reset(); | |
124 } | |
125 | |
126 void DragDropClientMac::SetRootView(View* view) { | |
127 drop_helper_.reset(new DropHelper(view)); | |
128 } | |
129 | |
130 gfx::Point DragDropClientMac::LocationInView(NSPoint point) const { | |
131 return gfx::Point(point.x, NSHeight([bridge_->ns_window() frame]) - point.y); | |
132 } | |
133 | |
134 } // namespace views | |
OLD | NEW |