correct theme gets applied when windows theme is changed while the app is running

This commit is contained in:
Pratik Anand 2023-05-09 14:00:18 -07:00
Родитель ec70cfb66f
Коммит 55756289ab
5 изменённых файлов: 44 добавлений и 2 удалений

Просмотреть файл

@ -7,6 +7,7 @@ namespace Param_RootNamespace;
public partial class App : Application
{
public static WindowEx MainWindow { get; } = new MainWindow();
public static UIElement? AppTitlebar;
public App()
{

Просмотреть файл

@ -1,4 +1,4 @@
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
@ -97,4 +97,24 @@ internal class TitleBarHelper
}
}
}
public static void ApplySystemThemeToCaptionButtons()
{
var res = Application.Current.Resources;
var frame = App.AppTitlebar as FrameworkElement;
if (frame != null)
{
if (frame.ActualTheme == ElementTheme.Dark)
{
res["WindowCaptionForeground"] = Colors.White;
}
else
{
res["WindowCaptionForeground"] = Colors.Black;
}
UpdateTitleBar(frame.ActualTheme);
}
}
}

Просмотреть файл

@ -1,9 +1,11 @@
using Param_RootNamespace.Helpers;
using Windows.UI.ViewManagement;
namespace Param_RootNamespace;
public sealed partial class MainWindow : WindowEx
{
public Microsoft.UI.Dispatching.DispatcherQueue dispatcherQueue;
private UISettings _settings;
public MainWindow()
{
InitializeComponent();
@ -11,5 +13,21 @@ public sealed partial class MainWindow : WindowEx
AppWindow.SetIcon(Path.Combine(AppContext.BaseDirectory, "Assets/WindowIcon.ico"));
Content = null;
Title = "AppDisplayName".GetLocalized();
// Theme change code picked from https://github.com/microsoft/WinUI-Gallery/pull/1239
dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread();
_settings = new UISettings();
_settings.ColorValuesChanged += _settings_ColorValuesChanged; // cannot use FrameworkElement.ActualThemeChanged event because the triggerTitleBarRepaint workaround no longer works
}
// this handles updating the caption button colors correctly when indows 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();
});
}
}

Просмотреть файл

@ -45,6 +45,7 @@ public sealed partial class ShellPage : Page
var resource = args.WindowActivationState == WindowActivationState.Deactivated ? "WindowCaptionForegroundDisabled" : "WindowCaptionForeground";
AppTitleBarText.Foreground = (SolidColorBrush)App.Current.Resources[resource];
App.AppTitlebar = AppTitleBarText as UIElement;
}
private void OnUnloaded(object sender, RoutedEventArgs e)

Просмотреть файл

@ -47,6 +47,8 @@ public sealed partial class ShellPage : Page
var resource = args.WindowActivationState == WindowActivationState.Deactivated ? "WindowCaptionForegroundDisabled" : "WindowCaptionForeground";
AppTitleBarText.Foreground = (SolidColorBrush)App.Current.Resources[resource];
App.AppTitlebar = AppTitleBarText as UIElement;
}
private void NavigationViewControl_DisplayModeChanged(NavigationView sender, NavigationViewDisplayModeChangedEventArgs args)