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

[WinUI] Fix gif animation initial state #20169

Merged
merged 1 commit into from
Feb 2, 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
20 changes: 17 additions & 3 deletions src/Core/src/Handlers/Image/ImageHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@

namespace Microsoft.Maui.Handlers
{
public partial class ImageHandler : ViewHandler<IImage, Image>
public partial class ImageHandler : ViewHandler<IImage, WImage>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image === WImage, just using the type alias as it got confusing.

{
protected override Image CreatePlatformView() => new Image();
protected override WImage CreatePlatformView() => new WImage();

protected override void DisconnectHandler(Image platformView)
protected override void ConnectHandler(WImage platformView)
{
platformView.ImageOpened += OnImageOpened;

base.ConnectHandler(platformView);
}

protected override void DisconnectHandler(WImage platformView)
{
platformView.ImageOpened -= OnImageOpened;

base.DisconnectHandler(platformView);
SourceLoader.Reset();
}
Expand All @@ -38,6 +47,11 @@ public static void MapSource(IImageHandler handler, IImage image) =>
public static Task MapSourceAsync(IImageHandler handler, IImage image) =>
handler.SourceLoader.UpdateImageSourceAsync();

void OnImageOpened(object sender, RoutedEventArgs e)
{
UpdateValue(nameof(IImage.IsAnimationPlaying));
}

partial class ImageImageSourcePartSetter
{
public override void SetImageSource(ImageSource? platformImage)
Expand Down
2 changes: 0 additions & 2 deletions src/Core/src/Platform/Windows/ImageSourcePartExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ internal static class ImageSourcePartExtensions
if (applied)
{
setImage(uiImage);
if (destinationContext is WImage imageView)
imageView.UpdateIsAnimationPlaying(image);
Comment on lines -45 to -46
Copy link
Member Author

@mattleibow mattleibow Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This did nothing as the image is only animatable after it is decoded - which is after it is on-screen.

}

events?.LoadingCompleted(applied);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Microsoft.Maui.Platform.MauiWebView.MauiWebView(Microsoft.Maui.Handlers.WebViewH
Microsoft.Maui.SizeRequest.Equals(Microsoft.Maui.SizeRequest other) -> bool
Microsoft.Maui.SoftInputExtensions
override Microsoft.Maui.Handlers.ContentViewHandler.DisconnectHandler(Microsoft.Maui.Platform.ContentPanel! platformView) -> void
override Microsoft.Maui.Handlers.ImageHandler.ConnectHandler(Microsoft.UI.Xaml.Controls.Image! platformView) -> void
override Microsoft.Maui.Handlers.MenuFlyoutHandler.DisconnectHandler(Microsoft.UI.Xaml.Controls.MenuFlyout! platformView) -> void
override Microsoft.Maui.Layouts.FlexBasis.Equals(object? obj) -> bool
override Microsoft.Maui.Layouts.FlexBasis.GetHashCode() -> int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ await InvokeOnMainThreadAsync(async () =>
#endif
)]
[InlineData("animated_heart.gif", true)]
#if !WINDOWS
[InlineData("animated_heart.gif", false)]
#endif
public async virtual Task AnimatedSourceInitializesCorrectly(string filename, bool isAnimating)
{
var image = new TStub
Expand All @@ -138,8 +136,10 @@ await InvokeOnMainThreadAsync(async () =>

await image.WaitUntilLoaded();

await AttachAndRun(GetPlatformImageView(handler), () =>
await AttachAndRun(GetPlatformImageView(handler), async () =>
{
await image.WaitUntilDecoded();

Assert.Equal(isAnimating, GetNativeIsAnimationPlaying(handler));
});
});
Expand Down
27 changes: 27 additions & 0 deletions src/Core/tests/DeviceTests/Stubs/ImageStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,32 @@ public static class ImageStubExtensions
{
public static Task WaitUntilLoaded(this IImageStub image, int timeout = 1000) =>
AssertEventually(() => !image.IsLoading, timeout: timeout, message: $"Image {image} did not load before timeout");

public static async Task WaitUntilDecoded(this IImageStub image, int timeout = 1000)
{
await WaitUntilLoaded(image, timeout);

#if WINDOWS
if (image.Handler.PlatformView is Microsoft.UI.Xaml.Controls.Image wimage)
{
var imageOpened = false;

wimage.ImageOpened += OnOpened;
wimage.ImageFailed += OnFailed;

await AssertEventually(() => imageOpened, timeout: timeout, message: $"Image {image} did not decode before timeout");

void OnOpened(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
imageOpened = true;
}

void OnFailed(object sender, Microsoft.UI.Xaml.ExceptionRoutedEventArgs e)
{
imageOpened = true;
}
}
#endif
}
}
}
Loading