-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Windows] Fix WebView Does Not Inherit App Theme #35037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b56ef4f
e63f0d1
f55d65b
06e96c1
2712ef9
c5f5228
9647889
c88039d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 34823, "WebView on Windows Does Not Inherit App Theme", PlatformAffected.UWP)] | ||
| public class Issue34823 : NavigationPage | ||
| { | ||
| public Issue34823() : base(new Issue34823MainPage()) | ||
| { | ||
| } | ||
|
|
||
| class Issue34823MainPage : ContentPage | ||
| { | ||
| public Issue34823MainPage() | ||
| { | ||
| BindingContext = this; | ||
| Title = "WebView Test"; | ||
|
|
||
| var webButton = new Button | ||
| { | ||
| Text = "Switch to web page", | ||
| AutomationId = "WebButton", | ||
| HorizontalOptions = LayoutOptions.Center, | ||
| Margin = new Thickness(10) | ||
| }; | ||
| webButton.Clicked += OnWebClicked; | ||
|
|
||
| var themeButton = new Button | ||
| { | ||
| Text = "Toggle Theme", | ||
| AutomationId = "ThemeButton", | ||
| HorizontalOptions = LayoutOptions.Center | ||
| }; | ||
| themeButton.Clicked += OnThemeButtonClicked; | ||
|
|
||
| Content = new ScrollView | ||
| { | ||
| Content = new VerticalStackLayout | ||
| { | ||
| Padding = 30, | ||
| Spacing = 25, | ||
| Children = | ||
| { | ||
| webButton, | ||
| new HorizontalStackLayout | ||
| { | ||
| HorizontalOptions = LayoutOptions.Center, | ||
| Children = | ||
| { | ||
| themeButton | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| void OnThemeButtonClicked(object sender, EventArgs e) | ||
| { | ||
| Application.Current!.UserAppTheme = Dark ? AppTheme.Light : AppTheme.Dark; | ||
| } | ||
|
|
||
| async void OnWebClicked(object sender, EventArgs e) | ||
| { | ||
| var webView = new WebView(); | ||
|
|
||
| var helpPage = new ContentPage | ||
| { | ||
| Title = "Web Page", | ||
| Content = new Grid | ||
| { | ||
| Children = | ||
| { | ||
| webView | ||
| } | ||
| } | ||
| }; | ||
| helpPage.SetAppThemeColor(BackgroundColorProperty, Colors.White, Colors.Black); | ||
|
|
||
| helpPage.NavigatedTo += async (_, __) => | ||
| { | ||
| webView.Source = new HtmlWebViewSource | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting |
||
| { | ||
| Html = """ | ||
| <html> | ||
| <head> | ||
| <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"> | ||
| <title>Preparing Help</title> | ||
| <style> | ||
| @media (prefers-color-scheme: dark) { | ||
| html, body { | ||
| color: white; | ||
| background-color: black; | ||
| } | ||
| } | ||
|
|
||
| @media (prefers-color-scheme: light) { | ||
| html, body { | ||
| color: black; | ||
| background-color: white; | ||
| } | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <center><h1>Text on a web page</h1></center> | ||
| </body> | ||
| </html> | ||
| """ | ||
| }; | ||
| }; | ||
|
Comment on lines
+78
to
+109
|
||
|
|
||
| await Navigation.PushAsync(helpPage); | ||
| } | ||
|
|
||
| public bool Dark | ||
| { | ||
| set | ||
| { | ||
| if (value != Dark) | ||
| { | ||
| Application.Current!.UserAppTheme = value ? AppTheme.Dark : AppTheme.Light; | ||
| } | ||
| } | ||
| get => | ||
| Application.Current?.UserAppTheme == AppTheme.Dark || | ||
| Application.Current?.RequestedTheme == AppTheme.Dark; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,37 @@ | ||||||||
| #if TEST_FAILS_ON_ANDROID // This test fails on Android because of user app theme is not responsive in Hostapp. | ||||||||
| using NUnit.Framework; | ||||||||
| using UITest.Appium; | ||||||||
| using UITest.Core; | ||||||||
|
|
||||||||
| namespace Microsoft.Maui.TestCases.Tests.Issues; | ||||||||
|
|
||||||||
| public class Issue34823 : _IssuesUITest | ||||||||
| { | ||||||||
| public Issue34823(TestDevice device) : base(device) | ||||||||
| { | ||||||||
| } | ||||||||
|
|
||||||||
| protected override bool ResetAfterEachTest => true; | ||||||||
|
|
||||||||
| public override string Issue => "WebView on Windows Does Not Inherit App Theme"; | ||||||||
|
|
||||||||
| [Test] | ||||||||
| [Category(UITestCategories.WebView)] | ||||||||
| public void WebViewWithLightTheme() | ||||||||
| { | ||||||||
| App.WaitForElement("WebButton"); | ||||||||
| App.Tap("WebButton"); | ||||||||
| VerifyScreenshot(); | ||||||||
| } | ||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
|
|
||||||||
| [Test] | ||||||||
| [Category(UITestCategories.WebView)] | ||||||||
| public void WebViewWithDarkTheme() | ||||||||
| { | ||||||||
| App.WaitForElement("WebButton"); | ||||||||
|
||||||||
| App.WaitForElement("WebButton"); | |
| App.WaitForElement("WebButton"); | |
| App.WaitForElement("ThemeButton"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue as WebViewWithLightTheme: tap-then-screenshot has no wait. In addition, the theme toggle in OnThemeButtonClicked is propagated asynchronously through MAUI to WinUI's RequestedTheme. Without a small wait between Tap("ThemeButton") and Tap("WebButton"), the new WebView's ActualTheme may still be Light when UpdateBackground runs.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| using System; | ||
| using Microsoft.Maui.Graphics; | ||
| using Microsoft.UI.Xaml; | ||
| using Microsoft.UI.Xaml.Controls; | ||
| using Microsoft.Web.WebView2.Core; | ||
|
|
||
|
|
@@ -50,7 +51,12 @@ internal static void UpdateBackground(this WebView2 platformWebView, IWebView we | |
| { | ||
| if (platformWebView.CoreWebView2 is not null) | ||
| { | ||
| platformWebView.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Auto; | ||
| platformWebView.CoreWebView2.Profile.PreferredColorScheme = platformWebView.ActualTheme switch | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| ElementTheme.Dark => CoreWebView2PreferredColorScheme.Dark, | ||
| ElementTheme.Light => CoreWebView2PreferredColorScheme.Light, | ||
| _ => CoreWebView2PreferredColorScheme.Auto | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation uses spaces here while surrounding lines use tabs.