Skip to content

Commit 75051eb

Browse files
Merge pull request #1373 from MicrosoftEdge/api-extended-new-window-requested-draft
API Spec: extended NewWindowRequestedEventArgs
2 parents 63dbaa5 + eaf1f2d commit 75051eb

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Background
2+
WebView2 raises the `NewWindowRequested` event when a new window is opened. The WebView2 team has been asked to include the name of the new window
3+
being created as a parameter of `NewWindowRequestedEventArgs`. This window name is the name given to the window when it is opened
4+
using `window.open(url, windowName)`.
5+
6+
In this document we describe the updated API. We'd appreciate your feedback.
7+
8+
# Description
9+
We propose extending the `NewWindowRequestedEventArgs` to provide access to the `Name` property.
10+
The `Name` property will return the name of the new window being created.
11+
12+
# Examples
13+
## C++: Get name of window
14+
15+
``` cpp
16+
wil::com_ptr<ICoreWebView2> m_webviewEventSource;
17+
EventRegistrationToken m_newWindowRequestedToken = {};
18+
19+
m_webviewEventSource->add_NewWindowRequested(
20+
Callback<ICoreWebView2NewWindowRequestedEventHandler>(
21+
[this](ICoreWebView2* sender, ICoreWebView2NewWindowRequestedEventArgs* args)
22+
-> HRESULT
23+
{
24+
wil::com_ptr<ICoreWebView2NewWindowRequestedEventArgs2> args2;
25+
26+
if(SUCCEEDED(args->QueryInterface(IID_PPV_ARGS(&args2))))
27+
{
28+
wil::unique_cotaskmem_string name;
29+
CHECK_FAILURE(args2->get_Name(&name));
30+
31+
wil::unique_cotaskmem_string uri;
32+
CHECK_FAILURE(args->get_Uri(&uri));
33+
34+
// Example usage of how the customer would use the window name to pass
35+
// additional context from their web content to their webview2 event handler.
36+
if (wcscmp(name.get(), L"openInNewBrowser") == 0)
37+
{
38+
ShellExecute(NULL, "open", uri.get(),
39+
NULL, NULL, SW_SHOWNORMAL);
40+
args->put_Handled(true);
41+
}
42+
else
43+
{
44+
HandleNewWindow(args); // App specific handling of opening a new window
45+
}
46+
}
47+
48+
return S_OK;
49+
})
50+
.Get(),
51+
&m_newWindowRequestedToken);
52+
```
53+
54+
## C#: Get name of window
55+
```c#
56+
webView.CoreWebView2.NewWindowRequested += WebView_NewWindowRequested;
57+
58+
void WebView_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
59+
{
60+
string newWindowName = e.Name;
61+
62+
// Example usage of how the customer would use the window name to pass
63+
// additional context from their web content to their webview2 event handler.
64+
if (newWindowName == "openInNewBrowser")
65+
{
66+
Process.Start(e.Uri);
67+
e.Handled = true;
68+
}
69+
else
70+
{
71+
HandleNewWindow(e); // App specific handling of opening a new window
72+
}
73+
}
74+
```
75+
76+
# API Details
77+
## C++
78+
```
79+
/// This is a continuation of the `ICoreWebView2NewWindowRequestedEventArgs` interface.
80+
[uuid(9bcea956-6e1f-43bc-bf02-0a360d73717b), object, pointer_default(unique)]
81+
interface ICoreWebView2NewWindowRequestedEventArgs2 : ICoreWebView2NewWindowRequestedEventArgs {
82+
/// Gets the name of the new window. This window can be created via `window.open(url, windowName)`,
83+
/// where the windowName parameter corresponds to `Name` property.
84+
/// If no windowName is passed to `window.open`, then the `Name` property
85+
/// will be set to an empty string. Additionally, if window is opened through other means,
86+
/// such as `<a target="windowName">...</a>` or `<iframe name="windowName>...</iframe>`,
87+
/// then the `Name` property will be set accordingly. In the case of target=_blank,
88+
/// the `Name` property will be an empty string.
89+
/// Opening a window via ctrl+clicking a link would result in the `Name` property
90+
/// being set to an empty string.
91+
[propget] HRESULT Name([out, retval] LPWSTR* value);
92+
}
93+
```
94+
95+
## C#
96+
```c#
97+
namespace Microsoft.Web.WebView2.Core
98+
{
99+
runtimeclass CoreWebView2NewWindowRequestedEventArgs
100+
{
101+
// The following properties already exist in the CoreWebView2NewWindowRequestedEventArgs
102+
// ICoreWebView2NewWindowRequestedEventArgs members
103+
// String Uri { get; };
104+
// CoreWebView2 NewWindow { get; set; };
105+
// Boolean Handled { get; set; };
106+
// Boolean IsUserInitiated { get; };
107+
// CoreWebView2WindowFeatures WindowFeatures { get; };
108+
109+
String Name { get; };
110+
111+
// The following already exists in the CoreWebView2NewWindowRequestedEventArgs
112+
// Windows.Foundation.Deferral GetDeferral();
113+
}
114+
}
115+
```

0 commit comments

Comments
 (0)