Skip to content

Commit

Permalink
fix: Make Clipboard.GetContentAsync reliable
Browse files Browse the repository at this point in the history
Previously we were executing the GetClipboardText method on UI thread which was unnecessary, and as it was executed asynchronously, it did not necessarily wait for the actual promise on JS side to finish executing. Therefore in most cases just the initial empty string content was returned.
  • Loading branch information
MartinZikmund committed Jun 6, 2022
1 parent 7fe39e5 commit 4762bb2
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/Uno.UWP/ApplicationModel/DataTransfer/Clipboard.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Windows.UI.Core;
using Uno.Extensions.Specialized;
using Uno.Foundation;
using System.Threading;

namespace Windows.ApplicationModel.DataTransfer
{
Expand Down Expand Up @@ -36,25 +37,15 @@ public static DataPackageView GetContent()
{
var dataPackage = new DataPackage();

dataPackage.SetDataProvider(
StandardDataFormats.Text,
async ct =>
{
var text = string.Empty;
await Uno.UI.Dispatching.CoreDispatcher.Main.RunAsync(
Uno.UI.Dispatching.CoreDispatcherPriority.High,
async _ => text = await GetClipboardText());

return text;
});
dataPackage.SetDataProvider(StandardDataFormats.Text, async ct => await GetClipboardText(ct));

return dataPackage.GetView();
}

private static async Task<string> GetClipboardText()
private static async Task<string> GetClipboardText(CancellationToken ct)
{
var command = $"{JsType}.getText();";
var text = await WebAssemblyRuntime.InvokeAsync(command);
var text = await WebAssemblyRuntime.InvokeAsync(command, ct);

return text;
}
Expand Down

0 comments on commit 4762bb2

Please sign in to comment.