Browse Source

Use "handled" for keybord input in Browser (#18349)

Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
pull/18412/head
Johan Polson 11 months ago
committed by GitHub
parent
commit
156f587cfa
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 15
      src/Browser/Avalonia.Browser/Interop/InputHelper.cs
  2. 19
      src/Browser/Avalonia.Browser/webapp/modules/avalonia/input.ts

15
src/Browser/Avalonia.Browser/Interop/InputHelper.cs

@ -13,16 +13,23 @@ internal static partial class InputHelper
return Task.CompletedTask;
}
public static Task<T> RedirectInputRetunAsync<T>(int topLevelId, Func<BrowserTopLevelImpl,T> handler, T @default)
{
if (BrowserTopLevelImpl.TryGetTopLevel(topLevelId) is { } topLevelImpl)
return Task.FromResult(handler(topLevelImpl));
return Task.FromResult(@default);
}
[JSImport("InputHelper.subscribeInputEvents", AvaloniaModule.MainModuleName)]
public static partial void SubscribeInputEvents(JSObject htmlElement, int topLevelId);
[JSExport]
public static Task OnKeyDown(int topLevelId, string code, string key, int modifier) =>
RedirectInputAsync(topLevelId, t => t.InputHandler.OnKeyDown(code, key, modifier));
public static Task<bool> OnKeyDown(int topLevelId, string code, string key, int modifier) =>
RedirectInputRetunAsync(topLevelId, t => t.InputHandler.OnKeyDown(code, key, modifier), false);
[JSExport]
public static Task OnKeyUp(int topLevelId, string code, string key, int modifier) =>
RedirectInputAsync(topLevelId, t => t.InputHandler.OnKeyUp(code, key, modifier));
public static Task<bool> OnKeyUp(int topLevelId, string code, string key, int modifier) =>
RedirectInputRetunAsync(topLevelId, t => t.InputHandler.OnKeyUp(code, key, modifier), false);
[JSExport]
public static Task OnBeforeInput(int topLevelId, string inputType, int start, int end) =>

19
src/Browser/Avalonia.Browser/webapp/modules/avalonia/input.ts

@ -95,16 +95,23 @@ export class InputHelper {
public static subscribeKeyEvents(element: HTMLInputElement, topLevelId: number) {
const keyDownHandler = (args: KeyboardEvent) => {
JsExports.InputHelper.OnKeyDown(topLevelId, args.code, args.key, this.getModifiers(args));
if (this.clipboardState !== ClipboardState.Pending) {
args.preventDefault();
}
JsExports.InputHelper.OnKeyDown(topLevelId, args.code, args.key, this.getModifiers(args))
.then((handled: boolean) => {
if (!handled || this.clipboardState !== ClipboardState.Pending) {
args.preventDefault();
}
});
};
element.addEventListener("keydown", keyDownHandler);
const keyUpHandler = (args: KeyboardEvent) => {
JsExports.InputHelper.OnKeyUp(topLevelId, args.code, args.key, this.getModifiers(args));
args.preventDefault();
JsExports.InputHelper.OnKeyUp(topLevelId, args.code, args.key, this.getModifiers(args))
.then((handled: boolean) => {
if (!handled) {
args.preventDefault();
}
});
if (this.rejectClipboard) {
this.rejectClipboard();
}

Loading…
Cancel
Save