Initialize Windows desktop skeleton

This commit is contained in:
Misaka
2026-05-15 20:12:07 +08:00
commit 498251ad79
12 changed files with 334 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
namespace WirelessTextSyncer.Windows.Models;
public enum SyncAction
{
InsertText,
Backspace,
Enter,
Ping
}

View File

@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;
namespace WirelessTextSyncer.Windows.Models;
public sealed record SyncMessage
{
[JsonPropertyName("action")]
public SyncAction Action { get; init; }
[JsonPropertyName("text")]
public string? Text { get; init; }
}

View File

@@ -0,0 +1,16 @@
namespace WirelessTextSyncer.Windows;
using WirelessTextSyncer.Windows.Tray;
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new TrayApplicationContext());
}
}

View File

@@ -0,0 +1,10 @@
namespace WirelessTextSyncer.Windows.Services;
public interface IKeyboardInjectionService
{
void InsertText(string text);
void Backspace();
void Enter();
}

View File

@@ -0,0 +1,24 @@
namespace WirelessTextSyncer.Windows.Services;
public sealed class KeyboardInjectionService : IKeyboardInjectionService
{
public void InsertText(string text)
{
if (string.IsNullOrEmpty(text))
{
return;
}
SendKeys.SendWait(text);
}
public void Backspace()
{
SendKeys.SendWait("{BACKSPACE}");
}
public void Enter()
{
SendKeys.SendWait("{ENTER}");
}
}

View File

@@ -0,0 +1,33 @@
using WirelessTextSyncer.Windows.Models;
namespace WirelessTextSyncer.Windows.Services;
public sealed class SyncMessageHandler
{
private readonly IKeyboardInjectionService keyboard;
public SyncMessageHandler(IKeyboardInjectionService keyboard)
{
this.keyboard = keyboard;
}
public void Handle(SyncMessage message)
{
switch (message.Action)
{
case SyncAction.InsertText:
keyboard.InsertText(message.Text ?? string.Empty);
break;
case SyncAction.Backspace:
keyboard.Backspace();
break;
case SyncAction.Enter:
keyboard.Enter();
break;
case SyncAction.Ping:
break;
default:
throw new InvalidOperationException($"Unsupported sync action: {message.Action}");
}
}
}

View File

@@ -0,0 +1,81 @@
using System.Net;
using System.Net.Sockets;
using System.Text.Json;
using Fleck;
using WirelessTextSyncer.Windows.Models;
namespace WirelessTextSyncer.Windows.Services;
public sealed class WebSocketServerService : IDisposable
{
private readonly SyncMessageHandler messageHandler;
private readonly List<IWebSocketConnection> clients = [];
private WebSocketServer? server;
public WebSocketServerService(SyncMessageHandler messageHandler)
{
this.messageHandler = messageHandler;
}
public int Port { get; private set; } = 8181;
public string LocalIpAddress { get; private set; } = "127.0.0.1";
public bool HasClient => clients.Count > 0;
public event EventHandler? StatusChanged;
public void Start(int port)
{
Port = port;
LocalIpAddress = ResolveLocalIpAddress();
server = new WebSocketServer($"ws://0.0.0.0:{Port}");
server.Start(socket =>
{
socket.OnOpen = () =>
{
clients.Add(socket);
StatusChanged?.Invoke(this, EventArgs.Empty);
};
socket.OnClose = () =>
{
clients.Remove(socket);
StatusChanged?.Invoke(this, EventArgs.Empty);
};
socket.OnMessage = HandleRawMessage;
});
}
public void Dispose()
{
foreach (var client in clients.ToArray())
{
client.Close();
}
clients.Clear();
server?.Dispose();
}
private void HandleRawMessage(string rawMessage)
{
var message = JsonSerializer.Deserialize<SyncMessage>(rawMessage);
if (message is null)
{
return;
}
messageHandler.Handle(message);
}
private static string ResolveLocalIpAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
var address = host.AddressList.FirstOrDefault(
address => address.AddressFamily == AddressFamily.InterNetwork
&& !IPAddress.IsLoopback(address));
return address?.ToString() ?? "127.0.0.1";
}
}

View File

@@ -0,0 +1,80 @@
using WirelessTextSyncer.Windows.Services;
namespace WirelessTextSyncer.Windows.Tray;
public sealed class TrayApplicationContext : ApplicationContext
{
private readonly WebSocketServerService server;
private readonly NotifyIcon notifyIcon;
public TrayApplicationContext()
{
var keyboard = new KeyboardInjectionService();
var handler = new SyncMessageHandler(keyboard);
server = new WebSocketServerService(handler);
server.StatusChanged += (_, _) => UpdateTrayText();
notifyIcon = new NotifyIcon
{
Icon = SystemIcons.Application,
Text = "WirelessTextSyncer starting...",
Visible = true,
ContextMenuStrip = BuildMenu()
};
notifyIcon.MouseClick += OnTrayIconClicked;
try
{
server.Start(8181);
UpdateTrayText();
}
catch (Exception ex)
{
notifyIcon.BalloonTipTitle = "WirelessTextSyncer";
notifyIcon.BalloonTipText = $"Failed to start: {ex.Message}";
notifyIcon.ShowBalloonTip(5000);
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
notifyIcon.Dispose();
server.Dispose();
}
base.Dispose(disposing);
}
private ContextMenuStrip BuildMenu()
{
var menu = new ContextMenuStrip();
menu.Items.Add("Copy connection address", null, (_, _) => CopyConnectionAddress());
menu.Items.Add("Exit", null, (_, _) => ExitThread());
return menu;
}
private void OnTrayIconClicked(object? sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
CopyConnectionAddress();
}
}
private void CopyConnectionAddress()
{
Clipboard.SetText($"ws://{server.LocalIpAddress}:{server.Port}");
notifyIcon.BalloonTipTitle = "WirelessTextSyncer";
notifyIcon.BalloonTipText = "Connection address copied.";
notifyIcon.ShowBalloonTip(1500);
}
private void UpdateTrayText()
{
var status = server.HasClient ? "connected" : "waiting";
notifyIcon.Text = $"WirelessTextSyncer {server.LocalIpAddress}:{server.Port} ({status})";
}
}

View File

@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Fleck" Version="1.2.0" />
<PackageReference Include="InputSimulatorPlus" Version="1.0.7" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.8" />
</ItemGroup>
</Project>