From 026ab9a982f6212eff2520303f900bca4410b7f4 Mon Sep 17 00:00:00 2001 From: Peter Kirmeier Date: Sun, 30 Apr 2023 21:10:55 +0200 Subject: [PATCH] Replace Hardcodet.NotifyIcon.Wpf v1.1.0 with H.NotifyIcon v2.0.108 The former is no longer maintained and the new project is base on the former one. The new one should also be usable for other platforms. --- SystemTrayMenu.csproj | 2 +- UserInterface/AppContextMenu.cs | 25 ++++++++++--------------- UserInterface/AppNotifyIcon.cs | 30 ++++++++++++++++++------------ 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/SystemTrayMenu.csproj b/SystemTrayMenu.csproj index 117170a..71d155d 100644 --- a/SystemTrayMenu.csproj +++ b/SystemTrayMenu.csproj @@ -173,7 +173,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/UserInterface/AppContextMenu.cs b/UserInterface/AppContextMenu.cs index cf35c2b..0d39559 100644 --- a/UserInterface/AppContextMenu.cs +++ b/UserInterface/AppContextMenu.cs @@ -8,20 +8,17 @@ namespace SystemTrayMenu.Helpers using System.Diagnostics; using System.Reflection; using System.Windows; - using System.Windows.Controls; + using System.Windows.Threading; + using H.NotifyIcon.Core; using SystemTrayMenu.Helpers.Updater; using SystemTrayMenu.UserInterface; using SystemTrayMenu.Utilities; - using SystemColors = System.Windows.SystemColors; internal class AppContextMenu { - public ContextMenu Create() + public PopupMenu Create() { - ContextMenu menu = new() - { - Background = SystemColors.ControlBrush, - }; + PopupMenu menu = new(); AddItem(menu, "Settings", () => SettingsWindow.ShowSingleInstance()); AddSeperator(menu); @@ -38,21 +35,19 @@ namespace SystemTrayMenu.Helpers return menu; } - private static void AddSeperator(ContextMenu menu) + private static void AddSeperator(PopupMenu menu) { - menu.Items.Add(new Separator()); + menu.Items.Add(new PopupMenuSeparator()); } private static void AddItem( - ContextMenu menu, + PopupMenu menu, string text, Action actionClick) { - menu.Items.Add(new MenuItem() - { - Header = text, - Command = new ActionCommand((_) => actionClick()), - }); + Dispatcher dispatcher = Dispatcher.CurrentDispatcher; + menu.Items.Add(new PopupMenuItem( + text, new ((_, _) => dispatcher.Invoke(actionClick)))); } private static void About() diff --git a/UserInterface/AppNotifyIcon.cs b/UserInterface/AppNotifyIcon.cs index e157218..ef59cad 100644 --- a/UserInterface/AppNotifyIcon.cs +++ b/UserInterface/AppNotifyIcon.cs @@ -5,41 +5,47 @@ namespace SystemTrayMenu.UserInterface { using System; - using System.Windows; - using Hardcodet.Wpf.TaskbarNotification; + using System.Windows.Threading; + using H.NotifyIcon.Core; using SystemTrayMenu.Helpers; - using SystemTrayMenu.Utilities; internal class AppNotifyIcon : IDisposable { - private readonly TaskbarIcon notifyIcon = new (); + private readonly Dispatcher dispatchter = Dispatcher.CurrentDispatcher; + private readonly TrayIconWithContextMenu notifyIcon = new (); public AppNotifyIcon() { - notifyIcon.ToolTipText = "SystemTrayMenu"; - notifyIcon.Icon = Config.GetAppIcon(); - notifyIcon.Visibility = Visibility.Visible; + notifyIcon.ToolTip = "SystemTrayMenu"; + notifyIcon.Icon = Config.GetAppIcon().Handle; notifyIcon.ContextMenu = new AppContextMenu().Create(); - notifyIcon.LeftClickCommand = new ActionCommand((_) => Click?.Invoke()); - notifyIcon.DoubleClickCommand = new ActionCommand((_) => Click?.Invoke()); + notifyIcon.MessageWindow.MouseEventReceived += (sender, e) => + { + if (e.MouseEvent == MouseEvent.IconLeftMouseUp || + e.MouseEvent == MouseEvent.IconLeftDoubleClick) + { + dispatchter.Invoke(() => Click?.Invoke()); + } + }; + notifyIcon.Create(); } public event Action? Click; public void Dispose() { - notifyIcon.Icon = null; + notifyIcon.Icon = IntPtr.Zero; notifyIcon.Dispose(); } public void LoadingStart() { - notifyIcon.Icon = Resources.StaticResources.LoadingIcon; + notifyIcon.Icon = Resources.StaticResources.LoadingIcon.Handle; } public void LoadingStop() { - notifyIcon.Icon = Config.GetAppIcon(); + notifyIcon.Icon = Config.GetAppIcon().Handle; } } } \ No newline at end of file