58 lines
1.8 KiB
C#
58 lines
1.8 KiB
C#
|
|
using System.IO;
|
|||
|
|
|
|||
|
|
namespace NeoUI.Appearance
|
|||
|
|
{
|
|||
|
|
/// <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;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|