Skip to content

A Swiss Army knife for developers.

Updated Clipboard API

Etienne Baudoux7f38b8410d20
diff --git a/src/app/dev/DevToys.Api/Core/IClipboard.cs b/src/app/dev/DevToys.Api/Core/IClipboard.cs
index 046d60a07..ad2d804bd 100644
--- a/src/app/dev/DevToys.Api/Core/IClipboard.cs
+++ b/src/app/dev/DevToys.Api/Core/IClipboard.cs
@@ -14,6 +14,33 @@ public interface IClipboard
/// <returns>The data currently stored in the system clipboard.</returns>
Task<object?> GetClipboardDataAsync();
+ /// <summary>
+ /// Retrieves text from the system clipboard.
+ /// </summary>
+ /// <remarks>
+ /// This method may try to access to the UI thread.
+ /// </remarks>
+ /// <returns>The text currently stored in the system clipboard, or null if nothing is present.</returns>
+ Task<string?> GetClipboardTextAsync();
+
+ /// <summary>
+ /// Retrieves files from the system clipboard.
+ /// </summary>
+ /// <remarks>
+ /// This method may try to access to the UI thread.
+ /// </remarks>
+ /// <returns>The files currently stored in the system clipboard, or null if nothing is present.</returns>
+ Task<FileInfo[]?> GetClipboardFilesAsync();
+
+ /// <summary>
+ /// Retrieves bitmap from the system clipboard.
+ /// </summary>
+ /// <remarks>
+ /// This method may try to access to the UI thread.
+ /// </remarks>
+ /// <returns>The bitmap currently stored in the system clipboard, or null if nothing is present.</returns>
+ Task<string?> GetClipboardBitmapAsync();
+
/// <summary>
/// Sets text to the system clipboard.
/// </summary>
@@ -29,8 +56,8 @@ public interface IClipboard
/// <remarks>
/// This method may try to access to the UI thread.
/// </remarks>
- /// <param name="data">The data to be stored in the system clipboard.</param>
- Task SetClipboardFilesAsync(string? data);
+ /// <param name="filePaths">The list of files to be stored in the system clipboard.</param>
+ Task SetClipboardFilesAsync(FileInfo[]? filePaths);
/// <summary>
/// Sets bitmap to the system clipboard.
diff --git a/src/app/dev/DevToys.Blazor/Components/Text/TextBox/TextBox.razor.cs b/src/app/dev/DevToys.Blazor/Components/Text/TextBox/TextBox.razor.cs
index 9f0a4ccc9..f8cd83d30 100644
--- a/src/app/dev/DevToys.Blazor/Components/Text/TextBox/TextBox.razor.cs
+++ b/src/app/dev/DevToys.Blazor/Components/Text/TextBox/TextBox.razor.cs
@@ -240,8 +240,8 @@ public partial class TextBox : MefComponentBase, IFocusable
private async Task OnPasteAsync(DropDownListItem _)
{
- object? clipboardData = await Clipboard.GetClipboardDataAsync();
- if (clipboardData is string clipboardDataString)
+ string? clipboardDataString = await Clipboard.GetClipboardTextAsync();
+ if (clipboardDataString is not null)
{
TextSpan selection = await GetSelectionAsync();
Text ??= string.Empty;
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UITextInputWrapper.razor.cs b/src/app/dev/DevToys.Blazor/Components/UIElements/UITextInputWrapper.razor.cs
index 15f01bf0e..67205fcd4 100644
--- a/src/app/dev/DevToys.Blazor/Components/UIElements/UITextInputWrapper.razor.cs
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UITextInputWrapper.razor.cs
@@ -164,8 +164,8 @@ public partial class UITextInputWrapper : MefComponentBase
private async Task OnPasteButtonClickAsync()
{
- object? clipboardContent = await Clipboard.GetClipboardDataAsync();
- if (clipboardContent is string clipboardString)
+ string? clipboardString = await Clipboard.GetClipboardTextAsync();
+ if (clipboardString is not null)
{
if (SettingsProvider.GetSetting(PredefinedSettings.TextEditorPasteClearsText))
{
diff --git a/src/app/dev/platforms/desktop/DevToys.MacOS/Core/Clipboard.cs b/src/app/dev/platforms/desktop/DevToys.MacOS/Core/Clipboard.cs
index cf430974c..f1e26883c 100644
--- a/src/app/dev/platforms/desktop/DevToys.MacOS/Core/Clipboard.cs
+++ b/src/app/dev/platforms/desktop/DevToys.MacOS/Core/Clipboard.cs
@@ -34,13 +34,31 @@ internal sealed partial class Clipboard : Api.IClipboard
return null;
}
+ public Task<string?> GetClipboardTextAsync()
+ {
+ // TODO
+ throw new NotImplementedException();
+ }
+
+ public Task<FileInfo[]?> GetClipboardFilesAsync()
+ {
+ // TODO
+ throw new NotImplementedException();
+ }
+
+ public Task<string?> GetClipboardBitmapAsync()
+ {
+ // TODO
+ throw new NotImplementedException();
+ }
+
public Task SetClipboardBitmapAsync(string? data)
{
// TODO
throw new NotImplementedException();
}
- public Task SetClipboardFilesAsync(string? data)
+ public Task SetClipboardFilesAsync(FileInfo[]? data)
{
// TODO
throw new NotImplementedException();
@@ -52,6 +70,9 @@ internal sealed partial class Clipboard : Api.IClipboard
throw new NotImplementedException();
}
- [LoggerMessage(1, LogLevel.Warning, "Failed to retrieve the clipboard data.")]
+ [LoggerMessage(0, LogLevel.Warning, "Failed to retrieve the clipboard data.")]
partial void LogGetClipboardFailed(Exception ex);
+
+ [LoggerMessage(1, LogLevel.Error, "Failed to set the clipboard text.")]
+ partial void LogSetClipboardTextFailed(Exception ex);
}
diff --git a/src/app/dev/platforms/desktop/DevToys.Windows/Core/Clipboard.cs b/src/app/dev/platforms/desktop/DevToys.Windows/Core/Clipboard.cs
index a309fb8d5..1ee2db754 100644
--- a/src/app/dev/platforms/desktop/DevToys.Windows/Core/Clipboard.cs
+++ b/src/app/dev/platforms/desktop/DevToys.Windows/Core/Clipboard.cs
@@ -1,4 +1,5 @@
-using System.IO;
+using System.Collections.Specialized;
+using System.IO;
using System.Windows.Threading;
using DevToys.Windows.Helpers;
using Microsoft.Extensions.Logging;
@@ -39,7 +40,7 @@ internal sealed partial class Clipboard : Api.IClipboard
files.Add(new FileInfo(filePath));
}
}
- return files;
+ return files.ToArray();
}
else if (System.Windows.Clipboard.ContainsImage())
{
@@ -56,18 +57,95 @@ internal sealed partial class Clipboard : Api.IClipboard
});
}
- public Task SetClipboardBitmapAsync(string? data)
+ public Task<string?> GetClipboardTextAsync()
+ {
+ return ThreadHelper.RunOnUIThreadAsync<string?>(
+ DispatcherPriority.Background,
+ () =>
+ {
+ try
+ {
+ if (System.Windows.Clipboard.ContainsText())
+ {
+ return System.Windows.Clipboard.GetText();
+ }
+ }
+ catch (Exception ex)
+ {
+ LogGetClipboardFailed(ex);
+ }
+
+ return null;
+ });
+ }
+
+ public Task<FileInfo[]?> GetClipboardFilesAsync()
+ {
+ return ThreadHelper.RunOnUIThreadAsync<FileInfo[]?>(
+ DispatcherPriority.Background,
+ () =>
+ {
+ try
+ {
+ if (System.Windows.Clipboard.ContainsFileDropList())
+ {
+ var files = new List<FileInfo>();
+ foreach (string? filePath in System.Windows.Clipboard.GetFileDropList())
+ {
+ if (!string.IsNullOrEmpty(filePath))
+ {
+ files.Add(new FileInfo(filePath));
+ }
+ }
+ return files.ToArray();
+ }
+ }
+ catch (Exception ex)
+ {
+ LogGetClipboardFailed(ex);
+ }
+
+ return null;
+ });
+ }
+
+ public Task<string?> GetClipboardBitmapAsync()
{
// TODO
throw new NotImplementedException();
}
- public Task SetClipboardFilesAsync(string? data)
+ public Task SetClipboardBitmapAsync(string? data)
{
// TODO
throw new NotImplementedException();
}
+ public Task SetClipboardFilesAsync(FileInfo[]? data)
+ {
+ return ThreadHelper.RunOnUIThreadAsync(
+ DispatcherPriority.Background,
+ () =>
+ {
+ try
+ {
+ var fileList = new StringCollection();
+ if (data is not null)
+ {
+ for (int i = 0; i < data.Length; i++)
+ {
+ fileList.Add(data[i].FullName);
+ }
+ }
+ System.Windows.Clipboard.SetFileDropList(fileList);
+ }
+ catch (Exception ex)
+ {
+ LogSetClipboardTextFailed(ex);
+ }
+ });
+ }
+
public Task SetClipboardTextAsync(string? data)
{
return ThreadHelper.RunOnUIThreadAsync(