[Run] Switch to WPF UI theme manager (#30520)

* Switch to WPF UI theme manager

* fix

* add theme manager

* fix

* fix

* update error icon

* moved image initialization

* cleanup
This commit is contained in:
Davide Giacometti
2023-12-21 13:56:48 +01:00
committed by GitHub
parent e73e73fa6c
commit ae21b0dc09
12 changed files with 153 additions and 123 deletions

View File

@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
@@ -9,7 +9,6 @@ using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using Common.UI;
using interop;
using ManagedCommon;
using Microsoft.PowerLauncher.Telemetry;
@@ -23,7 +22,6 @@ using Wox.Infrastructure.Image;
using Wox.Infrastructure.UserSettings;
using Wox.Plugin;
using Wox.Plugin.Logger;
using Wpf.Ui.Appearance;
using Stopwatch = Wox.Infrastructure.Stopwatch;
namespace PowerLauncher
@@ -82,7 +80,7 @@ namespace PowerLauncher
{
application.InitializeComponent();
NativeEventWaiter.WaitForEventLoop(
Common.UI.NativeEventWaiter.WaitForEventLoop(
Constants.RunExitEvent(),
() =>
{
@@ -122,8 +120,7 @@ namespace PowerLauncher
RegisterAppDomainExceptions();
RegisterDispatcherUnhandledException();
_themeManager = new ThemeManager(this);
ImageLoader.Initialize(_themeManager.GetCurrentTheme());
ImageLoader.Initialize();
_settingsVM = new SettingWindowViewModel();
_settings = _settingsVM.Settings;
@@ -136,6 +133,7 @@ namespace PowerLauncher
_mainVM = new MainViewModel(_settings, NativeThreadCTS.Token);
_mainWindow = new MainWindow(_settings, _mainVM, NativeThreadCTS.Token);
_themeManager = new ThemeManager(_settings, _mainWindow);
API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet, _themeManager);
_settingsReader = new SettingsReader(_settings, _themeManager);
_settingsReader.ReadSettings();
@@ -152,10 +150,6 @@ namespace PowerLauncher
_settingsReader.ReadSettingsOnChange();
_themeManager.ThemeChanged += OnThemeChanged;
OnThemeChanged(_settings.Theme, _settings.Theme);
textToLog.AppendLine("End PowerToys Run startup ---------------------------------------------------- ");
bootTime.Stop();
@@ -214,48 +208,6 @@ namespace PowerLauncher
};
}
/// <summary>
/// Callback when windows theme is changed.
/// </summary>
/// <param name="oldTheme">Previous Theme</param>
/// <param name="newTheme">Current Theme</param>
private void OnThemeChanged(Theme oldTheme, Theme newTheme)
{
// If OS theme is high contrast, don't change theme.
if (SystemParameters.HighContrast)
{
return;
}
ApplicationTheme theme = ApplicationTheme.Unknown;
switch (newTheme)
{
case Theme.Dark:
theme = ApplicationTheme.Dark; break;
case Theme.Light:
theme = ApplicationTheme.Light; break;
case Theme.HighContrastWhite:
case Theme.HighContrastBlack:
case Theme.HighContrastOne:
case Theme.HighContrastTwo:
theme = ApplicationTheme.HighContrast; break;
default:
break;
}
_mainWindow?.Dispatcher.Invoke(() =>
{
if (theme != ApplicationTheme.Unknown)
{
ApplicationThemeManager.Apply(theme);
}
});
ImageLoader.UpdateIconPath(newTheme);
_mainVM.Query();
}
/// <summary>
/// let exception throw as normal is better for Debug
/// </summary>
@@ -302,19 +254,12 @@ namespace PowerLauncher
Log.Info("Start PowerToys Run Exit---------------------------------------------------- ", GetType());
if (disposing)
{
if (_themeManager != null)
{
_themeManager.ThemeChanged -= OnThemeChanged;
}
API?.SaveAppAllSettings();
PluginManager.Dispose();
// Dispose needs to be called on the main Windows thread, since some resources owned by the thread need to be disposed.
_mainWindow?.Dispatcher.Invoke(Dispose);
API?.Dispose();
_mainVM?.Dispose();
_themeManager?.Dispose();
}
Log.Info("End PowerToys Run Exit ---------------------------------------------------- ", GetType());