Skip to content

Commit 1a67b0b

Browse files
MuyuanMSCopilot
andcommitted
Address review: handle HighContrast themes in initial ThemeMode selection
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 029dca0 commit 1a67b0b

3 files changed

Lines changed: 41 additions & 10 deletions

File tree

src/modules/launcher/PowerLauncher/App.xaml.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,13 @@ private void OnStartup(object sender, StartupEventArgs e)
154154

155155
_mainVM = new MainViewModel(_settings, NativeThreadCTS.Token);
156156

157-
// Set ThemeMode before MainWindow creation so Fluent static resources
158-
// (e.g. DefaultTextBoxStyle, CaptionTextBlockStyle) are available for XAML
159-
// parsing. ThemeManager will apply the correct final theme asynchronously.
160-
// For high-contrast themes, Dark is used as a temporary fallback because
161-
// ThemeManager will switch to ThemeMode.None and apply custom high-contrast
162-
// resource dictionaries once it processes the actual theme. Dark is chosen
163-
// because the high-contrast themes are visually closer to dark than light.
157+
// Set ThemeMode before MainWindow creation so the initial theme state is
158+
// consistent with ThemeManager. This avoids preloading Dark Fluent resources
159+
// when Windows starts in a High Contrast theme.
164160
var themeHelper = new ThemeHelper();
165161
var initialTheme = themeHelper.DetermineTheme(_settings.Theme);
166162
#pragma warning disable WPF0001
167-
Application.Current.ThemeMode = initialTheme == Theme.Light ? ThemeMode.Light : ThemeMode.Dark;
163+
Application.Current.ThemeMode = ThemeManager.GetThemeMode(initialTheme);
168164
#pragma warning restore WPF0001
169165

170166
_mainWindow = new MainWindow(_settings, _mainVM, NativeThreadCTS.Token);

src/modules/launcher/PowerLauncher/Helper/ThemeManager.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ public class ThemeManager : IDisposable
3232

3333
public event Common.UI.ThemeChangedHandler ThemeChanged;
3434

35+
internal static ThemeMode GetThemeMode(Theme theme) => theme switch
36+
{
37+
Theme.Light => ThemeMode.Light,
38+
Theme.Dark => ThemeMode.Dark,
39+
Theme.HighContrastBlack or Theme.HighContrastWhite or Theme.HighContrastOne or
40+
Theme.HighContrastTwo => ThemeMode.None,
41+
_ => ThemeMode.Dark,
42+
};
43+
3544
public ThemeManager(PowerToysRunSettings settings, MainWindow mainWindow)
3645
{
3746
_settings = settings;
@@ -65,7 +74,7 @@ private void SetSystemTheme(Theme theme)
6574
// Need to disable WPF0001 since setting Application.Current.ThemeMode is experimental
6675
// https://learn.microsoft.com/en-us/dotnet/desktop/wpf/whats-new/net90#set-in-code
6776
#pragma warning disable WPF0001
68-
Application.Current.ThemeMode = theme == Theme.Light ? ThemeMode.Light : ThemeMode.Dark;
77+
Application.Current.ThemeMode = GetThemeMode(theme);
6978
#pragma warning restore WPF0001
7079

7180
if (!OSVersionHelper.IsWindows11())
@@ -83,7 +92,7 @@ private void SetSystemTheme(Theme theme)
8392
// For high-contrast themes, disable WPF's Fluent theme manager to avoid conflicts
8493
// with the custom high-contrast resource dictionaries.
8594
#pragma warning disable WPF0001
86-
Application.Current.ThemeMode = ThemeMode.None;
95+
Application.Current.ThemeMode = GetThemeMode(theme);
8796
#pragma warning restore WPF0001
8897

8998
_isHighContrastMode = true;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Windows;
6+
using ManagedCommon;
7+
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
using PowerLauncher.Helper;
9+
10+
namespace Wox.Test;
11+
12+
[TestClass]
13+
public class ThemeManagerTest
14+
{
15+
[DataTestMethod]
16+
[DataRow(Theme.Light, ThemeMode.Light)]
17+
[DataRow(Theme.Dark, ThemeMode.Dark)]
18+
[DataRow(Theme.HighContrastBlack, ThemeMode.None)]
19+
[DataRow(Theme.HighContrastWhite, ThemeMode.None)]
20+
[DataRow(Theme.HighContrastOne, ThemeMode.None)]
21+
[DataRow(Theme.HighContrastTwo, ThemeMode.None)]
22+
public void GetThemeMode_ReturnsExpectedThemeMode(Theme theme, ThemeMode expectedThemeMode)
23+
{
24+
Assert.AreEqual(expectedThemeMode, ThemeManager.GetThemeMode(theme));
25+
}
26+
}

0 commit comments

Comments
 (0)