Skip to content

Commit 9afaa30

Browse files
committed
refactor based on feedback
1 parent 261be73 commit 9afaa30

1 file changed

Lines changed: 62 additions & 41 deletions

File tree

Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,83 @@
11
# 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.
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.
33

44
# Description
55
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, drag&drop objects from outside into current webview2 will be forbidden.
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.
88
To be more specific, some behaviors listed below will be impacted by the toggle of this property.
99

1010
* Drag&Drop files on disk to webview2
1111
* Drag&Drop hyperlinks from browser to webview2
1212
* Drag&Drop hyperlinks from one webview2 to another webview2
1313

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.
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.
1515

1616
By default, AllowExternalDrop is enabled to keep consistent with the behavior we had before the API is added.
1717

18-
Please note that drag&drop anything from webview2 to external(outside current webview2) will not be impacted by this property.
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.
1919

2020
# Examples
2121
## C++
2222

2323
```cpp
24-
wil::com_ptr<ICoreWebView2Controller4> m_controller4;
25-
void ToggleAllowDrop()
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)
2629
{
27-
if (m_controller4)
30+
// Disable external drop while navigating.
31+
auto controller4 = m_controller.try_query<ICoreWebView2Controller4>();
32+
if (controller4)
2833
{
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-
}
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));
3950
}
4051
}
4152
```
4253
4354
## C#
4455
```c#
45-
private CoreWebView2Controller controller
46-
void ToggleAllowDrop(object target, ExecutedRoutedEventArgs e)
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)
4761
{
48-
if (controller)
62+
// Disable external drop while navigating.
63+
if (controller != null)
4964
{
50-
controller.AllowDrop = !controller.AllowDrop;
65+
controller.AllowExternalDrop = false;
5166
}
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+
controller.AllowExternalDrop = allowExternalDropOnNavigationCompleted;
5277
}
5378
```
5479

5580
# 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-
6181

6282
# API Notes
6383
See [API Details](#api-details) section below for API reference.
@@ -66,18 +86,18 @@ See [API Details](#api-details) section below for API reference.
6686

6787
## Win32 C++
6888
```c#
69-
// This is the ICoreWebView2Controller interface.
89+
// This is the ICoreWebView2Controller4 interface.
7090
[uuid(320613e2-990f-4272-bf90-d243a4ff1b8a), object, pointer_default(unique)]
7191
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);
92+
/// Gets the `AllowExternalDrop` property which is used to configure the
93+
/// capability that dragging objects from outside the bounds of webview2 and
94+
/// dropping into webview2 is allowed or disallowed. The default value is
95+
/// TRUE.
96+
[propget] HRESULT AllowExternalDrop([ out, retval ] BOOL * value);
97+
/// Sets the `AllowExternalDrop` property which is used to configure the
98+
/// capability that dragging objects from outside the bounds of webview2 and
99+
/// dropping into webview2 is allowed or disallowed.
100+
[propput] HRESULT AllowExternalDrop([in] BOOL value);
81101
}
82102
```
83103

@@ -89,12 +109,13 @@ namespace Microsoft.Web.WebView2.Core
89109
{
90110
//
91111
// Summary:
92-
// Gets or sets the WebView allow drop property.
112+
// Gets or sets the WebView AllowExternalDrop property.
93113
//
94114
// 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; }
115+
// The AllowExternalDrop is to configure the capability that dragging objects from
116+
// outside the bounds of webview2 and dropping into webview2 is allowed or disallowed.
117+
// The default value is true.
118+
public bool AllowExternalDrop { get; set; }
98119
}
99120
}
100121
```

0 commit comments

Comments
 (0)