Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Avalonia.Browser running on .NET 9 #15521

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Browser/Avalonia.Browser/BrowserInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion src/Browser/Avalonia.Browser/Interop/CanvasHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public static (JSObject CanvasView, GLInfo? GLInfo) CreateSurface(
public static partial void OnSizeChanged(
JSObject canvasSurface,
[JSMarshalAs<JSType.Function<JSType.Number, JSType.Number, JSType.Number>>]
Action<int, int, double> onSizeChanged);
// TODO: this callback should be <int, int, double>. Revert after next .NET 9 preview.
Action<double, double, double> onSizeChanged);

[JSImport("CanvasFactory.create", AvaloniaModule.MainModuleName)]
private static partial JSObject Create(JSObject canvasSurface, int mode);
Expand Down
10 changes: 6 additions & 4 deletions src/Browser/Avalonia.Browser/Interop/InputHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ internal static partial class InputHelper
[JSImport("InputHelper.subscribeKeyEvents", AvaloniaModule.MainModuleName)]
public static partial void SubscribeKeyEvents(
JSObject htmlElement,
[JSMarshalAs<JSType.Function<JSType.String, JSType.String, JSType.Number, JSType.Boolean>>]
Func<string, string, int, bool> keyDown,
[JSMarshalAs<JSType.Function<JSType.String, JSType.String, JSType.Number, JSType.Boolean>>]
Func<string, string, int, bool> keyUp);
[JSMarshalAs<JSType.Function<JSType.String, JSType.String, JSType.String, JSType.Boolean>>]
// TODO: this callback should be <string, string, int, bool>. Revert after next .NET 9 preview.
Func<string, string, string, bool> keyDown,
[JSMarshalAs<JSType.Function<JSType.String, JSType.String, JSType.String, JSType.Boolean>>]
// TODO: this callback should be <string, string, int, bool>. Revert after next .NET 9 preview.
Func<string, string, string, bool> keyUp);

[JSImport("InputHelper.subscribeTextEvents", AvaloniaModule.MainModuleName)]
public static partial void SubscribeTextEvents(
Expand Down
4 changes: 2 additions & 2 deletions src/Browser/Avalonia.Browser/Rendering/BrowserSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/Browser/Avalonia.Browser/webapp/modules/avalonia/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -316,15 +316,15 @@ 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; }
if (args.altKey) { modifiers |= RawInputModifiers.Alt; }
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 {
Expand Down
Loading