From 11564beccae0b0746b6d62176cc689d2a608e1bf Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 16 Apr 2018 17:11:25 +1000 Subject: [PATCH] WPF - Improve DragDrop implementation Improved for when drag/drop both originate and end within the browser. Reuse the CEF IDragData for DragTargetDragEnter call No improvement has been made to dragging external items --- CefSharp.Wpf/ChromiumWebBrowser.cs | 61 ++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 8cf22ea07..2e8ccf35d 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -97,6 +97,12 @@ namespace CefSharp.Wpf /// user attempts to set after browser created) /// private IRequestContext requestContext; + /// + /// Keep a short term copy of IDragData, so when calling DoDragDrop, DragEnter is called, + /// we can reuse the drag data provided from CEF + /// + private IDragData currentDragData; + /// /// A flag that indicates whether or not the designer is active /// NOTE: Needs to be static for OnApplicationExit @@ -535,6 +541,13 @@ namespace CefSharp.Wpf BrowserSettings = null; } + //Incase we accidentally have a reference to the CEF drag data + if(currentDragData != null) + { + currentDragData.Dispose(); + currentDragData = null; + } + PresentationSource.RemoveSourceChangedHandler(this, PresentationSourceChangedHandler); // Release internal event listeners: @@ -661,14 +674,17 @@ namespace CefSharp.Wpf } /// - /// Starts the dragging. + /// Called when the user starts dragging content in the web view. + /// OS APIs that run a system message loop may be used within the StartDragging call. + /// Don't call any of IBrowserHost::DragSource*Ended* methods after returning false. + /// Call IBrowserHost.DragSourceEndedAt and DragSourceSystemDragEnded either synchronously or asynchronously to inform the web view that the drag operation has ended. /// - /// The drag data. - /// The mask. - /// The x. - /// The y. - /// true if XXXX, false otherwise. - bool IRenderWebBrowser.StartDragging(IDragData dragData, DragOperationsMask mask, int x, int y) + /// Contextual information about the dragged content + /// allowed operations + /// is the drag start location in screen coordinates + /// is the drag start location in screen coordinates + /// Return true to handle the drag operation. + bool IRenderWebBrowser.StartDragging(IDragData dragData, DragOperationsMask allowedOps, int x, int y) { var dataObject = new DataObject(); @@ -676,6 +692,10 @@ namespace CefSharp.Wpf dataObject.SetText(dragData.FragmentText, TextDataFormat.UnicodeText); dataObject.SetText(dragData.FragmentHtml, TextDataFormat.Html); + //Clone dragData for use in OnDragEnter event handler + currentDragData = dragData.Clone(); + currentDragData.ResetFileContents(); + // TODO: The following code block *should* handle images, but GetFileContents is // not yet implemented. //if (dragData.IsFile) @@ -691,8 +711,15 @@ namespace CefSharp.Wpf { if(browser != null) { - var results = DragDrop.DoDragDrop(this, dataObject, GetDragEffects(mask)); - browser.GetHost().DragSourceEndedAt(0, 0, GetDragOperationsMask(results)); + //DoDragDrop will fire DragEnter event + var result = DragDrop.DoDragDrop(this, dataObject, GetDragEffects(allowedOps)); + + //DragData was stored so when DoDragDrop fires DragEnter we reuse a clone of the IDragData provided here + currentDragData = null; + + //If result == DragDropEffects.None then we'll send DragOperationsMask.None + //effectively cancelling the drag operation + browser.GetHost().DragSourceEndedAt(x, y, GetDragOperationsMask(result)); browser.GetHost().DragSourceSystemDragEnded(); } }); @@ -1420,7 +1447,11 @@ namespace CefSharp.Wpf { if(browser != null) { - browser.GetHost().DragTargetDragDrop(GetMouseEvent(e)); + var mouseEvent = GetMouseEvent(e); + var effect = GetDragOperationsMask(e.AllowedEffects); + + browser.GetHost().DragTargetDragOver(mouseEvent, effect); + browser.GetHost().DragTargetDragDrop(mouseEvent); } } @@ -1459,7 +1490,15 @@ namespace CefSharp.Wpf { if(browser != null) { - browser.GetHost().DragTargetDragEnter(e.GetDragDataWrapper(), GetMouseEvent(e), GetDragOperationsMask(e.AllowedEffects)); + var mouseEvent = GetMouseEvent(e); + var effect = GetDragOperationsMask(e.AllowedEffects); + + //DoDragDrop will fire this handler for internally sourced Drag/Drop operations + //we use the existing IDragData (cloned copy) + var dragData = currentDragData ?? e.GetDragDataWrapper(); + + browser.GetHost().DragTargetDragEnter(dragData, mouseEvent, effect); + browser.GetHost().DragTargetDragOver(mouseEvent, effect); } }