 Chromium Code Reviews
 Chromium Code Reviews Issue 1964283002:
  MacViews: Implemented Drag & Drop  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1964283002:
  MacViews: Implemented Drag & Drop  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| Index: ui/views/cocoa/drag_drop_client_mac.mm | 
| diff --git a/ui/views/cocoa/drag_drop_client_mac.mm b/ui/views/cocoa/drag_drop_client_mac.mm | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..d7db786a02a5dd4bb307fe1ebea13c9d68f481a7 | 
| --- /dev/null | 
| +++ b/ui/views/cocoa/drag_drop_client_mac.mm | 
| @@ -0,0 +1,157 @@ | 
| +// Copyright 2016 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#import "ui/views/cocoa/drag_drop_client_mac.h" | 
| + | 
| +#include "base/mac/mac_util.h" | 
| +#include "base/strings/sys_string_conversions.h" | 
| +#include "ui/base/dragdrop/os_exchange_data.h" | 
| +#import "ui/base/dragdrop/os_exchange_data_provider_mac.h" | 
| +#include "ui/gfx/image/image_skia_util_mac.h" | 
| +#include "ui/views/drag_utils.h" | 
| +#import "ui/views/cocoa/bridged_content_view.h" | 
| +#import "ui/views/cocoa/bridged_native_widget.h" | 
| +#include "ui/views/widget/drop_helper.h" | 
| +#include "ui/views/widget/native_widget_mac.h" | 
| + | 
| +@interface CocoaDragDropDataProvider () { | 
| + 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.
 | 
| +} | 
| + | 
| +@end | 
| + | 
| +@implementation CocoaDragDropDataProvider | 
| + | 
| +- (id)initWithData:(const ui::OSExchangeData&)data { | 
| + if ((self = [super init])) { | 
| + data_.reset(new OSExchangeData(data.provider().Clone())); | 
| + } | 
| + return self; | 
| +} | 
| + | 
| +- (id)initWithPasteboard:(NSPasteboard*)pasteboard { | 
| + if ((self = [super init])) { | 
| + data_ = ui::OSExchangeDataProviderMac::CreateDataFromPasteboard(pasteboard); | 
| + } | 
| + return self; | 
| +} | 
| + | 
| +- (ui::OSExchangeData*)data { | 
| + return data_.get(); | 
| +} | 
| + | 
| +// NSPasteboardItemDataProvider protocol implementation. | 
| + | 
| +- (void)pasteboard:(NSPasteboard*)sender | 
| + item:(NSPasteboardItem*)item | 
| + provideDataForType:(NSString*)type { | 
| + const ui::OSExchangeDataProviderMac* provider = | 
| 
tapted
2016/05/31 11:49:58
nit: 
const ui::OSExchangeDataProviderMac& provid
 
spqchan
2016/05/31 23:06:13
Done.
 | 
| + static_cast<const ui::OSExchangeDataProviderMac*>(&data_->provider()); | 
| + NSData* ns_data = provider->GetNSDataForType(type); | 
| + [sender setData:ns_data forType:type]; | 
| +} | 
| + | 
| +@end | 
| + | 
| +namespace views { | 
| + | 
| +DragDropClientMac::DragDropClientMac(BridgedNativeWidget* bridge) | 
| + : 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.
 | 
| + DCHECK(bridge); | 
| + drop_helper_.reset(new DropHelper([bridge->ns_view() hostedView])); | 
| +} | 
| + | 
| +DragDropClientMac::~DragDropClientMac() {} | 
| + | 
| +void DragDropClientMac::StartDragAndDrop( | 
| + View* view, | 
| + const ui::OSExchangeData& data, | 
| + 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.
 | 
| + int operation, | 
| + ui::DragDropTypes::DragEventSource source) { | 
| + data_source_.reset([[CocoaDragDropDataProvider alloc] initWithData:data]); | 
| + view_ = view; | 
| + operation_ = operation; | 
| + | 
| + const ui::OSExchangeDataProviderMac& provider = | 
| + static_cast<const ui::OSExchangeDataProviderMac&>(data.provider()); | 
| + | 
| + // Synthesize an event for dragging, since we can't be sure that | 
| + // [NSApp currentEvent] will return a valid dragging event. | 
| + NSWindow* window = bridge_->ns_window(); | 
| + NSPoint position = [window mouseLocationOutsideOfEventStream]; | 
| + NSTimeInterval event_time = [[NSApp currentEvent] timestamp]; | 
| + NSEvent* event = [NSEvent mouseEventWithType:NSLeftMouseDragged | 
| + location:position | 
| + modifierFlags:NSLeftMouseDraggedMask | 
| + timestamp:event_time | 
| + windowNumber:[window windowNumber] | 
| + context:nil | 
| + eventNumber:0 | 
| + clickCount:1 | 
| + pressure:1.0]; | 
| + | 
| + NSImage* image = gfx::NSImageFromImageSkiaWithColorSpace( | 
| + provider.GetDragImage(), base::mac::GetSRGBColorSpace()); | 
| + | 
| + base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]); | 
| + [item setDataProvider:data_source_.get() | 
| + forTypes:ui::OSExchangeDataProviderMac:: | 
| + SupportedPasteboardTypes()]; | 
| + | 
| + 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.
 | 
