Skip to content

Commit

Permalink
Pause/Resume gif on image visibility changed (#58)
Browse files Browse the repository at this point in the history
* Pause/Resume gif on image visibility changed

* Rollback formatting changes
  • Loading branch information
khersonIT authored and thomaslevesque committed Feb 15, 2019
1 parent e07b77b commit ac96807
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
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();
}
}
}

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

0 comments on commit ac96807

Please sign in to comment.