1
0
Эх сурвалжийг харах

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

Co-authored-by: Julien Lebosquain <[email protected]>
Johan Polson 8 сар өмнө
parent
commit
156f587cfa

+ 11 - 4
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) =>

+ 13 - 6
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();
             }