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" | |
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), | |
63 bridge_(bridge), | |
64 is_run_loop_running_(false), | |
65 quit_closure_(base::Closure()) { | |
66 DCHECK(bridge); | |
67 drop_helper_.reset(new DropHelper(root_view)); | |
68 } | |
69 | |
70 DragDropClientMac::~DragDropClientMac() {} | |
71 | |
72 void DragDropClientMac::StartDragAndDrop( | |
73 View* view, | |
74 const ui::OSExchangeData& data, | |
75 int operation, | |
76 ui::DragDropTypes::DragEventSource source) { | |
77 data_source_.reset([[CocoaDragDropDataProvider alloc] initWithData:data]); | |
78 operation_ = operation; | |
79 | |
80 const ui::OSExchangeDataProviderMac& provider = | |
81 static_cast<const ui::OSExchangeDataProviderMac&>(data.provider()); | |
82 | |
83 // Synthesize an event for dragging, since we can't be sure that | |
84 // [NSApp currentEvent] will return a valid dragging event. | |
85 NSWindow* window = bridge_->ns_window(); | |
86 NSPoint position = [window mouseLocationOutsideOfEventStream]; | |
87 NSTimeInterval event_time = [[NSApp currentEvent] timestamp]; | |
88 NSEvent* event = [NSEvent mouseEventWithType:NSLeftMouseDragged | |
89 location:position | |
90 modifierFlags:NSLeftMouseDraggedMask | |
91 timestamp:event_time | |
92 windowNumber:[window windowNumber] | |
93 context:nil | |
94 eventNumber:0 | |
95 clickCount:1 | |
96 pressure:1.0]; | |
97 | |
98 NSImage* image = gfx::NSImageFromImageSkiaWithColorSpace( | |
99 provider.GetDragImage(), base::mac::GetSRGBColorSpace()); | |
100 | |
101 base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]); | |
102 [item setDataProvider:data_source_.get() | |
103 forTypes:ui::OSExchangeDataProviderMac:: | |
104 SupportedPasteboardTypes()]; | |
105 | |
106 base::scoped_nsobject<NSDraggingItem> drag_item( | |
107 [[NSDraggingItem alloc] initWithPasteboardWriter:item.get()]); | |
108 | |
109 // Subtract the image's height from the y location so that the mouse will be | |
110 // at the upper left corner of the image. | |
111 NSRect dragging_frame = | |
112 NSMakeRect([event locationInWindow].x, | |
113 [event locationInWindow].y - [image size].height, | |
114 [image size].width, [image size].height); | |
115 [drag_item setDraggingFrame:dragging_frame contents:image]; | |
116 | |
117 [bridge_->ns_view() beginDraggingSessionWithItems:@[ drag_item.get() ] | |
118 event:event | |
119 source:bridge_->ns_view()]; | |
120 base::RunLoop run_loop; | |
121 quit_closure_ = run_loop.QuitClosure(); | |
122 is_run_loop_running_ = YES; | |
123 run_loop.Run(); | |
124 } | |
125 | |
126 NSDragOperation DragDropClientMac::DragUpdate(id<NSDraggingInfo> sender) { | |
127 int drag_operation = ui::DragDropTypes::DRAG_NONE; | |
128 | |
129 // Since dragging from non MacView sources does not generate OSExchangeData, | |
130 // we need to generate one based on the provided pasteboard. | |
131 if (!data_source_.get()) { | |
132 data_source_.reset([[CocoaDragDropDataProvider alloc] | |
133 initWithPasteboard:[sender draggingPasteboard]]); | |
134 } | |
135 | |
136 drag_operation = drop_helper_->OnDragOver( | |
137 *[data_source_ data], LocationInView([sender draggingLocation]), | |
138 operation_); | |
139 return ui::DragDropTypes::DragOperationToNSDragOperation(drag_operation); | |
140 } | |
141 | |
142 NSDragOperation DragDropClientMac::Drop(id<NSDraggingInfo> sender) { | |
143 int drag_operation = drop_helper_->OnDrop( | |
144 *[data_source_ data], LocationInView([sender draggingLocation]), | |
145 operation_); | |
146 return ui::DragDropTypes::DragOperationToNSDragOperation(drag_operation); | |
147 } | |
148 | |
149 void DragDropClientMac::EndDrag() { | |
150 data_source_.reset(); | |
151 if (is_run_loop_running_) | |
tapted
2016/06/01 01:57:01
Does this work:
// Allow a test to invoke EndDr
spqchan
2016/06/01 19:09:29
Nice! Yep, it works
| |
152 quit_closure_.Run(); | |
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 |