|
| 1 | +WebView2 Window Controls |
| 2 | +=== |
1 | 3 |
|
| 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 proccess. This API allows devs to tell |
| 9 | +WebView2 that it should render it's 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 | +``` |
| 56 | +## .NET C# |
| 57 | +```c# |
| 58 | +// WebView2 control is defined in the xaml |
| 59 | +// <wv2:WebView2 x:Name="webView" Source="https://www.microsoft.com/"/> |
| 60 | +public MainWindow() |
| 61 | +{ |
| 62 | + InitializeComponent(); |
| 63 | + this.webView2Control.CoreWebView2InitializationCompleted |
| 64 | + += WebView2InitializationCompleted; |
| 65 | +} |
| 66 | +
|
| 67 | +private void WebView2InitializationCompleted( |
| 68 | + object sender, |
| 69 | + CoreWebView2InitializationCompletedEventArgs e) |
| 70 | +{ |
| 71 | + if (!e.IsSuccess) |
| 72 | + { |
| 73 | + MessageBox.Show($"WebView2 creation failed with exception = {e.InitializationException}"); |
| 74 | +
|
| 75 | + return; |
| 76 | + } |
| 77 | +
|
| 78 | + SetWindowControlsOverlaySupport(true); |
| 79 | +} |
| 80 | +
|
| 81 | +private void SetWindowControlsOverlaySupport(bool enabled) |
| 82 | +{ |
| 83 | + var coreWebView2Settings = this.webView2Control.CoreWebView2.Settings; |
| 84 | + coreWebView2Settings.IsWindowControlsOverlayEnabled = enabled; |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +# API Details |
| 89 | +## Win32 C++ |
| 90 | +```cpp |
| 91 | +[uuid(436CA5E2-2D50-43C7-9735-E760F299439E), object, pointer_default(unique)] |
| 92 | +interface ICoreWebView2Settings12 : ICoreWebView2Settings11 { |
| 93 | + /// Gets the `IsWindowControlsOverlayEnabled` property. |
| 94 | + [propget] HRESULT IsWindowControlsOverlayEnabled([out, retval] BOOL* value); |
| 95 | + |
| 96 | + |
| 97 | + /// The `IsWindowControlsOverlayEnabled` property allows devs to opt in/out of using |
| 98 | + /// the WV2 custom caption controls. Defaults to `FALSE`. |
| 99 | + /// |
| 100 | + /// When this property is `TRUE`, WV2 will draw it's own caption controls on the |
| 101 | + /// top right corner of the window. |
| 102 | + /// |
| 103 | + [propput] HRESULT IsWindowControlsOverlayEnabled([in] BOOL value); |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## .NET and WinRT |
| 108 | +```c# |
| 109 | +namespace Microsoft.Web.WebView2.Core |
| 110 | +{ |
| 111 | + runtimeclass CoreWebView2Settings |
| 112 | + { |
| 113 | + [interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2Settings12")] |
| 114 | + { |
| 115 | + Boolean IsWindowControlsOverlayEnabled { get; set; }; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +# Appendix |
| 124 | +To provide your app users with the best experience, it is important to handle webview |
| 125 | +initialization errors appropriatly. Provide your users with a way to close the window |
| 126 | +or restart the App. |
0 commit comments