Skip to content

Commit d05f9de

Browse files
authored
Fix: sync titlebar button colors with app theme changes (#2016)
## Description When the app theme is set to Light or Dark (not “Use system setting”) and the app theme differs from the system theme, the app initially opens in the system theme and then switches to the chosen app theme. Previously, the titlebar button colors were not updated during this theme change, resulting in low contrast. This fix updates the button colors whenever the root grid theme changes and removes the old broken logic. ## Motivation and Context - Fixes #1605 - Fixes #1970 ## How Has This Been Tested? Manually tested ## Screenshots (if appropriate): ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change)
1 parent 8ee9f99 commit d05f9de

File tree

2 files changed

+2
-16
lines changed

2 files changed

+2
-16
lines changed

WinUIGallery/Helpers/TitleBarHelper.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ public static Windows.UI.Color ApplySystemThemeToCaptionButtons(Window window, E
1919

2020
public static void SetCaptionButtonColors(Window window, Windows.UI.Color color)
2121
{
22-
var res = Application.Current.Resources;
23-
res["WindowCaptionForeground"] = color;
2422
window.AppWindow.TitleBar.ButtonForegroundColor = color;
2523
}
2624

WinUIGallery/MainWindow.xaml.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using System.Threading;
1515
using System.Threading.Tasks;
1616
using Windows.Foundation;
17-
using Windows.UI.ViewManagement;
1817
using WinUIGallery.Helpers;
1918
using WinUIGallery.Models;
2019
using WinUIGallery.Pages;
@@ -25,7 +24,6 @@ public sealed partial class MainWindow : Window
2524
{
2625
public Windows.System.VirtualKey ArrowKey;
2726
public Microsoft.UI.Dispatching.DispatcherQueue dispatcherQueue;
28-
private UISettings _settings;
2927

3028
public NavigationView NavigationView
3129
{
@@ -38,14 +36,14 @@ public MainWindow()
3836
{
3937
this.InitializeComponent();
4038
SetWindowProperties();
39+
RootGrid.ActualThemeChanged += (_,_) => TitleBarHelper.ApplySystemThemeToCaptionButtons(this, RootGrid.ActualTheme);
4140
dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread();
4241
}
4342

4443
private void RootGrid_Loaded(object sender, RoutedEventArgs e)
4544
{
4645
NavigationOrientationHelper.UpdateNavigationViewForElement(NavigationOrientationHelper.IsLeftMode());
47-
_settings = new UISettings();
48-
_settings.ColorValuesChanged += _settings_ColorValuesChanged; // cannot use FrameworkElement.ActualThemeChanged event because the triggerTitleBarRepaint workaround no longer works
46+
TitleBarHelper.ApplySystemThemeToCaptionButtons(this, RootGrid.ActualTheme);
4947
}
5048

5149
private void SetWindowProperties()
@@ -99,16 +97,6 @@ void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
9997
{
10098
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
10199
}
102-
// this handles updating the caption button colors correctly when windows system theme is changed
103-
// while the app is open
104-
private void _settings_ColorValuesChanged(UISettings sender, object args)
105-
{
106-
// This calls comes off-thread, hence we will need to dispatch it to current app's thread
107-
dispatcherQueue.TryEnqueue(() =>
108-
{
109-
_ = TitleBarHelper.ApplySystemThemeToCaptionButtons(this, rootFrame.ActualTheme);
110-
});
111-
}
112100

113101
// Wraps a call to rootFrame.Navigate to give the Page a way to know which NavigationRootPage is navigating.
114102
// Please call this function rather than rootFrame.Navigate to navigate the rootFrame.

0 commit comments

Comments
 (0)