mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 03:36:44 +02:00
Theme aware plugin (#4499)
* Migrate theme manager to infrastructure and added it as input to public API instance * Working event-delegate for PublicAPIInstance * Theme aware UWP applications * Theme aware program plugin * Update query icon on theme change * Theme aware calculator plugin * Fix issue with query running before theme change * Theme based changes in ImageLoader * Removed ErrorIcon direct references and added references from ImageLoader * Nit fixes * Removed unnecessary TODO in UWP.cs * Added preference to theme based icons * Added IDisposable interfaces to unsubscribe events
This commit is contained in:
committed by
GitHub
parent
d9597d5ad5
commit
d3b10d0d4d
@@ -17,6 +17,7 @@ using Wox.Infrastructure.Http;
|
||||
using Wox.Infrastructure.Image;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.UserSettings;
|
||||
using Wox.Plugin;
|
||||
using Wox.ViewModel;
|
||||
using Stopwatch = Wox.Infrastructure.Stopwatch;
|
||||
|
||||
@@ -31,6 +32,7 @@ namespace PowerLauncher
|
||||
private Settings _settings;
|
||||
private MainViewModel _mainVM;
|
||||
private MainWindow _mainWindow;
|
||||
private ThemeManager _themeManager;
|
||||
private SettingWindowViewModel _settingsVM;
|
||||
private readonly Alphabet _alphabet = new Alphabet();
|
||||
private StringMatcher _stringMatcher;
|
||||
@@ -70,7 +72,8 @@ namespace PowerLauncher
|
||||
RegisterAppDomainExceptions();
|
||||
RegisterDispatcherUnhandledException();
|
||||
|
||||
ImageLoader.Initialize();
|
||||
_themeManager = new ThemeManager(this);
|
||||
ImageLoader.Initialize(_themeManager.GetCurrentTheme());
|
||||
|
||||
_settingsVM = new SettingWindowViewModel();
|
||||
_settings = _settingsVM.Settings;
|
||||
@@ -80,11 +83,10 @@ namespace PowerLauncher
|
||||
StringMatcher.Instance = _stringMatcher;
|
||||
_stringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;
|
||||
|
||||
ThemeManager themeManager = new ThemeManager(this);
|
||||
PluginManager.LoadPlugins(_settings.PluginSettings);
|
||||
_mainVM = new MainViewModel(_settings);
|
||||
_mainWindow = new MainWindow(_settings, _mainVM);
|
||||
API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet);
|
||||
API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet, _themeManager);
|
||||
PluginManager.InitializePlugins(API);
|
||||
|
||||
Current.MainWindow = _mainWindow;
|
||||
@@ -105,6 +107,7 @@ namespace PowerLauncher
|
||||
|
||||
_mainVM.MainWindowVisibility = Visibility.Visible;
|
||||
_mainVM.ColdStartFix();
|
||||
_themeManager.ThemeChanged += OnThemeChanged;
|
||||
Log.Info("|App.OnStartup|End PowerToys Run startup ---------------------------------------------------- ");
|
||||
|
||||
bootTime.Stop();
|
||||
@@ -125,6 +128,17 @@ namespace PowerLauncher
|
||||
Current.SessionEnding += (s, e) => Dispose();
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
ImageLoader.UpdateIconPath(newTheme);
|
||||
_mainVM.Query();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// let exception throw as normal is better for Debug
|
||||
/// </summary>
|
||||
@@ -158,9 +172,13 @@ namespace PowerLauncher
|
||||
Log.Info("|App.OnExit| Start PowerToys Run Exit---------------------------------------------------- ");
|
||||
if (disposing)
|
||||
{
|
||||
_mainWindow.Dispose();
|
||||
_themeManager.ThemeChanged -= OnThemeChanged;
|
||||
API.SaveAppAllSettings();
|
||||
PluginManager.Dispose();
|
||||
_mainWindow.Dispose();
|
||||
API.Dispose();
|
||||
_mainVM.Dispose();
|
||||
_themeManager.Dispose();
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,154 +0,0 @@
|
||||
using ControlzEx.Theming;
|
||||
using MahApps.Metro.Theming;
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
|
||||
namespace Wox.Core.Resource
|
||||
{
|
||||
public class ThemeManager
|
||||
{
|
||||
private Theme currentTheme;
|
||||
private readonly Application App;
|
||||
private readonly string LightTheme = "Light.Accent1";
|
||||
private readonly string DarkTheme = "Dark.Accent1";
|
||||
private readonly string HighContrastOneTheme = "HighContrast.Accent2";
|
||||
private readonly string HighContrastTwoTheme = "HighContrast.Accent3";
|
||||
private readonly string HighContrastBlackTheme = "HighContrast.Accent4";
|
||||
private readonly string HighContrastWhiteTheme = "HighContrast.Accent5";
|
||||
|
||||
public ThemeManager(Application app)
|
||||
{
|
||||
this.App = app;
|
||||
|
||||
Uri HighContrastOneThemeUri = new Uri("pack://application:,,,/Themes/HighContrast1.xaml");
|
||||
Uri HighContrastTwoThemeUri = new Uri("pack://application:,,,/Themes/HighContrast2.xaml");
|
||||
Uri HighContrastBlackThemeUri = new Uri("pack://application:,,,/Themes/HighContrastWhite.xaml");
|
||||
Uri HighContrastWhiteThemeUri = new Uri("pack://application:,,,/Themes/HighContrastBlack.xaml");
|
||||
Uri LightThemeUri = new Uri("pack://application:,,,/Themes/Light.xaml");
|
||||
Uri DarkThemeUri = new Uri("pack://application:,,,/Themes/Dark.xaml");
|
||||
|
||||
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
|
||||
new LibraryTheme(HighContrastOneThemeUri,
|
||||
MahAppsLibraryThemeProvider.DefaultInstance));
|
||||
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
|
||||
new LibraryTheme(HighContrastTwoThemeUri,
|
||||
MahAppsLibraryThemeProvider.DefaultInstance));
|
||||
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
|
||||
new LibraryTheme(HighContrastBlackThemeUri,
|
||||
MahAppsLibraryThemeProvider.DefaultInstance));
|
||||
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
|
||||
new LibraryTheme(HighContrastWhiteThemeUri,
|
||||
MahAppsLibraryThemeProvider.DefaultInstance));
|
||||
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
|
||||
new LibraryTheme(LightThemeUri,
|
||||
MahAppsLibraryThemeProvider.DefaultInstance));
|
||||
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
|
||||
new LibraryTheme(DarkThemeUri,
|
||||
MahAppsLibraryThemeProvider.DefaultInstance));
|
||||
|
||||
ResetTheme();
|
||||
ControlzEx.Theming.ThemeManager.Current.ThemeSyncMode = ThemeSyncMode.SyncWithAppMode;
|
||||
ControlzEx.Theming.ThemeManager.Current.ThemeChanged += Current_ThemeChanged;
|
||||
SystemParameters.StaticPropertyChanged += (sender, args) =>
|
||||
{
|
||||
if (args.PropertyName == nameof(SystemParameters.HighContrast))
|
||||
{
|
||||
ResetTheme();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public static Theme GetHighContrastBaseType()
|
||||
{
|
||||
string RegistryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes";
|
||||
string theme = (string) Registry.GetValue(RegistryKey, "CurrentTheme", string.Empty);
|
||||
theme = theme.Split('\\').Last().Split('.').First().ToString();
|
||||
|
||||
if (theme == "hc1")
|
||||
return Theme.HighContrastOne;
|
||||
else if (theme == "hc2")
|
||||
return Theme.HighContrastTwo;
|
||||
else if (theme == "hcwhite")
|
||||
return Theme.HighContrastWhite;
|
||||
else if (theme == "hcblack")
|
||||
return Theme.HighContrastBlack;
|
||||
else
|
||||
return Theme.None;
|
||||
}
|
||||
|
||||
public void ResetTheme()
|
||||
{
|
||||
if (SystemParameters.HighContrast)
|
||||
{
|
||||
Theme highContrastBaseType = GetHighContrastBaseType();
|
||||
ChangeTheme(highContrastBaseType);
|
||||
}
|
||||
else
|
||||
{
|
||||
string baseColor = WindowsThemeHelper.GetWindowsBaseColor();
|
||||
ChangeTheme((Theme)Enum.Parse(typeof(Theme), baseColor));
|
||||
}
|
||||
}
|
||||
|
||||
private void ChangeTheme(Theme theme)
|
||||
{
|
||||
if (theme == currentTheme)
|
||||
return;
|
||||
if (theme == Theme.HighContrastOne)
|
||||
{
|
||||
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(this.App, this.HighContrastOneTheme);
|
||||
currentTheme = Theme.HighContrastOne;
|
||||
}
|
||||
else if (theme == Theme.HighContrastTwo)
|
||||
{
|
||||
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(this.App, this.HighContrastTwoTheme);
|
||||
currentTheme = Theme.HighContrastTwo;
|
||||
}
|
||||
else if (theme == Theme.HighContrastWhite)
|
||||
{
|
||||
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(this.App, this.HighContrastWhiteTheme);
|
||||
currentTheme = Theme.HighContrastWhite;
|
||||
}
|
||||
else if (theme == Theme.HighContrastBlack)
|
||||
{
|
||||
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(this.App, this.HighContrastBlackTheme);
|
||||
currentTheme = Theme.HighContrastBlack;
|
||||
}
|
||||
else if (theme == Theme.Light)
|
||||
{
|
||||
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(this.App, this.LightTheme);
|
||||
currentTheme = Theme.Light;
|
||||
}
|
||||
else if (theme == Theme.Dark)
|
||||
{
|
||||
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(this.App, this.DarkTheme);
|
||||
currentTheme = Theme.Dark;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentTheme = Theme.None;
|
||||
}
|
||||
Debug.WriteLine("Theme Changed to :" + currentTheme);
|
||||
}
|
||||
|
||||
private void Current_ThemeChanged(object sender, ThemeChangedEventArgs e)
|
||||
{
|
||||
ResetTheme();
|
||||
}
|
||||
}
|
||||
|
||||
public enum Theme
|
||||
{
|
||||
None,
|
||||
Light,
|
||||
Dark,
|
||||
HighContrastOne,
|
||||
HighContrastTwo,
|
||||
HighContrastBlack,
|
||||
HighContrastWhite,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user