更新整理
This commit is contained in:
@@ -4,8 +4,7 @@ using WPFluent.Extensions;
|
||||
namespace WPFluent.Appearance;
|
||||
|
||||
/// <summary>
|
||||
/// Allows to manage the application theme by swapping resource dictionaries containing dynamic resources with color
|
||||
/// information.
|
||||
/// 允许通过交换包含动态资源的资源字典来管理应用程序主题。
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// <code lang="csharp"> ApplicationThemeManager.Apply( ApplicationTheme.Light );</code> <code lang="csharp"> if
|
||||
@@ -27,19 +26,19 @@ public static class ApplicationThemeManager
|
||||
public static event ThemeChangedEvent? Changed;
|
||||
|
||||
/// <summary>
|
||||
/// Tries to guess the currently set application theme.
|
||||
/// 尝试猜测当前设置的应用程序主题。
|
||||
/// </summary>
|
||||
private static void FetchApplicationTheme()
|
||||
{
|
||||
ResourceDictionaryManager appDictionaries = new(LibraryNamespace);
|
||||
ResourceDictionary? themeDictionary = appDictionaries.GetDictionary("theme");
|
||||
ResourceDictionary? themeDictionary = appDictionaries.GetDictionary("themes");
|
||||
|
||||
if (themeDictionary == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string themeUri = themeDictionary.Source.ToString();
|
||||
var themeUri = themeDictionary.Source.ToString();
|
||||
|
||||
if (themeUri.Contains("light", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
@@ -50,15 +49,10 @@ public static class ApplicationThemeManager
|
||||
{
|
||||
_cachedApplicationTheme = ApplicationTheme.Dark;
|
||||
}
|
||||
|
||||
if (themeUri.Contains("highcontrast", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_cachedApplicationTheme = ApplicationTheme.HighContrast;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies Resources in the <paramref name="frameworkElement"/>.
|
||||
/// 将资源应用于 <paramref name="frameworkElement"/>.
|
||||
/// </summary>
|
||||
public static void Apply(FrameworkElement frameworkElement)
|
||||
{
|
||||
@@ -70,19 +64,10 @@ public static class ApplicationThemeManager
|
||||
frameworkElement.Resources.MergedDictionaries.Add(dictionary);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (System.Collections.DictionaryEntry resource in UiApplication.Current.Resources)
|
||||
{
|
||||
System.Diagnostics.Debug
|
||||
.WriteLine(
|
||||
$"INFO | {typeof(ApplicationThemeManager)} Copy Resource {resource.Key} - {resource.Value}",
|
||||
"WPFluent.Appearance");
|
||||
frameworkElement.Resources[resource.Key] = resource.Value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Changes the current application theme.
|
||||
/// 更改当前应用程序主题。
|
||||
/// </summary>
|
||||
/// <param name="applicationTheme">Theme to set.</param>
|
||||
/// <param name="backgroundEffect">Whether the custom background effect should be applied.</param>
|
||||
@@ -94,8 +79,9 @@ public static class ApplicationThemeManager
|
||||
{
|
||||
if (updateAccent)
|
||||
{
|
||||
//TODO
|
||||
ApplicationAccentColorManager.Apply(
|
||||
ApplicationAccentColorManager.GetColorizationColor(),
|
||||
Color.FromRgb(194, 115, 37),
|
||||
applicationTheme,
|
||||
false);
|
||||
}
|
||||
@@ -114,34 +100,36 @@ public static class ApplicationThemeManager
|
||||
case ApplicationTheme.Dark:
|
||||
themeDictionaryName = "Dark";
|
||||
break;
|
||||
case ApplicationTheme.HighContrast:
|
||||
themeDictionaryName = GetSystemTheme() switch
|
||||
{
|
||||
SystemTheme.HC1 => "HC1",
|
||||
SystemTheme.HC2 => "HC2",
|
||||
SystemTheme.HCBlack => "HCBlack",
|
||||
SystemTheme.HCWhite => "HCWhite",
|
||||
_ => "HCWhite",
|
||||
};
|
||||
case ApplicationTheme.Auto:
|
||||
break;
|
||||
case ApplicationTheme.Unknown:
|
||||
break;
|
||||
case ApplicationTheme.Light:
|
||||
themeDictionaryName = "Light";
|
||||
break;
|
||||
case ApplicationTheme.HC1:
|
||||
themeDictionaryName = "HC1";
|
||||
break;
|
||||
case ApplicationTheme.HC2:
|
||||
themeDictionaryName = "HC2";
|
||||
break;
|
||||
case ApplicationTheme.HCBlack:
|
||||
themeDictionaryName = "HCBlack";
|
||||
|
||||
break;
|
||||
case ApplicationTheme.HCWhite:
|
||||
themeDictionaryName = "HCWhite";
|
||||
break;
|
||||
}
|
||||
|
||||
bool isUpdated = appDictionaries.UpdateDictionary(
|
||||
"theme",
|
||||
new Uri($"{ThemesDictionaryPath}{themeDictionaryName}.xaml", UriKind.Absolute));
|
||||
|
||||
System.Diagnostics.Debug
|
||||
.WriteLine(
|
||||
$"INFO | {typeof(ApplicationThemeManager)} tries to update theme to {themeDictionaryName} ({applicationTheme}): {isUpdated}",
|
||||
nameof(ApplicationThemeManager));
|
||||
|
||||
if (!isUpdated)
|
||||
if (!appDictionaries.UpdateDictionary(
|
||||
"themes",
|
||||
new Uri($"{ThemesDictionaryPath}{themeDictionaryName}.xaml", UriKind.Absolute)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SystemThemeManager.UpdateSystemThemeCache();
|
||||
|
||||
_cachedApplicationTheme = applicationTheme;
|
||||
|
||||
Changed?.Invoke(applicationTheme, ApplicationAccentColorManager.SystemAccent);
|
||||
@@ -152,30 +140,8 @@ public static class ApplicationThemeManager
|
||||
}
|
||||
}
|
||||
|
||||
public static void ApplySystemTheme() { ApplySystemTheme(true); }
|
||||
|
||||
public static void ApplySystemTheme(bool updateAccent)
|
||||
{
|
||||
SystemThemeManager.UpdateSystemThemeCache();
|
||||
|
||||
SystemTheme systemTheme = GetSystemTheme();
|
||||
|
||||
ApplicationTheme themeToSet = ApplicationTheme.Light;
|
||||
|
||||
if (systemTheme is SystemTheme.Dark or SystemTheme.CapturedMotion or SystemTheme.Glow)
|
||||
{
|
||||
themeToSet = ApplicationTheme.Dark;
|
||||
}
|
||||
else if (systemTheme is SystemTheme.HC1 or SystemTheme.HC2 or SystemTheme.HCBlack or SystemTheme.HCWhite)
|
||||
{
|
||||
themeToSet = ApplicationTheme.HighContrast;
|
||||
}
|
||||
|
||||
Apply(themeToSet, updateAccent: updateAccent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets currently set application theme.
|
||||
/// 获取当前设置的应用程序主题。
|
||||
/// </summary>
|
||||
/// <returns><see cref="ApplicationTheme.Unknown"/> if something goes wrong.</returns>
|
||||
public static ApplicationTheme GetAppTheme()
|
||||
@@ -187,71 +153,4 @@ public static class ApplicationThemeManager
|
||||
|
||||
return _cachedApplicationTheme;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets currently set system theme.
|
||||
/// </summary>
|
||||
/// <returns><see cref="SystemTheme.Unknown"/> if something goes wrong.</returns>
|
||||
public static SystemTheme GetSystemTheme() { return SystemThemeManager.GetCachedSystemTheme(); }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value that indicates whether the application is matching the system theme.
|
||||
/// </summary>
|
||||
/// <returns><see langword="true"/> if the application has the same theme as the system.</returns>
|
||||
public static bool IsAppMatchesSystem()
|
||||
{
|
||||
ApplicationTheme appApplicationTheme = GetAppTheme();
|
||||
SystemTheme sysTheme = GetSystemTheme();
|
||||
|
||||
return appApplicationTheme switch
|
||||
{
|
||||
ApplicationTheme.Dark => sysTheme is SystemTheme.Dark or SystemTheme.CapturedMotion or SystemTheme.Glow,
|
||||
ApplicationTheme.Light => sysTheme is SystemTheme.Light or SystemTheme.Flow or SystemTheme.Sunrise,
|
||||
_ => appApplicationTheme == ApplicationTheme.HighContrast && SystemThemeManager.HighContrast
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value that indicates whether the application is currently using the high contrast theme.
|
||||
/// </summary>
|
||||
/// <returns><see langword="true"/> if application uses high contrast theme.</returns>
|
||||
public static bool IsHighContrast() => _cachedApplicationTheme == ApplicationTheme.HighContrast;
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the application and the operating system are currently working in a dark theme.
|
||||
/// </summary>
|
||||
public static bool IsMatchedDark()
|
||||
{
|
||||
ApplicationTheme appApplicationTheme = GetAppTheme();
|
||||
SystemTheme sysTheme = GetSystemTheme();
|
||||
|
||||
if (appApplicationTheme != ApplicationTheme.Dark)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return sysTheme is SystemTheme.Dark or SystemTheme.CapturedMotion or SystemTheme.Glow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the application and the operating system are currently working in a light theme.
|
||||
/// </summary>
|
||||
public static bool IsMatchedLight()
|
||||
{
|
||||
ApplicationTheme appApplicationTheme = GetAppTheme();
|
||||
SystemTheme sysTheme = GetSystemTheme();
|
||||
|
||||
if (appApplicationTheme != ApplicationTheme.Light)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return sysTheme is SystemTheme.Light or SystemTheme.Flow or SystemTheme.Sunrise;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value that indicates whether the Windows is currently using the high contrast theme.
|
||||
/// </summary>
|
||||
/// <returns><see langword="true"/> if system uses high contrast theme.</returns>
|
||||
public static bool IsSystemHighContrast() => SystemThemeManager.HighContrast;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user