using System.Diagnostics; 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; } /// Returns the contents of the OS clipboard if possible. /// The contents of the OS clipboard if successful. /// Thrown if it was not possible to copy from the OS clipboard. public string GetClipboardData () { try { string result = GetClipboardDataImpl (); if (result is null) { return string.Empty; } return GetClipboardDataImpl (); } catch (NotSupportedException ex) { throw new NotSupportedException ("Failed to copy from the OS clipboard.", ex); } } /// Pastes the to the OS clipboard if possible. /// The text to paste to the OS clipboard. /// Thrown if it was not possible to paste to the OS clipboard. public void SetClipboardData (string text) { if (text is null) { throw new ArgumentNullException (nameof (text)); } try { SetClipboardDataImpl (text); } catch (NotSupportedException ex) { throw new NotSupportedException ("Failed to paste to the OS clipboard.", ex); } } /// Copies the contents of the OS clipboard to if possible. /// The contents of the OS clipboard if successful. /// the OS clipboard was retrieved, otherwise. public bool TryGetClipboardData (out string result) { result = string.Empty; // Don't even try to read because environment is not set up. if (!IsSupported) { return false; } try { result = GetClipboardDataImpl (); return true; } catch (NotSupportedException ex) { Debug.WriteLine ($"TryGetClipboardData: {ex.Message}"); return false; } } /// Pastes the to the OS clipboard if possible. /// The text to paste to the OS clipboard. /// the OS clipboard was set, otherwise. 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 (NotSupportedException ex) { Debug.WriteLine ($"TrySetClipboardData: {ex.Message}"); return false; } } /// /// Returns the contents of the OS clipboard if possible. Implemented by -specific /// subclasses. /// /// The contents of the OS clipboard if successful. /// Thrown if it was not possible to copy from the OS clipboard. protected abstract string GetClipboardDataImpl (); /// /// Pastes the to the OS clipboard if possible. Implemented by /// -specific subclasses. /// /// The text to paste to the OS clipboard. /// Thrown if it was not possible to paste to the OS clipboard. protected abstract void SetClipboardDataImpl (string text); }