58 lines
1.6 KiB
C#
58 lines
1.6 KiB
C#
using WirelessTextSyncer.Windows.Models;
|
|
|
|
namespace WirelessTextSyncer.Windows.Services;
|
|
|
|
public sealed class SyncMessageHandler
|
|
{
|
|
private readonly IKeyboardInjectionService keyboard;
|
|
private string remoteText = string.Empty;
|
|
|
|
public SyncMessageHandler(IKeyboardInjectionService keyboard)
|
|
{
|
|
this.keyboard = keyboard;
|
|
}
|
|
|
|
public void Handle(SyncMessage message)
|
|
{
|
|
switch (message.Action)
|
|
{
|
|
case SyncAction.InsertText:
|
|
var insertedText = message.Text ?? string.Empty;
|
|
keyboard.InsertText(insertedText);
|
|
remoteText += insertedText;
|
|
break;
|
|
case SyncAction.ReplaceAll:
|
|
AppendSubmittedText(message.Text ?? string.Empty);
|
|
break;
|
|
case SyncAction.Backspace:
|
|
keyboard.Backspace();
|
|
if (remoteText.Length > 0)
|
|
{
|
|
remoteText = remoteText[..^1];
|
|
}
|
|
|
|
break;
|
|
case SyncAction.Enter:
|
|
keyboard.Enter();
|
|
remoteText += Environment.NewLine;
|
|
break;
|
|
case SyncAction.Ping:
|
|
break;
|
|
default:
|
|
throw new InvalidOperationException($"Unsupported sync action: {message.Action}");
|
|
}
|
|
}
|
|
|
|
private void AppendSubmittedText(string text)
|
|
{
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
return;
|
|
}
|
|
|
|
AppLogger.Info($"Appending submitted text length: {text.Length}");
|
|
keyboard.InsertText(text);
|
|
remoteText += text;
|
|
}
|
|
}
|