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.
This commit is contained in:
Peter Kirmeier 2023-04-30 21:10:55 +02:00
parent 6ffddc7e02
commit 026ab9a982
3 changed files with 29 additions and 28 deletions

View file

@ -173,7 +173,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="H.InputSimulator" Version="1.3.0" />
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="1.1.0" />
<PackageReference Include="H.NotifyIcon" Version="2.0.108" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View file

@ -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()

View file

@ -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;
}
}
}