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; } /// /// 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 { return GetClipboardDataImpl (); } catch (NotSupportedException ex) { throw new NotSupportedException ("Failed to copy from the OS clipboard.", ex); } } /// /// 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. /// /// 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) { try { SetClipboardDataImpl (text); } catch (NotSupportedException ex) { throw new NotSupportedException ("Failed to paste to the OS clipboard.", ex); } } /// /// 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); /// /// Copies the contents of the OS clipboard to if possible. /// /// The contents of the OS clipboard if successful, if not. /// the OS clipboard was retrieved, otherwise. 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 (NotSupportedException ex) { System.Diagnostics.Debug.WriteLine ($"TryGetClipboardData: {ex.Message}"); result = null; 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) { System.Diagnostics.Debug.WriteLine ($"TrySetClipboardData: {ex.Message}"); return false; } } } }