feat: 历史界面美化 + 按应用分类
实现两个用户需求:消息内容完整显示、按 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 条股票分析报告,通知正常弹。
This commit is contained in:
+153
-31
@@ -2,12 +2,17 @@ using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using WindowsGotify.Models;
|
||||
using WindowsGotify.Services;
|
||||
using WindowsGotify.Tray;
|
||||
|
||||
// 双开 WPF + WinForms 时,MessageBox 在两个命名空间都有,用别名强制走 WPF 的。
|
||||
// 双开 WPF + WinForms 时,这几个类型两个命名空间都有,固定走 WPF 的。
|
||||
using MessageBox = System.Windows.MessageBox;
|
||||
using Color = System.Windows.Media.Color;
|
||||
using Brush = System.Windows.Media.Brush;
|
||||
using Brushes = System.Windows.Media.Brushes;
|
||||
|
||||
namespace WindowsGotify;
|
||||
|
||||
@@ -21,6 +26,7 @@ public partial class MainWindow : Window
|
||||
private TrayIconService? _tray;
|
||||
private GotifyWebSocketService? _webSocket;
|
||||
private GotifyApiService? _apiService;
|
||||
private ApplicationCache? _appCache;
|
||||
|
||||
private readonly ObservableCollection<GotifyMessage> _history = new();
|
||||
|
||||
@@ -42,7 +48,8 @@ public partial class MainWindow : Window
|
||||
Logger logger,
|
||||
TrayIconService tray,
|
||||
GotifyWebSocketService webSocket,
|
||||
GotifyApiService apiService)
|
||||
GotifyApiService apiService,
|
||||
ApplicationCache appCache)
|
||||
{
|
||||
_config = config;
|
||||
_configService = configService;
|
||||
@@ -50,14 +57,29 @@ public partial class MainWindow : Window
|
||||
_tray = tray;
|
||||
_webSocket = webSocket;
|
||||
_apiService = apiService;
|
||||
_appCache = appCache;
|
||||
|
||||
UrlTextBox.Text = config.Gotify.Url;
|
||||
TokenBox.Password = config.Gotify.ClientToken;
|
||||
StatusText.Text = "状态:等待连接";
|
||||
UpdateAppCount();
|
||||
SetStatus("等待连接", StatusKind.Pending);
|
||||
UpdateHistoryCount();
|
||||
|
||||
// 实时消息追加到历史
|
||||
_webSocket.MessageReceived += OnLiveMessage;
|
||||
|
||||
// 应用缓存更新时,刷新已加载消息的 AppName
|
||||
if (_appCache != null)
|
||||
{
|
||||
_appCache.Updated += (_, _) => Dispatcher.Invoke(() =>
|
||||
{
|
||||
UpdateAppCount();
|
||||
// 已存在的消息重新解析 AppName(缓存里可能新增了之前缺失的应用)
|
||||
foreach (var m in _history) m.AppName = _appCache.GetName(m.AppId);
|
||||
// 详情面板如果当前选中的,刷新
|
||||
if (HistoryList.SelectedItem is GotifyMessage sel) RenderDetail(sel);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowFromTray()
|
||||
@@ -65,13 +87,14 @@ public partial class MainWindow : Window
|
||||
Show();
|
||||
WindowState = WindowState.Normal;
|
||||
Activate();
|
||||
// 每次打开都拉一次最新历史,确保和远端同步
|
||||
// 每次打开都拉一次最新历史和应用,确保和远端同步
|
||||
_ = LoadHistoryAsync();
|
||||
_ = RefreshAppsAsync(silent: true);
|
||||
}
|
||||
|
||||
private void OnLiveMessage(GotifyMessage msg)
|
||||
{
|
||||
// WebSocket 回调可能在非 UI 线程,切回 UI 线程
|
||||
Enrich(msg);
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
_history.Insert(0, msg);
|
||||
@@ -80,6 +103,18 @@ public partial class MainWindow : Window
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 拿到一条消息后,补 AppName(从缓存查),返回原对象。
|
||||
/// </summary>
|
||||
private GotifyMessage Enrich(GotifyMessage msg)
|
||||
{
|
||||
if (_appCache != null && string.IsNullOrEmpty(msg.AppName))
|
||||
{
|
||||
msg.AppName = _appCache.GetName(msg.AppId);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
private void TrimHistory()
|
||||
{
|
||||
while (_history.Count > MaxHistoryInUi)
|
||||
@@ -90,7 +125,27 @@ public partial class MainWindow : Window
|
||||
|
||||
private void UpdateHistoryCount()
|
||||
{
|
||||
HistoryCountText.Text = $"共 {_history.Count} 条(本地缓存上限 {MaxHistoryInUi})";
|
||||
HistoryCountText.Text = $"共 {_history.Count} 条";
|
||||
}
|
||||
|
||||
private void UpdateAppCount()
|
||||
{
|
||||
if (_appCache == null) { AppCountText.Text = " · 应用:?"; return; }
|
||||
var n = _appCache.Count;
|
||||
AppCountText.Text = n == 0 ? " · 应用:0(未拉取)" : $" · 应用:{n}";
|
||||
}
|
||||
|
||||
private enum StatusKind { Pending, Connected, Error, Info }
|
||||
private void SetStatus(string text, StatusKind kind)
|
||||
{
|
||||
StatusText.Text = $"状态:{text}";
|
||||
StatusDot.Fill = kind switch
|
||||
{
|
||||
StatusKind.Connected => new SolidColorBrush(Color.FromRgb(0x10, 0x81, 0x16)),
|
||||
StatusKind.Error => new SolidColorBrush(Color.FromRgb(0xD1, 0x34, 0x38)),
|
||||
StatusKind.Pending => new SolidColorBrush(Color.FromRgb(0xCA, 0x50, 0x10)),
|
||||
_ => new SolidColorBrush(Color.FromRgb(0x88, 0x88, 0x88))
|
||||
};
|
||||
}
|
||||
|
||||
private async Task LoadHistoryAsync()
|
||||
@@ -98,58 +153,122 @@ public partial class MainWindow : Window
|
||||
if (_apiService is null || _config is null) return;
|
||||
if (string.IsNullOrWhiteSpace(_config.Gotify.ClientToken))
|
||||
{
|
||||
StatusText.Text = "状态:未配置 Client Token,无法拉取历史";
|
||||
SetStatus("未配置 Client Token,无法拉取历史", StatusKind.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
StatusText.Text = "状态:正在拉取历史...";
|
||||
SetStatus("正在拉取历史...", StatusKind.Pending);
|
||||
var limit = Math.Clamp(_config.History.ViewerLimit, 1, 100);
|
||||
var messages = await _apiService.GetRecentMessagesAsync(limit, CancellationToken.None);
|
||||
foreach (var m in messages) Enrich(m);
|
||||
|
||||
// 用清空 + 批量 Insert 让 UI 一次性刷新,避免多次 CollectionChanged
|
||||
_history.Clear();
|
||||
foreach (var m in messages)
|
||||
{
|
||||
_history.Add(m);
|
||||
}
|
||||
foreach (var m in messages) _history.Add(m);
|
||||
UpdateHistoryCount();
|
||||
StatusText.Text = $"状态:已加载 {messages.Count} 条历史消息";
|
||||
SetStatus($"已加载 {messages.Count} 条历史消息", StatusKind.Connected);
|
||||
_logger?.Info($"History loaded. count={messages.Count}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.Error("Load history failed.", ex);
|
||||
StatusText.Text = $"状态:拉取历史失败 - {ex.Message}";
|
||||
SetStatus($"拉取历史失败 - {ex.Message}", StatusKind.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RefreshAppsAsync(bool silent = false)
|
||||
{
|
||||
if (_appCache is null) return;
|
||||
if (_config is null || string.IsNullOrWhiteSpace(_config.Gotify.ClientToken))
|
||||
{
|
||||
if (!silent) SetStatus("未配置 Client Token,无法拉取应用", StatusKind.Error);
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
if (!silent) SetStatus("正在拉取应用列表...", StatusKind.Pending);
|
||||
await _appCache.RefreshAsync();
|
||||
UpdateAppCount();
|
||||
if (!silent) SetStatus($"已加载 {_appCache.Count} 个应用", StatusKind.Connected);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.Error("Refresh applications failed.", ex);
|
||||
if (!silent) SetStatus($"拉取应用失败 - {ex.Message}", StatusKind.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async void RefreshAppsButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
await RefreshAppsAsync();
|
||||
}
|
||||
|
||||
private async void RefreshButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
await LoadHistoryAsync();
|
||||
}
|
||||
|
||||
private void HistoryList_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
|
||||
private void HistoryList_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (HistoryList.SelectedItem is not GotifyMessage msg) return;
|
||||
var url = msg.ClickUrl;
|
||||
if (string.IsNullOrWhiteSpace(url)) return;
|
||||
|
||||
try
|
||||
if (HistoryList.SelectedItem is GotifyMessage msg)
|
||||
{
|
||||
Process.Start(new ProcessStartInfo
|
||||
{
|
||||
FileName = url,
|
||||
UseShellExecute = true
|
||||
});
|
||||
_logger?.Info($"Opened click url. id={msg.Id}, url={url}");
|
||||
RenderDetail(msg);
|
||||
}
|
||||
catch (Exception ex)
|
||||
else
|
||||
{
|
||||
_logger?.Error("Failed to open click url.", ex);
|
||||
MessageBox.Show($"无法打开链接:{ex.Message}", "Windows Gotify",
|
||||
MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
DetailPanel.Visibility = Visibility.Collapsed;
|
||||
DetailEmptyHint.Visibility = Visibility.Visible;
|
||||
}
|
||||
}
|
||||
|
||||
private void RenderDetail(GotifyMessage msg)
|
||||
{
|
||||
DetailEmptyHint.Visibility = Visibility.Collapsed;
|
||||
DetailPanel.Visibility = Visibility.Visible;
|
||||
|
||||
DetailAppName.Text = string.IsNullOrWhiteSpace(msg.AppName) ? msg.TitleDisplay : msg.AppName;
|
||||
DetailAppName.Foreground = (Brush)(new AppNameToBrushConverter().Convert(msg.AppName, typeof(Brush), null, System.Globalization.CultureInfo.CurrentCulture) ?? Brushes.Black);
|
||||
|
||||
var app = _appCache?.Get(msg.AppId);
|
||||
DetailAppDesc.Text = app?.Description ?? "(无应用描述)";
|
||||
DetailAppDesc.Visibility = string.IsNullOrWhiteSpace(app?.Description) ? Visibility.Collapsed : Visibility.Visible;
|
||||
|
||||
DetailPriority.Text = $"{msg.PriorityDisplay} ({(msg.Priority >= 8 ? "紧急-告警音" : msg.Priority >= 5 ? "重要" : "一般")})";
|
||||
DetailDate.Text = msg.Date.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");
|
||||
DetailTitle.Text = msg.TitleDisplay;
|
||||
DetailAppId.Text = msg.AppId.ToString();
|
||||
DetailMessage.Text = msg.Message ?? "";
|
||||
|
||||
var url = msg.ClickUrl;
|
||||
if (!string.IsNullOrWhiteSpace(url))
|
||||
{
|
||||
DetailUrl.Text = url;
|
||||
DetailUrl.Tag = url;
|
||||
DetailUrlLabel.Visibility = Visibility.Visible;
|
||||
DetailUrl.Visibility = Visibility.Visible;
|
||||
}
|
||||
else
|
||||
{
|
||||
DetailUrlLabel.Visibility = Visibility.Collapsed;
|
||||
DetailUrl.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
}
|
||||
|
||||
private void DetailUrl_Click(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (DetailUrl.Tag is string url && !string.IsNullOrWhiteSpace(url))
|
||||
{
|
||||
try
|
||||
{
|
||||
Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.Error("Failed to open click url.", ex);
|
||||
MessageBox.Show($"无法打开链接:{ex.Message}", "Windows Gotify",
|
||||
MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,6 +299,9 @@ public partial class MainWindow : Window
|
||||
|
||||
await _configService.SaveAsync(_config);
|
||||
_webSocket?.RequestReconnect();
|
||||
// URL/Token 变了,重新拉应用和历史
|
||||
_ = RefreshAppsAsync(silent: true);
|
||||
await LoadHistoryAsync();
|
||||
|
||||
MessageBox.Show("配置已保存,正在重新连接。", "Windows Gotify",
|
||||
MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
|
||||
Reference in New Issue
Block a user