Initialize Windows desktop skeleton
This commit is contained in:
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
bin/
|
||||||
|
obj/
|
||||||
|
.vs/
|
||||||
|
.vscode/
|
||||||
|
*.user
|
||||||
|
*.suo
|
||||||
|
*.log
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
|
||||||
20
README.md
Normal file
20
README.md
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# WirelessTextSyncer Desktop
|
||||||
|
|
||||||
|
Windows tray server for WirelessTextSyncer.
|
||||||
|
|
||||||
|
## Stack
|
||||||
|
|
||||||
|
- .NET 8
|
||||||
|
- WinForms tray application
|
||||||
|
- Fleck WebSocket server
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
dotnet restore
|
||||||
|
dotnet build
|
||||||
|
dotnet run --project .\WirelessTextSyncer.Windows\WirelessTextSyncer.Windows.csproj
|
||||||
|
```
|
||||||
|
|
||||||
|
The initial server listens on `ws://0.0.0.0:8181`.
|
||||||
|
|
||||||
22
WirelessTextSyncer.Desktop.sln
Normal file
22
WirelessTextSyncer.Desktop.sln
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.0.31903.59
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WirelessTextSyncer.Windows", "WirelessTextSyncer.Windows\WirelessTextSyncer.Windows.csproj", "{7AD6081A-F4EB-45AA-917B-2CCFA54E0A67}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{7AD6081A-F4EB-45AA-917B-2CCFA54E0A67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{7AD6081A-F4EB-45AA-917B-2CCFA54E0A67}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{7AD6081A-F4EB-45AA-917B-2CCFA54E0A67}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{7AD6081A-F4EB-45AA-917B-2CCFA54E0A67}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
9
WirelessTextSyncer.Windows/Models/SyncAction.cs
Normal file
9
WirelessTextSyncer.Windows/Models/SyncAction.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace WirelessTextSyncer.Windows.Models;
|
||||||
|
|
||||||
|
public enum SyncAction
|
||||||
|
{
|
||||||
|
InsertText,
|
||||||
|
Backspace,
|
||||||
|
Enter,
|
||||||
|
Ping
|
||||||
|
}
|
||||||
12
WirelessTextSyncer.Windows/Models/SyncMessage.cs
Normal file
12
WirelessTextSyncer.Windows/Models/SyncMessage.cs
Normal 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; }
|
||||||
|
}
|
||||||
16
WirelessTextSyncer.Windows/Program.cs
Normal file
16
WirelessTextSyncer.Windows/Program.cs
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
namespace WirelessTextSyncer.Windows.Services;
|
||||||
|
|
||||||
|
public interface IKeyboardInjectionService
|
||||||
|
{
|
||||||
|
void InsertText(string text);
|
||||||
|
|
||||||
|
void Backspace();
|
||||||
|
|
||||||
|
void Enter();
|
||||||
|
}
|
||||||
@@ -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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
33
WirelessTextSyncer.Windows/Services/SyncMessageHandler.cs
Normal file
33
WirelessTextSyncer.Windows/Services/SyncMessageHandler.cs
Normal 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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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";
|
||||||
|
}
|
||||||
|
}
|
||||||
80
WirelessTextSyncer.Windows/Tray/TrayApplicationContext.cs
Normal file
80
WirelessTextSyncer.Windows/Tray/TrayApplicationContext.cs
Normal 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})";
|
||||||
|
}
|
||||||
|
}
|
||||||
17
WirelessTextSyncer.Windows/WirelessTextSyncer.Windows.csproj
Normal file
17
WirelessTextSyncer.Windows/WirelessTextSyncer.Windows.csproj
Normal 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>
|
||||||
Reference in New Issue
Block a user