diff --git a/App.xaml.cs b/App.xaml.cs index 81c3a96..2bf7de7 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -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(); } diff --git a/Converters/ValueConverters.cs b/Converters/ValueConverters.cs new file mode 100644 index 0000000..93f2aa8 --- /dev/null +++ b/Converters/ValueConverters.cs @@ -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(); +} + +/// +/// 应用名 → 稳定颜色(用名字的 hash 决定 hue),让每个应用在历史里视觉上可区分。 +/// +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(); +} diff --git a/MainWindow.xaml b/MainWindow.xaml index 79bbe31..fcbb6d6 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -1,14 +1,92 @@ + Width="1100" + Height="720" + MinWidth="900" + MinHeight="560" + WindowStartupLocation="CenterScreen" + Background="#FAFAFA"> - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -17,109 +95,244 @@ - - + + - + FontSize="24" + FontWeight="Bold" + Foreground="#222" /> + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + Padding="12" + Margin="0,0,0,8"> + + + + + + + + + - - - - - - - - - - - + + + + + + - - -