Files
Windows-Gotify-Client/App.xaml.cs
T
KennethCheng 021a45465d feat: 应用图标 + 浅色/深色主题 + 消息复制 + 详情可选中复制
实现 4 个用户需求:

1. 应用图标(Gotify Ducker 吉祥物)
   - Assets/gotify-mascot.png:用户提供的 PNG(196x196)
   - Assets/Build-Icon.ps1:用 System.Drawing 高质量缩放生成多分辨率 ICO(16/24/32/48/64/128/196),PNG 数据嵌入 ICO 容器(Win10+ 原生支持)
   - Assets/gotify-mascot.ico:7 个分辨率的 PNG-in-ICO,41KB
   - WindowsGotify.csproj 加 <ApplicationIcon> 让 EXE 资源带这个图标
   - csproj 加 <None Update=Assets\gotify-mascot.ico><CopyToOutputDirectory> 发布时一并拷
   - TrayIconService 加 LoadMascotIcon() 静态方法,优先从 EXE 同目录 Assets\ 下找,找不到回退到 SystemIcons.Application
   - Windows 通知中心的 toast 会自动用 ApplicationIcon(无需单独设)

2. 浅色/深色主题
   - AppConfig 新增 UiConfig { Theme: "Light"|"Dark"|"System" }
   - Themes/Light.xaml + Themes/Dark.xaml:两个 ResourceDictionary,定义 WindowBackground/CardBackground/CardAltBackground/BorderColor/PrimaryText/SecondaryText/MutedText/AccentBrush/SuccessDot/ErrorDot/PendingDot/PriorityUrgent/High/Normal/Low 等
   - Services/ThemeService.cs:静态类,Apply(name) 卸载旧主题字典 + 加载新主题字典到 Application.Resources.MergedDictionaries,Changed 事件,Toggle() 切换
   - MainWindow.xaml 所有颜色全部改用 {DynamicResource Xxx},主题切换时立刻刷新
   - PriorityToBrushConverter 改用 Application.Current.TryFindResource("PriorityXxx") 拿色,跟着主题走
   - 主窗右上角加 ☼/☾ 切换按钮,点一下切主题并自动写回 config.json

3. 消息正文复制按钮
   - 详情面板"消息正文"标签右侧加 [复制] 按钮
   - CopyMessageButton_Click:用 System.Windows.Clipboard.SetText 把全文(应用名/标题/时间/优先级/正文/URL)写进剪贴板
   - 按钮反馈:复制后短暂显示"已复制"1.5 秒再恢复
   - BuildFullMessageText 静态方法统一格式化

4. 详情面板可选中复制
   - DetailMessage 从 TextBlock 改成 TextBox(IsReadOnly=True, BorderThickness=0, Background=Transparent, IsReadOnlyCaretVisible=True)
   - DetailTitle / DetailUrl 也改成 TextBox IsReadOnly,用户能 Ctrl+A 全选、鼠标拉黑
   - DetailUrl 双击打开(之前是单击,TextBlock 没法选中;TextBox 单击会进入文字选择模式影响双击识别,所以改成双击)

WPF+WinForms 双开:Application/Brush/Color/Clipboard/Button 多个类型两个命名空间都有,ThemeService/ValueConverters/MainWindow.xaml.cs 都加 using alias 固定走 WPF 的;TrayIconService 加 using System.IO 补 Path/File。

实测:新进程跑起来后 ApplicationCache refreshed count=6(用户新建了一个 app),WebSocket connected,真实消息流处理正常(短信号码 19999999999/15889931832),通知弹出。
2026-09-06 23:02:03 +08:00

159 lines
5.0 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();
// 加载主题(必须在 StartAsync 之前,这样主窗第一次 Show 时资源已经在)
Services.ThemeService.Apply(config.Ui.Theme);
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);
}
}