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:
@@ -0,0 +1,78 @@
|
||||
using System.Globalization;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Media;
|
||||
|
||||
// 双开 WPF + WinForms 时,Color 在两个命名空间都有,固定走 WPF 的。
|
||||
using Color = System.Windows.Media.Color;
|
||||
|
||||
namespace WindowsGotify;
|
||||
|
||||
public sealed class PriorityToBrushConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (parameter is string p && p == "hideIfEmpty" && value is string s)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(s) || s == "(无标题)" ? Visibility.Collapsed : Visibility.Visible;
|
||||
}
|
||||
|
||||
if (value is int p1)
|
||||
{
|
||||
return p1 switch
|
||||
{
|
||||
>= 8 => new SolidColorBrush(Color.FromRgb(0xD1, 0x34, 0x38)), // 紧急 - 红
|
||||
>= 5 => new SolidColorBrush(Color.FromRgb(0xCA, 0x50, 0x10)), // 重要 - 橙
|
||||
>= 1 => new SolidColorBrush(Color.FromRgb(0x4A, 0x55, 0x68)), // 一般 - 灰蓝
|
||||
_ => new SolidColorBrush(Color.FromRgb(0x88, 0x88, 0x88)) // 静默 - 浅灰
|
||||
};
|
||||
}
|
||||
return new SolidColorBrush(Colors.Gray);
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object? parameter, CultureInfo culture)
|
||||
=> throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 应用名 → 稳定颜色(用名字的 hash 决定 hue),让每个应用在历史里视觉上可区分。
|
||||
/// </summary>
|
||||
public sealed class AppNameToBrushConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
var name = value as string ?? "";
|
||||
if (string.IsNullOrWhiteSpace(name)) return new SolidColorBrush(Color.FromRgb(0x55, 0x55, 0x55));
|
||||
|
||||
// 简单 FNV-1a 32-bit hash → hue 0..360
|
||||
uint hash = 2166136261u;
|
||||
foreach (var c in name)
|
||||
{
|
||||
hash ^= c;
|
||||
hash *= 16777619u;
|
||||
}
|
||||
var hue = hash % 360;
|
||||
return new SolidColorBrush(HsvToColor(hue, 0.55, 0.75));
|
||||
}
|
||||
|
||||
private static Color HsvToColor(double hue, double sat, double val)
|
||||
{
|
||||
var c = val * sat;
|
||||
var x = c * (1 - Math.Abs((hue / 60) % 2 - 1));
|
||||
var m = val - c;
|
||||
double r, g, b;
|
||||
if (hue < 60) { r = c; g = x; b = 0; }
|
||||
else if (hue < 120) { r = x; g = c; b = 0; }
|
||||
else if (hue < 180) { r = 0; g = c; b = x; }
|
||||
else if (hue < 240) { r = 0; g = x; b = c; }
|
||||
else if (hue < 300) { r = x; g = 0; b = c; }
|
||||
else { r = c; g = 0; b = x; }
|
||||
return Color.FromRgb(
|
||||
(byte)Math.Round((r + m) * 255),
|
||||
(byte)Math.Round((g + m) * 255),
|
||||
(byte)Math.Round((b + m) * 255));
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object? parameter, CultureInfo culture)
|
||||
=> throw new NotSupportedException();
|
||||
}
|
||||
Reference in New Issue
Block a user