Replace focused text from Android source
This commit is contained in:
@@ -5,6 +5,7 @@ namespace WirelessTextSyncer.Windows.Services;
|
||||
public sealed class SyncMessageHandler
|
||||
{
|
||||
private readonly IKeyboardInjectionService keyboard;
|
||||
private string remoteText = string.Empty;
|
||||
|
||||
public SyncMessageHandler(IKeyboardInjectionService keyboard)
|
||||
{
|
||||
@@ -16,13 +17,24 @@ public sealed class SyncMessageHandler
|
||||
switch (message.Action)
|
||||
{
|
||||
case SyncAction.InsertText:
|
||||
keyboard.InsertText(message.Text ?? string.Empty);
|
||||
var insertedText = message.Text ?? string.Empty;
|
||||
keyboard.InsertText(insertedText);
|
||||
remoteText += insertedText;
|
||||
break;
|
||||
case SyncAction.ReplaceAll:
|
||||
ReplaceAll(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;
|
||||
@@ -30,4 +42,18 @@ public sealed class SyncMessageHandler
|
||||
throw new InvalidOperationException($"Unsupported sync action: {message.Action}");
|
||||
}
|
||||
}
|
||||
|
||||
private void ReplaceAll(string newText)
|
||||
{
|
||||
if (newText == remoteText)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AppLogger.Info(
|
||||
$"Hard replacing focused text. Old mirrored length: {remoteText.Length}, new length: {newText.Length}");
|
||||
|
||||
keyboard.ReplaceFocusedText(newText);
|
||||
remoteText = newText;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user