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 #import "ui/views/cocoa/drag_drop_client_mac.h" | |
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 #import "ui/base/dragdrop/os_exchange_data_provider_mac.h" | |
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" | |
tapted
2016/06/02 00:24:51
nit: not needed since it's in the header
spqchan
2016/06/02 22:01:19
Done
| |
16 #include "ui/views/widget/native_widget_mac.h" | |
17 | |
18 @interface CocoaDragDropDataProvider () | |
19 - (id)initWithData:(const ui::OSExchangeData&)data; | |
20 - (id)initWithPasteboard:(NSPasteboard*)pasteboard; | |
21 @end | |
22 | |
23 @implementation CocoaDragDropDataProvider | |
24 | |
25 std::unique_ptr<ui::OSExchangeData> data_; | |
26 | |
27 - (id)initWithData:(const ui::OSExchangeData&)data { | |
28 if ((self = [super init])) { | |
29 data_.reset(new OSExchangeData(data.provider().Clone())); | |
30 } | |
31 return self; | |
32 } | |
33 | |
34 - (id)initWithPasteboard:(NSPasteboard*)pasteboard { | |
35 if ((self = [super init])) { | |
36 data_ = ui::OSExchangeDataProviderMac::CreateDataFromPasteboard(pasteboard); | |
37 } | |
38 return self; | |
39 } | |
40 | |
41 - (ui::OSExchangeData*)data { | |
42 return data_.get(); | |
43 } | |
44 | |
45 // NSPasteboardItemDataProvider protocol implementation. | |
46 | |
47 - (void)pasteboard:(NSPasteboard*)sender | |
48 item:(NSPasteboardItem*)item | |
49 provideDataForType:(NSString*)type { | |
50 const ui::OSExchangeDataProviderMac& provider = | |
51 static_cast<const ui::OSExchangeDataProviderMac&>(data_->provider()); | |
52 NSData* ns_data = provider.GetNSDataForType(type); | |
53 [sender setData:ns_data forType:type]; | |
54 } | |
55 | |
56 @end | |
57 | |
58 namespace views { | |
59 | |
60 DragDropClientMac::DragDropClientMac(BridgedNativeWidget* bridge, | |
61 View* root_view) | |
62 : operation_(0), bridge_(bridge), quit_closure_(base::Closure()) { | |
63 DCHECK(bridge); | |
64 drop_helper_.reset(new DropHelper(root_view)); | |
tapted
2016/06/02 00:24:51
moving this up to the initializer list on line 62
spqchan
2016/06/02 22:01:19
Ahh right, my bad. Thanks for letting me know!
| |
65 } | |
66 | |
67 DragDropClientMac::~DragDropClientMac() {} | |
68 | |
69 void DragDropClientMac::StartDragAndDrop( | |
70 View* view, | |
71 const ui::OSExchangeData& data, | |
72 int operation, | |
73 ui::DragDropTypes::DragEventSource source) { | |
74 data_source_.reset([[CocoaDragDropDataProvider alloc] initWithData:data]); | |
75 operation_ = operation; | |
76 | |
77 const ui::OSExchangeDataProviderMac& provider = | |
78 static_cast<const ui::OSExchangeDataProviderMac&>(data.provider()); | |
79 | |
80 // Synthesize an event for dragging, since we can't be sure that | |
81 // [NSApp currentEvent] will return a valid dragging event. | |
82 NSWindow* window = bridge_->ns_window(); | |
83 NSPoint position = [window mouseLocationOutsideOfEventStream]; | |
84 NSTimeInterval event_time = [[NSApp currentEvent] timestamp]; | |
85 NSEvent* event = [NSEvent mouseEventWithType:NSLeftMouseDragged | |
86 location:position | |
87 modifierFlags:NSLeftMouseDraggedMask | |
88 timestamp:event_time | |
89 windowNumber:[window windowNumber] | |
90 context:nil | |
91 eventNumber:0 | |
92 clickCount:1 | |
93 pressure:1.0]; | |
94 | |
95 NSImage* image = gfx::NSImageFromImageSkiaWithColorSpace( | |
96 provider.GetDragImage(), base::mac::GetSRGBColorSpace()); | |
97 | |
98 base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]); | |
99 [item setDataProvider:data_source_.get() | |
100 forTypes:ui::OSExchangeDataProviderMac:: | |
101 SupportedPasteboardTypes()]; | |
102 | |
103 base::scoped_nsobject<NSDraggingItem> drag_item( | |
104 [[NSDraggingItem alloc] initWithPasteboardWriter:item.get()]); | |
105 | |
106 // Subtract the image's height from the y location so that the mouse will be | |
107 // at the upper left corner of the image. | |
108 NSRect dragging_frame = | |
109 NSMakeRect([event locationInWindow].x, | |
110 [event locationInWindow].y - [image size].height, | |
111 [image size].width, [image size].height); | |
112 [drag_item setDraggingFrame:dragging_frame contents:image]; | |
113 | |
114 [bridge_->ns_view() beginDraggingSessionWithItems:@[ drag_item.get() ] | |
115 event:event | |
116 source:bridge_->ns_view()]; | |
117 base::RunLoop run_loop; | |
tapted
2016/06/02 00:24:51
nit: we should comment here, something like
//
spqchan
2016/06/02 22:01:19
Done.
| |
118 quit_closure_ = run_loop.QuitClosure(); | |
119 run_loop.Run(); | |
120 } | |
121 | |
122 NSDragOperation DragDropClientMac::DragUpdate(id<NSDraggingInfo> sender) { | |
123 int drag_operation = ui::DragDropTypes::DRAG_NONE; | |
124 | |
125 // Since dragging from non MacView sources does not generate OSExchangeData, | |
126 // we need to generate one based on the provided pasteboard. | |
127 if (!data_source_.get()) { | |
128 data_source_.reset([[CocoaDragDropDataProvider alloc] | |
129 initWithPasteboard:[sender draggingPasteboard]]); | |
130 } | |
131 | |
132 drag_operation = drop_helper_->OnDragOver( | |
133 *[data_source_ data], LocationInView([sender draggingLocation]), | |
134 operation_); | |
135 return ui::DragDropTypes::DragOperationToNSDragOperation(drag_operation); | |
136 } | |
137 | |
138 NSDragOperation DragDropClientMac::Drop(id<NSDraggingInfo> sender) { | |
139 int drag_operation = drop_helper_->OnDrop( | |
140 *[data_source_ data], LocationInView([sender draggingLocation]), | |
141 operation_); | |
142 return ui::DragDropTypes::DragOperationToNSDragOperation(drag_operation); | |
143 } | |
144 | |
145 void DragDropClientMac::EndDrag() { | |
146 data_source_.reset(); | |
147 | |
148 // Allow a test to invoke EndDrag() without spinning the nested run loop. | |
149 if (!quit_closure_.is_null()) { | |
150 quit_closure_.Run(); | |
151 quit_closure_.Reset(); | |
152 } | |
153 } | |
154 | |
155 gfx::Point DragDropClientMac::LocationInView(NSPoint point) const { | |
156 return gfx::Point(point.x, NSHeight([bridge_->ns_window() frame]) - point.y); | |
157 } | |
158 | |
159 } // namespace views | |
OLD | NEW |