using System.Reflection;
using System.Windows.Media.Animation;
using NeumUI.Extensions;
namespace NeumUI.Appearance;
///
/// 允许通过交换包含动态资源的资源字典来管理应用程序主题。
///
///
/// ThemeManager.SwitchThemeMode();
///
public class ThemeManager
{
///
/// 获取当前程序集的命名空间。
/// 该属性返回一个字符串,表示执行此代码的程序集的名称。此命名空间用于构建资源字典路径或其它需要引用程序集名称的地方。
///
public static string LibraryNamespace => Assembly.GetExecutingAssembly().GetName().Name;
///
/// 获取主题资源字典的路径。
/// 该属性返回一个字符串,表示应用程序中主题资源字典文件的位置。此路径用于加载不同的主题样式文件(如Light.xaml, Dark.xaml等)。
///
public static string ThemesDictionaryPath => $"pack://application:,,,/{LibraryNamespace};component/Themes/";
private static ThemeMode _activeThemeMode = ThemeMode.Light;
private static ThemePalette _activeThemePalette = ThemePalette.DaybreakBlue;
///
///
///
public static event Action? ThemeModeChanged;
///
///
///
public static event Action? ThemePaletteChanged;
#region Animation
private static Task AnimateBrushColorAsync(SolidColorBrush brush, Color toColor, int durationMs = 500)
{
if (brush.IsFrozen)
return Task.CompletedTask; // 冻结的Brush无法动画,直接返回完成的Task
var tcs = new TaskCompletionSource();
var anim = new ColorAnimation
{
To = toColor,
Duration = TimeSpan.FromMilliseconds(durationMs),
EasingFunction = new CubicEase { EasingMode = EasingMode.EaseInOut },
FillBehavior = FillBehavior.HoldEnd
};
// 在动画完成时标记任务完成
anim.Completed += (_, _) => tcs.SetResult(true);
brush.BeginAnimation(SolidColorBrush.ColorProperty, anim);
return tcs.Task;
}
#endregion
///
/// 获取当前设置的应用程序主题。
///
public static ThemeMode GetAppThemeMode()
{
ResourceDictionaryManager appDictionaries = new();
var themeDictionary = appDictionaries.LookupDictionary("themes");
if (themeDictionary == null)
{
return ThemeMode.Light;
}
var themeUri = themeDictionary.Source.ToString();
if (themeUri.Contains("light", StringComparison.OrdinalIgnoreCase))
{
_activeThemeMode = ThemeMode.Light;
}
if (themeUri.Contains("dark", StringComparison.OrdinalIgnoreCase))
{
_activeThemeMode = ThemeMode.Dark;
}
return _activeThemeMode;
}
///
/// 将资源应用于 .比如窗口
///
public static void ApplyResourcesToElement(FrameworkElement frameworkElement)
{
if (frameworkElement.Resources.MergedDictionaries.Count <
Current.Resources.MergedDictionaries.Count)
{
foreach (var dictionary in ThemeManager.Current.Resources.MergedDictionaries)
{
frameworkElement.Resources.MergedDictionaries.Add(dictionary);
}
}
}
///
/// 应用指定的主题调色板到应用程序。
///
/// 要应用的主题调色板。
public static void ApplyThemePalette(ThemePalette themePalette)
{
if (themePalette == ThemePalette.DaybreakBlue)
{
return;
}
_activeThemePalette = themePalette;
ThemePaletteChanged?.Invoke(themePalette);
}
///
/// 设置应用程序的主题模式。
///
/// 要设置的主题模式,可以是浅色或深色。
/// 如果提供的主题模式无效,则抛出此异常。
public static void SetThemeMode(ThemeMode themeMode)
{
GetAppThemeMode();
if (themeMode == _activeThemeMode)
{
return;
}
_activeThemeMode = themeMode;
var themeDictionaryName = themeMode switch
{
ThemeMode.Light => "Light",
ThemeMode.Dark => "Dark",
_ => throw new ArgumentOutOfRangeException(nameof(themeMode), themeMode, null)
};
ResourceDictionaryManager appDictionaries = new();
if (!appDictionaries.UpdateDictionary(
"themes",
new Uri($"{ThemesDictionaryPath}{themeDictionaryName}.xaml", UriKind.Absolute)))
{
return;
}
ThemeModeChanged?.Invoke(themeMode);
}
///
/// 更改当前应用程序明暗主题。
///
public static void SwitchThemeMode()
{
GetAppThemeMode();
var toThemeMode = _activeThemeMode == ThemeMode.Light ? ThemeMode.Dark : ThemeMode.Light;
var themeDictionaryName = toThemeMode switch
{
ThemeMode.Light => "Light",
ThemeMode.Dark => "Dark",
_ => throw new NotImplementedException(),
};
#region 动画过渡
// // 1. 加载目标主题的资源字典,但先不合并
// ResourceDictionary newTheme = new ResourceDictionary
// {
// Source = new Uri($"{ThemesDictionaryPath}{themeDictionaryName}.xaml", UriKind.Absolute)
// };
// // 2. 遍历新主题中的画刷资源
// foreach (var key in newTheme.Keys)
// {
// // 只处理 SolidColorBrush
// if (newTheme[key] is SolidColorBrush newBrush && Current.Resources[key] is SolidColorBrush currentBrush)
// {
//
// // var fromColor = animatableBrush.Color;
// var toColor = new SolidColorBrush(newBrush.Color).Color;
//
// // 3. 在当前的应用程序资源中找到同名的画刷
// // 4. 创建颜色动画
// var colorAnimation = new ColorAnimation
// {
// // From = fromColor,
// To = toColor,
// Duration = new Duration(TimeSpan.FromMilliseconds(1000)), // 动画时长
// EasingFunction = new QuarticEase { EasingMode = EasingMode.EaseInOut } // 缓动函数,使动画更自然
// };
//
// // 5. 对当前画刷的 Color 属性应用动画
// currentBrush.BeginAnimation(SolidColorBrush.ColorProperty, colorAnimation);
// }
// }
#endregion
ResourceDictionaryManager appDictionaries = new();
if (!appDictionaries.UpdateDictionary(
"themes",
new Uri($"{ThemesDictionaryPath}{themeDictionaryName}.xaml", UriKind.Absolute)))
{
return;
}
_activeThemeMode = toThemeMode;
ThemeModeChanged?.Invoke(toThemeMode);
}
#region UiApplication
private static ThemeManager? _themeManagerInstance;
private readonly Application? application;
private Window? mainWindow;
private ResourceDictionary? resources;
///
/// 初始化 实例.
///
private ThemeManager(Application? application)
{
if (application is null)
{
return;
}
if (!ApplicationHasResources(application))
{
return;
}
this.application = application;
System.Diagnostics.Debug
.WriteLine($"INFO | {typeof(ThemeManager)} application is {this.application}", ThemeManager.LibraryNamespace);
}
private static bool ApplicationHasResources(Application application)
{
return application
.Resources.MergedDictionaries
.Where(e => e.Source is not null)
.Any(e => e.Source
.ToString()
.Contains(ThemeManager.LibraryNamespace, StringComparison.OrdinalIgnoreCase));
}
///
/// 将应用程序的 变为 shutdown 模式。
///
public void Shutdown()
{
application?.Shutdown();
}
///
/// 获取或设置应用程序的资源。
///
public object TryFindResource(object resourceKey)
{
return Resources[resourceKey];
}
///
/// 获取当前UI应用程序。
///
public static ThemeManager Current
{
get
{
_themeManagerInstance ??= new ThemeManager(Application.Current);
return _themeManagerInstance;
}
}
///
/// 获取或设置应用程序的主窗口。
///
public Window? MainWindow
{
get => application?.MainWindow ?? mainWindow;
set
{
if (application != null)
{
application.MainWindow = value;
}
mainWindow = value;
}
}
///
/// 获取或设置应用程序的资源。
///
public ResourceDictionary Resources
{
get
{
if (resources is null)
{
resources = [];
try
{
var themesDictionary = new ThemesDictionary();
var controlsDictionary = new ControlsDictionary();
resources.MergedDictionaries.Add(themesDictionary);
resources.MergedDictionaries.Add(controlsDictionary);
}
catch
{
// ignored
}
}
return application?.Resources ?? resources;
}
set
{
if (application is not null)
{
application.Resources = value;
}
resources = value;
}
}
#endregion
}