|
| 1 | +Theme API |
| 2 | +=== |
| 3 | + |
| 4 | +# Background |
| 5 | +This API's main use is to set the overall theme for WebView2. The options are similar to Edge: match the system default theme, change to light theme, or change to dark theme. |
| 6 | +This API has 2 main changes relevant to the end users. First, it sets the theme for WebView2 UI like dialogs, prompts, context menu, etc. And second, this API sets the ['prefers-color-scheme'](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme) [media feature](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries#media_features). Websites typically media query for this setting in order to set CSS themes for light/dark. |
| 7 | + |
| 8 | +Please note this API will only set the overall appearance, but not theme as defined in the Edge browser. |
| 9 | +For reference, in the screenshot below, this API is meant to expose the Overall Appearance section as a WebView2 API. |
| 10 | + |
| 11 | + |
| 12 | +# Examples |
| 13 | + |
| 14 | +## C++ |
| 15 | + |
| 16 | +```cpp |
| 17 | +wil::com_ptr<ICoreWebView2> m_webView; |
| 18 | + |
| 19 | +void ViewComponent::SetTheme(COREWEBVIEW2_THEME_KIND value) |
| 20 | +{ |
| 21 | + wil::com_ptr<ICoreWebView2_7> webView7; |
| 22 | + webView7 = m_webView.try_query<ICoreWebView2_7>(); |
| 23 | + |
| 24 | + if (webView7) |
| 25 | + { |
| 26 | + wil::com_ptr<ICoreWebView2Profile> profile; |
| 27 | + CHECK_FAILURE(webView7->get_Profile(&profile)); |
| 28 | + |
| 29 | + auto profile3 = profile.try_query<ICoreWebView2Profile3>(); |
| 30 | + if (profile3) |
| 31 | + { |
| 32 | + profile3->put_Theme(value); |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | +
|
| 38 | +## C# |
| 39 | +
|
| 40 | +```c# |
| 41 | +
|
| 42 | +private CoreWebView2 m_webView; |
| 43 | +
|
| 44 | +void SetTheme(CoreWebView2ThemeKind value) |
| 45 | +{ |
| 46 | + // Check for runtime support |
| 47 | + try |
| 48 | + { |
| 49 | + m_webView.Profile.Theme = value; |
| 50 | + } |
| 51 | + catch (NotImplementedException exception) |
| 52 | + { |
| 53 | + MessageBox.Show(this, "Set Theme Failed: " + exception.Message, |
| 54 | + "Set Theme"); |
| 55 | + } |
| 56 | +} |
| 57 | +
|
| 58 | +``` |
| 59 | + |
| 60 | +# API Details |
| 61 | +## C++ |
| 62 | +```cpp |
| 63 | +[uuid(5f817cce-5d36-4cd0-a1d5-aaaf95c8685f), object, pointer_default(unique)] |
| 64 | +interface ICoreWebView2Profile3 : ICoreWebView2Profile2 { |
| 65 | + /// The Theme property sets the overall theme of the WebView2's associated |
| 66 | + /// with this profile. This sets the theme for WebView2 UI like dialogs, |
| 67 | + /// prompts, context menus, and so on. Additionally, this sets the |
| 68 | + /// ['prefers-color-scheme'](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme) |
| 69 | + /// CSS media feature. |
| 70 | + /// |
| 71 | + /// The default value for this is COREWEBVIEW2_THEME_KIND_SYSTEM, which will |
| 72 | + /// follow whatever theme Windows is currently set to. |
| 73 | + /// |
| 74 | + /// \snippet ViewComponent.cpp SetTheme |
| 75 | + /// Returns the value of the `Theme` property. |
| 76 | + [propget] HRESULT Theme( |
| 77 | + [out, retval] COREWEBVIEW2_THEME_KIND* value); |
| 78 | + |
| 79 | + /// Sets the `Theme` property. |
| 80 | + [propput] HRESULT Theme( |
| 81 | + [in] COREWEBVIEW2_THEME_KIND value); |
| 82 | +} |
| 83 | + |
| 84 | +/// An enum to represent the options for WebView2 Theme: system, light, or dark. |
| 85 | +[v1_enum] |
| 86 | +typedef enum COREWEBVIEW2_THEME_KIND { |
| 87 | + /// System theme |
| 88 | + COREWEBVIEW2_THEME_KIND_SYSTEM, |
| 89 | + |
| 90 | + /// Light theme |
| 91 | + COREWEBVIEW2_THEME_KIND_LIGHT, |
| 92 | + |
| 93 | + /// Dark theme |
| 94 | + COREWEBVIEW2_THEME_KIND_DARK |
| 95 | +} COREWEBVIEW2_THEME_KIND; |
| 96 | +``` |
| 97 | + |
| 98 | +### C# |
| 99 | +```c# |
| 100 | +namespace Microsoft.Web.WebView2.Core |
| 101 | +{ |
| 102 | + [doc_string("An enum to represent the options for WebView2 Theme: system, light, or dark.")] |
| 103 | + enum CoreWebView2ThemeKind |
| 104 | + { |
| 105 | + [doc_string("System theme.")] |
| 106 | + System = 0, |
| 107 | + [doc_string("Light theme.")] |
| 108 | + Light = 1, |
| 109 | + [doc_string("Dark theme.")] |
| 110 | + Dark = 2, |
| 111 | + }; |
| 112 | + |
| 113 | + [doc_string("Multiple profiles can be created under a single user data directory but with separated cookies, user preference settings, and various data storage etc..")] |
| 114 | + runtimeclass CoreWebView2Profile |
| 115 | + { |
| 116 | + // ... |
| 117 | +
|
| 118 | + [interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2Profile3")] |
| 119 | + { |
| 120 | + [doc_string("The Theme property sets the overall theme of the WebView2's associated with this profile. This sets the theme for WebView2 UI like dialogs, prompts, context menus, and so on. Additionally, this sets the ['prefers-color-scheme'](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme) CSS media feature. The default value for this is COREWEBVIEW2_THEME_KIND_SYSTEM, which will follow whatever theme Windows is currently set to.")] |
| 121 | + CoreWebView2ThemeKind Theme { get; set }; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
0 commit comments