2025-08-24 13:49:55 +08:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
using System.Windows.Markup;
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-02 17:30:30 +08:00
|
|
|
|
namespace VariaStudio.Appearance;
|
2025-08-24 13:49:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 提供字典实现,该字典通过合并一个“模式”字典和一个依赖于模式的“调色板”字典来动态构建完整的主题。
|
|
|
|
|
|
/// 此版本已根据指定的文件夹结构进行调整。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Localizability(LocalizationCategory.Ignore)]
|
|
|
|
|
|
[Ambient]
|
|
|
|
|
|
[UsableDuringInitialization(true)]
|
|
|
|
|
|
public class ThemesDictionary : ResourceDictionary
|
|
|
|
|
|
{
|
2025-08-25 17:30:53 +08:00
|
|
|
|
private readonly ResourceDictionary modeDictionary = new ResourceDictionary();
|
|
|
|
|
|
private readonly ResourceDictionary paletteDictionary = new ResourceDictionary();
|
2025-08-24 13:49:55 +08:00
|
|
|
|
|
2025-08-25 17:30:53 +08:00
|
|
|
|
private ThemeMode? currentMode;
|
|
|
|
|
|
private ThemePalette? currentPalette;
|
2025-08-24 13:49:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构造函数,将内部字典添加到 MergedDictionaries 集合。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public ThemesDictionary()
|
|
|
|
|
|
{
|
2025-08-25 17:30:53 +08:00
|
|
|
|
MergedDictionaries.Add(modeDictionary);
|
|
|
|
|
|
MergedDictionaries.Add(paletteDictionary);
|
2025-08-24 13:49:55 +08:00
|
|
|
|
|
|
|
|
|
|
if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
|
|
|
|
|
|
{
|
|
|
|
|
|
UpdateSources();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据当前的 Mode 和 Palette 更新内部字典的 Source。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void UpdateSources()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 设计时模式:加载一个固定的、安全的主题组合以确保设计器稳定。
|
|
|
|
|
|
if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
|
|
|
|
|
|
{
|
|
|
|
|
|
// 加载 Mode 文件: .../Themes/Light.xaml
|
|
|
|
|
|
var defaultModeUri = new Uri($"{ThemeManager.ThemesDictionaryPath}Light.xaml", UriKind.RelativeOrAbsolute);
|
2025-08-25 17:30:53 +08:00
|
|
|
|
if (modeDictionary.Source != defaultModeUri)
|
|
|
|
|
|
modeDictionary.Source = defaultModeUri;
|
2025-08-24 13:49:55 +08:00
|
|
|
|
|
2025-12-23 21:35:54 +08:00
|
|
|
|
// 加载 Palette 文件: .../Themes/Accents/LightBlue.xaml
|
|
|
|
|
|
var defaultPaletteUri = new Uri($"{ThemeManager.ThemesDictionaryPath}Accents/LightBlue.xaml", UriKind.RelativeOrAbsolute);
|
2025-08-25 17:30:53 +08:00
|
|
|
|
if (paletteDictionary.Source != defaultPaletteUri)
|
|
|
|
|
|
paletteDictionary.Source = defaultPaletteUri;
|
2025-08-24 13:49:55 +08:00
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 运行时模式:根据当前已有的属性值进行更新。
|
|
|
|
|
|
|
|
|
|
|
|
// 更新 Mode 字典 (只要 Mode 有值就更新)
|
2025-08-25 17:30:53 +08:00
|
|
|
|
if (currentMode.HasValue)
|
2025-08-24 13:49:55 +08:00
|
|
|
|
{
|
2026-01-02 17:30:30 +08:00
|
|
|
|
// 路径示例: /VariaStudio;component/Appearance/Themes/Dark.xaml
|
2025-08-25 17:30:53 +08:00
|
|
|
|
var modeSourceUri = new Uri($"{ThemeManager.ThemesDictionaryPath}{currentMode.Value}.xaml", UriKind.RelativeOrAbsolute);
|
|
|
|
|
|
if (modeDictionary.Source != modeSourceUri)
|
2025-08-24 13:49:55 +08:00
|
|
|
|
{
|
2025-08-25 17:30:53 +08:00
|
|
|
|
modeDictionary.Source = modeSourceUri;
|
2025-08-24 13:49:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新 Palette 字典 (必须在 Mode 和 Palette 都有值的情况下才更新)
|
2025-08-25 17:30:53 +08:00
|
|
|
|
if (currentMode.HasValue && currentPalette.HasValue)
|
2025-08-24 13:49:55 +08:00
|
|
|
|
{
|
2025-08-25 17:30:53 +08:00
|
|
|
|
var paletteName = $"{currentMode.Value}{currentPalette.Value}";
|
2026-01-02 17:30:30 +08:00
|
|
|
|
// 路径示例: /VariaStudio;component/Appearance/Themes/Accents/DarkGreen.xaml
|
2025-12-23 21:35:54 +08:00
|
|
|
|
var paletteSourceUri = new Uri($"{ThemeManager.ThemesDictionaryPath}Accents/{paletteName}.xaml", UriKind.RelativeOrAbsolute);
|
2025-08-25 17:30:53 +08:00
|
|
|
|
if (paletteDictionary.Source != paletteSourceUri)
|
2025-08-24 13:49:55 +08:00
|
|
|
|
{
|
2025-08-25 17:30:53 +08:00
|
|
|
|
paletteDictionary.Source = paletteSourceUri;
|
2025-08-24 13:49:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置应用程序的主题模式(明或暗)。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public ThemeMode Mode
|
|
|
|
|
|
{
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
2025-08-25 17:30:53 +08:00
|
|
|
|
if (currentMode != value)
|
2025-08-24 13:49:55 +08:00
|
|
|
|
{
|
2025-08-25 17:30:53 +08:00
|
|
|
|
currentMode = value;
|
2025-08-24 13:49:55 +08:00
|
|
|
|
UpdateSources();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置应用程序的主题调色板(颜色)。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public ThemePalette Palette
|
|
|
|
|
|
{
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
2025-08-25 17:30:53 +08:00
|
|
|
|
if (currentPalette != value)
|
2025-08-24 13:49:55 +08:00
|
|
|
|
{
|
2025-08-25 17:30:53 +08:00
|
|
|
|
currentPalette = value;
|
2025-08-24 13:49:55 +08:00
|
|
|
|
UpdateSources();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|