using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Terminal.Gui {
///
/// Shared abstract class to enforce rules from the implementation of the interface.
///
public abstract class ClipboardBase : IClipboard {
///
/// Returns true if the environmental dependencies are in place to interact with the OS clipboard
///
public abstract bool IsSupported { get; }
///
/// Get the operation system clipboard.
///
/// Thrown if it was not possible to read the clipboard contents
public string GetClipboardData ()
{
try {
return GetClipboardDataImpl ();
} catch (Exception ex) {
throw new NotSupportedException ("Failed to read clipboard.", ex);
}
}
///
/// Get the operation system clipboard.
///
protected abstract string GetClipboardDataImpl ();
///
/// Sets the operation system clipboard.
///
///
/// Thrown if it was not possible to set the clipboard contents
public void SetClipboardData (string text)
{
try {
SetClipboardDataImpl (text);
} catch (Exception ex) {
throw new NotSupportedException ("Failed to write to clipboard.", ex);
}
}
///
/// Sets the operation system clipboard.
///
///
protected abstract void SetClipboardDataImpl (string text);
///
/// Gets the operation system clipboard if possible.
///
/// Clipboard contents read
/// true if it was possible to read the OS clipboard.
public bool TryGetClipboardData (out string result)
{
// Don't even try to read because environment is not set up.
if (!IsSupported) {
result = null;
return false;
}
try {
result = GetClipboardDataImpl ();
while (result == null) {
result = GetClipboardDataImpl ();
}
return true;
} catch (Exception) {
result = null;
return false;
}
}
///
/// Sets the operation system clipboard if possible.
///
///
/// True if the clipboard content was set successfully
public bool TrySetClipboardData (string text)
{
// Don't even try to set because environment is not set up
if (!IsSupported) {
return false;
}
try {
SetClipboardDataImpl (text);
return true;
} catch (Exception) {
return false;
}
}
}
}