Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Unified Diff: ui/views/cocoa/drag_drop_client_mac.mm

Issue 1964283002: MacViews: Implemented Drag & Drop (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Forgot to add the test Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..dfe1f7608357676b040949a52ed4f844e1e2cf8f
--- /dev/null
+++ b/ui/views/cocoa/drag_drop_client_mac.mm
@@ -0,0 +1,134 @@
+// 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.
+
+#include "ui/views/cocoa/drag_drop_client_mac.h"
tapted 2016/05/24 08:06:02 nit: import
spqchan 2016/05/26 01:56:54 Done.
+
+#include "base/mac/mac_util.h"
+#include "base/strings/sys_string_conversions.h"
+#include "ui/base/dragdrop/os_exchange_data.h"
+#include "ui/base/dragdrop/os_exchange_data_provider_mac.h"
tapted 2016/05/24 08:06:02 nit:import
spqchan 2016/05/26 01:56:55 Done.
+#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"
+
+// This class acts as a bridge between NSPasteboardItem and OSExchangeData by
+// implementing NSPasteboardItemDataProvider and writing data from
+// OSExchangeData into the pasteboard.
+@interface CocoaDragDropDataProvider () {
+ std::unique_ptr<ui::OSExchangeData> data_;
+}
+
+@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();
+}
+
+- (void)pasteboard:(NSPasteboard*)sender
tapted 2016/05/24 08:06:02 // NSPasteboardItemDataProvider protocol implement
spqchan 2016/05/26 01:56:55 Done.
+ item:(NSPasteboardItem*)item
tapted 2016/05/24 08:06:02 nit: align colons
spqchan 2016/05/26 01:56:55 Done.
+ provideDataForType:(NSString*)type {
+ const ui::OSExchangeDataProviderMac* provider =
+ 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)
+ : bridge_(bridge) {
+ DCHECK(bridge);
+ drop_helper_.reset(new DropHelper([bridge->ns_view() hostedView]));
+}
+
+DragDropClientMac::~DragDropClientMac() {}
+
+void DragDropClientMac::StartDragAndDrop(
+ const ui::OSExchangeData& data,
+ const gfx::Point& location,
+ int operation,
+ ui::DragDropTypes::DragEventSource source) {
+ data_source_.reset([[CocoaDragDropDataProvider alloc] initWithData:data]);
+ operation_ = operation;
+
+ const ui::OSExchangeDataProviderMac* provider =
tapted 2016/05/24 08:06:02 const ui::OSExchangeDataProviderMac& (i.e. cast t
spqchan 2016/05/26 01:56:54 Done.
+ static_cast<const ui::OSExchangeDataProviderMac*>(&data.provider());
+ const base::NativeEvent event = provider->GetEvent()->native_event();
tapted 2016/05/24 08:06:02 NSEvent* event? (`const` and NS* objects tend not
spqchan 2016/05/26 01:56:54 Removed
+
+ NSImage* image = gfx::NSImageFromImageSkiaWithColorSpace(
+ provider->GetDragImage(), base::mac::GetSRGBColorSpace());
+
+ base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]);
+ [item setDataProvider:data_source_.get()
+ forTypes:provider->GetPasteboardTypes()];
+
+ base::scoped_nsobject<NSDraggingItem> dragItem(
+ [[NSDraggingItem alloc] initWithPasteboardWriter:item.get()]);
+ NSRect draggingFrame =
+ 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:nil];
tapted 2016/05/24 08:06:02 looks like source: must be non-nil to compile agai
spqchan 2016/05/26 01:56:55 Done.
+}
+
+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() {
+ 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

Powered by Google App Engine
This is Rietveld 408576698