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_; | |
|
tapted
2016/05/31 11:49:58
ah, so I think this can actually go to the @implem
spqchan
2016/05/31 23:06:12
Done.
| |
| 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 = | |
|
tapted
2016/05/31 11:49:58
nit:
const ui::OSExchangeDataProviderMac& provid
spqchan
2016/05/31 23:06:13
Done.
| |
| 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) { | |
|
tapted
2016/05/31 11:49:58
add (if we keep it): view_(nullptr)
spqchan
2016/05/31 23:06:13
Done.
| |
| 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, | |
|
tapted
2016/05/31 11:49:58
We're not using |location| here...We should probab
spqchan
2016/05/31 23:06:13
Done.
| |
| 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:ui::OSExchangeDataProviderMac:: | |
| 101 SupportedPasteboardTypes()]; | |
| 102 | |
| 103 base::scoped_nsobject<NSDraggingItem> dragItem( | |
|
tapted
2016/05/31 11:49:58
nit: dragItem -> drag_item since we're in a C++ fn
spqchan
2016/05/31 23:06:13
Done.
| |
| 104 [[NSDraggingItem alloc] initWithPasteboardWriter:item.get()]); | |
| 105 NSRect draggingFrame = | |
|
tapted
2016/05/31 11:49:58
Maybe
NSRect dragging_frame = { [event locationI
spqchan
2016/05/31 23:06:13
Done.
| |
| 106 NSMakeRect([event locationInWindow].x, | |
| 107 [event locationInWindow].y - [image size].height, | |
| 108 [image size].width, [image size].height); | |
| 109 [dragItem setDraggingFrame:draggingFrame contents:image]; | |
| 110 | |
| 111 [bridge_->ns_view() beginDraggingSessionWithItems:@[ dragItem.get() ] | |
| 112 event:event | |
| 113 source:bridge_->ns_view()]; | |
| 114 | |
| 115 while (data_source_.get()) { | |
| 116 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode | |
|
tapted
2016/05/31 11:49:58
We should use the run loop stuff in base (what you
spqchan
2016/05/31 23:06:13
Done.
| |
| 117 beforeDate:[NSDate distantFuture]]; | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 NSDragOperation DragDropClientMac::DragUpdate(id<NSDraggingInfo> sender) { | |
| 122 int drag_operation = ui::DragDropTypes::DRAG_NONE; | |
| 123 | |
| 124 // Since dragging from non MacView sources does not generate OSExchangeData, | |
| 125 // we need to generate one based on the provided pasteboard. | |
| 126 if (!data_source_.get()) { | |
| 127 data_source_.reset([[CocoaDragDropDataProvider alloc] | |
| 128 initWithPasteboard:[sender draggingPasteboard]]); | |
| 129 } | |
| 130 | |
| 131 drag_operation = drop_helper_->OnDragOver( | |
| 132 *[data_source_ data], LocationInView([sender draggingLocation]), | |
| 133 operation_); | |
| 134 return ui::DragDropTypes::DragOperationToNSDragOperation(drag_operation); | |
| 135 } | |
| 136 | |
| 137 NSDragOperation DragDropClientMac::Drop(id<NSDraggingInfo> sender) { | |
| 138 int drag_operation = drop_helper_->OnDrop( | |
| 139 *[data_source_ data], LocationInView([sender draggingLocation]), | |
| 140 operation_); | |
| 141 return ui::DragDropTypes::DragOperationToNSDragOperation(drag_operation); | |
| 142 } | |
| 143 | |
| 144 void DragDropClientMac::EndDrag() { | |
| 145 view_ = nullptr; | |
| 146 data_source_.reset(); | |
| 147 } | |
| 148 | |
| 149 void DragDropClientMac::SetRootView(View* view) { | |
| 150 drop_helper_.reset(new DropHelper(view)); | |
| 151 } | |
| 152 | |
| 153 gfx::Point DragDropClientMac::LocationInView(NSPoint point) const { | |
| 154 return gfx::Point(point.x, NSHeight([bridge_->ns_window() frame]) - point.y); | |
| 155 } | |
| 156 | |
| 157 } // namespace views | |
| OLD | NEW |