From 365a8250c7f63da22246e7c18a8d50b3d1e3501d Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Mon, 24 Jan 2022 11:59:59 +0000 Subject: [PATCH] fix stability of wasm init. --- .../Avalonia.Web.Blazor/AvaloniaView.razor.cs | 56 +++++++++---------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/src/Web/Avalonia.Web.Blazor/AvaloniaView.razor.cs b/src/Web/Avalonia.Web.Blazor/AvaloniaView.razor.cs index bb8ae9e119..07c776efb4 100644 --- a/src/Web/Avalonia.Web.Blazor/AvaloniaView.razor.cs +++ b/src/Web/Avalonia.Web.Blazor/AvaloniaView.razor.cs @@ -287,17 +287,22 @@ namespace Avalonia.Web.Blazor _topLevelImpl.SetSurface(_context, _jsGlInfo, ColorType, new PixelSize((int)_canvasSize.Width, (int)_canvasSize.Height), _dpi); + + _interop.SetCanvasSize((int)(_canvasSize.Width * _dpi), (int)(_canvasSize.Height * _dpi)); _initialised = true; - _topLevel.Prepare(); - - _topLevel.Renderer.Start(); - - Invalidate(); - - _sizeWatcher = await SizeWatcherInterop.ImportAsync(Js, _htmlCanvas, OnSizeChanged); - _dpiWatcher = await DpiWatcherInterop.ImportAsync(Js, OnDpiChanged); + Threading.Dispatcher.UIThread.Post(async () => + { + _interop.RequestAnimationFrame(true); + + _sizeWatcher = await SizeWatcherInterop.ImportAsync(Js, _htmlCanvas, OnSizeChanged); + _dpiWatcher = await DpiWatcherInterop.ImportAsync(Js, OnDpiChanged); + + _topLevel.Prepare(); + + _topLevel.Renderer.Start(); + }); } } @@ -336,37 +341,30 @@ namespace Avalonia.Web.Blazor private void OnDpiChanged(double newDpi) { - _dpi = newDpi; + if (Math.Abs(_dpi - newDpi) > 0.0001) + { + _dpi = newDpi; + + _interop.SetCanvasSize((int)(_canvasSize.Width * _dpi), (int)(_canvasSize.Height * _dpi)); - _topLevelImpl.SetClientSize(_canvasSize, _dpi); - - ForceBlit(); + _topLevelImpl.SetClientSize(_canvasSize, _dpi); - Invalidate(); + ForceBlit(); + } } private void OnSizeChanged(SKSize newSize) { - _canvasSize = newSize; - - _interop.SetCanvasSize((int)(_canvasSize.Width * _dpi), (int)(_canvasSize.Height * _dpi)); - - _topLevelImpl.SetClientSize(_canvasSize, _dpi); + if (_canvasSize != newSize) + { + _canvasSize = newSize; - ForceBlit(); + _interop.SetCanvasSize((int)(_canvasSize.Width * _dpi), (int)(_canvasSize.Height * _dpi)); - Invalidate(); - } + _topLevelImpl.SetClientSize(_canvasSize, _dpi); - public void Invalidate() - { - if (!_initialised || _jsGlInfo == null) - { - Console.WriteLine("invalidate ignored"); - return; + ForceBlit(); } - - _interop.RequestAnimationFrame(true); } public void SetActive(bool active)