Skip to content

Commit d395730

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

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
+155
Original file line numberDiff line numberDiff line change
@@ -1 +1,156 @@
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+
this.webView2Control.NavigationCompleted
80+
+= NavigationCompleted ;
81+
}
82+
83+
private void WebView2InitializationCompleted(
84+
object sender,
85+
CoreWebView2InitializationCompletedEventArgs e)
86+
{
87+
if (!e.IsSuccess)
88+
{
89+
MessageBox.Show($"WebView2 creation failed with exception = {e.InitializationException}");
90+
91+
return;
92+
}
93+
94+
SetWindowControlsOverlaySupport(true);
95+
}
96+
97+
private void NavigationCompleted(
98+
object sender,
99+
CoreWebView2NavigationCompletedEventArgs e)
100+
{
101+
Windows.Foundation.Rect bounds = CoreWebView2Controller.QueryWindowControlsOverlayBounds();
102+
}
103+
104+
private void SetWindowControlsOverlaySupport(bool enabled)
105+
{
106+
var coreWebView2Settings = this.webView2Control.CoreWebView2.Settings;
107+
coreWebView2Settings.IsWindowControlsOverlayEnabled = enabled;
108+
}
109+
```
110+
111+
# API Details
112+
## Win32 C++
113+
```cpp
114+
[uuid(436CA5E2-2D50-43C7-9735-E760F299439E), object, pointer_default(unique)]
115+
interface ICoreWebView2Settings12 : ICoreWebView2Settings11 {
116+
/// Gets the `IsWindowControlsOverlayEnabled` property.
117+
[propget] HRESULT IsWindowControlsOverlayEnabled([out, retval] BOOL* value);
118+
119+
120+
/// The `IsWindowControlsOverlayEnabled` property allows devs to opt in/out of using
121+
/// the WV2 custom caption controls. Defaults to `FALSE`.
122+
///
123+
/// When this property is `TRUE`, WV2 will draw it's own caption controls on the
124+
/// top right corner of the window.
125+
///
126+
[propput] HRESULT IsWindowControlsOverlayEnabled([in] BOOL value);
127+
}
128+
```
129+
130+
## .NET and WinRT
131+
```c#
132+
namespace Microsoft.Web.WebView2.Core
133+
{
134+
runtimeclass CoreWebView2Settings
135+
{
136+
[interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2Settings11")]
137+
{
138+
Boolean IsWindowControlsOverlayEnabled { get; set; };
139+
}
140+
}
141+
142+
unsealed runtimeclass CoreWebView2Controller {
143+
[interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2Controller5")]
144+
{
145+
Windows.Foundation.Rect QueryWindowControlsOverlayBounds();
146+
}
147+
}
148+
}
149+
```
150+
151+
152+
153+
# Appendix
154+
To provide your app users with the best experience, it is important to handle webview
155+
initialization errors appropriatly. Provide your users with a way to close the window
156+
or restart the App.

0 commit comments

Comments
 (0)