2025-08-20 12:10:35 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
2026-01-02 17:30:41 +08:00
|
|
|
|
namespace Melskin.Appearance
|
2025-08-20 12:10:35 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 主题偏好存储:简单文本文件:第一行 ThemeMode,第二行 ThemePalette
|
|
|
|
|
|
/// 文件位置:%LOCALAPPDATA%\{LibraryNamespace}\theme.pref
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal static class ThemePreferenceStore
|
|
|
|
|
|
{
|
|
|
|
|
|
private static readonly string Folder =
|
|
|
|
|
|
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
|
|
|
|
ThemeManager.LibraryNamespace ?? "AppTheme");
|
|
|
|
|
|
|
|
|
|
|
|
private static readonly string FilePath = Path.Combine(Folder, "theme.pref");
|
|
|
|
|
|
|
|
|
|
|
|
public static void Save(ThemeMode themeMode, ThemePalette palette)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Directory.Exists(Folder))
|
|
|
|
|
|
Directory.CreateDirectory(Folder);
|
|
|
|
|
|
|
|
|
|
|
|
File.WriteAllLines(FilePath, new[] { themeMode.ToString(), palette.ToString() });
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
// 忽略写入异常
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 读取偏好;成功返回 true,否则 false。不存在或解析失败不抛异常。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static bool TryLoad(out ThemeMode? mode, out ThemePalette? palette)
|
|
|
|
|
|
{
|
|
|
|
|
|
mode = null;
|
|
|
|
|
|
palette = null;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!File.Exists(FilePath))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
var lines = File.ReadAllLines(FilePath);
|
|
|
|
|
|
if (lines.Length > 0 && Enum.TryParse(lines[0], true, out ThemeMode m))
|
|
|
|
|
|
mode = m;
|
|
|
|
|
|
if (lines.Length > 1 && Enum.TryParse(lines[1], true, out ThemePalette p))
|
|
|
|
|
|
palette = p;
|
|
|
|
|
|
|
|
|
|
|
|
return mode.HasValue || palette.HasValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|