Chromium Code Reviews| 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 std::unique_ptr<ui::OSExchangeData> data_; | |
| 20 } | |
| 21 | |
| 22 @end | |
| 23 | |
| 24 @implementation CocoaDragDropDataProvider | |
| 25 | |
| 26 - (id)initWithData:(const ui::OSExchangeData&)data { | |
| 27 if ((self = [super init])) { | |
| 28 data_.reset(new OSExchangeData(data.provider().Clone())); | |
| 29 } | |
| 30 return self; | |
| 31 } | |
| 32 | |
| 33 - (id)initWithPasteboard:(NSPasteboard*)pasteboard { | |
| 34 if ((self = [super init])) { | |
| 35 data_ = ui::OSExchangeDataProviderMac::CreateDataFromPasteboard(pasteboard); | |
| 36 } | |
| 37 return self; | |
| 38 } | |
| 39 | |
| 40 - (ui::OSExchangeData*)data { | |
| 41 return data_.get(); | |
| 42 } | |
| 43 | |
| 44 // NSPasteboardItemDataProvider protocol implementation. | |
| 45 | |
| 46 - (void)pasteboard:(NSPasteboard*)sender | |
| 47 item:(NSPasteboardItem*)item | |
| 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 : operation_(0), 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 View* view, | |
| 69 const ui::OSExchangeData& data, | |
| 70 const gfx::Point& location, | |
| 71 int operation, | |
| 72 ui::DragDropTypes::DragEventSource source) { | |
| 73 data_source_.reset([[CocoaDragDropDataProvider alloc] initWithData:data]); | |
| 74 view_ = view; | |
| 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:provider.GetPasteboardTypes()]; | |
| 101 | |
| 102 base::scoped_nsobject<NSDraggingItem> dragItem( | |
| 103 [[NSDraggingItem alloc] initWithPasteboardWriter:item.get()]); | |
| 104 NSRect draggingFrame = | |
| 105 NSMakeRect([event locationInWindow].x, | |
| 106 [event locationInWindow].y - [image size].height, | |
| 107 [image size].width, [image size].height); | |
| 108 [dragItem setDraggingFrame:draggingFrame contents:image]; | |
| 109 | |
| 110 [bridge_->ns_view() beginDraggingSessionWithItems:@[ dragItem.get() ] | |
| 111 event:event | |
| 112 source:nil]; | |
|
tapted
2016/05/26 12:31:42
ping? (The 10.11 SDK requires this to be non-nil)
spqchan
2016/05/27 23:25:48
Ah sorry, it looks like when I switched branches,
| |
| 113 } | |
| 114 | |
| 115 NSDragOperation DragDropClientMac::DragUpdate(id<NSDraggingInfo> sender) { | |
| 116 int drag_operation = ui::DragDropTypes::DRAG_NONE; | |
| 117 | |
| 118 // Since dragging from non MacView sources does not generate OSExchangeData, | |
| 119 // we need to generate one based on the provided pasteboard. | |
| 120 if (!data_source_.get()) { | |
| 121 data_source_.reset([[CocoaDragDropDataProvider alloc] | |
| 122 initWithPasteboard:[sender draggingPasteboard]]); | |
| 123 } | |
| 124 | |
| 125 drag_operation = drop_helper_->OnDragOver( | |
| 126 *[data_source_ data], LocationInView([sender draggingLocation]), | |
| 127 operation_); | |
| 128 return ui::DragDropTypes::DragOperationToNSDragOperation(drag_operation); | |
| 129 } | |
| 130 | |
| 131 NSDragOperation DragDropClientMac::Drop(id<NSDraggingInfo> sender) { | |
| 132 int drag_operation = drop_helper_->OnDrop( | |
| 133 *[data_source_ data], LocationInView([sender draggingLocation]), | |
| 134 operation_); | |
| 135 return ui::DragDropTypes::DragOperationToNSDragOperation(drag_operation); | |
| 136 } | |
| 137 | |
| 138 void DragDropClientMac::EndDrag() { | |
| 139 bridge_->native_widget_mac()->GetWidget()->DraggingEnded(view_); | |
| 140 view_ = nullptr; | |
| 141 data_source_.reset(); | |
| 142 } | |
| 143 | |
| 144 void DragDropClientMac::SetRootView(View* view) { | |
| 145 drop_helper_.reset(new DropHelper(view)); | |
| 146 } | |
| 147 | |
| 148 gfx::Point DragDropClientMac::LocationInView(NSPoint point) const { | |
| 149 return gfx::Point(point.x, NSHeight([bridge_->ns_window() frame]) - point.y); | |
| 150 } | |
| 151 | |
| 152 } // namespace views | |
| OLD | NEW |