Skip to content

Commit bad09b9

Browse files
committed
Create CoreWebView2ControllerOptions.DefaultBackgroundColor.md
Update to address feedback from review comments
1 parent 0ba6096 commit bad09b9

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
wil::com_ptr<ICoreWebView2ControllerOptions> stagingOptions;
34+
auto result = options->QueryInterface(IID_PPV_ARGS(&stagingOptions));
35+
36+
if (SUCCEEDED(result))
37+
{
38+
COREWEBVIEW2_COLOR wvColor{255, 223, 225, 225};
39+
stagingOptions->put_DefaultBackgroundColor(wvColor);
40+
41+
m_environment->CreateCoreWebView2Controller(
42+
m_mainWindow, options.Get(),
43+
Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
44+
this, &AppWindow::OnCreateCoreWebView2ControllerCompleted).Get());
45+
}
46+
47+
}
48+
```
49+
50+
51+
52+
## C#
53+
```c#
54+
public MainWindow()
55+
{
56+
InitializeComponent();
57+
SetDefaultBackgroundColor();
58+
}
59+
60+
private void SetDefaultBackgroundColor()
61+
{
62+
CoreWebView2ControllerOptions options = WebView2.CoreWebView2.Environment.CreateCoreWebView2ControllerOptions();
63+
options.DefaultBackgroundColor = Color.FromArgb(0, 0, 255);
64+
WebView2.CoreWebView2.Environment.CreateCoreWebView2ControllerAsync(parentHwnd, options);
65+
}
66+
67+
```
68+
69+
70+
71+
# API Details
72+
73+
## Win32 C++
74+
```cpp
75+
/// This interface extends the ICoreWebView2ControllerOptions interface to expose the DefaultBackgroundColor property.
76+
/// It is encouraged to transition away from the environment variable and use this API solution to apply the property.
77+
78+
[uuid(df9cb70b-8d87-5bca-ae4b-6f23285e8d94), object, pointer_default(unique)]
79+
interface ICoreWebView2ControllerOptions4 : IUnknown {
80+
81+
/// This API allows users to initialize the `DefaultBackgroundColor` early,
82+
/// preventing a white flash that can happen while WebView2 is loading when
83+
/// the background color is set to something other than white. With early
84+
/// initialization, the color remains consistent from the start. After
85+
/// initialization, `ICoreWebView2Controller2::get_DefaultBackgroundColor`
86+
/// will return the value set using this API.
87+
///
88+
/// The `DefaultBackgroundColor` is the color that renders underneath all web
89+
/// content. This means WebView renders this color when there is no web
90+
/// content loaded. It is important to note that the default color is white.
91+
/// Currently this API only supports opaque colors and transparency. It will
92+
/// fail for colors with alpha values that don't equal 0 or 255 ie. translucent
93+
/// colors are not supported. It also does not support transparency on Windows 7.
94+
/// On Windows 7, setting DefaultBackgroundColor to a Color with an Alpha value
95+
/// other than 255 will result in failure. On any OS above Win7, choosing a
96+
/// transparent color will result in showing hosting app content. This means
97+
/// webpages without explicit background properties defined will render web
98+
/// content over hosting app content.
99+
100+
[propget] HRESULT DefaultBackgroundColor([out, retval] COREWEBVIEW2_COLOR* value);
101+
[propput] HRESULT DefaultBackgroundColor([in] COREWEBVIEW2_COLOR value);
102+
103+
}
104+
```
105+
106+
107+
108+
## .NET WinRT
109+
110+
```cpp
111+
namespace Microsoft.Web.WebView2.Core
112+
{
113+
runtimeclass CoreWebView2ControllerOptions
114+
{
115+
// ...
116+
[interface_name("ICoreWebView2ControllerOptions4")]
117+
{
118+
Windows.UI.Color DefaultBackgroundColor { get; set; };
119+
}
120+
}
121+
}
122+
}
123+
124+
```

0 commit comments

Comments
 (0)