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:
+7
-1
@@ -16,6 +16,7 @@ public partial class App : Application
|
||||
private Services.GotifyApiService? _apiService;
|
||||
private Services.GotifyWebSocketService? _webSocketService;
|
||||
private Services.NotificationService? _notificationService;
|
||||
private Services.ApplicationCache? _appCache;
|
||||
private Tray.TrayIconService? _trayService;
|
||||
private MainWindow? _mainWindow;
|
||||
|
||||
@@ -37,6 +38,7 @@ public partial class App : Application
|
||||
_stateService = new Services.StateService(_logger);
|
||||
_apiService = new Services.GotifyApiService(config, _logger);
|
||||
_notificationService = new Services.NotificationService(config, _logger);
|
||||
_appCache = new Services.ApplicationCache(_apiService, _logger);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -74,13 +76,17 @@ public partial class App : Application
|
||||
_logger,
|
||||
_trayService,
|
||||
_webSocketService,
|
||||
_apiService);
|
||||
_apiService,
|
||||
_appCache);
|
||||
|
||||
if (config.Startup.Enabled)
|
||||
{
|
||||
Utils.StartupManager.EnableCurrentUserStartup(_logger);
|
||||
}
|
||||
|
||||
// 后台异步拉一次应用列表,不阻塞启动
|
||||
_ = _appCache.RefreshAsync();
|
||||
|
||||
await _webSocketService.StartAsync();
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
+314
-101
@@ -1,14 +1,92 @@
|
||||
<Window x:Class="WindowsGotify.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:WindowsGotify"
|
||||
Title="Windows Gotify"
|
||||
Width="780"
|
||||
Height="620"
|
||||
MinWidth="640"
|
||||
MinHeight="480"
|
||||
WindowStartupLocation="CenterScreen">
|
||||
Width="1100"
|
||||
Height="720"
|
||||
MinWidth="900"
|
||||
MinHeight="560"
|
||||
WindowStartupLocation="CenterScreen"
|
||||
Background="#FAFAFA">
|
||||
|
||||
<Grid Margin="20">
|
||||
<Window.Resources>
|
||||
<!-- 优先级颜色 -->
|
||||
<local:PriorityToBrushConverter x:Key="PriorityToBrush"/>
|
||||
<local:AppNameToBrushConverter x:Key="AppNameToBrush"/>
|
||||
|
||||
<!-- 历史列表行模板 -->
|
||||
<DataTemplate x:Key="MessageRowTemplate">
|
||||
<Grid Margin="0,4" Height="Auto">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="44"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- 左侧优先级徽章 -->
|
||||
<Border Grid.RowSpan="2"
|
||||
Grid.Column="0"
|
||||
Background="{Binding Priority, Converter={StaticResource PriorityToBrush}}"
|
||||
CornerRadius="4"
|
||||
Margin="0,2,8,2"
|
||||
Padding="4,2"
|
||||
VerticalAlignment="Top"
|
||||
HorizontalAlignment="Stretch">
|
||||
<TextBlock Text="{Binding PriorityDisplay}"
|
||||
Foreground="White"
|
||||
FontWeight="Bold"
|
||||
FontSize="11"
|
||||
HorizontalAlignment="Center" />
|
||||
</Border>
|
||||
|
||||
<!-- 第一行:应用名 + 时间 -->
|
||||
<DockPanel Grid.Row="0" Grid.Column="1">
|
||||
<TextBlock DockPanel.Dock="Right"
|
||||
Text="{Binding DateDisplay}"
|
||||
Foreground="#888"
|
||||
FontSize="11"
|
||||
VerticalAlignment="Center"
|
||||
Margin="8,0,0,0" />
|
||||
<TextBlock Text="{Binding AppName}"
|
||||
FontWeight="Bold"
|
||||
FontSize="13"
|
||||
VerticalAlignment="Center"
|
||||
Foreground="{Binding AppName, Converter={StaticResource AppNameToBrush}}" />
|
||||
</DockPanel>
|
||||
|
||||
<!-- 第二行:标题(无则隐藏)+ 消息预览 -->
|
||||
<StackPanel Grid.Row="1" Grid.Column="1" Margin="0,2,0,0">
|
||||
<TextBlock Text="{Binding TitleDisplay}"
|
||||
FontWeight="SemiBold"
|
||||
TextTrimming="CharacterEllipsis"
|
||||
Visibility="{Binding TitleDisplay, Converter={StaticResource PriorityToBrush}, ConverterParameter=hideIfEmpty}">
|
||||
<TextBlock.Style>
|
||||
<Style TargetType="TextBlock">
|
||||
<Setter Property="Visibility" Value="Visible"/>
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding TitleDisplay}" Value="(无标题)">
|
||||
<Setter Property="Visibility" Value="Collapsed"/>
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</TextBlock.Style>
|
||||
</TextBlock>
|
||||
<TextBlock Text="{Binding Message}"
|
||||
TextWrapping="Wrap"
|
||||
MaxHeight="44"
|
||||
TextTrimming="CharacterEllipsis"
|
||||
Foreground="#444"
|
||||
FontSize="12" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</Window.Resources>
|
||||
|
||||
<Grid Margin="16">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
@@ -17,109 +95,244 @@
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- 标题 -->
|
||||
<StackPanel Grid.Row="0">
|
||||
<!-- 头部标题 -->
|
||||
<StackPanel Grid.Row="0" Margin="0,0,0,12">
|
||||
<TextBlock Text="Windows Gotify"
|
||||
FontSize="26"
|
||||
FontWeight="Bold" />
|
||||
<TextBlock Text="Gotify → Windows 原生通知 · 应用内查看历史"
|
||||
Margin="0,2,0,14"
|
||||
Foreground="Gray" />
|
||||
FontSize="24"
|
||||
FontWeight="Bold"
|
||||
Foreground="#222" />
|
||||
<TextBlock Text="Gotify → Windows 原生通知 · 应用内按应用分类查看历史"
|
||||
Margin="0,2,0,0"
|
||||
Foreground="#888"
|
||||
FontSize="12" />
|
||||
</StackPanel>
|
||||
|
||||
<!-- 设置区 -->
|
||||
<Grid Grid.Row="1">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<TextBlock Grid.Row="0" Grid.Column="0" Text="Gotify URL" />
|
||||
<TextBox x:Name="UrlTextBox"
|
||||
Grid.Row="1"
|
||||
Grid.Column="0"
|
||||
Margin="0,4,12,12"
|
||||
Height="30" />
|
||||
|
||||
<TextBlock Grid.Row="0" Grid.Column="1" Text="Client Token" />
|
||||
<PasswordBox x:Name="TokenBox"
|
||||
Grid.Row="1"
|
||||
Grid.Column="1"
|
||||
Margin="0,4,0,12"
|
||||
Height="30" />
|
||||
|
||||
<TextBlock Grid.Row="2" Grid.ColumnSpan="2"
|
||||
x:Name="StatusText"
|
||||
FontSize="14"
|
||||
Margin="0,0,0,4" />
|
||||
<TextBlock Grid.Row="3" Grid.ColumnSpan="2"
|
||||
Text="配置保存在 %LOCALAPPDATA%\WindowsGotify\config.json"
|
||||
Foreground="Gray"
|
||||
FontSize="11" />
|
||||
</Grid>
|
||||
|
||||
<!-- 历史区 -->
|
||||
<Border Grid.Row="3"
|
||||
BorderBrush="#DDD"
|
||||
<Border Grid.Row="1"
|
||||
Background="White"
|
||||
BorderBrush="#E0E0E0"
|
||||
BorderThickness="1"
|
||||
CornerRadius="4"
|
||||
Padding="0"
|
||||
Margin="0,12,0,0">
|
||||
<DockPanel>
|
||||
<Grid DockPanel.Dock="Top" Margin="10,8,10,4">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Text="历史消息(点击行可在浏览器打开链接,留空则只选中)"
|
||||
FontWeight="Bold"
|
||||
VerticalAlignment="Center" />
|
||||
<TextBlock x:Name="HistoryCountText"
|
||||
Grid.Column="1"
|
||||
Foreground="Gray"
|
||||
VerticalAlignment="Center" />
|
||||
</Grid>
|
||||
Padding="12"
|
||||
Margin="0,0,0,8">
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<ListView x:Name="HistoryList"
|
||||
BorderThickness="0"
|
||||
MouseDoubleClick="HistoryList_MouseDoubleClick">
|
||||
<ListView.View>
|
||||
<GridView>
|
||||
<GridViewColumn Header="时间" Width="150" DisplayMemberBinding="{Binding DateDisplay}"/>
|
||||
<GridViewColumn Header="优先级" Width="60" DisplayMemberBinding="{Binding PriorityDisplay}"/>
|
||||
<GridViewColumn Header="标题" Width="120" DisplayMemberBinding="{Binding TitleDisplay}"/>
|
||||
<GridViewColumn Header="内容" Width="380" DisplayMemberBinding="{Binding Message}"/>
|
||||
</GridView>
|
||||
</ListView.View>
|
||||
</ListView>
|
||||
</DockPanel>
|
||||
<TextBlock Grid.Row="0" Grid.Column="0"
|
||||
Text="Gotify URL"
|
||||
FontSize="11"
|
||||
Foreground="#666" />
|
||||
<TextBox x:Name="UrlTextBox"
|
||||
Grid.Row="1"
|
||||
Grid.Column="0"
|
||||
Margin="0,2,12,0"
|
||||
Height="28"
|
||||
Padding="6,0"
|
||||
FontSize="12" />
|
||||
|
||||
<TextBlock Grid.Row="0" Grid.Column="1"
|
||||
Text="Client Token"
|
||||
FontSize="11"
|
||||
Foreground="#666" />
|
||||
<PasswordBox x:Name="TokenBox"
|
||||
Grid.Row="1"
|
||||
Grid.Column="1"
|
||||
Margin="0,2,0,0"
|
||||
Height="28"
|
||||
Padding="6,0"
|
||||
FontSize="12" />
|
||||
</Grid>
|
||||
</Border>
|
||||
|
||||
<!-- 按钮区 -->
|
||||
<StackPanel Grid.Row="4"
|
||||
Orientation="Horizontal"
|
||||
HorizontalAlignment="Right"
|
||||
Margin="0,12,0,0">
|
||||
<Button Content="刷新历史"
|
||||
Width="100"
|
||||
Height="34"
|
||||
Margin="0,0,8,0"
|
||||
Click="RefreshButton_Click" />
|
||||
<Button Content="测试通知"
|
||||
Width="100"
|
||||
Height="34"
|
||||
Margin="0,0,8,0"
|
||||
Click="TestButton_Click" />
|
||||
<Button Content="保存并重连"
|
||||
Width="120"
|
||||
Height="34"
|
||||
Click="SaveButton_Click" />
|
||||
</StackPanel>
|
||||
<!-- 状态 + 操作 -->
|
||||
<DockPanel Grid.Row="2" Margin="0,4,0,8">
|
||||
<StackPanel DockPanel.Dock="Left" Orientation="Horizontal" VerticalAlignment="Center">
|
||||
<Ellipse x:Name="StatusDot" Width="8" Height="8" Fill="#999" Margin="0,0,6,0"/>
|
||||
<TextBlock x:Name="StatusText"
|
||||
Text="状态:等待连接"
|
||||
FontSize="12"
|
||||
VerticalAlignment="Center" />
|
||||
<TextBlock x:Name="AppCountText"
|
||||
Text=" · 应用:?"
|
||||
FontSize="12"
|
||||
Foreground="#666"
|
||||
VerticalAlignment="Center"
|
||||
Margin="12,0,0,0" />
|
||||
</StackPanel>
|
||||
<StackPanel DockPanel.Dock="Right" Orientation="Horizontal" HorizontalAlignment="Right">
|
||||
<Button Content="刷新应用"
|
||||
Width="86" Height="30" Margin="0,0,6,0"
|
||||
Click="RefreshAppsButton_Click" />
|
||||
<Button Content="刷新历史"
|
||||
Width="86" Height="30" Margin="0,0,6,0"
|
||||
Click="RefreshButton_Click" />
|
||||
<Button Content="测试通知"
|
||||
Width="86" Height="30" Margin="0,0,6,0"
|
||||
Click="TestButton_Click" />
|
||||
<Button Content="保存并重连"
|
||||
Width="100" Height="30"
|
||||
Background="#0078D4" Foreground="White" BorderThickness="0"
|
||||
Click="SaveButton_Click" />
|
||||
</StackPanel>
|
||||
</DockPanel>
|
||||
|
||||
<!-- 主区域:历史(左) + 详情(右) -->
|
||||
<Grid Grid.Row="3">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="2*" MinWidth="320"/>
|
||||
<ColumnDefinition Width="6"/>
|
||||
<ColumnDefinition Width="3*" MinWidth="380"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<!-- 历史列表 -->
|
||||
<Border Grid.Column="0"
|
||||
Background="White"
|
||||
BorderBrush="#E0E0E0"
|
||||
BorderThickness="1"
|
||||
CornerRadius="4">
|
||||
<DockPanel>
|
||||
<Border DockPanel.Dock="Top"
|
||||
Background="#F5F5F5"
|
||||
BorderBrush="#E0E0E0"
|
||||
BorderThickness="0,0,0,1"
|
||||
Padding="12,8">
|
||||
<DockPanel>
|
||||
<TextBlock Text="历史消息"
|
||||
FontWeight="Bold"
|
||||
FontSize="12"
|
||||
VerticalAlignment="Center" />
|
||||
<TextBlock x:Name="HistoryCountText"
|
||||
DockPanel.Dock="Right"
|
||||
Text="共 0 条"
|
||||
FontSize="11"
|
||||
Foreground="#888"
|
||||
VerticalAlignment="Center" />
|
||||
</DockPanel>
|
||||
</Border>
|
||||
<ListView x:Name="HistoryList"
|
||||
BorderThickness="0"
|
||||
ItemTemplate="{StaticResource MessageRowTemplate}"
|
||||
SelectionChanged="HistoryList_SelectionChanged"
|
||||
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
|
||||
ScrollViewer.VerticalScrollBarVisibility="Auto">
|
||||
<ListView.ItemContainerStyle>
|
||||
<Style TargetType="ListViewItem">
|
||||
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
|
||||
<Setter Property="Padding" Value="10,4"/>
|
||||
<Setter Property="BorderBrush" Value="#EEE"/>
|
||||
<Setter Property="BorderThickness" Value="0,0,0,1"/>
|
||||
</Style>
|
||||
</ListView.ItemContainerStyle>
|
||||
</ListView>
|
||||
</DockPanel>
|
||||
</Border>
|
||||
|
||||
<GridSplitter Grid.Column="1"
|
||||
Width="6"
|
||||
HorizontalAlignment="Stretch"
|
||||
Background="Transparent" />
|
||||
|
||||
<!-- 详情面板 -->
|
||||
<Border Grid.Column="2"
|
||||
Background="White"
|
||||
BorderBrush="#E0E0E0"
|
||||
BorderThickness="1"
|
||||
CornerRadius="4">
|
||||
<ScrollViewer VerticalScrollBarVisibility="Auto">
|
||||
<Grid Margin="16">
|
||||
<TextBlock x:Name="DetailEmptyHint"
|
||||
Text="← 左侧选一条消息查看详情"
|
||||
Foreground="#AAA"
|
||||
FontSize="13"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center" />
|
||||
<StackPanel x:Name="DetailPanel" Visibility="Collapsed">
|
||||
<!-- 应用名(大标题,带颜色) -->
|
||||
<TextBlock x:Name="DetailAppName"
|
||||
FontSize="22"
|
||||
FontWeight="Bold"
|
||||
Margin="0,0,0,4" />
|
||||
<TextBlock x:Name="DetailAppDesc"
|
||||
Foreground="#888"
|
||||
FontSize="12"
|
||||
Margin="0,0,0,12"
|
||||
TextWrapping="Wrap" />
|
||||
|
||||
<!-- 元数据行 -->
|
||||
<Border Background="#F5F5F5"
|
||||
CornerRadius="4"
|
||||
Padding="10,8"
|
||||
Margin="0,0,0,12">
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="4"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="4"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="4"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<TextBlock Grid.Row="0" Grid.Column="0" Text="优先级" Foreground="#666" FontSize="11" Width="60"/>
|
||||
<TextBlock Grid.Row="0" Grid.Column="1" x:Name="DetailPriority" FontSize="12" />
|
||||
<TextBlock Grid.Row="2" Grid.Column="0" Text="时间" Foreground="#666" FontSize="11" Width="60"/>
|
||||
<TextBlock Grid.Row="2" Grid.Column="1" x:Name="DetailDate" FontSize="12" />
|
||||
<TextBlock Grid.Row="4" Grid.Column="0" Text="标题" Foreground="#666" FontSize="11" Width="60"/>
|
||||
<TextBlock Grid.Row="4" Grid.Column="1" x:Name="DetailTitle" FontSize="12" TextWrapping="Wrap"/>
|
||||
<TextBlock Grid.Row="6" Grid.Column="0" Text="App ID" Foreground="#666" FontSize="11" Width="60"/>
|
||||
<TextBlock Grid.Row="6" Grid.Column="1" x:Name="DetailAppId" FontSize="12" />
|
||||
</Grid>
|
||||
</Border>
|
||||
|
||||
<TextBlock Text="消息正文" Foreground="#666" FontSize="11" Margin="0,0,0,4"/>
|
||||
<Border Background="#F5F5F5"
|
||||
CornerRadius="4"
|
||||
Padding="10,8"
|
||||
Margin="0,0,0,12">
|
||||
<TextBlock x:Name="DetailMessage"
|
||||
TextWrapping="Wrap"
|
||||
FontSize="13"
|
||||
LineHeight="20" />
|
||||
</Border>
|
||||
|
||||
<TextBlock x:Name="DetailUrlLabel"
|
||||
Text="点击链接"
|
||||
Foreground="#666"
|
||||
FontSize="11"
|
||||
Margin="0,0,0,4"
|
||||
Visibility="Collapsed" />
|
||||
<TextBlock x:Name="DetailUrl"
|
||||
Foreground="#0078D4"
|
||||
FontSize="12"
|
||||
TextDecorations="Underline"
|
||||
Cursor="Hand"
|
||||
MouseLeftButtonUp="DetailUrl_Click"
|
||||
TextWrapping="Wrap"
|
||||
Margin="0,0,0,12"
|
||||
Visibility="Collapsed" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</ScrollViewer>
|
||||
</Border>
|
||||
</Grid>
|
||||
|
||||
<!-- 底部提示 -->
|
||||
<TextBlock Grid.Row="4"
|
||||
Text="配置保存在 %LOCALAPPDATA%\WindowsGotify\config.json"
|
||||
Foreground="#999"
|
||||
FontSize="10"
|
||||
Margin="0,8,0,0" />
|
||||
</Grid>
|
||||
</Window>
|
||||
|
||||
+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);
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace WindowsGotify.Models;
|
||||
|
||||
public sealed class GotifyApplication
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public long Id { get; set; }
|
||||
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; } = "";
|
||||
|
||||
[JsonPropertyName("description")]
|
||||
public string? Description { get; set; }
|
||||
|
||||
[JsonPropertyName("defaultPriority")]
|
||||
public int DefaultPriority { get; set; }
|
||||
|
||||
[JsonPropertyName("createdAt")]
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
|
||||
[JsonPropertyName("internal")]
|
||||
public bool Internal { get; set; }
|
||||
}
|
||||
@@ -26,9 +26,15 @@ public sealed class GotifyMessage
|
||||
[JsonPropertyName("extras")]
|
||||
public Dictionary<string, JsonElement>? Extras { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 由 UI 在加入历史列表前,从 ApplicationCache 解析填入;不会从 JSON 反序列化。
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public string AppName { get; set; } = "";
|
||||
|
||||
[JsonIgnore]
|
||||
public string TitleDisplay =>
|
||||
string.IsNullOrWhiteSpace(Title) ? $"(App {AppId})" : Title!;
|
||||
string.IsNullOrWhiteSpace(Title) ? "(无标题)" : Title!;
|
||||
|
||||
[JsonIgnore]
|
||||
public string DateDisplay =>
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
using System.Collections.Concurrent;
|
||||
using WindowsGotify.Models;
|
||||
|
||||
namespace WindowsGotify.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 缓存 Gotify Applications 列表(从 GET /application 拉取),供 UI 用来把消息的 AppId 解析为可读应用名。
|
||||
/// </summary>
|
||||
public sealed class ApplicationCache
|
||||
{
|
||||
private readonly ConcurrentDictionary<long, GotifyApplication> _apps = new();
|
||||
private readonly GotifyApiService _api;
|
||||
private readonly Logger _logger;
|
||||
private DateTimeOffset _lastFetchedAt = DateTimeOffset.MinValue;
|
||||
|
||||
public ApplicationCache(GotifyApiService api, Logger logger)
|
||||
{
|
||||
_api = api;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public int Count => _apps.Count;
|
||||
|
||||
public event EventHandler? Updated;
|
||||
|
||||
/// <summary>
|
||||
/// 拉一次最新的应用列表。失败抛异常,调用方决定怎么处理(忽略 / 重试 / 提示)。
|
||||
/// </summary>
|
||||
public async Task RefreshAsync(CancellationToken ct = default)
|
||||
{
|
||||
var list = await _api.GetApplicationsAsync(ct);
|
||||
_apps.Clear();
|
||||
foreach (var app in list)
|
||||
{
|
||||
_apps[app.Id] = app;
|
||||
}
|
||||
_lastFetchedAt = DateTimeOffset.UtcNow;
|
||||
_logger.Info($"ApplicationCache refreshed. count={list.Count}");
|
||||
Updated?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 把 appId 解析为应用名。缓存里没有就回退 "App {id}"。
|
||||
/// </summary>
|
||||
public string GetName(long appId)
|
||||
{
|
||||
return _apps.TryGetValue(appId, out var app) && !string.IsNullOrWhiteSpace(app.Name)
|
||||
? app.Name
|
||||
: $"App {appId}";
|
||||
}
|
||||
|
||||
public GotifyApplication? Get(long appId)
|
||||
{
|
||||
_apps.TryGetValue(appId, out var app);
|
||||
return app;
|
||||
}
|
||||
|
||||
public DateTimeOffset LastFetchedAt => _lastFetchedAt;
|
||||
}
|
||||
@@ -90,4 +90,19 @@ public sealed class GotifyApiService
|
||||
// Gotify 的 since=0 行为:返回数据库里所有消息中的 limit 条;按 id 降序再取一次保证正确
|
||||
return list.OrderByDescending(m => m.Id).Take(Math.Clamp(limit, 1, 100)).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 拉取所有 Gotify Application,供 UI 用来把消息 AppId 解析为应用名。
|
||||
/// </summary>
|
||||
public async Task<List<GotifyApplication>> GetApplicationsAsync(CancellationToken ct)
|
||||
{
|
||||
SyncAuthHeaders();
|
||||
var path = "application?limit=999";
|
||||
using var response = await _httpClient.GetAsync(BuildUri(path), ct);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
await using var stream = await response.Content.ReadAsStreamAsync(ct);
|
||||
var list = await JsonSerializer.DeserializeAsync<List<GotifyApplication>>(stream, cancellationToken: ct);
|
||||
return list ?? new List<GotifyApplication>();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user