|
| 1 | +DragStarting |
| 2 | +=== |
| 3 | + |
| 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. |
| 6 | + |
| 7 | +# Examples |
| 8 | +## 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 event args to start their own |
| 11 | +drag or they can just set `Handled` to `FALSE` to ignore the drag. |
| 12 | + |
| 13 | +```C++ |
| 14 | +CHECK_FAILURE(m_compControllerStaging->add_DragStarting( |
| 15 | + Callback<ICoreWebView2StagingDragStartingEventHandler>( |
| 16 | + [this](ICoreWebView2CompositionController* sender, ICoreWebView2StagingDragStartingEventArgs* args) |
| 17 | + { |
| 18 | + if (m_dragOverrideMode != DragOverrideMode::OVERRIDE) |
| 19 | + { |
| 20 | + // If the event is marked handled, WebView2 will not execute its drag logic. |
| 21 | + args->put_Handled(m_dragOverrideMode == DragOverrideMode::NOOP); |
| 22 | + return S_OK; |
| 23 | + } |
| 24 | + |
| 25 | + COREWEBVIEW2_DRAG_EFFECTS allowedEffects; |
| 26 | + POINT dragPosition; |
| 27 | + wil::com_ptr<IDataObject> dragData; |
| 28 | + wil::com_ptr<ICoreWebView2Deferral> deferral; |
| 29 | + |
| 30 | + args->get_DragAllowedOperations(&allowedEffects); |
| 31 | + args->get_DragPosition(&dragPosition); |
| 32 | + args->get_DragData(&dragData); |
| 33 | + args->GetDeferral(&deferral); |
| 34 | + |
| 35 | + HRESULT hr = S_OK; |
| 36 | + if (!m_oleInitialized) |
| 37 | + { |
| 38 | + hr = OleInitialize(nullptr); |
| 39 | + if (SUCCEEDED(hr)) |
| 40 | + { |
| 41 | + m_oleInitialized = true; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if (!m_dropSource) |
| 46 | + { |
| 47 | + m_dropSource = Make<ScenarioDragDropOverrideDropSource>(); |
| 48 | + } |
| 49 | + |
| 50 | + if (SUCCEEDED(hr)) |
| 51 | + { |
| 52 | + DWORD effect; |
| 53 | + DWORD okEffects = DROPEFFECT_NONE; |
| 54 | + if (allowedEffects & COREWEBVIEW2_DRAG_EFFECTS_COPY) |
| 55 | + { |
| 56 | + okEffects |= DROPEFFECT_COPY; |
| 57 | + } |
| 58 | + if (allowedEffects & COREWEBVIEW2_DRAG_EFFECTS_MOVE) |
| 59 | + { |
| 60 | + okEffects |= DROPEFFECT_MOVE; |
| 61 | + } |
| 62 | + if (allowedEffects & COREWEBVIEW2_DRAG_EFFECTS_LINK) |
| 63 | + { |
| 64 | + okEffects |= DROPEFFECT_LINK; |
| 65 | + } |
| 66 | + |
| 67 | + hr = DoDragDrop(dragData.get(), m_dropSource.get(), okEffects, &effect); |
| 68 | + } |
| 69 | + |
| 70 | + deferral->Complete(); |
| 71 | + args->put_Handled(TRUE); |
| 72 | + |
| 73 | + return hr; |
| 74 | + }) |
| 75 | + .Get(), |
| 76 | + &m_dragStartingToken)); |
| 77 | +``` |
| 78 | +
|
| 79 | +# API Details |
| 80 | +```C++ |
| 81 | +/// Flags enum that represents the effects that a given WebView2 drag drop operation can have. |
| 82 | +[v1_enum] |
| 83 | +typedef enum COREWEBVIEW2_DRAG_EFFECTS { |
| 84 | + /// Drag operation supports no effect. |
| 85 | + COREWEBVIEW2_DRAG_EFFECTS_NONE = 0x0, |
| 86 | + /// Drag operation supports copying data. |
| 87 | + COREWEBVIEW2_DRAG_EFFECTS_COPY = 0x1, |
| 88 | + /// Drag operation supports moving data. |
| 89 | + COREWEBVIEW2_DRAG_EFFECTS_MOVE = 0x2, |
| 90 | + /// Drag operation supports linking data. |
| 91 | + COREWEBVIEW2_DRAG_EFFECTS_LINK = 0x4, |
| 92 | +} COREWEBVIEW2_DRAG_EFFECTS; |
| 93 | +cpp_quote("DEFINE_ENUM_FLAG_OPERATORS(COREWEBVIEW2_DRAG_EFFECTS)") |
| 94 | +
|
| 95 | +/// Event args for the `DragStarting` event. |
| 96 | +[uuid(edb6b243-334f-59d0-b3b3-de87dd401adc), object, pointer_default(unique)] |
| 97 | +interface ICoreWebView2StagingDragStartingEventArgs : IUnknown { |
| 98 | + /// The operations this drag data supports. |
| 99 | + [propget] HRESULT DragAllowedOperations([out, retval] COREWEBVIEW2_DRAG_EFFECTS* value); |
| 100 | +
|
| 101 | +
|
| 102 | + /// The data being dragged. |
| 103 | + [propget] HRESULT DragData([out, retval] IDataObject** value); |
| 104 | +
|
| 105 | + /// The position at which drag was detected. |
| 106 | + [propget] HRESULT DragPosition([out, retval] POINT* value); |
| 107 | +
|
| 108 | +
|
| 109 | + /// Gets the `Handled` property. |
| 110 | + [propget] HRESULT Handled([out, retval] BOOL* value); |
| 111 | +
|
| 112 | +
|
| 113 | + /// Indicates whether this event has been handled by the app. If the |
| 114 | + /// app handles this event, WebView2 will not initiate drag drop. If |
| 115 | + /// the app does not handle the event, WebView2 will initiate its own |
| 116 | + /// drag drop logic. |
| 117 | + [propput] HRESULT Handled([in] BOOL value); |
| 118 | +
|
| 119 | +
|
| 120 | +
|
| 121 | + /// Returns an `ICoreWebView2Deferral` object. Use this operation to complete |
| 122 | + /// the CoreWebView2DragStartingEventArgs. |
| 123 | + /// |
| 124 | + /// Until the deferral is completed, subsequent attempts to initiate drag |
| 125 | + /// in the WebView2 will fail and if the cursor was changed as part of |
| 126 | + /// drag it will not restore. |
| 127 | + HRESULT GetDeferral( |
| 128 | + [out, retval] ICoreWebView2Deferral** value); |
| 129 | +
|
| 130 | +
|
| 131 | +} |
| 132 | +
|
| 133 | +/// Receives `DragStarting` events. |
| 134 | +[uuid(3b149321-83c3-5d1f-b03f-a42899bc1c15), object, pointer_default(unique)] |
| 135 | +interface ICoreWebView2StagingDragStartingEventHandler : IUnknown { |
| 136 | + /// Provides the event args for the corresponding event. |
| 137 | + HRESULT Invoke( |
| 138 | + [in] ICoreWebView2CompositionController* sender, |
| 139 | + [in] ICoreWebView2StagingDragStartingEventArgs* args); |
| 140 | +} |
| 141 | +
|
| 142 | +/// A continuation of the ICoreWebView2CompositionController4 interface. |
| 143 | +/// This interface includes an API which exposes the DragStarting event. |
| 144 | +[uuid(975d6824-6a02-5e98-ab7c-e4679d5357f4), object, pointer_default(unique)] |
| 145 | +interface ICoreWebView2StagingCompositionController : IUnknown { |
| 146 | + /// Adds an event handler for the `DragStarting` event. |
| 147 | + /// Adds an event handler for the `DragStarting` event. `DragStarting` is |
| 148 | + /// raised when the WebView2 detects a drag started within the WebView2. |
| 149 | + /// This event can be used to override WebView2's default drag starting |
| 150 | + /// logic. |
| 151 | + HRESULT add_DragStarting( |
| 152 | + [in] ICoreWebView2StagingDragStartingEventHandler* eventHandler, |
| 153 | + [out] EventRegistrationToken* token); |
| 154 | +
|
| 155 | + /// Removes an event handler previously added with `add_DragStarting`. |
| 156 | + HRESULT remove_DragStarting( |
| 157 | + [in] EventRegistrationToken token); |
| 158 | +
|
| 159 | +
|
| 160 | +} |
| 161 | +``` |
| 162 | +```c# (but really MIDL3) |
| 163 | + runtimeclass CoreWebView2DragStartingEventArgs |
| 164 | + { |
| 165 | + |
| 166 | + Windows.ApplicationModel.DataTransfer.DataPackageOperation DragAllowedOperations { get; }; |
| 167 | + |
| 168 | + Windows.ApplicationModel.DataTransfer.DataPackage DragData { get; }; |
| 169 | + |
| 170 | + Windows.Foundation.Point DragPosition { get; }; |
| 171 | + |
| 172 | + Boolean Handled { get; set; }; |
| 173 | + |
| 174 | + |
| 175 | + Windows.Foundation.Deferral GetDeferral(); |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | + runtimeclass CoreWebView2CompositionController : CoreWebView2Controller |
| 182 | + { |
| 183 | + // ... |
| 184 | + [interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2StagingCompositionController")] |
| 185 | + { |
| 186 | + |
| 187 | + event Windows.Foundation.TypedEventHandler<CoreWebView2CompositionController, CoreWebView2DragStartingEventArgs> DragStarting; |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | + } |
| 192 | + // ... |
| 193 | + } |
| 194 | +``` |
0 commit comments