refactor: modularize text injection with a factory and dedicated backend implementations, including a new Wayland clipboard option.
This commit is contained in:
81
IO/Injectors/XdotoolTextInjector.cs
Normal file
81
IO/Injectors/XdotoolTextInjector.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System.Diagnostics;
|
||||
using Toak.Core;
|
||||
using Toak.Core.Interfaces;
|
||||
|
||||
namespace Toak.IO.Injectors;
|
||||
|
||||
/// <summary>
|
||||
/// Text injector that uses <c>xdotool</c> to type text on X11.
|
||||
/// </summary>
|
||||
public class XdotoolTextInjector(INotifications notifications) : ITextInjector
|
||||
{
|
||||
private readonly INotifications _notifications = notifications;
|
||||
|
||||
public Task InjectTextAsync(string text)
|
||||
{
|
||||
Logger.LogDebug("Injecting text using xdotool...");
|
||||
if (string.IsNullOrWhiteSpace(text)) return Task.CompletedTask;
|
||||
|
||||
try
|
||||
{
|
||||
var pInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = Constants.Commands.TypeX11,
|
||||
Arguments = $"type --clearmodifiers --delay {Constants.Defaults.DefaultTypeDelayMs} \"{text.Replace("\"", "\\\"")}\"",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
var p = Process.Start(pInfo);
|
||||
p?.WaitForExit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[XdotoolTextInjector] Error injecting text: {ex.Message}");
|
||||
_notifications.Notify("Injection Error", "Could not type text into window.");
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task<string> InjectStreamAsync(IAsyncEnumerable<string> tokenStream)
|
||||
{
|
||||
Logger.LogDebug("Setting up stream injection using xdotool...");
|
||||
var fullText = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
var pInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = Constants.Commands.TypeX11,
|
||||
Arguments = $"type --clearmodifiers --delay {Constants.Defaults.DefaultTypeDelayMs} --file -",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
RedirectStandardInput = true
|
||||
};
|
||||
|
||||
using var process = Process.Start(pInfo);
|
||||
if (process == null) return string.Empty;
|
||||
|
||||
Logger.LogDebug("Started xdotool stream process, waiting for tokens...");
|
||||
|
||||
await foreach (var token in tokenStream)
|
||||
{
|
||||
Logger.LogDebug($"Injecting token: '{token}'");
|
||||
fullText += token;
|
||||
await process.StandardInput.WriteAsync(token);
|
||||
await process.StandardInput.FlushAsync();
|
||||
}
|
||||
|
||||
Logger.LogDebug("Stream injection complete. Closing standard input.");
|
||||
process.StandardInput.Close();
|
||||
await process.WaitForExitAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[XdotoolTextInjector] Error injecting text stream: {ex.Message}");
|
||||
_notifications.Notify("Injection Error", "Could not type text stream into window.");
|
||||
}
|
||||
|
||||
return fullText;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user