Add tray status icons

This commit is contained in:
Misaka
2026-05-16 21:02:30 +08:00
parent 4caa21c4d2
commit 0611966be9
5 changed files with 69 additions and 6 deletions

View File

@@ -8,6 +8,8 @@ public sealed class TrayApplicationContext : ApplicationContext
private readonly WebSocketServerService server;
private readonly DiscoveryResponderService discovery;
private readonly NotifyIcon notifyIcon;
private readonly Icon connectedIcon;
private readonly Icon waitIcon;
private ToolStripMenuItem? clipboardPasteMenuItem;
private ToolStripMenuItem? sendInputTypingMenuItem;
@@ -18,12 +20,14 @@ public sealed class TrayApplicationContext : ApplicationContext
var handler = new SyncMessageHandler(keyboard);
server = new WebSocketServerService(handler);
server.StatusChanged += (_, _) => UpdateTrayText();
server.StatusChanged += (_, _) => UpdateTrayStatus();
discovery = new DiscoveryResponderService(() => server.LocalIpAddress, () => server.Port);
connectedIcon = TrayIconFactory.CreateConnectedIcon();
waitIcon = TrayIconFactory.CreateWaitIcon();
notifyIcon = new NotifyIcon
{
Icon = SystemIcons.Application,
Icon = waitIcon,
Text = "WirelessTextSyncer starting...",
Visible = true,
ContextMenuStrip = BuildMenu()
@@ -34,7 +38,7 @@ public sealed class TrayApplicationContext : ApplicationContext
{
server.Start(8181);
discovery.Start();
UpdateTrayText();
UpdateTrayStatus();
}
catch (Exception ex)
{
@@ -50,6 +54,8 @@ public sealed class TrayApplicationContext : ApplicationContext
if (disposing)
{
notifyIcon.Dispose();
connectedIcon.Dispose();
waitIcon.Dispose();
discovery.Dispose();
server.Dispose();
}
@@ -131,10 +137,12 @@ public sealed class TrayApplicationContext : ApplicationContext
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})";
notifyIcon.Icon = connected ? connectedIcon : waitIcon;
notifyIcon.Text = text.Length > 63 ? text[..63] : text;
}
}