Skip to content

Commit 657def7

Browse files
Included a new API spec for Webview2 Window Controls Overlay
1 parent 5d6debb commit 657def7

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
+139
Original file line numberDiff line numberDiff line change
@@ -1 +1,140 @@
1+
WebView2 Window Controls
2+
===
13

4+
# Background
5+
The goal of this API is to provide devs with a cleaner way to build apps where
6+
the entire Window UI is rendered by WebView2. Till now, it hasn't been possible
7+
for the window/caption control buttons (minimize, maximize, restore, close) to
8+
be rendered and controlled by the browser process. This API allows devs to tell
9+
WebView2 that it should render its own window control buttons.
10+
11+
# Description
12+
`IsWindowControlsOverlayEnabled` defaults to `FALSE`. Disabling/Enabling
13+
`IsWindowControlsOverlayEnabled` takes effect after the next navigation.
14+
15+
# Examples
16+
17+
## Win32 C++
18+
```cpp
19+
void AppWindow::InitializeWebView()
20+
{
21+
22+
CreateCoreWebView2EnvironmentWithOptions(
23+
browserExecutableFolder,
24+
userDataFolder,
25+
corewebview2environmentoptions,
26+
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
27+
this,
28+
[this](HRESULT result, ICoreWebView2Environment* environment) -> {
29+
CHECK_FAILURE(hr);
30+
31+
CHECK_FAILURE(m_webViewEnvironment->CreateCoreWebView2Controller(
32+
mainwindowhwnd,
33+
Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
34+
this,
35+
&AppWindow::OnCreateCoreWebView2ControllerCompleted).Get()));
36+
}).Get());
37+
}
38+
39+
HRESULT AppWindow::OnCreateCoreWebView2ControllerCompleted(
40+
HRESULT result, ICoreWebView2Controller* controller)
41+
{
42+
CHECK_FAILURE(hr);
43+
44+
wil::com_ptr<ICoreWebView2> coreWebView2;
45+
CHECK_FAILURE(controller->get_CoreWebView2(&coreWebView2));
46+
47+
wil::com_ptr<ICoreWebView2Settings> m_settings;
48+
CHECK_FAILURE(coreWebView2->get_Settings(&m_settings));
49+
50+
wil::com_ptr<ICoreWebView2Settings12> coreWebView2Settings12;
51+
coreWebView2Settings12 = m_settings.try_query<ICoreWebView2Settings12>();
52+
CHECK_FEATURE_RETURN(coreWebView2Settings12);
53+
CHECK_FAILURE(coreWebView2Settings12->IsWindowControlsOverlayEnabled(true));
54+
55+
CHECK_FAILURE(coreWebView2->add_NavigationCompleted(
56+
Callback<ICoreWebView2NavigationCompletedEventHandler>(
57+
[this, webviewEventView](ICoreWebView2* sender, ICoreWebView2NavigationCompletedEventArgs* args)
58+
-> HRESULT {
59+
60+
RECT bounds;
61+
controller->QueryWindowControlsOverlayBounds(&bounds);
62+
return S_OK;
63+
})
64+
.Get(),
65+
&m_navigationCompletedToken));
66+
67+
CHECK_FAILURE(m_webView->Navigate(L"www.bing.com"));
68+
}
69+
```
70+
## .NET C#
71+
```c#
72+
// WebView2 control is defined in the xaml
73+
// <wv2:WebView2 x:Name="webView" Source="https://www.microsoft.com/"/>
74+
public MainWindow()
75+
{
76+
InitializeComponent();
77+
this.webView2Control.CoreWebView2InitializationCompleted
78+
+= WebView2InitializationCompleted;
79+
}
80+
81+
private void WebView2InitializationCompleted(
82+
object sender,
83+
CoreWebView2InitializationCompletedEventArgs e)
84+
{
85+
if (!e.IsSuccess)
86+
{
87+
MessageBox.Show($"WebView2 creation failed with exception = {e.InitializationException}");
88+
89+
return;
90+
}
91+
92+
SetWindowControlsOverlaySupport(true);
93+
}
94+
95+
private void SetWindowControlsOverlaySupport(bool enabled)
96+
{
97+
var coreWebView2Settings = this.webView2Control.CoreWebView2.Settings;
98+
coreWebView2Settings.IsWindowControlsOverlayEnabled = enabled;
99+
}
100+
```
101+
102+
# API Details
103+
## Win32 C++
104+
```cpp
105+
[uuid(436CA5E2-2D50-43C7-9735-E760F299439E), object, pointer_default(unique)]
106+
interface ICoreWebView2Settings12 : ICoreWebView2Settings11 {
107+
/// Gets the `IsWindowControlsOverlayEnabled` property.
108+
[propget] HRESULT IsWindowControlsOverlayEnabled([out, retval] BOOL* value);
109+
110+
111+
/// The `IsWindowControlsOverlayEnabled` property allows devs to opt in/out of using
112+
/// the WV2 custom caption controls. Defaults to `FALSE`.
113+
///
114+
/// When this property is `TRUE`, WV2 will draw it's own caption controls on the
115+
/// top right corner of the window.
116+
///
117+
[propput] HRESULT IsWindowControlsOverlayEnabled([in] BOOL value);
118+
}
119+
```
120+
121+
## .NET and WinRT
122+
```c#
123+
namespace Microsoft.Web.WebView2.Core
124+
{
125+
runtimeclass CoreWebView2Settings
126+
{
127+
[interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2Settings12")]
128+
{
129+
Boolean IsWindowControlsOverlayEnabled { get; set; };
130+
}
131+
}
132+
}
133+
```
134+
135+
136+
137+
# Appendix
138+
To provide your app users with the best experience, it is important to handle webview
139+
initialization errors appropriatly. Provide your users with a way to close the window
140+
or restart the App.

0 commit comments

Comments
 (0)