2020-05-06 19:17:25 -04:00
|
|
|
|
using ElectronNET.API.Entities;
|
2017-10-23 19:08:10 +02:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
2021-08-26 14:22:54 +02:00
|
|
|
|
using System.Runtime.Versioning;
|
2017-10-23 19:08:10 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace ElectronNET.API
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Perform copy and paste operations on the system clipboard.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public sealed class Clipboard
|
|
|
|
|
|
{
|
|
|
|
|
|
private static Clipboard _clipboard;
|
2021-09-15 11:14:45 +02:00
|
|
|
|
private static readonly object _syncRoot = new();
|
2017-10-23 19:08:10 +02:00
|
|
|
|
|
|
|
|
|
|
internal Clipboard() { }
|
|
|
|
|
|
|
|
|
|
|
|
internal static Clipboard Instance
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_clipboard == null)
|
|
|
|
|
|
{
|
2017-11-04 00:16:14 +01:00
|
|
|
|
lock (_syncRoot)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_clipboard == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_clipboard = new Clipboard();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-10-23 19:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _clipboard;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Read the content in the clipboard as plain text.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
/// <returns>The content in the clipboard as plain text.</returns>
|
2021-08-23 11:22:37 +02:00
|
|
|
|
public Task<string> ReadTextAsync(string type = "") => BridgeConnector.OnResult<string>("clipboard-readText", "clipboard-readText-Completed", type);
|
2017-10-23 19:08:10 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Writes the text into the clipboard as plain text.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="text"></param>
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
public void WriteText(string text, string type = "")
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("clipboard-writeText", text, type);
|
2017-10-23 19:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The content in the clipboard as markup.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2021-08-23 11:22:37 +02:00
|
|
|
|
public Task<string> ReadHTMLAsync(string type = "") => BridgeConnector.OnResult<string>("clipboard-readHTML", "clipboard-readHTML-Completed", type);
|
2017-10-23 19:08:10 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Writes markup to the clipboard.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="markup"></param>
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
public void WriteHTML(string markup, string type = "")
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("clipboard-writeHTML", markup, type);
|
2017-10-23 19:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The content in the clipboard as RTF.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2021-08-23 11:22:37 +02:00
|
|
|
|
public Task<string> ReadRTFAsync(string type = "") => BridgeConnector.OnResult<string>("clipboard-readRTF", "clipboard-readRTF-Completed", type);
|
2017-10-23 19:08:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Writes the text into the clipboard in RTF.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="text"></param>
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
public void WriteRTF(string text, string type = "")
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("clipboard-writeHTML", text, type);
|
2017-10-23 19:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 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.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2021-08-26 14:22:54 +02:00
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
|
|
|
|
[SupportedOSPlatform("macos")]
|
2021-08-23 11:22:37 +02:00
|
|
|
|
public Task<ReadBookmark> ReadBookmarkAsync() => BridgeConnector.OnResult<ReadBookmark>("clipboard-readBookmark", "clipboard-readBookmark-Completed");
|
2017-10-23 19:08:10 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 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.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="title"></param>
|
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
|
/// <param name="type"></param>
|
2021-08-26 14:22:54 +02:00
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
|
|
|
|
[SupportedOSPlatform("macos")]
|
2017-10-23 19:08:10 +02:00
|
|
|
|
public void WriteBookmark(string title, string url, string type = "")
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("clipboard-writeBookmark", title, url, type);
|
2017-10-23 19:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 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.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2021-08-26 14:22:54 +02:00
|
|
|
|
[SupportedOSPlatform("macos")]
|
2021-08-23 11:22:37 +02:00
|
|
|
|
public Task<string> ReadFindTextAsync() => BridgeConnector.OnResult<string>("clipboard-readFindText", "clipboard-readFindText-Completed");
|
2017-10-23 19:08:10 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// macOS: Writes the text into the find pasteboard as plain text. This method uses
|
|
|
|
|
|
/// synchronous IPC when called from the renderer process.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="text"></param>
|
2021-08-26 14:22:54 +02:00
|
|
|
|
[SupportedOSPlatform("macos")]
|
2017-10-23 19:08:10 +02:00
|
|
|
|
public void WriteFindText(string text)
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("clipboard-writeFindText", text);
|
2017-10-23 19:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Clears the clipboard content.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
public void Clear(string type = "")
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("clipboard-clear", type);
|
2017-10-23 19:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// An array of supported formats for the clipboard type.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2021-08-23 11:22:37 +02:00
|
|
|
|
public Task<string[]> AvailableFormatsAsync(string type = "") => BridgeConnector.OnResult<string[]>("clipboard-availableFormats", "clipboard-availableFormats-Completed", type);
|
2017-10-23 19:08:10 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Writes data to the clipboard.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
public void Write(Data data, string type = "")
|
|
|
|
|
|
{
|
2021-08-26 16:08:11 +02:00
|
|
|
|
BridgeConnector.Emit("clipboard-write", JObject.FromObject(data, _jsonSerializer), type);
|
2017-10-23 19:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-26 19:21:29 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Reads an image from the clipboard.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2021-08-23 11:22:37 +02:00
|
|
|
|
public Task<NativeImage> ReadImageAsync(string type = "") => BridgeConnector.OnResult<NativeImage>("clipboard-readImage", "clipboard-readImage-Completed", type);
|
2020-04-26 19:21:29 -04:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Writes an image to the clipboard.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="image"></param>
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
public void WriteImage(NativeImage image, string type = "")
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("clipboard-writeImage", JsonConvert.SerializeObject(image), type);
|
2020-04-26 19:21:29 -04:00
|
|
|
|
}
|
2021-08-26 16:08:11 +02:00
|
|
|
|
|
2021-09-15 11:14:45 +02:00
|
|
|
|
private static readonly JsonSerializer _jsonSerializer = new()
|
2021-08-26 16:08:11 +02:00
|
|
|
|
{
|
|
|
|
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
|
|
|
|
|
NullValueHandling = NullValueHandling.Ignore,
|
|
|
|
|
|
DefaultValueHandling = DefaultValueHandling.Ignore
|
|
|
|
|
|
};
|
2017-10-23 19:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|