实现两个用户需求:消息内容完整显示、按 Gotify Application 分类。 新增: - Models/GotifyApplication.cs:对应 GET /application 返回字段(id/name/description/defaultPriority/createdAt/internal) - Services/ApplicationCache.cs:在内存里缓存 id→Application 映射,提供 GetName/Get 解析,Updated 事件触发 UI 刷新 - Services/GotifyApiService.GetApplicationsAsync:GET /application?limit=999 拉取应用列表 - Converters/ValueConverters.cs: - PriorityToBrushConverter:优先级 0/1-4/5-7/8+ 分别用浅灰/灰蓝/橙/红区分 - AppNameToBrushConverter:对应用名做 FNV-1a 32-bit hash → HSL 色相,让每个应用在历史里视觉上可区分 - GotifyMessage.AppName 字段(JsonIgnore),由 UI 在加入历史前从缓存查后填入 主窗重构(两栏布局): - 左侧历史列表:用 DataTemplate 自定义每行(优先级徽章 + 应用名彩色 + 标题 + 消息预览 TextWrapping+Trimming),不再用 GridView 单行 - 右侧详情面板:选中消息后显示完整内容(应用名大标题、描述、优先级+说明、时间、标题、App ID、消息正文、URL 链接) - 顶部状态条:状态点颜色(绿/橙/红/灰)+ 应用数显示(实时更新) - 新增按钮「刷新应用」,手动 GET /application,用于用户新建 Gotify app 后同步 - URL 链接(如果有)在详情面板显示,点击用默认浏览器打开 - SaveButton 在保存配置后自动重新拉应用列表+历史,不用重启 App.xaml.cs 改造:创建 ApplicationCache,启动时后台异步刷新一次,订阅 Updated 事件让 MainWindow 重新解析已加载消息的 AppName(因为消息先到,缓存可能后到) Color/Brush/MessageBox 在 WPF+WinForms 双开项目里撞名,ValueConverters 和 MainWindow.xaml.cs 都加 using alias 固定走 WPF。 实测:启动后 ApplicationCache refreshed count=5,WebSocket connected,历史拉了 50 条股票分析报告,通知正常弹。
156 lines
4.8 KiB
C#
156 lines
4.8 KiB
C#
using System.Diagnostics;
|
|
using System.Windows;
|
|
using Microsoft.Windows.AppNotifications;
|
|
|
|
// 双开 WPF + WinForms 时,Application 在两个命名空间都有,用别名强制走 WPF 的。
|
|
using Application = System.Windows.Application;
|
|
|
|
namespace WindowsGotify;
|
|
|
|
public partial class App : Application
|
|
{
|
|
private static Mutex? _mutex;
|
|
private Services.ConfigService? _configService;
|
|
private Services.Logger? _logger;
|
|
private Services.StateService? _stateService;
|
|
private Services.GotifyApiService? _apiService;
|
|
private Services.GotifyWebSocketService? _webSocketService;
|
|
private Services.NotificationService? _notificationService;
|
|
private Services.ApplicationCache? _appCache;
|
|
private Tray.TrayIconService? _trayService;
|
|
private MainWindow? _mainWindow;
|
|
|
|
protected override async void OnStartup(StartupEventArgs e)
|
|
{
|
|
base.OnStartup(e);
|
|
|
|
_mutex = new Mutex(true, "WindowsGotifyAgent-SingleInstance", out bool createdNew);
|
|
if (!createdNew)
|
|
{
|
|
Shutdown();
|
|
return;
|
|
}
|
|
|
|
_logger = new Services.Logger();
|
|
_configService = new Services.ConfigService(_logger);
|
|
var config = await _configService.LoadAsync();
|
|
|
|
_stateService = new Services.StateService(_logger);
|
|
_apiService = new Services.GotifyApiService(config, _logger);
|
|
_notificationService = new Services.NotificationService(config, _logger);
|
|
_appCache = new Services.ApplicationCache(_apiService, _logger);
|
|
|
|
try
|
|
{
|
|
// NotificationInvoked 必须在 Register 之前注册。
|
|
AppNotificationManager.Default.NotificationInvoked += OnNotificationInvoked;
|
|
AppNotificationManager.Default.Register();
|
|
_logger.Info("Windows App SDK notification manager registered.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error("Notification registration failed.", ex);
|
|
}
|
|
|
|
_mainWindow = new MainWindow();
|
|
_mainWindow.Hide();
|
|
|
|
_trayService = new Tray.TrayIconService(
|
|
onShowSettings: () => _mainWindow.ShowFromTray(),
|
|
onTest: async () => await SendTestNotificationAsync(),
|
|
onReconnect: () => _webSocketService?.RequestReconnect(),
|
|
onExit: () => Shutdown()
|
|
);
|
|
|
|
_webSocketService = new Services.GotifyWebSocketService(
|
|
config,
|
|
_logger,
|
|
_stateService,
|
|
_apiService,
|
|
_notificationService,
|
|
status => _trayService.SetStatus(status));
|
|
|
|
_mainWindow.Initialize(
|
|
config,
|
|
_configService,
|
|
_logger,
|
|
_trayService,
|
|
_webSocketService,
|
|
_apiService,
|
|
_appCache);
|
|
|
|
if (config.Startup.Enabled)
|
|
{
|
|
Utils.StartupManager.EnableCurrentUserStartup(_logger);
|
|
}
|
|
|
|
// 后台异步拉一次应用列表,不阻塞启动
|
|
_ = _appCache.RefreshAsync();
|
|
|
|
await _webSocketService.StartAsync();
|
|
}
|
|
|
|
private async Task SendTestNotificationAsync()
|
|
{
|
|
if (_notificationService is null)
|
|
return;
|
|
|
|
await _notificationService.ShowAsync(
|
|
new Models.GotifyMessage
|
|
{
|
|
Id = 0,
|
|
AppId = 0,
|
|
Message = "Windows Gotify Agent 测试通知。",
|
|
Title = "Gotify 测试",
|
|
Priority = 5,
|
|
Date = DateTimeOffset.UtcNow
|
|
});
|
|
}
|
|
|
|
private void OnNotificationInvoked(
|
|
AppNotificationManager sender,
|
|
AppNotificationActivatedEventArgs args)
|
|
{
|
|
Dispatcher.Invoke(() =>
|
|
{
|
|
try
|
|
{
|
|
// Windows App SDK 2.4.0:args.Arguments 已经是 IDictionary<string, string>,由 AddArgument(key, value) 注入
|
|
if (args.Arguments.TryGetValue("url", out var url) && !string.IsNullOrWhiteSpace(url))
|
|
{
|
|
Process.Start(new ProcessStartInfo
|
|
{
|
|
FileName = url,
|
|
UseShellExecute = true
|
|
});
|
|
}
|
|
else
|
|
{
|
|
_mainWindow?.ShowFromTray();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger?.Error("Notification invocation failed.", ex);
|
|
}
|
|
});
|
|
}
|
|
|
|
protected override void OnExit(ExitEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
_webSocketService?.Stop();
|
|
_trayService?.Dispose();
|
|
AppNotificationManager.Default.Unregister();
|
|
AppNotificationManager.Default.NotificationInvoked -= OnNotificationInvoked;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger?.Error("Application cleanup failed.", ex);
|
|
}
|
|
|
|
base.OnExit(e);
|
|
}
|
|
}
|