Skip to content

Commit

Permalink
fix(wasm): key tracking when no one is subscribing to Key<Down|Up>
Browse files Browse the repository at this point in the history
  • Loading branch information
ramezgerges committed Nov 5, 2024
1 parent 9ccb7bf commit f2f751b
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Uno.UI/ts/WindowManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ namespace Uno.UI {
private static dispatchSuspendingMethod: any;
private static getDependencyPropertyValueMethod: any;
private static setDependencyPropertyValueMethod: any;
private static keyTrackingMethod: any;

private constructor(private containerElementId: string, private loadingElementId: string) {
this.initDom();
Expand Down Expand Up @@ -1437,6 +1438,7 @@ namespace Uno.UI {
WindowManager.dispatchEventMethod = exports.Microsoft.UI.Xaml.UIElement.DispatchEvent;
WindowManager.focusInMethod = exports.Microsoft.UI.Xaml.Input.FocusManager.ReceiveFocusNative;
WindowManager.dispatchSuspendingMethod = exports.Microsoft.UI.Xaml.Application.DispatchSuspending;
WindowManager.keyTrackingMethod = (<any>globalThis).DotnetExports.Uno.Uno.UI.Core.KeyboardStateTracker.UpdateKeyStateNative;
} else {
throw `Unable to find dotnet exports`;
}
Expand All @@ -1451,6 +1453,13 @@ namespace Uno.UI {
document.body.addEventListener("focusin", this.onfocusin);
document.body.appendChild(this.containerElement);

// On WASM, if no one subscribes to key<Down|Up>, not only will the event not fire on any UIElement,
// but the browser won't even notify us that a key was pressed/released, and this breaks KeyboardStateTracker
// key tracking, which depends on RaiseEvent being called even if no one is subscribing. Instead, we
// subscribe on the body and make sure to call KeyboardStateTracker ourselves here.
document.body.addEventListener("keydown", this.onBodyKeyDown);
document.body.addEventListener("keyup", this.onBodyKeyUp);

window.addEventListener("resize", x => WindowManager.resize());
window.addEventListener("contextmenu", x => {
if (!(x.target instanceof HTMLInputElement) ||
Expand Down Expand Up @@ -1593,6 +1602,14 @@ namespace Uno.UI {
public moveWindow(x: number, y: number) {
window.moveTo(x, y);
}

private onBodyKeyDown(event: KeyboardEvent) {
WindowManager.keyTrackingMethod(event.key, true);
}

private onBodyKeyUp(event: KeyboardEvent) {
WindowManager.keyTrackingMethod(event.key, false);
}
}

if (typeof define === "function") {
Expand Down

0 comments on commit f2f751b

Please sign in to comment.