refactor: modernize code, improve performance, and clean up various components.
This commit is contained in:
@@ -1,31 +1,25 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
using Toak.Core.Interfaces;
|
||||
|
||||
namespace Toak.IO;
|
||||
|
||||
public class ClipboardManager : IClipboardManager
|
||||
public class ClipboardManager(INotifications notifications) : IClipboardManager
|
||||
{
|
||||
private readonly INotifications _notifications;
|
||||
|
||||
public ClipboardManager(INotifications notifications)
|
||||
{
|
||||
_notifications = notifications;
|
||||
}
|
||||
private readonly INotifications _notifications = notifications;
|
||||
|
||||
public void Copy(string text)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(text)) return;
|
||||
try
|
||||
{
|
||||
string sessionType = Environment.GetEnvironmentVariable("XDG_SESSION_TYPE")?.ToLowerInvariant() ?? "";
|
||||
var sessionType = Environment.GetEnvironmentVariable("XDG_SESSION_TYPE")?.ToLowerInvariant() ?? "";
|
||||
|
||||
ProcessStartInfo pInfo;
|
||||
if (sessionType == "wayland")
|
||||
{
|
||||
pInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = Toak.Core.Constants.Commands.ClipboardWayland,
|
||||
FileName = Core.Constants.Commands.ClipboardWayland,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
RedirectStandardInput = true
|
||||
@@ -35,7 +29,7 @@ public class ClipboardManager : IClipboardManager
|
||||
{
|
||||
pInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = Toak.Core.Constants.Commands.ClipboardX11,
|
||||
FileName = Core.Constants.Commands.ClipboardX11,
|
||||
Arguments = "-selection clipboard",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
@@ -44,14 +38,12 @@ public class ClipboardManager : IClipboardManager
|
||||
}
|
||||
|
||||
var process = Process.Start(pInfo);
|
||||
if (process != null)
|
||||
if (process == null) return;
|
||||
using (var sw = process.StandardInput)
|
||||
{
|
||||
using (var sw = process.StandardInput)
|
||||
{
|
||||
sw.Write(text);
|
||||
}
|
||||
process.WaitForExit();
|
||||
sw.Write(text);
|
||||
}
|
||||
process.WaitForExit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
using Toak.Core.Interfaces;
|
||||
|
||||
namespace Toak.IO;
|
||||
@@ -16,7 +15,7 @@ public class Notifications : INotifications
|
||||
{
|
||||
var pInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = Toak.Core.Constants.Commands.Notify,
|
||||
FileName = Core.Constants.Commands.Notify,
|
||||
Arguments = $"-a \"Toak\" \"{summary}\" \"{body}\"",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true
|
||||
@@ -66,7 +65,7 @@ public class Notifications : INotifications
|
||||
|
||||
var pInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = Toak.Core.Constants.Commands.PlaySound,
|
||||
FileName = Core.Constants.Commands.PlaySound,
|
||||
Arguments = $"\"{absolutePath}\"",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true
|
||||
|
||||
@@ -1,19 +1,12 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
using Toak.Core;
|
||||
|
||||
using Toak.Core.Interfaces;
|
||||
|
||||
namespace Toak.IO;
|
||||
|
||||
public class TextInjector : ITextInjector
|
||||
public class TextInjector(INotifications notifications) : ITextInjector
|
||||
{
|
||||
private readonly INotifications _notifications;
|
||||
|
||||
public TextInjector(INotifications notifications)
|
||||
{
|
||||
_notifications = notifications;
|
||||
}
|
||||
private readonly INotifications _notifications = notifications;
|
||||
|
||||
public Task InjectTextAsync(string text, string backend = "xdotool")
|
||||
{
|
||||
@@ -28,8 +21,8 @@ public class TextInjector : ITextInjector
|
||||
Logger.LogDebug($"Injecting text using wtype...");
|
||||
pInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = Toak.Core.Constants.Commands.TypeWayland,
|
||||
Arguments = $"-d {Toak.Core.Constants.Defaults.DefaultTypeDelayMs} \"{text.Replace("\"", "\\\"")}\"",
|
||||
FileName = Constants.Commands.TypeWayland,
|
||||
Arguments = $"-d {Constants.Defaults.DefaultTypeDelayMs} \"{text.Replace("\"", "\\\"")}\"",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
@@ -39,7 +32,7 @@ public class TextInjector : ITextInjector
|
||||
Logger.LogDebug($"Injecting text using ydotool...");
|
||||
pInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = Toak.Core.Constants.Commands.TypeYdotool,
|
||||
FileName = Constants.Commands.TypeYdotool,
|
||||
Arguments = $"type \"{text.Replace("\"", "\\\"")}\"",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true
|
||||
@@ -50,8 +43,8 @@ public class TextInjector : ITextInjector
|
||||
Logger.LogDebug($"Injecting text using xdotool...");
|
||||
pInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = Toak.Core.Constants.Commands.TypeX11,
|
||||
Arguments = $"type --clearmodifiers --delay {Toak.Core.Constants.Defaults.DefaultTypeDelayMs} \"{text.Replace("\"", "\\\"")}\"",
|
||||
FileName = Constants.Commands.TypeX11,
|
||||
Arguments = $"type --clearmodifiers --delay {Constants.Defaults.DefaultTypeDelayMs} \"{text.Replace("\"", "\\\"")}\"",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
@@ -69,7 +62,7 @@ public class TextInjector : ITextInjector
|
||||
|
||||
public async Task<string> InjectStreamAsync(IAsyncEnumerable<string> tokenStream, string backend)
|
||||
{
|
||||
string fullText = string.Empty;
|
||||
var fullText = string.Empty;
|
||||
try
|
||||
{
|
||||
ProcessStartInfo pInfo;
|
||||
@@ -78,8 +71,8 @@ public class TextInjector : ITextInjector
|
||||
Logger.LogDebug($"Setting up stream injection using wtype...");
|
||||
pInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = Toak.Core.Constants.Commands.TypeWayland,
|
||||
Arguments = $"-d {Toak.Core.Constants.Defaults.DefaultTypeDelayMs} -",
|
||||
FileName = Constants.Commands.TypeWayland,
|
||||
Arguments = $"-d {Constants.Defaults.DefaultTypeDelayMs} -",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
RedirectStandardInput = true
|
||||
@@ -94,7 +87,7 @@ public class TextInjector : ITextInjector
|
||||
fullText += token;
|
||||
var chunkInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = Toak.Core.Constants.Commands.TypeYdotool,
|
||||
FileName = Constants.Commands.TypeYdotool,
|
||||
Arguments = $"type \"{token.Replace("\"", "\\\"")}\"",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true
|
||||
@@ -109,8 +102,8 @@ public class TextInjector : ITextInjector
|
||||
Logger.LogDebug($"Setting up stream injection using xdotool...");
|
||||
pInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = Toak.Core.Constants.Commands.TypeX11,
|
||||
Arguments = $"type --clearmodifiers --delay {Toak.Core.Constants.Defaults.DefaultTypeDelayMs} --file -",
|
||||
FileName = Constants.Commands.TypeX11,
|
||||
Arguments = $"type --clearmodifiers --delay {Constants.Defaults.DefaultTypeDelayMs} --file -",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
RedirectStandardInput = true
|
||||
|
||||
Reference in New Issue
Block a user