From 4b8f01d13b33c4f52a6a63001e3096cf037af8d5 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Fri, 26 Apr 2024 00:53:37 -0700 Subject: [PATCH] Fix Avalonia.Browser running on .NET 9 --- src/Browser/Avalonia.Browser/BrowserInputHandler.cs | 8 ++++---- src/Browser/Avalonia.Browser/Interop/CanvasHelper.cs | 3 ++- src/Browser/Avalonia.Browser/Interop/InputHelper.cs | 10 ++++++---- .../Avalonia.Browser/Rendering/BrowserSurface.cs | 4 ++-- .../Avalonia.Browser/webapp/modules/avalonia/input.ts | 8 ++++---- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Browser/Avalonia.Browser/BrowserInputHandler.cs b/src/Browser/Avalonia.Browser/BrowserInputHandler.cs index edd66b5a06b..b264c86db5c 100644 --- a/src/Browser/Avalonia.Browser/BrowserInputHandler.cs +++ b/src/Browser/Avalonia.Browser/BrowserInputHandler.cs @@ -250,9 +250,9 @@ public bool OnDragEvent(JSObject args) && dropEffect != DragDropEffects.None; } - private bool OnKeyDown(string code, string key, int modifier) + private bool OnKeyDown(string code, string key, string modifier) { - var handled = RawKeyboardEvent(RawKeyEventType.KeyDown, code, key, (RawInputModifiers)modifier); + var handled = RawKeyboardEvent(RawKeyEventType.KeyDown, code, key, (RawInputModifiers)int.Parse(modifier)); if (!handled && key.Length == 1) { @@ -262,9 +262,9 @@ private bool OnKeyDown(string code, string key, int modifier) return handled; } - private bool OnKeyUp(string code, string key, int modifier) + private bool OnKeyUp(string code, string key, string modifier) { - return RawKeyboardEvent(RawKeyEventType.KeyUp, code, key, (RawInputModifiers)modifier); + return RawKeyboardEvent(RawKeyEventType.KeyUp, code, key, (RawInputModifiers)int.Parse(modifier)); } private bool RawPointerEvent( diff --git a/src/Browser/Avalonia.Browser/Interop/CanvasHelper.cs b/src/Browser/Avalonia.Browser/Interop/CanvasHelper.cs index e725c2997a5..331777191ac 100644 --- a/src/Browser/Avalonia.Browser/Interop/CanvasHelper.cs +++ b/src/Browser/Avalonia.Browser/Interop/CanvasHelper.cs @@ -36,7 +36,8 @@ public static (JSObject CanvasView, GLInfo? GLInfo) CreateSurface( public static partial void OnSizeChanged( JSObject canvasSurface, [JSMarshalAs>] - Action onSizeChanged); + // TODO: this callback should be . Revert after next .NET 9 preview. + Action onSizeChanged); [JSImport("CanvasFactory.create", AvaloniaModule.MainModuleName)] private static partial JSObject Create(JSObject canvasSurface, int mode); diff --git a/src/Browser/Avalonia.Browser/Interop/InputHelper.cs b/src/Browser/Avalonia.Browser/Interop/InputHelper.cs index 0b9fe1d95cd..928c0e23c65 100644 --- a/src/Browser/Avalonia.Browser/Interop/InputHelper.cs +++ b/src/Browser/Avalonia.Browser/Interop/InputHelper.cs @@ -10,10 +10,12 @@ internal static partial class InputHelper [JSImport("InputHelper.subscribeKeyEvents", AvaloniaModule.MainModuleName)] public static partial void SubscribeKeyEvents( JSObject htmlElement, - [JSMarshalAs>] - Func keyDown, - [JSMarshalAs>] - Func keyUp); + [JSMarshalAs>] + // TODO: this callback should be . Revert after next .NET 9 preview. + Func keyDown, + [JSMarshalAs>] + // TODO: this callback should be . Revert after next .NET 9 preview. + Func keyUp); [JSImport("InputHelper.subscribeTextEvents", AvaloniaModule.MainModuleName)] public static partial void SubscribeTextEvents( diff --git a/src/Browser/Avalonia.Browser/Rendering/BrowserSurface.cs b/src/Browser/Avalonia.Browser/Rendering/BrowserSurface.cs index aa2b4cf0141..8f73a67c77f 100644 --- a/src/Browser/Avalonia.Browser/Rendering/BrowserSurface.cs +++ b/src/Browser/Avalonia.Browser/Rendering/BrowserSurface.cs @@ -87,11 +87,11 @@ public virtual void Dispose() ClientSize = default; } - private void OnSizeChanged(int pixelWidth, int pixelHeight, double dpr) + private void OnSizeChanged(double pixelWidth, double pixelHeight, double dpr) { var oldScaling = Scaling; var oldClientSize = ClientSize; - RenderSize = new PixelSize(pixelWidth, pixelHeight); + RenderSize = new PixelSize((int)pixelWidth, (int)pixelHeight); ClientSize = RenderSize.ToSize(dpr); Scaling = dpr; if (oldClientSize != ClientSize) diff --git a/src/Browser/Avalonia.Browser/webapp/modules/avalonia/input.ts b/src/Browser/Avalonia.Browser/webapp/modules/avalonia/input.ts index 8a2d1569f57..df4fc19e6bd 100644 --- a/src/Browser/Avalonia.Browser/webapp/modules/avalonia/input.ts +++ b/src/Browser/Avalonia.Browser/webapp/modules/avalonia/input.ts @@ -74,8 +74,8 @@ export class InputHelper { public static subscribeKeyEvents( element: HTMLInputElement, - keyDownCallback: (code: string, key: string, modifiers: RawInputModifiers) => boolean, - keyUpCallback: (code: string, key: string, modifiers: RawInputModifiers) => boolean) { + keyDownCallback: (code: string, key: string, modifiers: string) => boolean, + keyUpCallback: (code: string, key: string, modifiers: string) => boolean) { const keyDownHandler = (args: KeyboardEvent) => { if (keyDownCallback(args.code, args.key, this.getModifiers(args))) { if (this.clipboardState !== ClipboardState.Pending) { @@ -316,7 +316,7 @@ export class InputHelper { inputElement.style.width = `${inputElement.scrollWidth}px`; } - private static getModifiers(args: KeyboardEvent): RawInputModifiers { + private static getModifiers(args: KeyboardEvent): string { let modifiers = RawInputModifiers.None; if (args.ctrlKey) { modifiers |= RawInputModifiers.Control; } @@ -324,7 +324,7 @@ export class InputHelper { if (args.shiftKey) { modifiers |= RawInputModifiers.Shift; } if (args.metaKey) { modifiers |= RawInputModifiers.Meta; } - return modifiers; + return modifiers.toString(); } public static setPointerCapture(containerElement: HTMLInputElement, pointerId: number): void {