|
| 1 | +# Background |
| 2 | +Currently the functionality of dragging and dropping external objects(e.g. files, hyperlinks) into webview2 is default enabled in webview2 and there is no way to disable it. Some developers may want to disbale this functionality in their applications based upon their scenarios. According to such feature requirements, we add the new API to provide developers with the capability to configure the external drag&drop functionality. |
| 3 | + |
| 4 | +# Description |
| 5 | +We add a new `AllowExternalDrop` property in `CoreWebView2Controller`. |
| 6 | +This API allows end developers to toggle the external drag&drop functionality easily. |
| 7 | +If it's disabled, any drag&drop actions will keep out of work when trying to drag and drop objects from outside into current webview2. |
| 8 | +To be more specific, some behaviors listed below will be impacted by the toggle of this property. |
| 9 | + |
| 10 | +* Drag&Drop files on disk to webview2 |
| 11 | +* Drag&Drop hyperlinks from browser to webview2 |
| 12 | +* Drag&Drop hyperlinks from one webview2 to another webview2 |
| 13 | + |
| 14 | +Some behaviors are not impacted by the toggle of this property like drag&drop selected text to webview2. That means toggle of this property has no impact on the expected behavior of this action. |
| 15 | + |
| 16 | +By default, AllowExternalDrop is enabled to keep consistent with the behavior we had before the API is added. |
| 17 | + |
| 18 | +Please note that drag&drop anything from webview2 to external(outside current webview2) will not be impacted by this property. |
| 19 | + |
| 20 | +# Examples |
| 21 | +## C++ |
| 22 | + |
| 23 | +```cpp |
| 24 | +wil::com_ptr<ICoreWebView2Controller4> m_controller4; |
| 25 | +void ToggleAllowDrop() |
| 26 | +{ |
| 27 | + if (m_controller4) |
| 28 | + { |
| 29 | + BOOL allowDrop; |
| 30 | + CHECK_FAILURE(m_controller4->get_AllowDrop(&allowDrop)); |
| 31 | + if (allowDrop) |
| 32 | + { |
| 33 | + CHECK_FAILURE(m_controller4->put_AllowDrop(FALSE)); |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + CHECK_FAILURE(m_controller4->put_AllowDrop(TRUE)); |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## C# |
| 44 | +```c# |
| 45 | +private CoreWebView2Controller controller |
| 46 | +void ToggleAllowDrop(object target, ExecutedRoutedEventArgs e) |
| 47 | +{ |
| 48 | + if (controller) |
| 49 | + { |
| 50 | + controller.AllowDrop = !controller.AllowDrop; |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +# Remarks |
| 56 | +The `AllowDrop` property already exists in some controls of .net UI framework like WPF and WinForms. |
| 57 | +The .net control wrapper for webview2 natively inherits this property. |
| 58 | +But actually it doesn't really take effect until this new API is added. |
| 59 | +When the new API is promoted to public, we will adjust the WPF/WinForms webview2 control to consume the new API accordingly. |
| 60 | + |
| 61 | + |
| 62 | +# API Notes |
| 63 | +See [API Details](#api-details) section below for API reference. |
| 64 | + |
| 65 | +# API Details |
| 66 | + |
| 67 | +## Win32 C++ |
| 68 | +```c# |
| 69 | +// This is the ICoreWebView2Controller interface. |
| 70 | +[uuid(320613e2-990f-4272-bf90-d243a4ff1b8a), object, pointer_default(unique)] |
| 71 | +interface ICoreWebView2Controller4 : ICoreWebView2Controller3 { |
| 72 | + /// Gets the `AllowDrop` property which is used to configure the capability |
| 73 | + /// that dropping files into webview2 is allowed or permitted. |
| 74 | + /// The default value is TRUE. |
| 75 | + /// |
| 76 | + /// \snippet SettingsComponent.cpp ToggleAllowDrop |
| 77 | + [propget] HRESULT AllowDrop([out, retval] BOOL* value); |
| 78 | + /// Sets the `AllowDrop` property which is used to configure the capability |
| 79 | + /// that dropping files into webview2 is allowed or permitted. |
| 80 | + [propput] HRESULT AllowDrop([in] BOOL value); |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## .NET and WinRT |
| 85 | +```c# |
| 86 | +namespace Microsoft.Web.WebView2.Core |
| 87 | +{ |
| 88 | + public class CoreWebView2Controller |
| 89 | + { |
| 90 | + // |
| 91 | + // Summary: |
| 92 | + // Gets or sets the WebView allow drop property. |
| 93 | + // |
| 94 | + // Remarks: |
| 95 | + // The AllowDrop is to configure the capability that dropping files into webview2 |
| 96 | + // is allowed or permitted. The default value is true. |
| 97 | + public bool AllowDrop { get; set; } |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
0 commit comments