5ce2c8a982
为 Claude Code 提供原生 Windows toast 通知:点击跳回原窗口、切回 Windows Terminal 标签、跨虚拟桌面、调用方图标、非阻塞投递;NativeAOT 单文件分发。
64 lines
1.6 KiB
C#
64 lines
1.6 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using Notify.Models;
|
|
using Notify.Serialization;
|
|
|
|
namespace Notify.Services;
|
|
|
|
/// <summary>
|
|
/// 加载/保存弹窗设置,并在变更时通知订阅者
|
|
/// </summary>
|
|
public sealed class SettingsService
|
|
{
|
|
private static readonly string Dir =
|
|
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ClaudeCodeNotify");
|
|
|
|
private static readonly string FilePath = Path.Combine(Dir, "settings.json");
|
|
|
|
public ToastSettings Current { get; private set; } = new();
|
|
|
|
/// <summary>
|
|
/// 设置被保存后触发
|
|
/// </summary>
|
|
public event Action<ToastSettings>? Changed;
|
|
|
|
public void Load()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(FilePath))
|
|
{
|
|
var json = File.ReadAllText(FilePath);
|
|
var loaded = JsonSerializer.Deserialize(json, AppJsonContext.Default.ToastSettings);
|
|
if (loaded is not null)
|
|
{
|
|
Current = loaded;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// 配置损坏则回退到默认值,不影响保活
|
|
Current = new ToastSettings();
|
|
}
|
|
}
|
|
|
|
public void Save(ToastSettings settings)
|
|
{
|
|
Current = settings;
|
|
try
|
|
{
|
|
Directory.CreateDirectory(Dir);
|
|
var json = JsonSerializer.Serialize(settings, AppJsonContext.Default.ToastSettings);
|
|
File.WriteAllText(FilePath, json);
|
|
}
|
|
catch
|
|
{
|
|
// 落盘失败不致命,内存里仍生效
|
|
}
|
|
|
|
Changed?.Invoke(Current);
|
|
}
|
|
}
|