@@ -2,85 +2,87 @@ DragStarting
2
2
===
3
3
4
4
# 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.
6
9
7
10
# Examples
8
11
## 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.
14
17
15
18
``` 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)
19
25
{
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};
29
29
wil::com_ptr<IDataObject > dragData;
30
- wil::com_ptr<ICoreWebView2Deferral> deferral;
31
30
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));
46
34
47
35
if (!m_dropSource)
48
36
{
49
37
m_dropSource = Make<ScenarioDragDropOverrideDropSource>();
50
38
}
51
39
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)
53
51
{
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;
70
53
}
71
54
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));
74
59
75
60
return hr;
76
61
})
77
62
.Get(),
78
63
&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));
79
78
```
80
79
81
80
# API Details
82
81
```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.
84
86
[v1_enum]
85
87
typedef enum COREWEBVIEW2_DRAG_EFFECTS {
86
88
/// Drag operation supports no effect.
@@ -96,16 +98,18 @@ cpp_quote("DEFINE_ENUM_FLAG_OPERATORS(COREWEBVIEW2_DRAG_EFFECTS)")
96
98
97
99
/// Event args for the `DragStarting` event.
98
100
[uuid(edb6b243-334f-59d0-b3b3-de87dd401adc), object, pointer_default(unique)]
99
- interface ICoreWebView2StagingDragStartingEventArgs : IUnknown {
101
+ interface ICoreWebView2DragStartingEventArgs : IUnknown {
100
102
/// 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);
102
105
103
106
104
107
/// The data being dragged.
105
- [propget] HRESULT DragData ([out, retval] IDataObject** value);
108
+ [propget] HRESULT Data ([out, retval] IDataObject** value);
106
109
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);
109
113
110
114
111
115
/// Gets the `Handled` property.
@@ -134,24 +138,25 @@ interface ICoreWebView2StagingDragStartingEventArgs : IUnknown {
134
138
135
139
/// Receives `DragStarting` events.
136
140
[uuid(3b149321-83c3-5d1f-b03f-a42899bc1c15), object, pointer_default(unique)]
137
- interface ICoreWebView2StagingDragStartingEventHandler : IUnknown {
141
+ interface ICoreWebView2DragStartingEventHandler : IUnknown {
138
142
/// Provides the event args for the corresponding event.
139
143
HRESULT Invoke(
140
- [in] ICoreWebView2CompositionController * sender,
141
- [in] ICoreWebView2StagingDragStartingEventArgs * args);
144
+ [in] ICoreWebView2CompositionController5 * sender,
145
+ [in] ICoreWebView2DragStartingEventArgs * args);
142
146
}
143
147
144
148
/// A continuation of the ICoreWebView2CompositionController4 interface.
145
149
/// This interface includes an API which exposes the DragStarting event.
146
150
[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 {
149
152
/// Adds an event handler for the `DragStarting` event. `DragStarting` is
150
153
/// 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.
153
158
HRESULT add_DragStarting(
154
- [in] ICoreWebView2StagingDragStartingEventHandler * eventHandler,
159
+ [in] ICoreWebView2DragStartingEventHandler * eventHandler,
155
160
[out] EventRegistrationToken* token);
156
161
157
162
/// Removes an event handler previously added with `add_DragStarting`.
@@ -162,14 +167,16 @@ interface ICoreWebView2StagingCompositionController : IUnknown {
162
167
}
163
168
```
164
169
``` c# (but really MIDL3)
170
+ namespace Microoft .Web .WebView2 {
165
171
runtimeclass CoreWebView2DragStartingEventArgs
166
172
{
167
173
168
- Windows .ApplicationModel .DataTransfer .DataPackageOperation DragAllowedOperations { get ; };
174
+ Windows.ApplicationModel.DataTransfer.DataPackageOperation
175
+ AllowedOperations { get; };
169
176
170
- Windows .ApplicationModel .DataTransfer .DataPackage DragData { get ; };
177
+ Windows .ApplicationModel .DataTransfer .DataPackage Data { get ; };
171
178
172
- Windows .Foundation .Point DragPosition { get ; };
179
+ Windows .Foundation .Point Position { get ; };
173
180
174
181
Boolean Handled { get ; set ; };
175
182
@@ -183,14 +190,15 @@ interface ICoreWebView2StagingCompositionController : IUnknown {
183
190
runtimeclass CoreWebView2CompositionController : CoreWebView2Controller
184
191
{
185
192
// ...
186
- [interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2StagingCompositionController " )]
193
+ [interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2CompositionController5 " )]
187
194
{
188
-
189
- event Windows . Foundation . TypedEventHandler < CoreWebView2CompositionController , CoreWebView2DragStartingEventArgs > DragStarting ;
190
-
195
+ event Windows . Foundation . TypedEventHandler <
196
+ CoreWebView2CompositionController ,
197
+ CoreWebView2DragStartingEventArgs > DragStarting ;
191
198
192
199
193
200
}
194
201
// ...
195
202
}
203
+ }
196
204
```
0 commit comments