Remotely/Desktop.Linux/Services/ClipboardServiceLinux.cs
2021-07-29 07:56:44 -07:00

79 lines
2.2 KiB
C#

using Remotely.Desktop.Core.Interfaces;
using Remotely.Shared.Utilities;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Remotely.Desktop.Linux.Services
{
public class ClipboardServiceLinux : IClipboardService
{
private CancellationTokenSource cancelTokenSource;
public event EventHandler<string> ClipboardTextChanged;
private string ClipboardText { get; set; }
public void BeginWatching()
{
try
{
StopWatching();
}
finally
{
cancelTokenSource = new CancellationTokenSource();
_ = Task.Run(() => WatchClipboard(cancelTokenSource.Token));
}
}
public async Task SetText(string clipboardText)
{
try
{
if (string.IsNullOrWhiteSpace(clipboardText))
{
await App.Current.Clipboard.ClearAsync();
}
else
{
await App.Current.Clipboard.SetTextAsync(clipboardText);
}
}
catch (Exception ex)
{
Logger.Write(ex);
}
}
public void StopWatching()
{
cancelTokenSource?.Cancel();
cancelTokenSource?.Dispose();
}
private void WatchClipboard(CancellationToken cancelToken)
{
while (!cancelToken.IsCancellationRequested &&
!Environment.HasShutdownStarted)
{
try
{
var currentText = EnvironmentHelper.StartProcessWithResults("xclip", "-o");
// TODO: Switch back when fixed.
//var currentText = await App.Current.Clipboard.GetTextAsync();
if (!string.IsNullOrEmpty(currentText) && currentText != ClipboardText)
{
ClipboardText = currentText;
ClipboardTextChanged?.Invoke(this, ClipboardText);
}
}
finally
{
Thread.Sleep(500);
}
}
}
}
}