-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support for DataTransferManager on WASM
- Loading branch information
1 parent
568f02a
commit 97ccc3f
Showing
16 changed files
with
267 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Uno.UI/ts/Windows/ApplicationModel/DataTransfer/DataTransferManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
interface NavigatorDataTransferManager { | ||
share(data: any): Promise<void>; | ||
} | ||
|
||
interface Navigator extends NavigatorDataTransferManager { } | ||
|
||
namespace Windows.ApplicationModel.DataTransfer { | ||
|
||
export class DataTransferManager { | ||
|
||
public static isSupported(): boolean { | ||
var navigatorAny = navigator as any; | ||
return typeof navigatorAny.share === "function"; | ||
} | ||
|
||
public static async showShareUI(title: string, text: string, url: string): Promise<string> { | ||
var data: any = {}; | ||
if (title) { | ||
data.title = title; | ||
} | ||
if (text) { | ||
data.text = text; | ||
} | ||
if (url) { | ||
data.url = url; | ||
} | ||
|
||
try { | ||
await navigator.share(data); | ||
return "true"; | ||
} | ||
catch { | ||
return "false"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 5 additions & 26 deletions
31
src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,15 @@ | ||
using Windows.Foundation; | ||
#if __WASM__ | ||
using Windows.Foundation; | ||
|
||
namespace Windows.ApplicationModel.DataTransfer | ||
{ | ||
public partial class DataTransferManager | ||
{ | ||
public static bool IsSupported() | ||
private DataTransferManager() | ||
{ | ||
throw new global::System.NotImplementedException("The member bool DataTransferManager.IsSupported() is not implemented in Uno."); | ||
} | ||
|
||
public static void ShowShareUI() | ||
{ | ||
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.ApplicationModel.DataTransfer.DataTransferManager", "void DataTransferManager.ShowShareUI()"); | ||
} | ||
|
||
public static DataTransferManager GetForCurrentView() | ||
{ | ||
throw new global::System.NotImplementedException("The member DataTransferManager DataTransferManager.GetForCurrentView() is not implemented in Uno."); | ||
} | ||
|
||
public event TypedEventHandler<DataTransferManager, DataRequestedEventArgs> DataRequested | ||
{ | ||
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")] | ||
add | ||
{ | ||
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.ApplicationModel.DataTransfer.DataTransferManager", "event TypedEventHandler<DataTransferManager, DataRequestedEventArgs> DataTransferManager.DataRequested"); | ||
} | ||
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")] | ||
remove | ||
{ | ||
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.ApplicationModel.DataTransfer.DataTransferManager", "event TypedEventHandler<DataTransferManager, DataRequestedEventArgs> DataTransferManager.DataRequested"); | ||
} | ||
} | ||
public event TypedEventHandler<DataTransferManager, DataRequestedEventArgs> DataRequested; | ||
} | ||
} | ||
#endif |
12 changes: 12 additions & 0 deletions
12
src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.other.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#if !__WASM__ | ||
|
||
namespace Windows.ApplicationModel.DataTransfer | ||
{ | ||
public partial class DataTransferManager | ||
{ | ||
public static bool IsSupported() => false; | ||
|
||
public static DataTransferManager GetForCurrentView() => throw new NotSupportedException("DataTransferManager is not yet supported on this platform."); | ||
} | ||
} | ||
#endif |
66 changes: 66 additions & 0 deletions
66
src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.wasm.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using Uno.Foundation; | ||
|
||
namespace Windows.ApplicationModel.DataTransfer | ||
{ | ||
public partial class DataTransferManager | ||
{ | ||
private const string JsType = "Windows.ApplicationModel.DataTransfer.DataTransferManager"; | ||
|
||
private static Lazy<DataTransferManager> _instance = new Lazy<DataTransferManager>(() => new DataTransferManager()); | ||
|
||
public static bool IsSupported() => bool.TryParse(WebAssemblyRuntime.InvokeJS($"{JsType}.isSupported()"), out var result) && result; | ||
|
||
public static DataTransferManager GetForCurrentView() => _instance.Value; | ||
|
||
public static async void ShowShareUI() | ||
{ | ||
var dataTransferManager = _instance.Value; | ||
var args = new DataRequestedEventArgs(); | ||
dataTransferManager.DataRequested?.Invoke(dataTransferManager, args); | ||
var dataPackage = args.Request.Data; | ||
var dataPackageView = args.Request.Data.GetView(); | ||
|
||
var title = dataPackage.Properties.Title != null ? $"\"{WebAssemblyRuntime.EscapeJs(dataPackage.Properties.Title)}\"" : null; | ||
|
||
string? text; | ||
if (dataPackageView.Contains(StandardDataFormats.Text)) | ||
{ | ||
text = await dataPackageView.GetTextAsync(); | ||
} | ||
else | ||
{ | ||
text = dataPackage.Properties.Description; | ||
} | ||
text = text != null ? $"\"{WebAssemblyRuntime.EscapeJs(text)}\"" : null; | ||
|
||
Uri? uri = null; | ||
if (dataPackageView.Contains(StandardDataFormats.Uri)) | ||
{ | ||
uri = await dataPackageView.GetUriAsync(); | ||
} | ||
else if (dataPackageView.Contains(StandardDataFormats.WebLink)) | ||
{ | ||
uri = await dataPackageView.GetWebLinkAsync(); | ||
} | ||
else if (dataPackageView.Contains(StandardDataFormats.ApplicationLink)) | ||
{ | ||
uri = await dataPackageView.GetApplicationLinkAsync(); | ||
} | ||
|
||
var uriText = uri != null ? $"\"{WebAssemblyRuntime.EscapeJs(uri.ToString())}\"" : null; | ||
|
||
var result = await WebAssemblyRuntime.InvokeAsync($"{JsType}.showShareUI({title ?? "null"},{text ?? "null"},{uriText ?? "null"})"); | ||
if (bool.TryParse(result, out var boolResult) && boolResult) | ||
{ | ||
dataPackage.OnShareCompleted(); | ||
} | ||
else | ||
{ | ||
dataPackage.OnShareCanceled(); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Uno.UWP/ApplicationModel/DataTransfer/ShareCompletedEventArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Windows.ApplicationModel.DataTransfer | ||
{ | ||
public partial class ShareCompletedEventArgs | ||
{ | ||
internal ShareCompletedEventArgs() | ||
{ | ||
} | ||
|
||
public ShareTargetInfo ShareTarget { get; } = new ShareTargetInfo(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Uno.UWP/ApplicationModel/DataTransfer/ShareTargetInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#nullable enable | ||
|
||
namespace Windows.ApplicationModel.DataTransfer | ||
{ | ||
public partial class ShareTargetInfo | ||
{ | ||
internal ShareTargetInfo() | ||
{ | ||
} | ||
|
||
public string AppUserModelId { get; } = ""; | ||
|
||
public ShareProvider? ShareProvider { get; } = null; | ||
} | ||
} |
Oops, something went wrong.