Skip to content

Commit

Permalink
perf: Add DataTransferManager bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ebariche committed May 20, 2023
1 parent b9571c7 commit 3769dae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ internal partial class DataTransferManager
{
internal static partial class NativeMethods
{
private const string JsType = "globalThis.Windows.ApplicationModel.DataTransfer.DataTransferManager";

[JSImport($"{JsType}.isSupported")]
internal static partial bool IsSupported();

[JSImport($"{JsType}.showShareUI")]
internal static partial Task<string> ShowShareUIAsync(string title, string text, string uri);
}
}
}
#endif
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ namespace Windows.ApplicationModel.DataTransfer
{
public partial class DataTransferManager
{
#if !NET7_0_OR_GREATER
private const string JsType = "Windows.ApplicationModel.DataTransfer.DataTransferManager";
#endif

public static bool IsSupported() => bool.TryParse(WebAssemblyRuntime.InvokeJS($"{JsType}.isSupported()"), out var result) && result;
public static bool IsSupported() =>
#if NET7_0_OR_GREATER
NativeMethods.IsSupported();
#else
bool.TryParse(WebAssemblyRuntime.InvokeJS($"{JsType}.isSupported()"), out var result) && result;
#endif

private static async Task<bool> ShowShareUIAsync(ShareUIOptions options, DataPackage dataPackage)
{
var dataPackageView = dataPackage.GetView();

var title = dataPackage.Properties.Title != null ? $"\"{WebAssemblyRuntime.EscapeJs(dataPackage.Properties.Title)}\"" : null;

string? text;
if (dataPackageView.Contains(StandardDataFormats.Text))
{
Expand All @@ -31,13 +36,19 @@ private static async Task<bool> ShowShareUIAsync(ShareUIOptions options, DataPac
{
text = dataPackage.Properties.Description;
}
text = text != null ? $"\"{WebAssemblyRuntime.EscapeJs(text)}\"" : null;

var uri = await GetSharedUriAsync(dataPackageView);

#if NET7_0_OR_GREATER
var result = await NativeMethods.ShowShareUIAsync(dataPackage.Properties.Title, text, uri?.OriginalString);
#else
var title = dataPackage.Properties.Title != null ? $"\"{WebAssemblyRuntime.EscapeJs(dataPackage.Properties.Title)}\"" : null;
text = text != null ? $"\"{WebAssemblyRuntime.EscapeJs(text)}\"" : null;
var uriText = uri != null ? $"\"{WebAssemblyRuntime.EscapeJs(uri.OriginalString)}\"" : null;

var result = await WebAssemblyRuntime.InvokeAsync($"{JsType}.showShareUI({title ?? "null"},{text ?? "null"},{uriText ?? "null"})");
#endif

return bool.TryParse(result, out var boolResult) && boolResult;
}
}
Expand Down

0 comments on commit 3769dae

Please sign in to comment.