@@ -2,85 +2,87 @@ DragStarting
22===
33
44# Background
5- The WebView2 team has been asked to provide a way to override the default drag drop behavior when running in visual hosting mode. This event allows you to know when a drag is initiated in WebView2 and provides the state necessary to override the default WebView2 drag operation with your own logic.
5+ The WebView2 team has been asked to provide a way to override the default drag
6+ drop behavior when running in visual hosting mode. This event allows you to know
7+ when a drag is initiated in WebView2 and provides the state necessary to override
8+ the default WebView2 drag operation with your own logic.
69
710# Examples
811## DragStarting
9- Users can use ` add_DragStarting ` on the CompositionController to add an event handler
10- that is invoked when drag is starting. They can use the the event args to start their own
11- drag. Notably the ` Deferral ` can be used to execute any async drag logic and call back into
12- the WebView at a later time. The ` Handled ` property lets the WebView2 know whether to
13- exercise its own drag logic or not.
12+ Users can use ` add_DragStarting ` on the CompositionController to add an event
13+ handler that is invoked when drag is starting. They can use the the event args
14+ to start their own drag. Notably the ` Deferral ` can be used to execute any async
15+ drag logic and call back into the WebView at a later time. The ` Handled `
16+ property lets the WebView2 know whether to exercise its own drag logic or not.
1417
1518``` C++
16- CHECK_FAILURE (m_compControllerStaging->add_DragStarting(
17- Callback<ICoreWebView2StagingDragStartingEventHandler >(
18- [ this] (ICoreWebView2CompositionController* sender, ICoreWebView2StagingDragStartingEventArgs* args)
19+ // Using DragStarting to simply make a synchronous DoDragDrop call instead of
20+ // having WebView2 do it.
21+ CHECK_FAILURE (m_compController5->add_DragStarting(
22+ Callback<ICoreWebView2DragStartingEventHandler >(
23+ [ this] (ICoreWebView2CompositionController5* sender,
24+ ICoreWebView2DragStartingEventArgs* args)
1925 {
20- if (m_dragOverrideMode != DragOverrideMode::OVERRIDE)
21- {
22- // If the event is marked handled, WebView2 will not execute its drag logic.
23- args->put_Handled(m_dragOverrideMode == DragOverrideMode::NOOP);
24- return S_OK;
25- }
26-
27- COREWEBVIEW2_DRAG_EFFECTS allowedEffects;
28- POINT dragPosition;
26+ COREWEBVIEW2_DRAG_EFFECTS allowedEffects =
27+ COREWEBVIEW2_DRAG_EFFECTS_NONE;
28+ POINT dragPosition = {0, 0};
2929 wil::com_ptr<IDataObject > dragData;
30- wil::com_ptr<ICoreWebView2Deferral> deferral;
3130
32- args->get_DragAllowedOperations(&allowedEffects);
33- args->get_DragPosition(&dragPosition);
34- args->get_DragData(&dragData);
35- args->GetDeferral(&deferral);
36-
37- HRESULT hr = S_OK;
38- if (!m_oleInitialized)
39- {
40- hr = OleInitialize(nullptr);
41- if (SUCCEEDED(hr))
42- {
43- m_oleInitialized = true;
44- }
45- }
31+ CHECK_FAILURE(args->get_AllowedOperations(&allowedEffects));
32+ CHECK_FAILURE(args->get_Position(&dragPosition));
33+ CHECK_FAILURE(args->get_Data(&dragData));
4634
4735 if (!m_dropSource)
4836 {
4937 m_dropSource = Make<ScenarioDragDropOverrideDropSource>();
5038 }
5139
52- if (SUCCEEDED(hr))
40+ DWORD effect;
41+ DWORD okEffects = DROPEFFECT_NONE;
42+ if (allowedEffects & COREWEBVIEW2_DRAG_EFFECTS_COPY)
43+ {
44+ okEffects |= DROPEFFECT_COPY;
45+ }
46+ if (allowedEffects & COREWEBVIEW2_DRAG_EFFECTS_MOVE)
47+ {
48+ okEffects |= DROPEFFECT_MOVE;
49+ }
50+ if (allowedEffects & COREWEBVIEW2_DRAG_EFFECTS_LINK)
5351 {
54- DWORD effect;
55- DWORD okEffects = DROPEFFECT_NONE;
56- if (allowedEffects & COREWEBVIEW2_DRAG_EFFECTS_COPY)
57- {
58- okEffects |= DROPEFFECT_COPY;
59- }
60- if (allowedEffects & COREWEBVIEW2_DRAG_EFFECTS_MOVE)
61- {
62- okEffects |= DROPEFFECT_MOVE;
63- }
64- if (allowedEffects & COREWEBVIEW2_DRAG_EFFECTS_LINK)
65- {
66- okEffects |= DROPEFFECT_LINK;
67- }
68-
69- hr = DoDragDrop(dragData.get(), m_dropSource.get(), okEffects, &effect);
52+ okEffects |= DROPEFFECT_LINK;
7053 }
7154
72- deferral->Complete();
73- args->put_Handled(TRUE);
55+ HRESULT hr = DoDragDrop(
56+ dragData.get(), m_dropSource.get(), okEffects, &effect);
57+
58+ args->put_Handled(SUCCEEDED(hr));
7459
7560 return hr;
7661 })
7762 .Get(),
7863 &m_dragStartingToken));
64+
65+ // Using DragStarting to no-op a drag operation.
66+ CHECK_FAILURE(m_compController5->add_DragStarting(
67+ Callback<ICoreWebView2DragStartingEventHandler >(
68+ [ this] (ICoreWebView2CompositionController5* sender,
69+ ICoreWebView2DragStartingEventArgs* args)
70+ {
71+ // If the event is marked handled, WebView2 will not execute its
72+ // drag logic.
73+ args->put_Handled(m_dragOverrideMode == DragOverrideMode::NOOP);
74+ return S_OK;
75+ })
76+ .Get(),
77+ &m_dragStartingToken));
7978```
8079
8180# API Details
8281```C++
83- /// Flags enum that represents the effects that a given WebView2 drag drop operation can have.
82+ /// Flags enum that represents the effects that a given WebView2 drag drop
83+ /// operation can have. The values of this enum align with the ole DROPEFFECT
84+ /// constant with the exception of DROPEFFECT_SCROLL which is only relevant for
85+ /// drop and therefore unsupported.
8486[v1_enum]
8587typedef enum COREWEBVIEW2_DRAG_EFFECTS {
8688 /// Drag operation supports no effect.
@@ -96,16 +98,18 @@ cpp_quote("DEFINE_ENUM_FLAG_OPERATORS(COREWEBVIEW2_DRAG_EFFECTS)")
9698
9799/// Event args for the `DragStarting` event.
98100[uuid(edb6b243-334f-59d0-b3b3-de87dd401adc), object, pointer_default(unique)]
99- interface ICoreWebView2StagingDragStartingEventArgs : IUnknown {
101+ interface ICoreWebView2DragStartingEventArgs : IUnknown {
100102 /// The operations this drag data supports.
101- [propget] HRESULT DragAllowedOperations([out, retval] COREWEBVIEW2_DRAG_EFFECTS* value);
103+ [propget] HRESULT AllowedOperations(
104+ [out, retval] COREWEBVIEW2_DRAG_EFFECTS* value);
102105
103106
104107 /// The data being dragged.
105- [propget] HRESULT DragData ([out, retval] IDataObject** value);
108+ [propget] HRESULT Data ([out, retval] IDataObject** value);
106109
107- /// The position at which drag was detected.
108- [propget] HRESULT DragPosition([out, retval] POINT* value);
110+ /// The position at which drag was detected. This position is given in
111+ /// screen pixel coordinates as opposed to WebView2 relative coordinates.
112+ [propget] HRESULT Position([out, retval] POINT* value);
109113
110114
111115 /// Gets the `Handled` property.
@@ -134,24 +138,25 @@ interface ICoreWebView2StagingDragStartingEventArgs : IUnknown {
134138
135139/// Receives `DragStarting` events.
136140[uuid(3b149321-83c3-5d1f-b03f-a42899bc1c15), object, pointer_default(unique)]
137- interface ICoreWebView2StagingDragStartingEventHandler : IUnknown {
141+ interface ICoreWebView2DragStartingEventHandler : IUnknown {
138142 /// Provides the event args for the corresponding event.
139143 HRESULT Invoke(
140- [in] ICoreWebView2CompositionController * sender,
141- [in] ICoreWebView2StagingDragStartingEventArgs * args);
144+ [in] ICoreWebView2CompositionController5 * sender,
145+ [in] ICoreWebView2DragStartingEventArgs * args);
142146}
143147
144148/// A continuation of the ICoreWebView2CompositionController4 interface.
145149/// This interface includes an API which exposes the DragStarting event.
146150[uuid(975d6824-6a02-5e98-ab7c-e4679d5357f4), object, pointer_default(unique)]
147- interface ICoreWebView2StagingCompositionController : IUnknown {
148- /// Adds an event handler for the `DragStarting` event.
151+ interface ICoreWebView2CompositionController5 : IUnknown {
149152 /// Adds an event handler for the `DragStarting` event. `DragStarting` is
150153 /// raised when the WebView2 detects a drag started within the WebView2.
151- /// This event can be used to override WebView2's default drag starting
152- /// logic.
154+ /// WebView2's default drag behavior is to synchronously call DoDragDrop when
155+ /// it detects drag. This event's args expose the data WebView2 uses to call
156+ /// DoDragDrop to allow users to implement their own drag logic and override
157+ /// WebView2's.
153158 HRESULT add_DragStarting(
154- [in] ICoreWebView2StagingDragStartingEventHandler * eventHandler,
159+ [in] ICoreWebView2DragStartingEventHandler * eventHandler,
155160 [out] EventRegistrationToken* token);
156161
157162 /// Removes an event handler previously added with `add_DragStarting`.
@@ -162,14 +167,16 @@ interface ICoreWebView2StagingCompositionController : IUnknown {
162167}
163168```
164169``` c# (but really MIDL3)
170+ namespace Microoft .Web .WebView2 {
165171 runtimeclass CoreWebView2DragStartingEventArgs
166172 {
167173
168- Windows .ApplicationModel .DataTransfer .DataPackageOperation DragAllowedOperations { get ; };
174+ Windows.ApplicationModel.DataTransfer.DataPackageOperation
175+ AllowedOperations { get; };
169176
170- Windows .ApplicationModel .DataTransfer .DataPackage DragData { get ; };
177+ Windows .ApplicationModel .DataTransfer .DataPackage Data { get ; };
171178
172- Windows .Foundation .Point DragPosition { get ; };
179+ Windows .Foundation .Point Position { get ; };
173180
174181 Boolean Handled { get ; set ; };
175182
@@ -183,14 +190,15 @@ interface ICoreWebView2StagingCompositionController : IUnknown {
183190 runtimeclass CoreWebView2CompositionController : CoreWebView2Controller
184191 {
185192 // ...
186- [interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2StagingCompositionController " )]
193+ [interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2CompositionController5 " )]
187194 {
188-
189- event Windows . Foundation . TypedEventHandler < CoreWebView2CompositionController , CoreWebView2DragStartingEventArgs > DragStarting ;
190-
195+ event Windows . Foundation . TypedEventHandler <
196+ CoreWebView2CompositionController ,
197+ CoreWebView2DragStartingEventArgs > DragStarting ;
191198
192199
193200 }
194201 // ...
195202 }
203+ }
196204```
0 commit comments