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

Pause/Resume gif on image visibility changed #58

Merged
merged 2 commits into from
Feb 15, 2019
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
1 change: 1 addition & 0 deletions WpfAnimatedGif.Demo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<RadioButton Margin="5" Content="Specific count" IsChecked="{Binding UseSpecificRepeatCount}" />
<TextBox Margin="5" Text="{Binding RepeatCount}" Width="20" />
<CheckBox Margin="5" Content="Auto start" IsChecked="{Binding AutoStart}" />
<CheckBox Margin="5" Content="Visibility" IsChecked="{Binding GifVisible}" />
</StackPanel>

<StackPanel Grid.Row="1" Grid.Column="2" HorizontalAlignment="Right" Orientation="Horizontal">
Expand Down
14 changes: 13 additions & 1 deletion WpfAnimatedGif.Demo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,19 @@ public bool AutoStart
OnPropertyChanged("AutoStart");
}
}


private bool _gifVisible = true;
public bool GifVisible
{
get { return _gifVisible; }
set
{
_gifVisible = value;
img.Visibility = _gifVisible ? Visibility.Visible : Visibility.Collapsed;

OnPropertyChanged("GifVisible");
}
}

public event PropertyChangedEventHandler PropertyChanged;

Expand Down
28 changes: 25 additions & 3 deletions WpfAnimatedGif/ImageBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ private static void AnimatedSourceChanged(DependencyObject o, DependencyProperty
{
imageControl.Loaded -= ImageControlLoaded;
imageControl.Unloaded -= ImageControlUnloaded;
imageControl.IsVisibleChanged -= VisibilityChanged;

AnimationCache.DecrementReferenceCount(oldValue, GetRepeatBehavior(imageControl));
var controller = GetAnimationController(imageControl);
if (controller != null)
Expand All @@ -300,11 +302,31 @@ private static void AnimatedSourceChanged(DependencyObject o, DependencyProperty
{
imageControl.Loaded += ImageControlLoaded;
imageControl.Unloaded += ImageControlUnloaded;
imageControl.IsVisibleChanged += VisibilityChanged;

if (imageControl.IsLoaded)
InitAnimationOrImage(imageControl);
}
}

private static void VisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender is Image img && img.IsLoaded)
{
var controller = GetAnimationController(img);
if (controller != null)
{
if ((bool)e.NewValue)
{
controller.GotoFrame(0);
controller.Play();
}
else
controller.Pause();
Copy link
Member

Choose a reason for hiding this comment

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

Minor issue: while I'm not against omitting braces in general, I prefer to keep the if and else blocks consistent. Could you please add braces there?

}
}
}

private static void ImageControlLoaded(object sender, RoutedEventArgs e)
{
Image imageControl = sender as Image;
Expand Down Expand Up @@ -372,7 +394,7 @@ private static void InitAnimationOrImage(Image imageControl)
bool isInDesignMode = DesignerProperties.GetIsInDesignMode(imageControl);
bool animateInDesignMode = GetAnimateInDesignMode(imageControl);
bool shouldAnimate = !isInDesignMode || animateInDesignMode;

// For a BitmapImage with a relative UriSource, the loading is deferred until
// BaseUri is set. This method will be called again when BaseUri is set.
bool isLoadingDeferred = IsLoadingDeferred(source, imageControl);
Expand Down Expand Up @@ -537,7 +559,7 @@ private static BitmapDecoder GetDecoder(BitmapSource image, Image imageControl,
Stream stream = null;
Uri uri = null;
BitmapCreateOptions createOptions = BitmapCreateOptions.None;

var bmp = image as BitmapImage;
if (bmp != null)
{
Expand Down Expand Up @@ -625,7 +647,7 @@ private static GifFile DecodeGifFile(Uri uri)
if (uri.Authority == "siteoforigin:,,,")
sri = Application.GetRemoteStream(uri);
else
sri = Application.GetResourceStream(uri);
sri = Application.GetResourceStream(uri);

if (sri != null)
stream = sri.Stream;
Expand Down