|
| 1 | +CoreWebView2ControllerOptions.DefaultBackgroundColor |
| 2 | +=== |
| 3 | +# Background |
| 4 | + |
| 5 | +Previously, there was a fix to address an issue where the background color controller property |
| 6 | +was applied too late, causing a disruptive white flash during the WebView2 loading process. |
| 7 | + |
| 8 | +This fix required using an environment variable. However, this workaround was not meant to be |
| 9 | +a long-term solution. Therefore, we need to add this setting to `ICoreWebView2ControllerOptions` |
| 10 | +to apply the color early in the loading process. |
| 11 | + |
| 12 | +# Description |
| 13 | + |
| 14 | +This interface extends the `ICoreWebView2ControllerOptions` to expose the `DefaultBackgroundColor` |
| 15 | +property as an option. |
| 16 | +The `CoreWebView2ControllerOptions.DefaultBackgroundColor` API allows users to set the |
| 17 | +`DefaultBackgroundColor` property at initialization. |
| 18 | + |
| 19 | +This is useful when setting it using the existing [`CoreWebView2Controller.DefaultBackgroundColor API`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2controller.defaultbackgroundcolor?view=webview2-dotnet-1.0.2792.45) |
| 20 | +applies the color too late. |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +# Examples |
| 25 | + |
| 26 | +## Win32 C++ |
| 27 | +```cpp |
| 28 | + HRESULT AppWindow::CreateControllerWithOptions() |
| 29 | +{ |
| 30 | + wil::com_ptr<ICoreWebView2ControllerOptions> options; |
| 31 | + HRESULT hr = m_environment->CreateCoreWebView2ControllerOptions(&options); |
| 32 | + |
| 33 | + if (hr == E_INVALIDARG) |
| 34 | + { |
| 35 | + ShowFailure(hr, L"Invalid profile name."); |
| 36 | + return S_OK; |
| 37 | + } |
| 38 | + |
| 39 | + wil::com_ptr<ICoreWebView2ControllerOptions4> options4; |
| 40 | + auto result = options->QueryInterface(IID_PPV_ARGS(&options4)); |
| 41 | + |
| 42 | + if (SUCCEEDED(result)) |
| 43 | + { |
| 44 | + COREWEBVIEW2_COLOR wvColor{255, 85, 0, 225}; |
| 45 | + options4->put_DefaultBackgroundColor(wvColor); |
| 46 | + } |
| 47 | + |
| 48 | + m_environment->CreateCoreWebView2ControllerWithOptions( |
| 49 | + m_mainWindow, |
| 50 | + options.Get(), |
| 51 | + Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>( |
| 52 | + this, &AppWindow::OnCreateCoreWebView2ControllerCompleted).Get()); |
| 53 | + |
| 54 | + return S_OK; |
| 55 | + |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +## C# |
| 62 | +```c# |
| 63 | +public MainWindow() |
| 64 | +{ |
| 65 | + InitializeComponent(); |
| 66 | + SetDefaultBackgroundColor(); |
| 67 | +} |
| 68 | + |
| 69 | +private async Task |
| 70 | +SetDefaultBackgroundColor() |
| 71 | +{ |
| 72 | + CoreWebView2Environment environment = await |
| 73 | + CoreWebView2Environment.CreateAsync(); |
| 74 | + CoreWebView2ControllerOptions options = environment.CreateCoreWebView2ControllerOptions(); |
| 75 | + options.DefaultBackgroundColor = Color.FromArgb(255, 85, 0, 255); |
| 76 | + await WebView2.EnsureCoreWebView2Async(environment, options); |
| 77 | +} |
| 78 | + |
| 79 | +``` |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +# API Details |
| 84 | + |
| 85 | +## Win32 C++ |
| 86 | + ```cpp |
| 87 | +/// This interface extends the ICoreWebView2ControllerOptions interface to expose |
| 88 | +/// DefaultBackgroundColor property. It is encouraged to transition away from the |
| 89 | +/// environment variable and use this API solution to apply the property. |
| 90 | + |
| 91 | +[uuid(df9cb70b-8d87-5bca-ae4b-6f23285e8d94), object, pointer_default(unique)] |
| 92 | +interface ICoreWebView2ControllerOptions4 : ICoreWebView2ControllerOptions3 { |
| 93 | + |
| 94 | + /// This API allows users to initialize the `DefaultBackgroundColor` early, |
| 95 | + /// preventing a white flash that can occur while WebView2 is loading when |
| 96 | + /// the background color is set to something other than white. With early |
| 97 | + /// initialization, the color remains consistent from the start. After |
| 98 | + /// initialization, `ICoreWebView2Controller2::get_DefaultBackgroundColor` |
| 99 | + /// will return the value set using this API. |
| 100 | + /// |
| 101 | + /// The `DefaultBackgroundColor` is the color that renders underneath all web |
| 102 | + /// content. This means WebView renders this color when there is no web |
| 103 | + /// content loaded. When no background color is defined in WebView2, it uses |
| 104 | + /// the `DefaultBackgroundColor` property to render the background. |
| 105 | + /// By default, this color is set to white. |
| 106 | + /// |
| 107 | + /// This API only supports opaque colors and full transparency. It will |
| 108 | + /// fail for colors with alpha values that don't equal 0 or 255. |
| 109 | + /// When WebView2 is set to be fully transparent, it does not render a background, |
| 110 | + /// allowing the content from windows behind it to be visible. |
| 111 | + |
| 112 | + [propget] HRESULT DefaultBackgroundColor([out, retval] COREWEBVIEW2_COLOR* value); |
| 113 | + [propput] HRESULT DefaultBackgroundColor([in] COREWEBVIEW2_COLOR value); |
| 114 | + |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +## .NET WinRT |
| 121 | + |
| 122 | +```cpp |
| 123 | +namespace Microsoft.Web.WebView2.Core |
| 124 | +{ |
| 125 | + runtimeclass CoreWebView2ControllerOptions |
| 126 | + { |
| 127 | + // ... |
| 128 | + [interface_name("ICoreWebView2ControllerOptions4")] |
| 129 | + { |
| 130 | + Windows.UI.Color DefaultBackgroundColor { get; set; }; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +``` |
0 commit comments