Add tray status icons
This commit is contained in:
BIN
WirelessTextSyncer.Windows/Resources/Icons/tray-connected.png
Normal file
BIN
WirelessTextSyncer.Windows/Resources/Icons/tray-connected.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
BIN
WirelessTextSyncer.Windows/Resources/Icons/tray-wait.png
Normal file
BIN
WirelessTextSyncer.Windows/Resources/Icons/tray-wait.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.7 KiB |
@@ -8,6 +8,8 @@ public sealed class TrayApplicationContext : ApplicationContext
|
|||||||
private readonly WebSocketServerService server;
|
private readonly WebSocketServerService server;
|
||||||
private readonly DiscoveryResponderService discovery;
|
private readonly DiscoveryResponderService discovery;
|
||||||
private readonly NotifyIcon notifyIcon;
|
private readonly NotifyIcon notifyIcon;
|
||||||
|
private readonly Icon connectedIcon;
|
||||||
|
private readonly Icon waitIcon;
|
||||||
private ToolStripMenuItem? clipboardPasteMenuItem;
|
private ToolStripMenuItem? clipboardPasteMenuItem;
|
||||||
private ToolStripMenuItem? sendInputTypingMenuItem;
|
private ToolStripMenuItem? sendInputTypingMenuItem;
|
||||||
|
|
||||||
@@ -18,12 +20,14 @@ public sealed class TrayApplicationContext : ApplicationContext
|
|||||||
var handler = new SyncMessageHandler(keyboard);
|
var handler = new SyncMessageHandler(keyboard);
|
||||||
|
|
||||||
server = new WebSocketServerService(handler);
|
server = new WebSocketServerService(handler);
|
||||||
server.StatusChanged += (_, _) => UpdateTrayText();
|
server.StatusChanged += (_, _) => UpdateTrayStatus();
|
||||||
discovery = new DiscoveryResponderService(() => server.LocalIpAddress, () => server.Port);
|
discovery = new DiscoveryResponderService(() => server.LocalIpAddress, () => server.Port);
|
||||||
|
connectedIcon = TrayIconFactory.CreateConnectedIcon();
|
||||||
|
waitIcon = TrayIconFactory.CreateWaitIcon();
|
||||||
|
|
||||||
notifyIcon = new NotifyIcon
|
notifyIcon = new NotifyIcon
|
||||||
{
|
{
|
||||||
Icon = SystemIcons.Application,
|
Icon = waitIcon,
|
||||||
Text = "WirelessTextSyncer starting...",
|
Text = "WirelessTextSyncer starting...",
|
||||||
Visible = true,
|
Visible = true,
|
||||||
ContextMenuStrip = BuildMenu()
|
ContextMenuStrip = BuildMenu()
|
||||||
@@ -34,7 +38,7 @@ public sealed class TrayApplicationContext : ApplicationContext
|
|||||||
{
|
{
|
||||||
server.Start(8181);
|
server.Start(8181);
|
||||||
discovery.Start();
|
discovery.Start();
|
||||||
UpdateTrayText();
|
UpdateTrayStatus();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -50,6 +54,8 @@ public sealed class TrayApplicationContext : ApplicationContext
|
|||||||
if (disposing)
|
if (disposing)
|
||||||
{
|
{
|
||||||
notifyIcon.Dispose();
|
notifyIcon.Dispose();
|
||||||
|
connectedIcon.Dispose();
|
||||||
|
waitIcon.Dispose();
|
||||||
discovery.Dispose();
|
discovery.Dispose();
|
||||||
server.Dispose();
|
server.Dispose();
|
||||||
}
|
}
|
||||||
@@ -131,10 +137,12 @@ public sealed class TrayApplicationContext : ApplicationContext
|
|||||||
notifyIcon.ShowBalloonTip(1500);
|
notifyIcon.ShowBalloonTip(1500);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateTrayText()
|
private void UpdateTrayStatus()
|
||||||
{
|
{
|
||||||
var status = server.HasClient ? "connected" : "waiting";
|
var connected = server.HasClient;
|
||||||
|
var status = connected ? "connected" : "waiting";
|
||||||
var text = $"WirelessTextSyncer {server.LocalIpAddress}:{server.Port} ({status})";
|
var text = $"WirelessTextSyncer {server.LocalIpAddress}:{server.Port} ({status})";
|
||||||
|
notifyIcon.Icon = connected ? connectedIcon : waitIcon;
|
||||||
notifyIcon.Text = text.Length > 63 ? text[..63] : text;
|
notifyIcon.Text = text.Length > 63 ? text[..63] : text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
50
WirelessTextSyncer.Windows/Tray/TrayIconFactory.cs
Normal file
50
WirelessTextSyncer.Windows/Tray/TrayIconFactory.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
using System.Drawing.Imaging;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace WirelessTextSyncer.Windows.Tray;
|
||||||
|
|
||||||
|
internal static class TrayIconFactory
|
||||||
|
{
|
||||||
|
private const string ConnectedResourceName = "WirelessTextSyncer.TrayConnected.png";
|
||||||
|
private const string WaitResourceName = "WirelessTextSyncer.TrayWait.png";
|
||||||
|
|
||||||
|
public static Icon CreateConnectedIcon()
|
||||||
|
{
|
||||||
|
return CreateIconFromResource(ConnectedResourceName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Icon CreateWaitIcon()
|
||||||
|
{
|
||||||
|
return CreateIconFromResource(WaitResourceName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Icon CreateIconFromResource(string resourceName)
|
||||||
|
{
|
||||||
|
var assembly = Assembly.GetExecutingAssembly();
|
||||||
|
using var stream = assembly.GetManifestResourceStream(resourceName)
|
||||||
|
?? throw new InvalidOperationException($"Tray icon resource not found: {resourceName}");
|
||||||
|
using var source = new Bitmap(stream);
|
||||||
|
using var bitmap = new Bitmap(source.Width, source.Height, PixelFormat.Format32bppArgb);
|
||||||
|
using (var graphics = Graphics.FromImage(bitmap))
|
||||||
|
{
|
||||||
|
graphics.Clear(Color.Transparent);
|
||||||
|
graphics.DrawImage(source, 0, 0, source.Width, source.Height);
|
||||||
|
}
|
||||||
|
|
||||||
|
var handle = bitmap.GetHicon();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var icon = Icon.FromHandle(handle);
|
||||||
|
return (Icon)icon.Clone();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
DestroyIcon(handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[DllImport("user32.dll", SetLastError = true)]
|
||||||
|
[return: MarshalAs(UnmanagedType.Bool)]
|
||||||
|
private static extern bool DestroyIcon(IntPtr handle);
|
||||||
|
}
|
||||||
@@ -13,4 +13,9 @@
|
|||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.8" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.8" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
<ItemGroup>
|
||||||
|
<EmbeddedResource Include="Resources\Icons\tray-connected.png" LogicalName="WirelessTextSyncer.TrayConnected.png" />
|
||||||
|
<EmbeddedResource Include="Resources\Icons\tray-wait.png" LogicalName="WirelessTextSyncer.TrayWait.png" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user