From 11d51fcebddbfc14c91007b7bb59a114893b7bf1 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Wed, 14 Feb 2024 22:23:00 -0800 Subject: [PATCH] Fix Browser issues on .NET 8 --- .../ControlCatalog.Browser/EmbedSample.Browser.cs | 15 +++++++++------ .../Avalonia.Browser/Avalonia.Browser.csproj | 4 ---- .../Avalonia.Browser/Interop/CanvasHelper.cs | 6 ------ .../build/Avalonia.Browser.targets | 15 +++++++++------ src/Browser/Avalonia.Browser/build/interop.js | 13 ------------- .../webapp/modules/avalonia/canvas.ts | 10 ++++++++-- 6 files changed, 26 insertions(+), 37 deletions(-) delete mode 100644 src/Browser/Avalonia.Browser/build/interop.js diff --git a/samples/ControlCatalog.Browser/EmbedSample.Browser.cs b/samples/ControlCatalog.Browser/EmbedSample.Browser.cs index 1bd226d5782..7bf3891a81b 100644 --- a/samples/ControlCatalog.Browser/EmbedSample.Browser.cs +++ b/samples/ControlCatalog.Browser/EmbedSample.Browser.cs @@ -21,14 +21,17 @@ public IPlatformHandle CreateControl(bool isSecond, IPlatformHandle parent, Func } else { - var defaultHandle = (JSObjectControlHandle)createDefault(); + var parentContainer = (JSObjectControlHandle)createDefault(); - _ = JSHost.ImportAsync("embed.js", "./embed.js").ContinueWith(_ => - { - EmbedInterop.AddAppButton(defaultHandle.Object); - }, TaskScheduler.FromCurrentSynchronizationContext()); + AddButton(parentContainer.Object); + + return parentContainer; - return defaultHandle; + static async void AddButton(JSObject parent) + { + await JSHost.ImportAsync("embed.js", "./embed.js"); + EmbedInterop.AddAppButton(parent); + } } } } diff --git a/src/Browser/Avalonia.Browser/Avalonia.Browser.csproj b/src/Browser/Avalonia.Browser/Avalonia.Browser.csproj index fa8c08cda78..3d3aa6aca9a 100644 --- a/src/Browser/Avalonia.Browser/Avalonia.Browser.csproj +++ b/src/Browser/Avalonia.Browser/Avalonia.Browser.csproj @@ -36,10 +36,6 @@ true build/;buildTransitive/ - - true - build/interop.js;buildTransitive/interop.js - true staticwebassets/ diff --git a/src/Browser/Avalonia.Browser/Interop/CanvasHelper.cs b/src/Browser/Avalonia.Browser/Interop/CanvasHelper.cs index 9e182eaa094..cdcda6a3202 100644 --- a/src/Browser/Avalonia.Browser/Interop/CanvasHelper.cs +++ b/src/Browser/Avalonia.Browser/Interop/CanvasHelper.cs @@ -8,14 +8,8 @@ internal record GLInfo(int ContextId, uint FboId, int Stencils, int Samples, int internal static partial class CanvasHelper { - - [DllImport("libSkiaSharp", CallingConvention = CallingConvention.Cdecl)] - static extern JSObject InterceptGLObject(); - public static GLInfo InitialiseGL(JSObject canvas, Action renderFrameCallback) { - InterceptGLObject(); - var info = InitGL(canvas, canvas.GetPropertyAsString("id")!, renderFrameCallback); var glInfo = new GLInfo( diff --git a/src/Browser/Avalonia.Browser/build/Avalonia.Browser.targets b/src/Browser/Avalonia.Browser/build/Avalonia.Browser.targets index 15b6c9f87fe..53ad40168a2 100644 --- a/src/Browser/Avalonia.Browser/build/Avalonia.Browser.targets +++ b/src/Browser/Avalonia.Browser/build/Avalonia.Browser.targets @@ -1,10 +1,13 @@ - $(EmccExtraLDFlags) --js-library="$(MSBuildThisFileDirectory)/interop.js" - $(EmccFlags) -sERROR_ON_UNDEFINED_SYMBOLS=0 $(EmccFlags) -sUSE_WEBGL2=1 -sMAX_WEBGL_VERSION=2 -lGL + + + + + @@ -16,12 +19,12 @@ - - + + - - + + diff --git a/src/Browser/Avalonia.Browser/build/interop.js b/src/Browser/Avalonia.Browser/build/interop.js deleted file mode 100644 index c7ae3a56c78..00000000000 --- a/src/Browser/Avalonia.Browser/build/interop.js +++ /dev/null @@ -1,13 +0,0 @@ -var LibraryExample = { - // Internal functions - $EXAMPLE: { - internal_func: function () { - } - }, - InterceptGLObject: function () { - globalThis.AvaloniaGL = GL - } -} - -autoAddDeps(LibraryExample, '$EXAMPLE') -mergeInto(LibraryManager.library, LibraryExample) diff --git a/src/Browser/Avalonia.Browser/webapp/modules/avalonia/canvas.ts b/src/Browser/Avalonia.Browser/webapp/modules/avalonia/canvas.ts index 800a93a2209..f938f6f871e 100644 --- a/src/Browser/Avalonia.Browser/webapp/modules/avalonia/canvas.ts +++ b/src/Browser/Avalonia.Browser/webapp/modules/avalonia/canvas.ts @@ -10,6 +10,12 @@ type CanvasElement = { Canvas: Canvas | undefined; } & HTMLCanvasElement; +function getGL(): any { + const self = globalThis as any; + const module = self.Module ?? self.getDotnetRuntime(0)?.Module; + return module?.GL ?? self.AvaloniaGL ?? self.SkiaSharpGL; +} + export class Canvas { static elements: Map; @@ -60,7 +66,7 @@ export class Canvas { return; } - const GL = (globalThis as any).AvaloniaGL; + const GL = getGL(); // make current GL.makeContextCurrent(ctx); @@ -179,7 +185,7 @@ export class Canvas { renderViaOffscreenBackBuffer: 1 }; - const GL = (globalThis as any).AvaloniaGL; + const GL = getGL(); let ctx: WebGLRenderingContext = GL.createContext(htmlCanvas, contextAttributes);