using NStack;
using System;
namespace Terminal.Gui {
///
/// Provides cut, copy, and paste support for the clipboard with OS interaction.
///
public static class Clipboard {
static ustring contents;
///
/// Get or sets the operation system clipboard, otherwise the contents field.
///
public static ustring Contents {
get {
try {
if (IsSupported) {
return Application.Driver.Clipboard.GetClipboardData ();
} else {
return contents;
}
} catch (Exception) {
return contents;
}
}
set {
try {
if (IsSupported && value != null) {
Application.Driver.Clipboard.SetClipboardData (value.ToString ());
}
contents = value;
} catch (Exception) {
contents = value;
}
}
}
///
/// Returns true if the environmental dependencies are in place to interact with the OS clipboard.
///
public static bool IsSupported { get; } = Application.Driver.Clipboard.IsSupported;
///
/// Gets the operation system clipboard if possible.
///
/// Clipboard contents read
/// true if it was possible to read the OS clipboard.
public static bool TryGetClipboardData (out string result)
{
if (Application.Driver.Clipboard.TryGetClipboardData (out result)) {
if (contents != result) {
contents = result;
}
return true;
}
return false;
}
///
/// Sets the operation system clipboard if possible.
///
///
/// True if the clipboard content was set successfully.
public static bool TrySetClipboardData (string text)
{
if (Application.Driver.Clipboard.TrySetClipboardData (text)) {
contents = text;
return true;
}
return false;
}
}
}