PowerToys/src/modules/launcher/Wox.Plugin/ThemeManager.cs

183 lines
7.0 KiB
C#
Raw Normal View History

// 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.
using System;
using System.Linq;
using System.Windows;
using ManagedCommon;
using Microsoft.Win32;
namespace Wox.Plugin
{
public class ThemeManager : IDisposable
{
private readonly Application _app;
private const string LightTheme = "Light.Accent1";
private const string DarkTheme = "Dark.Accent1";
private const string HighContrastOneTheme = "HighContrast.Accent2";
private const string HighContrastTwoTheme = "HighContrast.Accent3";
private const string HighContrastBlackTheme = "HighContrast.Accent4";
private const string HighContrastWhiteTheme = "HighContrast.Accent5";
private Theme currentTheme;
[fxcop] Fixes for Wox.Plugin (1of3) (#7457) * Added CultureInfo (CA1307: Specify StringComparison for clarity / CA1304: Specify CultureInfo) * Check arguments and throw ArgumentNullException (CA1062: Validate arguments of public methods) * Changed url parameter from string to System.Uri and added null checks (CA1054: URI parameters should not be strings) * Rethrow exception without specifying the exception explicitly (CA2200: Rethrow to preserve stack details) * Changed from Collection property to methods for PluginMetadata::ActionKeywords (CA2227: Collection properties should be read only) * Changed from Collection property to methods for Result::GetTitleHighlightData (CA2227: Collection properties should be read only) * Made Collection property read-only and added parameter in constructor for Result::SubTitleHighlightData (CA2227: Collection properties should be read only) * Made Collection property read only and added parameter in constructor for ResultUpdatedEventArgs::Results (CA2227: Collection properties should be read only) * CA1507: Use nameof in place of string * Removed initialization for ThemeManager::_disposed (CA1805: Do not initialize unnecessarily) * Changed Query::Terms array property to ReadOnlyCollection and added private set (CA1819: Properties should not return arrays) * CA1060: Move P/Invokes to NativeMethods class * CA1806: Do not ignore method results * CA2101: Specify marshaling for P/Invoke string arguments * Removed unnecessary empty interface IFeatures (CA1040: Avoid empty interfaces) - Removed IFeatures interface and references - Renamed IFeatures.cs to IContextMenu.cs according to guidelines * Added comments for CultureInfo (CA1307: Specify StringComparison for clarity / CA1304: Specify CultureInfo) * Added localization for Wox.Plugin and localized strings in FilesFolders.cs
2020-10-27 06:14:33 +08:00
private bool _disposed;
public event ThemeChangedHandler ThemeChanged;
public ThemeManager(Application app)
{
_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 ControlzEx.Theming.LibraryTheme(
highContrastOneThemeUri,
CustomLibraryThemeProvider.DefaultInstance));
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
new ControlzEx.Theming.LibraryTheme(
highContrastTwoThemeUri,
CustomLibraryThemeProvider.DefaultInstance));
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
new ControlzEx.Theming.LibraryTheme(
highContrastBlackThemeUri,
CustomLibraryThemeProvider.DefaultInstance));
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
new ControlzEx.Theming.LibraryTheme(
highContrastWhiteThemeUri,
CustomLibraryThemeProvider.DefaultInstance));
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
new ControlzEx.Theming.LibraryTheme(
lightThemeUri,
CustomLibraryThemeProvider.DefaultInstance));
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
new ControlzEx.Theming.LibraryTheme(
darkThemeUri,
CustomLibraryThemeProvider.DefaultInstance));
ResetTheme();
ControlzEx.Theming.ThemeManager.Current.ThemeSyncMode = ControlzEx.Theming.ThemeSyncMode.SyncAll;
ControlzEx.Theming.ThemeManager.Current.ThemeChanged += Current_ThemeChanged;
}
public Theme GetCurrentTheme()
{
return currentTheme;
}
private 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();
switch (theme)
{
case "hc1":
return Theme.HighContrastOne;
case "hc2":
return Theme.HighContrastTwo;
case "hcwhite":
return Theme.HighContrastWhite;
case "hcblack":
return Theme.HighContrastBlack;
default:
return Theme.HighContrastOne;
}
}
private void ResetTheme()
{
ChangeTheme(currentTheme, false);
}
public void ChangeTheme(Theme theme, bool forceSystem)
{
Theme oldTheme = currentTheme;
if (theme == Theme.System)
{
currentTheme = Theme.System;
if (ControlzEx.Theming.WindowsThemeHelper.IsHighContrastEnabled())
{
Theme highContrastBaseType = GetHighContrastBaseType();
ChangeTheme(highContrastBaseType, true);
}
else
{
string baseColor = ControlzEx.Theming.WindowsThemeHelper.GetWindowsBaseColor();
ChangeTheme((Theme)Enum.Parse(typeof(Theme), baseColor), true);
}
}
else if (theme == Theme.HighContrastOne)
{
currentTheme = Theme.HighContrastOne;
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(_app, HighContrastOneTheme);
}
else if (theme == Theme.HighContrastTwo)
{
currentTheme = Theme.HighContrastTwo;
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(_app, HighContrastTwoTheme);
}
else if (theme == Theme.HighContrastWhite)
{
currentTheme = Theme.HighContrastWhite;
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(_app, HighContrastWhiteTheme);
}
else if (theme == Theme.HighContrastBlack)
{
currentTheme = Theme.HighContrastBlack;
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(_app, HighContrastBlackTheme);
}
else if (theme == Theme.Light)
{
currentTheme = Theme.Light;
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(_app, LightTheme);
}
else if (theme == Theme.Dark)
{
currentTheme = Theme.Dark;
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(_app, DarkTheme);
}
ThemeChanged?.Invoke(oldTheme, currentTheme);
if (forceSystem)
{
currentTheme = Theme.System;
}
}
private void Current_ThemeChanged(object sender, ControlzEx.Theming.ThemeChangedEventArgs e)
{
ResetTheme();
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
ControlzEx.Theming.ThemeManager.Current.ThemeChanged -= Current_ThemeChanged;
_disposed = true;
}
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
public delegate void ThemeChangedHandler(Theme oldTheme, Theme newTheme);
}