|
| 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->CreateCoreWebView2Controller( |
| 49 | + m_mainWindow, |
| 50 | + SUCCEEDED(result) ? options4.Get() : 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 void SetDefaultBackgroundColor() |
| 70 | +{ |
| 71 | + CoreWebView2Environment environment = CoreWebView2Environment.CreateAsync(); |
| 72 | + CoreWebView2ControllerOptions options = environment.CreateCoreWebView2ControllerOptions(); |
| 73 | + options.DefaultBackgroundColor = Color.FromArgb(255, 85, 0, 255); |
| 74 | + WebView2.EnsureCoreWebView2Async(environment, options); |
| 75 | +} |
| 76 | + |
| 77 | +``` |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +# API Details |
| 82 | + |
| 83 | +## Win32 C++ |
| 84 | + ```cpp |
| 85 | +/// This interface extends the ICoreWebView2ControllerOptions interface to expose the DefaultBackgroundColor property. |
| 86 | +/// It is encouraged to transition away from the environment variable and use this API solution to apply the property. |
| 87 | + |
| 88 | +[uuid(df9cb70b-8d87-5bca-ae4b-6f23285e8d94), object, pointer_default(unique)] |
| 89 | +interface ICoreWebView2ControllerOptions4 : IUnknown { |
| 90 | + |
| 91 | + /// This API allows users to initialize the `DefaultBackgroundColor` early, |
| 92 | + /// preventing a white flash that can happen while WebView2 is loading when |
| 93 | + /// the background color is set to something other than white. With early |
| 94 | + /// initialization, the color remains consistent from the start. After |
| 95 | + /// initialization, `ICoreWebView2Controller2::get_DefaultBackgroundColor` |
| 96 | + /// will return the value set using this API. |
| 97 | + /// |
| 98 | + /// The `DefaultBackgroundColor` is the color that renders underneath all web |
| 99 | + /// content. This means WebView renders this color when there is no web |
| 100 | + /// content loaded. When no background color is defined in WebView2, it uses |
| 101 | + /// the `DefaultBackgroundColor` property to render the background. |
| 102 | + /// By default, this color is set to white. |
| 103 | + /// |
| 104 | + /// Currently this API only supports opaque colors and transparency. It will |
| 105 | + /// fail for colors with alpha values that don't equal 0 or 255 ie. translucent |
| 106 | + /// colors are not supported. When WebView2 is set to have a transparent background, |
| 107 | + /// it renders the content of the parent window behind it. |
| 108 | + |
| 109 | + [propget] HRESULT DefaultBackgroundColor([out, retval] COREWEBVIEW2_COLOR* value); |
| 110 | + [propput] HRESULT DefaultBackgroundColor([in] COREWEBVIEW2_COLOR value); |
| 111 | + |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +## .NET WinRT |
| 118 | + |
| 119 | +```cpp |
| 120 | +namespace Microsoft.Web.WebView2.Core |
| 121 | +{ |
| 122 | + runtimeclass CoreWebView2ControllerOptions |
| 123 | + { |
| 124 | + // ... |
| 125 | + [interface_name("ICoreWebView2ControllerOptions4")] |
| 126 | + { |
| 127 | + Windows.UI.Color DefaultBackgroundColor { get; set; }; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +``` |
0 commit comments