| + [[NSDraggingItem alloc] initWithPasteboardWriter:item.get()]); | 
| + NSRect draggingFrame = | 
| 
tapted
2016/05/31 11:49:58
Maybe
  NSRect dragging_frame = { [event locationI
 
spqchan
2016/05/31 23:06:13
Done.
 | 
| + NSMakeRect([event locationInWindow].x, | 
| + [event locationInWindow].y - [image size].height, | 
| + [image size].width, [image size].height); | 
| + [dragItem setDraggingFrame:draggingFrame contents:image]; | 
| + | 
| + [bridge_->ns_view() beginDraggingSessionWithItems:@[ dragItem.get() ] | 
| + event:event | 
| + source:bridge_->ns_view()]; | 
| + | 
| + while (data_source_.get()) { | 
| + [[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.
 | 
| + beforeDate:[NSDate distantFuture]]; | 
| + } | 
| +} | 
| + | 
| +NSDragOperation DragDropClientMac::DragUpdate(id<NSDraggingInfo> sender) { | 
| + int drag_operation = ui::DragDropTypes::DRAG_NONE; | 
| + | 
| + // Since dragging from non MacView sources does not generate OSExchangeData, | 
| + // we need to generate one based on the provided pasteboard. | 
| + if (!data_source_.get()) { | 
| + data_source_.reset([[CocoaDragDropDataProvider alloc] | 
| + initWithPasteboard:[sender draggingPasteboard]]); | 
| + } | 
| + | 
| + drag_operation = drop_helper_->OnDragOver( | 
| + *[data_source_ data], LocationInView([sender draggingLocation]), | 
| + operation_); | 
| + return ui::DragDropTypes::DragOperationToNSDragOperation(drag_operation); | 
| +} | 
| + | 
| +NSDragOperation DragDropClientMac::Drop(id<NSDraggingInfo> sender) { | 
| + int drag_operation = drop_helper_->OnDrop( | 
| + *[data_source_ data], LocationInView([sender draggingLocation]), | 
| + operation_); | 
| + return ui::DragDropTypes::DragOperationToNSDragOperation(drag_operation); | 
| +} | 
| + | 
| +void DragDropClientMac::EndDrag() { | 
| + view_ = nullptr; | 
| + data_source_.reset(); | 
| +} | 
| + | 
| +void DragDropClientMac::SetRootView(View* view) { | 
| + drop_helper_.reset(new DropHelper(view)); | 
| +} | 
| + | 
| +gfx::Point DragDropClientMac::LocationInView(NSPoint point) const { | 
| + return gfx::Point(point.x, NSHeight([bridge_->ns_window() frame]) - point.y); | 
| +} | 
| + | 
| +} // namespace views |