Skip to content

Commit

Permalink
fix: Ensure LoadCompleted is fired on UI thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Dec 4, 2023
1 parent c56e433 commit 70cd310
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using Private.Infrastructure;
using Uno.UI.Dispatching;
using Uno.UI.RuntimeTests.Helpers;
using Windows.UI.Composition;
using Windows.UI.Xaml;
Expand All @@ -18,6 +19,10 @@ public class Given_CompositionNineGridBrush
[RunsOnUIThread]
public async Task When_Source_Changes()
{
var expectedThreadId = -1;
NativeDispatcher.Main.Enqueue(() => expectedThreadId = Environment.CurrentManagedThreadId, NativeDispatcherPriority.High);
await TestServices.WindowHelper.WaitFor(() => expectedThreadId != -1);

var compositor = Window.Current.Compositor;

var onlineSource = new Image
Expand Down Expand Up @@ -56,6 +61,11 @@ public async Task When_Source_Changes()
bool loadCompleted = false;
surface.LoadCompleted += async (s, o) =>
{
if (Environment.CurrentManagedThreadId != expectedThreadId)
{
Assert.Fail("LoadCompleted event is run on thread pool incorrectly");
}
if (o.Status == Windows.UI.Xaml.Media.LoadedImageSourceLoadStatus.Success)
{
var offlineBrush = compositor.CreateSurfaceBrush(surface);
Expand Down
23 changes: 13 additions & 10 deletions src/Uno.UI/UI/Xaml/Media/LoadedImageSurface.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Windows.UI.Xaml.Media.Imaging;
using Uno.UI.Composition;
using Windows.Graphics.Display;
using Uno.UI.Dispatching;

namespace Windows.UI.Xaml.Media
{
Expand All @@ -30,6 +31,14 @@ internal LoadedImageSurface(Action<LoadedImageSurface> loadAction)
public static LoadedImageSurface StartLoadFromUri(Uri uri) => StartLoadFromUri(uri, null, null);
public static LoadedImageSurface StartLoadFromUri(Uri uri, Size desiredMaxSize) => StartLoadFromUri(uri, (int)desiredMaxSize.Width, (int)desiredMaxSize.Height);

private void RaiseLoadCompleted(LoadedImageSourceLoadStatus status)
{
if (LoadCompleted is not null)
{
NativeDispatcher.Main.Enqueue(() => LoadCompleted.Invoke(this, new(status)));
}
}

private static LoadedImageSurface StartLoadFromUri(Uri uri, int? width, int? height)
{
var retVal = new LoadedImageSurface(async (LoadedImageSurface imgSurf) =>
Expand Down Expand Up @@ -70,10 +79,7 @@ private static LoadedImageSurface StartLoadFromUri(Uri uri, int? width, int? hei
}
catch
{
if (imgSurf.LoadCompleted is not null)
{
imgSurf.LoadCompleted(imgSurf, new LoadedImageSourceLoadCompletedEventArgs(LoadedImageSourceLoadStatus.NetworkError));
}
imgSurf.RaiseLoadCompleted(LoadedImageSourceLoadStatus.NetworkError);
}
if (stream is not null)
Expand All @@ -90,16 +96,13 @@ private static LoadedImageSurface StartLoadFromUri(Uri uri, int? width, int? hei
imgSurf._naturalPhysicalSize = imgSurf._decodedPhysicalSize;
}
if (imgSurf.LoadCompleted is not null)
{
imgSurf.LoadCompleted(imgSurf, new LoadedImageSourceLoadCompletedEventArgs(result.success ? LoadedImageSourceLoadStatus.Success : LoadedImageSourceLoadStatus.InvalidFormat));
}
imgSurf.RaiseLoadCompleted(result.success ? LoadedImageSourceLoadStatus.Success : LoadedImageSourceLoadStatus.InvalidFormat);
stream.Dispose();
}
else
{
imgSurf.LoadCompleted?.Invoke(imgSurf, new LoadedImageSourceLoadCompletedEventArgs(LoadedImageSourceLoadStatus.Other));
imgSurf.RaiseLoadCompleted(LoadedImageSourceLoadStatus.Other);
}
}
else
Expand Down Expand Up @@ -130,7 +133,7 @@ private static LoadedImageSurface StartLoadFromStream(IRandomAccessStream stream
imgSurf._naturalPhysicalSize = imgSurf._decodedPhysicalSize;
}
imgSurf.LoadCompleted?.Invoke(imgSurf, new LoadedImageSourceLoadCompletedEventArgs(result.success ? LoadedImageSourceLoadStatus.Success : LoadedImageSourceLoadStatus.InvalidFormat));
imgSurf.RaiseLoadCompleted(result.success ? LoadedImageSourceLoadStatus.Success : LoadedImageSourceLoadStatus.InvalidFormat);
});

return retVal;
Expand Down

0 comments on commit 70cd310

Please sign in to comment.