Skip to content

Commit

Permalink
chore: Fix svg
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Apr 23, 2024
1 parent b4698be commit 4a3f8e7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 93 deletions.
119 changes: 26 additions & 93 deletions src/SamplesApp/UITests.Shared/Helpers/WaitableSampleImageHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
Expand All @@ -7,115 +8,47 @@ namespace UITests.Shared.Helpers;

internal static class WaitableSampleImageHelpers
{
public static Task WaitAllImages(params ImageBrush[] images)
private static Task WaitImage(ImageBrush image)
{
int counter = 0;
var tcs = new TaskCompletionSource();
foreach (var image in images)
{
image.ImageOpened += (_, _) =>
{
counter++;
if (counter == images.Length)
{
tcs.SetResult();
}
};

image.ImageFailed += (_, _) =>
{
counter++;
if (counter == images.Length)
{
tcs.SetResult();
}
};
}

image.ImageOpened += (_, _) => tcs.SetResult();
image.ImageFailed += (_, _) => tcs.SetResult();
return tcs.Task;
}

public static Task WaitAllImages(params Image[] images)
private static Task WaitImage(Image image)
{
int counter = 0;
var tcs = new TaskCompletionSource();
foreach (var image in images)
{
image.ImageOpened += (_, _) =>
{
counter++;
if (counter == images.Length)
{
tcs.SetResult();
}
};

image.ImageFailed += (_, _) =>
{
counter++;
if (counter == images.Length)
{
tcs.SetResult();
}
};
}

image.ImageOpened += (_, _) => tcs.SetResult();
image.ImageFailed += (_, _) => tcs.SetResult();
return tcs.Task;
}

public static Task WaitAllImages(params BitmapImage[] images)
private static Task WaitImage(SvgImageSource image)
{
int counter = 0;
var tcs = new TaskCompletionSource();
foreach (var image in images)
{
image.ImageOpened += (_, _) =>
{
counter++;
if (counter == images.Length)
{
tcs.SetResult();
}
};

image.ImageFailed += (_, _) =>
{
counter++;
if (counter == images.Length)
{
tcs.SetResult();
}
};
}

image.Opened += (_, _) => tcs.SetResult();
image.OpenFailed += (_, _) => tcs.SetResult();
return tcs.Task;
}

public static Task WaitAllImages(params SvgImageSource[] images)
private static Task WaitImage(BitmapImage image)
{
int counter = 0;
var tcs = new TaskCompletionSource();
foreach (var image in images)
{
image.Opened += (_, _) =>
{
counter++;
if (counter == images.Length)
{
tcs.SetResult();
}
};

image.OpenFailed += (_, _) =>
{
counter++;
if (counter == images.Length)
{
tcs.SetResult();
}
};
}

image.ImageOpened += (_, _) => tcs.SetResult();
image.ImageFailed += (_, _) => tcs.SetResult();
return tcs.Task;
}

public static Task WaitAllImages(params ImageBrush[] images)
=> Task.WhenAll(images.Select(WaitImage));

public static Task WaitAllImages(params Image[] images)
=> Task.WhenAll(images.Select(WaitImage));

public static Task WaitAllImages(params SvgImageSource[] images)
=> Task.WhenAll(images.Select(WaitImage));

public static Task WaitAllImages(params BitmapImage[] images)
=> Task.WhenAll(images.Select(WaitImage));
}
9 changes: 9 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/Image/Image.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,26 @@ private void InitializeSvgSource(SvgImageSource source)
_svgCanvas = source.GetCanvas();
AddChild(_svgCanvas);
source.SourceLoaded += OnSvgSourceLoaded;
source.OpenFailed += OnSvgSourceFailed;
_sourceDisposable.Disposable = Disposable.Create(() =>
{
RemoveChild(_svgCanvas);
source.SourceLoaded -= OnSvgSourceLoaded;
source.OpenFailed -= OnSvgSourceFailed;
_svgCanvas = null;
});
}

private void OnSvgSourceLoaded(object sender, EventArgs args)
{
InvalidateMeasure();
ImageOpened?.Invoke(this, new RoutedEventArgs(this));
}

private void OnSvgSourceFailed(SvgImageSource sender, SvgImageSourceFailedEventArgs args)
{
InvalidateMeasure();
ImageFailed?.Invoke(this, new ExceptionRoutedEventArgs(this, "Failed to load Svg source"));
}

private void InitializeImageSource(ImageSource source)
Expand Down

0 comments on commit 4a3f8e7

Please sign in to comment.