Skip to content

Commit 4e3c287

Browse files
WPF - Ensure only single drag operation is emit on DragSourceEndedAt (#4814)
* refactor: ensure only single event is sent to browser host * fix: remove unnecessary code * fix: update comments
1 parent 3f15ee8 commit 4e3c287

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

CefSharp.Wpf/ChromiumWebBrowser.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

55
using System;
6+
using System.Collections.Generic;
67
using System.Runtime.CompilerServices;
78
using System.Threading;
89
using System.Threading.Tasks;
@@ -133,7 +134,7 @@ public partial class ChromiumWebBrowser : Control, IRenderWebBrowser, IWpfWebBro
133134
/// <summary>
134135
/// Keep the current drag&amp;drop effects to return the appropriate effects on drag over.
135136
/// </summary>
136-
private DragDropEffects currentDragDropEffects;
137+
private DragDropEffects? currentDragDropEffects;
137138
/// <summary>
138139
/// A flag that indicates whether or not the designer is active
139140
/// NOTE: Needs to be static for OnApplicationExit
@@ -919,15 +920,25 @@ protected virtual bool StartDragging(IDragData dragData, DragOperationsMask allo
919920
{
920921
if (browser != null)
921922
{
922-
//DoDragDrop will fire DragEnter event
923+
// DoDragDrop will fire DragEnter event
923924
var result = DragDrop.DoDragDrop(this, dataObject, allowedOps.GetDragEffects());
925+
var dragEffects = currentDragDropEffects.HasValue ? currentDragDropEffects.Value : DragDropEffects.None;
924926

925-
//DragData was stored so when DoDragDrop fires DragEnter we reuse a clone of the IDragData provided here
927+
// Ensure the drag and drop operation wasn't cancelled
928+
var finalEffectMask = dragEffects.GetDragOperationsMask() & result.GetDragOperationsMask();
929+
930+
// We shouldn't have an instance where finalEffectMask signfies multiple effects, as the operation
931+
// set by UpdateDragCursor should only ever be none, move, copy, or link. However, if it does, we'll
932+
// just default to copy, as that reflects the defaulting behavior of GetDragEffects.
933+
var finalSingleEffect = finalEffectMask.HasFlag(DragOperationsMask.Every) ? DragOperationsMask.Copy : finalEffectMask;
934+
935+
// DragData was stored so when DoDragDrop fires DragEnter we reuse a clone of the IDragData provided here
926936
currentDragData = null;
937+
currentDragDropEffects = null;
927938

928-
//If result == DragDropEffects.None then we'll send DragOperationsMask.None
929-
//effectively cancelling the drag operation
930-
browser.GetHost().DragSourceEndedAt(x, y, result.GetDragOperationsMask());
939+
// If result or the last recorded drag drop effect were DragDropEffects.None
940+
// then we'll send DragOperationsMask.None, effectively cancelling the drag operation
941+
browser.GetHost().DragSourceEndedAt(x, y, finalSingleEffect);
931942
browser.GetHost().DragSourceSystemDragEnded();
932943
}
933944
});
@@ -1683,7 +1694,6 @@ private void OnDrop(object sender, DragEventArgs e)
16831694
{
16841695
var mouseEvent = GetMouseEvent(e);
16851696
var effect = e.AllowedEffects.GetDragOperationsMask();
1686-
16871697
browser.GetHost().DragTargetDragOver(mouseEvent, effect);
16881698
browser.GetHost().DragTargetDragDrop(mouseEvent);
16891699
}
@@ -1713,7 +1723,7 @@ private void OnDragOver(object sender, DragEventArgs e)
17131723
{
17141724
browser.GetHost().DragTargetDragOver(GetMouseEvent(e), e.AllowedEffects.GetDragOperationsMask());
17151725
}
1716-
e.Effects = currentDragDropEffects;
1726+
e.Effects = currentDragDropEffects.HasValue ? currentDragDropEffects.Value : DragDropEffects.None;
17171727
e.Handled = true;
17181728
}
17191729

@@ -1731,6 +1741,7 @@ private void OnDragEnter(object sender, DragEventArgs e)
17311741

17321742
//DoDragDrop will fire this handler for internally sourced Drag/Drop operations
17331743
//we use the existing IDragData (cloned copy)
1744+
17341745
var dragData = currentDragData ?? e.GetDragData();
17351746

17361747
browser.GetHost().DragTargetDragEnter(dragData, mouseEvent, effect);
@@ -2323,7 +2334,7 @@ protected override void OnMouseWheel(MouseWheelEventArgs e)
23232334
deltaX: isShiftKeyDown ? e.Delta : 0,
23242335
deltaY: !isShiftKeyDown ? e.Delta : 0,
23252336
modifiers: modifiers);
2326-
2337+
23272338
e.Handled = true;
23282339
}
23292340

@@ -2469,7 +2480,7 @@ private void OnMouseButton(MouseButtonEventArgs e)
24692480
{
24702481
clickCount = 1;
24712482
}
2472-
2483+
24732484
MousePositionTransform.TransformMousePoint(ref point);
24742485
browser.GetHost().SendMouseClickEvent((int)point.X, (int)point.Y, (MouseButtonType)e.ChangedButton, mouseUp, clickCount, modifiers);
24752486
}

0 commit comments

Comments
 (0)