Skip to content

Commit

Permalink
chore: Updated WASM implementation to use new InvokeAsync feature
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed May 19, 2020
1 parent d5d94fc commit f41f7da
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 83 deletions.
28 changes: 3 additions & 25 deletions src/Uno.UI.Wasm/WasmScripts/Uno.UI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ declare namespace Uno.Utils {
static startContentChanged(): void;
static stopContentChanged(): void;
static setText(text: string): string;
static getText(requestId: string): void;
static getText(): Promise<string>;
private static onClipboardChanged;
}
}
Expand Down Expand Up @@ -677,14 +677,13 @@ declare class WindowManagerSetContentHtmlParams {
static unmarshal(pData: number): WindowManagerSetContentHtmlParams;
}
declare class WindowManagerSetElementTransformParams {
HtmlId: number;
M11: number;
M12: number;
M21: number;
M22: number;
M31: number;
M32: number;
ClipToBounds: boolean;
HtmlId: number;
static unmarshal(pData: number): WindowManagerSetElementTransformParams;
}
declare class WindowManagerSetNameParams {
Expand Down Expand Up @@ -737,7 +736,7 @@ declare namespace Uno.UI.Interop {
private static dispatchResultMethod;
private static dispatchErrorMethod;
private static init;
static Invoke(h: number, m: () => Promise<string>): void;
static Invoke(handle: number, promiseFunction: () => Promise<string>): void;
}
}
declare module Uno.UI {
Expand Down Expand Up @@ -954,24 +953,3 @@ declare namespace Windows.Phone.Devices.Notification {
static vibrate(duration: number): boolean;
}
}
declare namespace Windows.UI.Xaml.Media.Animation {
class RenderingLoopFloatAnimator {
private managedHandle;
private static activeInstances;
static createInstance(managedHandle: string, jsHandle: number): void;
static getInstance(jsHandle: number): RenderingLoopFloatAnimator;
static destroyInstance(jsHandle: number): void;
private constructor();
SetStartFrameDelay(delay: number): void;
SetAnimationFramesInterval(): void;
EnableFrameReporting(): void;
DisableFrameReporting(): void;
private onFrame;
private unscheduleFrame;
private scheduleDelayedFrame;
private scheduleAnimationFrame;
private _delayRequestId?;
private _frameRequestId?;
private _isEnabled;
}
}
59 changes: 48 additions & 11 deletions src/Uno.UI.Wasm/WasmScripts/Uno.UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,17 @@ var Uno;
}
return "ok";
}
static getText(requestId) {
if (!Clipboard.dispatchGetContent) {
Clipboard.dispatchGetContent = Module.mono_bind_static_method("[Uno] Windows.ApplicationModel.DataTransfer.Clipboard:DispatchGetContent");
}
var nav = navigator;
static getText() {
const nav = navigator;
if (nav.clipboard) {
var promise = nav.clipboard.readText();
promise.then((clipText) => Clipboard.dispatchGetContent(requestId, clipText), (_) => Clipboard.dispatchGetContent(requestId, null));
}
else {
Clipboard.dispatchGetContent(requestId, null);
return nav.clipboard.readText();
}
return Promise.resolve(null);
}
static onClipboardChanged() {
if (!Clipboard.dispatchContentChanged) {
Clipboard.dispatchContentChanged = Module.mono_bind_static_method("[Uno] Windows.ApplicationModel.DataTransfer.Clipboard:DispatchContentChanged");
Clipboard.dispatchContentChanged =
Module.mono_bind_static_method("[Uno] Windows.ApplicationModel.DataTransfer.Clipboard:DispatchContentChanged");
}
Clipboard.dispatchContentChanged();
}
Expand Down Expand Up @@ -2437,6 +2432,48 @@ PointerEvent.prototype.isOverDeep = function (element) {
}
};
var Uno;
(function (Uno) {
var UI;
(function (UI) {
var Interop;
(function (Interop) {
class AsyncInteropHelper {
static init() {
if (AsyncInteropHelper.dispatchErrorMethod) {
return; // already initialized
}
const w = window;
AsyncInteropHelper.dispatchResultMethod =
w.Module.mono_bind_static_method("[Uno.Foundation] Uno.Foundation.WebAssemblyRuntime:DispatchAsyncResult");
AsyncInteropHelper.dispatchErrorMethod =
w.Module.mono_bind_static_method("[Uno.Foundation] Uno.Foundation.WebAssemblyRuntime:DispatchAsyncError");
}
static Invoke(handle, promiseFunction) {
AsyncInteropHelper.init();
try {
promiseFunction()
.then(str => {
if (typeof str == "string") {
AsyncInteropHelper.dispatchResultMethod(handle, str);
}
else {
AsyncInteropHelper.dispatchResultMethod(handle, null);
}
})
.catch(err => {
AsyncInteropHelper.dispatchErrorMethod(handle, err);
});
}
catch (err) {
AsyncInteropHelper.dispatchErrorMethod(handle, err);
}
}
}
Interop.AsyncInteropHelper = AsyncInteropHelper;
})(Interop = UI.Interop || (UI.Interop = {}));
})(UI = Uno.UI || (Uno.UI = {}));
})(Uno || (Uno = {}));
var Uno;
(function (Uno) {
var Foundation;
(function (Foundation) {
Expand Down
16 changes: 3 additions & 13 deletions src/Uno.UI.Wasm/ts/Clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,12 @@ namespace Uno.Utils {
return "ok";
}

public static getText(requestId: string): void {
if (!Clipboard.dispatchGetContent) {
Clipboard.dispatchGetContent =
(<any>Module).mono_bind_static_method(
"[Uno] Windows.ApplicationModel.DataTransfer.Clipboard:DispatchGetContent");
}

public static getText(): Promise<string> {
const nav = navigator as NavigatorClipboard;
if (nav.clipboard) {
const promise = nav.clipboard.readText();
promise.then(
(clipText : string) => Clipboard.dispatchGetContent(requestId, clipText),
(_ : any) => Clipboard.dispatchGetContent(requestId, null));
} else {
Clipboard.dispatchGetContent(requestId, null);
return nav.clipboard.readText();
}
return Promise.resolve(null)
}

private static onClipboardChanged() {
Expand Down
1 change: 0 additions & 1 deletion src/Uno.UI/LinkerDefinition.Wasm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<assembly fullname="Uno">
<type fullname="Windows.ApplicationModel.DataTransfer.Clipboard">
<method name="DispatchContentChanged" />
<method name="DispatchGetContent" />
</type>
<type fullname="Windows.UI.Core.CoreDispatcher" />
<type fullname="Windows.UI.Core.SystemNavigationManager" />
Expand Down
24 changes: 4 additions & 20 deletions src/Uno.UWP/ApplicationModel/DataTransfer/Clipboard.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ public static partial class Clipboard
{
private const string JsType = "Uno.Utils.Clipboard";

private static Dictionary<string, TaskCompletionSource<string>> _getTextRequests =
new Dictionary<string, TaskCompletionSource<string>>();

public static void Clear() => SetClipboardText(string.Empty);

public static void SetContent(DataPackage content)
Expand All @@ -26,22 +23,17 @@ public static DataPackageView GetContent()
{
var dataPackageView = new DataPackageView();

// get clipboard text
var getTextRequestId = Guid.NewGuid().ToString();
var getTextCompletionSource = new TaskCompletionSource<string>();
dataPackageView.SetFormatTask(StandardDataFormats.Text, getTextCompletionSource.Task);
_getTextRequests.Add(getTextRequestId, getTextCompletionSource);
var command = $"{JsType}.getText(\"{getTextRequestId}\");";
WebAssemblyRuntime.InvokeJS(command);

var command = $"{JsType}.getText()";
var getTextTask = WebAssemblyRuntime.InvokeAsync(command);
dataPackageView.SetFormatTask(StandardDataFormats.Text, getTextTask);

return dataPackageView;
}

private static void SetClipboardText(string text)
{
var escapedText = WebAssemblyRuntime.EscapeJs(text);
var command = $"{JsType}.setText(\"{escapedText}\");";

WebAssemblyRuntime.InvokeJS(command);
}

Expand All @@ -62,13 +54,5 @@ public static int DispatchContentChanged()
OnContentChanged();
return 0;
}

public static int DispatchGetContent(string requestId, string content)
{
// If user didn't grant permission, content will be null.
// in such case, we just return empty string.
_getTextRequests[requestId].SetResult(content ?? "");
return 0;
}
}
}
13 changes: 0 additions & 13 deletions src/Uno.UWP/ApplicationModel/DataTransfer/DataPackage.wasm.cs

This file was deleted.

0 comments on commit f41f7da

Please sign in to comment.