using ElectronNET.API.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Threading.Tasks;
namespace ElectronNET.API
{
///
/// Perform copy and paste operations on the system clipboard.
///
public sealed class Clipboard
{
private static Clipboard _clipboard;
private static object _syncRoot = new object();
internal Clipboard() { }
internal static Clipboard Instance
{
get
{
if (_clipboard == null)
{
lock (_syncRoot)
{
if (_clipboard == null)
{
_clipboard = new Clipboard();
}
}
}
return _clipboard;
}
}
///
/// Read the content in the clipboard as plain text.
///
///
/// The content in the clipboard as plain text.
public Task ReadTextAsync(string type = "")
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("clipboard-readText-Completed", (text) =>
{
BridgeConnector.Socket.Off("clipboard-readText-Completed");
taskCompletionSource.SetResult(text.ToString());
});
BridgeConnector.Socket.Emit("clipboard-readText", type);
return taskCompletionSource.Task;
}
///
/// Writes the text into the clipboard as plain text.
///
///
///
public void WriteText(string text, string type = "")
{
BridgeConnector.Socket.Emit("clipboard-writeText", text, type);
}
///
/// The content in the clipboard as markup.
///
///
///
public Task ReadHTMLAsync(string type = "")
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("clipboard-readHTML-Completed", (text) =>
{
BridgeConnector.Socket.Off("clipboard-readHTML-Completed");
taskCompletionSource.SetResult(text.ToString());
});
BridgeConnector.Socket.Emit("clipboard-readHTML", type);
return taskCompletionSource.Task;
}
///
/// Writes markup to the clipboard.
///
///
///
public void WriteHTML(string markup, string type = "")
{
BridgeConnector.Socket.Emit("clipboard-writeHTML", markup, type);
}
///
/// The content in the clipboard as RTF.
///
///
///
public Task ReadRTFAsync(string type = "")
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("clipboard-readRTF-Completed", (text) =>
{
BridgeConnector.Socket.Off("clipboard-readRTF-Completed");
taskCompletionSource.SetResult(text.ToString());
});
BridgeConnector.Socket.Emit("clipboard-readRTF", type);
return taskCompletionSource.Task;
}
///
/// Writes the text into the clipboard in RTF.
///
///
///
public void WriteRTF(string text, string type = "")
{
BridgeConnector.Socket.Emit("clipboard-writeHTML", text, type);
}
///
/// Returns an Object containing title and url keys representing
/// the bookmark in the clipboard. The title and url values will
/// be empty strings when the bookmark is unavailable.
///
///
public Task ReadBookmarkAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("clipboard-readBookmark-Completed", (bookmark) =>
{
BridgeConnector.Socket.Off("clipboard-readBookmark-Completed");
taskCompletionSource.SetResult(((JObject)bookmark).ToObject());
});
BridgeConnector.Socket.Emit("clipboard-readBookmark");
return taskCompletionSource.Task;
}
///
/// Writes the title and url into the clipboard as a bookmark.
///
/// Note: Most apps on Windows don’t support pasting bookmarks
/// into them so you can use clipboard.write to write both a
/// bookmark and fallback text to the clipboard.
///
///
///
///
public void WriteBookmark(string title, string url, string type = "")
{
BridgeConnector.Socket.Emit("clipboard-writeBookmark", title, url, type);
}
///
/// macOS: The text on the find pasteboard. This method uses synchronous IPC
/// when called from the renderer process. The cached value is reread from the
/// find pasteboard whenever the application is activated.
///
///
public Task ReadFindTextAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("clipboard-readFindText-Completed", (text) =>
{
BridgeConnector.Socket.Off("clipboard-readFindText-Completed");
taskCompletionSource.SetResult(text.ToString());
});
BridgeConnector.Socket.Emit("clipboard-readFindText");
return taskCompletionSource.Task;
}
///
/// macOS: Writes the text into the find pasteboard as plain text. This method uses
/// synchronous IPC when called from the renderer process.
///
///
public void WriteFindText(string text)
{
BridgeConnector.Socket.Emit("clipboard-writeFindText", text);
}
///
/// Clears the clipboard content.
///
///
public void Clear(string type = "")
{
BridgeConnector.Socket.Emit("clipboard-clear", type);
}
///
/// An array of supported formats for the clipboard type.
///
///
///
public Task AvailableFormatsAsync(string type = "")
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("clipboard-availableFormats-Completed", (formats) =>
{
BridgeConnector.Socket.Off("clipboard-availableFormats-Completed");
taskCompletionSource.SetResult(((JArray)formats).ToObject());
});
BridgeConnector.Socket.Emit("clipboard-availableFormats", type);
return taskCompletionSource.Task;
}
///
/// Writes data to the clipboard.
///
///
///
public void Write(Data data, string type = "")
{
BridgeConnector.Socket.Emit("clipboard-write", JObject.FromObject(data, _jsonSerializer), type);
}
private JsonSerializer _jsonSerializer = new JsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
};
}
}