Skip to content

Fix: sync titlebar button colors with app theme changes #2016

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions WinUIGallery/Helpers/TitleBarHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public static Windows.UI.Color ApplySystemThemeToCaptionButtons(Window window, E

public static void SetCaptionButtonColors(Window window, Windows.UI.Color color)
{
var res = Application.Current.Resources;
res["WindowCaptionForeground"] = color;
window.AppWindow.TitleBar.ButtonForegroundColor = color;
}

Expand Down
16 changes: 2 additions & 14 deletions WinUIGallery/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.ViewManagement;
using WinUIGallery.Helpers;
using WinUIGallery.Models;
using WinUIGallery.Pages;
Expand All @@ -25,7 +24,6 @@ public sealed partial class MainWindow : Window
{
public Windows.System.VirtualKey ArrowKey;
public Microsoft.UI.Dispatching.DispatcherQueue dispatcherQueue;
private UISettings _settings;

public NavigationView NavigationView
{
Expand All @@ -38,14 +36,14 @@ public MainWindow()
{
this.InitializeComponent();
SetWindowProperties();
RootGrid.ActualThemeChanged += (_,_) => TitleBarHelper.ApplySystemThemeToCaptionButtons(this, RootGrid.ActualTheme);
dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread();
}

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

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

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