Skip to content

Commit 41f9aec

Browse files
authored
Merge pull request #1523 from MicrosoftEdge/api-allowdrop
API Review: AllowDrop.md
2 parents 10c24d4 + e618dcd commit 41f9aec

1 file changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Background
2+
Currently dragging and dropping external objects (e.g. files, hyperlinks) into webview2 is by default enabled and there is no way to disable it. Some developers may want to disable this functionality in their applications. To allow for this, we add a new API to provide developers with the capability to disable 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, dragging objects from outside the bounds of the WebView and dropping into the WebView will be disallowed.
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 dragging and dropping 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 and drop anything from webview2 to external(outside the bounds of the WebView) will not be impacted by this property.
19+
20+
# Examples
21+
## C++
22+
23+
```cpp
24+
// This hypothetical app allows dropping external content
25+
// only on the AttachReceipts page.
26+
wil::com_ptr<ICoreWebView2Controller> m_controller;
27+
BOOL m_allowExternalDropOnNavigationCompleted;
28+
HRESULT OnNavigationStarting(ICoreWebView2* sender, ICoreWebView2NavigationStartingEventArgs* args)
29+
{
30+
// Disable external drop while navigating.
31+
auto controller4 = m_controller.try_query<ICoreWebView2Controller4>();
32+
if (controller4)
33+
{
34+
CHECK_FAILURE(controller4->put_AllowExternalDrop(FALSE));
35+
}
36+
37+
// Decide whether to enable external drop when we finish navigating.
38+
// Enable it only when AttachReceipts page is navigated.
39+
wil::unique_cotaskmem_string uri;
40+
CHECK_FAILURE(args->get_Uri(&uri));
41+
m_allowExternalDropOnNavigationCompleted = uri_equal(uri.get(), L"myapp://AttachReceipts.html");
42+
}
43+
44+
HRESULT OnNavigationCompleted(ICoreWebView2* sender, ICoreWebView2NavigationCompletedEventArgs* args)
45+
{
46+
auto controller4 = m_controller.try_query<ICoreWebView2Controller4>();
47+
if (controller4)
48+
{
49+
CHECK_FAILURE(controller4->put_AllowExternalDrop(m_allowExternalDropOnNavigationCompleted));
50+
}
51+
}
52+
```
53+
54+
## C#
55+
```c#
56+
// This hypothetical app allows dropping external content
57+
// only on the AttachReceipts page.
58+
private CoreWebView2Controller controller;
59+
private bool allowExternalDropOnNavigationCompleted;
60+
void OnNavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs args)
61+
{
62+
// Disable external drop while navigating.
63+
if (controller != null)
64+
{
65+
controller.AllowExternalDrop = false;
66+
}
67+
68+
// Decide whether to enable external drop when we finish navigating.
69+
// Enable it only when AttachReceipts page is navigated.
70+
string uri = args.Uri;
71+
allowExternalDropOnNavigationCompleted = uri.Equals("myapp://AttachReceipts.html");
72+
}
73+
74+
void OnNavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs args)
75+
{
76+
if (controller != null)
77+
{
78+
controller.AllowExternalDrop = allowExternalDropOnNavigationCompleted;
79+
}
80+
}
81+
```
82+
83+
# Remarks
84+
85+
# API Notes
86+
See [API Details](#api-details) section below for API reference.
87+
88+
# API Details
89+
90+
## Win32 C++
91+
```c#
92+
// This is the ICoreWebView2Controller4 interface.
93+
[uuid(320613e2-990f-4272-bf90-d243a4ff1b8a), object, pointer_default(unique)]
94+
interface ICoreWebView2Controller4 : ICoreWebView2Controller3 {
95+
/// Gets the `AllowExternalDrop` property which is used to configure the
96+
/// capability that dragging objects from outside the bounds of webview2 and
97+
/// dropping into webview2 is allowed or disallowed. The default value is
98+
/// TRUE.
99+
[propget] HRESULT AllowExternalDrop([ out, retval ] BOOL * value);
100+
/// Sets the `AllowExternalDrop` property which is used to configure the
101+
/// capability that dragging objects from outside the bounds of webview2 and
102+
/// dropping into webview2 is allowed or disallowed.
103+
[propput] HRESULT AllowExternalDrop([in] BOOL value);
104+
}
105+
```
106+
107+
## .NET and WinRT
108+
```c#
109+
namespace Microsoft.Web.WebView2.Core
110+
{
111+
public class CoreWebView2Controller
112+
{
113+
//
114+
// Summary:
115+
// Gets or sets the WebView AllowExternalDrop property.
116+
//
117+
// Remarks:
118+
// The AllowExternalDrop is to configure the capability that dragging objects from
119+
// outside the bounds of webview2 and dropping into webview2 is allowed or disallowed.
120+
// The default value is true.
121+
public bool AllowExternalDrop { get; set; }
122+
}
123+
}
124+
```

0 commit comments

Comments
 (0)