2020-10-30 18:54:50 +08:00
|
|
|
// Copyright (c) Microsoft Corporation
|
2020-08-06 05:06:55 +08:00
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
2020-06-06 00:58:30 +08:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Windows;
|
2020-10-24 06:05:07 +08:00
|
|
|
using ManagedCommon;
|
2020-08-06 05:06:55 +08:00
|
|
|
using Microsoft.Win32;
|
2020-06-06 00:58:30 +08:00
|
|
|
|
2020-06-27 08:42:06 +08:00
|
|
|
namespace Wox.Plugin
|
2020-06-06 00:58:30 +08:00
|
|
|
{
|
2020-06-27 08:42:06 +08:00
|
|
|
public class ThemeManager : IDisposable
|
2020-06-06 00:58:30 +08:00
|
|
|
{
|
2020-08-12 04:41:41 +08:00
|
|
|
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";
|
|
|
|
|
2020-06-06 00:58:30 +08:00
|
|
|
private Theme currentTheme;
|
2020-10-27 06:14:33 +08:00
|
|
|
private bool _disposed;
|
2020-06-06 00:58:30 +08:00
|
|
|
|
2020-06-27 08:42:06 +08:00
|
|
|
public event ThemeChangedHandler ThemeChanged;
|
|
|
|
|
2020-06-06 00:58:30 +08:00
|
|
|
public ThemeManager(Application app)
|
|
|
|
{
|
2020-08-12 04:41:41 +08:00
|
|
|
_app = app;
|
2020-06-06 00:58:30 +08:00
|
|
|
|
2020-08-12 04:41:41 +08:00
|
|
|
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");
|
2020-06-06 00:58:30 +08:00
|
|
|
|
|
|
|
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
|
2020-10-24 06:05:07 +08:00
|
|
|
new ControlzEx.Theming.LibraryTheme(
|
2020-08-12 04:41:41 +08:00
|
|
|
highContrastOneThemeUri,
|
2020-10-30 04:31:32 +08:00
|
|
|
CustomLibraryThemeProvider.DefaultInstance));
|
2020-06-06 00:58:30 +08:00
|
|
|
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
|
2020-10-24 06:05:07 +08:00
|
|
|
new ControlzEx.Theming.LibraryTheme(
|
2020-08-12 04:41:41 +08:00
|
|
|
highContrastTwoThemeUri,
|
2020-10-30 04:31:32 +08:00
|
|
|
CustomLibraryThemeProvider.DefaultInstance));
|
2020-06-06 00:58:30 +08:00
|
|
|
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
|
2020-10-24 06:05:07 +08:00
|
|
|
new ControlzEx.Theming.LibraryTheme(
|
2020-08-12 04:41:41 +08:00
|
|
|
highContrastBlackThemeUri,
|
2020-10-30 04:31:32 +08:00
|
|
|
CustomLibraryThemeProvider.DefaultInstance));
|
2020-06-06 00:58:30 +08:00
|
|
|
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
|
2020-10-24 06:05:07 +08:00
|
|
|
new ControlzEx.Theming.LibraryTheme(
|
2020-08-12 04:41:41 +08:00
|
|
|
highContrastWhiteThemeUri,
|
2020-10-30 04:31:32 +08:00
|
|
|
CustomLibraryThemeProvider.DefaultInstance));
|
2020-06-06 00:58:30 +08:00
|
|
|
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
|
2020-10-24 06:05:07 +08:00
|
|
|
new ControlzEx.Theming.LibraryTheme(
|
2020-08-12 04:41:41 +08:00
|
|
|
lightThemeUri,
|
2020-10-30 04:31:32 +08:00
|
|
|
CustomLibraryThemeProvider.DefaultInstance));
|
2020-06-06 00:58:30 +08:00
|
|
|
ControlzEx.Theming.ThemeManager.Current.AddLibraryTheme(
|
2020-10-24 06:05:07 +08:00
|
|
|
new ControlzEx.Theming.LibraryTheme(
|
2020-08-12 04:41:41 +08:00
|
|
|
darkThemeUri,
|
2020-10-30 04:31:32 +08:00
|
|
|
CustomLibraryThemeProvider.DefaultInstance));
|
2020-06-06 00:58:30 +08:00
|
|
|
|
|
|
|
ControlzEx.Theming.ThemeManager.Current.ThemeChanged += Current_ThemeChanged;
|
2020-10-30 18:54:50 +08:00
|
|
|
|
2020-10-31 18:58:31 +08:00
|
|
|
// Currently there is an issue in ControlzEx, so we must use SyncAll to sync also HighContrast themes.
|
|
|
|
// We can change this after using next release.
|
|
|
|
ControlzEx.Theming.ThemeManager.Current.ThemeSyncMode = ControlzEx.Theming.ThemeSyncMode.SyncAll;
|
2020-10-30 18:54:50 +08:00
|
|
|
|
|
|
|
ControlzEx.Theming.ThemeManager.Current.SyncTheme();
|
2020-06-06 00:58:30 +08:00
|
|
|
}
|
|
|
|
|
2020-06-27 08:42:06 +08:00
|
|
|
public Theme GetCurrentTheme()
|
|
|
|
{
|
|
|
|
return currentTheme;
|
|
|
|
}
|
2020-07-23 04:27:17 +08:00
|
|
|
|
2020-06-27 08:42:06 +08:00
|
|
|
private static Theme GetHighContrastBaseType()
|
2020-06-06 00:58:30 +08:00
|
|
|
{
|
2020-08-12 04:41:41 +08:00
|
|
|
string registryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes";
|
|
|
|
string theme = (string)Registry.GetValue(registryKey, "CurrentTheme", string.Empty);
|
2020-06-06 00:58:30 +08:00
|
|
|
theme = theme.Split('\\').Last().Split('.').First().ToString();
|
|
|
|
|
2020-08-12 04:41:41 +08:00
|
|
|
switch (theme)
|
|
|
|
{
|
|
|
|
case "hc1":
|
|
|
|
return Theme.HighContrastOne;
|
|
|
|
case "hc2":
|
|
|
|
return Theme.HighContrastTwo;
|
|
|
|
case "hcwhite":
|
|
|
|
return Theme.HighContrastWhite;
|
|
|
|
case "hcblack":
|
|
|
|
return Theme.HighContrastBlack;
|
|
|
|
default:
|
2020-10-24 06:05:07 +08:00
|
|
|
return Theme.HighContrastOne;
|
2020-08-12 04:41:41 +08:00
|
|
|
}
|
2020-06-06 00:58:30 +08:00
|
|
|
}
|
|
|
|
|
2020-06-27 08:42:06 +08:00
|
|
|
private void ResetTheme()
|
2020-06-06 00:58:30 +08:00
|
|
|
{
|
2020-10-24 06:05:07 +08:00
|
|
|
ChangeTheme(currentTheme, false);
|
2020-06-06 00:58:30 +08:00
|
|
|
}
|
|
|
|
|
2020-10-24 06:05:07 +08:00
|
|
|
public void ChangeTheme(Theme theme, bool forceSystem)
|
2020-06-06 00:58:30 +08:00
|
|
|
{
|
2020-06-27 08:42:06 +08:00
|
|
|
Theme oldTheme = currentTheme;
|
2020-08-12 04:41:41 +08:00
|
|
|
|
2020-10-24 06:05:07 +08:00
|
|
|
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)
|
2020-06-06 00:58:30 +08:00
|
|
|
{
|
|
|
|
currentTheme = Theme.HighContrastOne;
|
2020-10-31 18:58:31 +08:00
|
|
|
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(_app, HighContrastOneTheme, true);
|
2020-06-06 00:58:30 +08:00
|
|
|
}
|
|
|
|
else if (theme == Theme.HighContrastTwo)
|
|
|
|
{
|
|
|
|
currentTheme = Theme.HighContrastTwo;
|
2020-10-31 18:58:31 +08:00
|
|
|
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(_app, HighContrastTwoTheme, true);
|
2020-06-06 00:58:30 +08:00
|
|
|
}
|
|
|
|
else if (theme == Theme.HighContrastWhite)
|
|
|
|
{
|
|
|
|
currentTheme = Theme.HighContrastWhite;
|
2020-10-31 18:58:31 +08:00
|
|
|
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(_app, HighContrastWhiteTheme, true);
|
2020-06-06 00:58:30 +08:00
|
|
|
}
|
|
|
|
else if (theme == Theme.HighContrastBlack)
|
|
|
|
{
|
|
|
|
currentTheme = Theme.HighContrastBlack;
|
2020-10-31 18:58:31 +08:00
|
|
|
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(_app, HighContrastBlackTheme, true);
|
2020-06-06 00:58:30 +08:00
|
|
|
}
|
|
|
|
else if (theme == Theme.Light)
|
|
|
|
{
|
|
|
|
currentTheme = Theme.Light;
|
2020-10-24 06:05:07 +08:00
|
|
|
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(_app, LightTheme);
|
2020-06-06 00:58:30 +08:00
|
|
|
}
|
|
|
|
else if (theme == Theme.Dark)
|
|
|
|
{
|
|
|
|
currentTheme = Theme.Dark;
|
2020-10-24 06:05:07 +08:00
|
|
|
ControlzEx.Theming.ThemeManager.Current.ChangeTheme(_app, DarkTheme);
|
2020-06-06 00:58:30 +08:00
|
|
|
}
|
2020-08-06 05:06:55 +08:00
|
|
|
|
2020-06-27 08:42:06 +08:00
|
|
|
ThemeChanged?.Invoke(oldTheme, currentTheme);
|
2020-10-24 06:05:07 +08:00
|
|
|
|
|
|
|
if (forceSystem)
|
|
|
|
{
|
|
|
|
currentTheme = Theme.System;
|
|
|
|
}
|
2020-06-06 00:58:30 +08:00
|
|
|
}
|
|
|
|
|
2020-10-24 06:05:07 +08:00
|
|
|
private void Current_ThemeChanged(object sender, ControlzEx.Theming.ThemeChangedEventArgs e)
|
2020-06-06 00:58:30 +08:00
|
|
|
{
|
2020-10-31 18:58:31 +08:00
|
|
|
ControlzEx.Theming.ThemeManager.Current.ThemeChanged -= Current_ThemeChanged;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ResetTheme();
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
ControlzEx.Theming.ThemeManager.Current.ThemeChanged += Current_ThemeChanged;
|
|
|
|
}
|
2020-06-06 00:58:30 +08:00
|
|
|
}
|
2020-06-27 08:42:06 +08:00
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (!_disposed)
|
2020-07-23 04:27:17 +08:00
|
|
|
{
|
2020-06-27 08:42:06 +08:00
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
ControlzEx.Theming.ThemeManager.Current.ThemeChanged -= Current_ThemeChanged;
|
|
|
|
_disposed = true;
|
2020-07-23 04:27:17 +08:00
|
|
|
}
|
2020-06-27 08:42:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(disposing: true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
2020-06-06 00:58:30 +08:00
|
|
|
}
|
|
|
|
|
2020-06-27 08:42:06 +08:00
|
|
|
public delegate void ThemeChangedHandler(Theme oldTheme, Theme newTheme);
|
2020-06-06 00:58:30 +08:00
|
|
|
}
|