63 lines
1.8 KiB
C#
63 lines
1.8 KiB
C#
using System.Diagnostics;
|
|
|
|
using Toak.Core.Interfaces;
|
|
|
|
namespace Toak.IO;
|
|
|
|
public class ClipboardManager : IClipboardManager
|
|
{
|
|
private readonly INotifications _notifications;
|
|
|
|
public ClipboardManager(INotifications notifications)
|
|
{
|
|
_notifications = notifications;
|
|
}
|
|
|
|
public void Copy(string text)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(text)) return;
|
|
try
|
|
{
|
|
string sessionType = Environment.GetEnvironmentVariable("XDG_SESSION_TYPE")?.ToLowerInvariant() ?? "";
|
|
|
|
ProcessStartInfo pInfo;
|
|
if (sessionType == "wayland")
|
|
{
|
|
pInfo = new ProcessStartInfo
|
|
{
|
|
FileName = Toak.Core.Constants.Commands.ClipboardWayland,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true,
|
|
RedirectStandardInput = true
|
|
};
|
|
}
|
|
else
|
|
{
|
|
pInfo = new ProcessStartInfo
|
|
{
|
|
FileName = Toak.Core.Constants.Commands.ClipboardX11,
|
|
Arguments = "-selection clipboard",
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true,
|
|
RedirectStandardInput = true
|
|
};
|
|
}
|
|
|
|
var process = Process.Start(pInfo);
|
|
if (process != null)
|
|
{
|
|
using (var sw = process.StandardInput)
|
|
{
|
|
sw.Write(text);
|
|
}
|
|
process.WaitForExit();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[ClipboardManager] Error copying text: {ex.Message}");
|
|
_notifications.Notify("Clipboard Error", "Could not copy text to clipboard.");
|
|
}
|
|
}
|
|
}
|