Skip to content

Commit

Permalink
Merge pull request #4274 from Sergio0694/bugfix/implicit-animations-r…
Browse files Browse the repository at this point in the history
…eset

Bugfix/implicit animations reset
  • Loading branch information
michael-hawker authored Sep 25, 2021
2 parents d775ea4 + 4612a38 commit fef65a5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Numerics;
using Microsoft.Toolkit.Uwp.UI;
using Microsoft.Toolkit.Uwp.UI.Animations;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Hosting;
Expand All @@ -18,6 +19,8 @@ public sealed partial class ImplicitAnimationsPage : IXamlRenderListener
{
private Random _random = new Random();
private UIElement _element;
private ImplicitAnimationSet _animationSet;
private bool _areAnimationsToggled;

public ImplicitAnimationsPage()
{
Expand All @@ -28,6 +31,8 @@ public ImplicitAnimationsPage()
public void OnXamlRendered(FrameworkElement control)
{
_element = control.FindChild("Element");
_animationSet = Implicit.GetAnimations(_element);
_areAnimationsToggled = true;
}

private void Load()
Expand Down Expand Up @@ -60,6 +65,16 @@ private void Load()
1);
}
});

SampleController.Current.RegisterNewCommand("Toggle animations", (sender, args) =>
{
if (_element != null)
{
Implicit.SetAnimations(_element, _areAnimationsToggled ? null : _animationSet);

_areAnimationsToggled = !_areAnimationsToggled;
}
});
}
}
}
66 changes: 42 additions & 24 deletions Microsoft.Toolkit.Uwp.UI.Animations/Implicit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static ImplicitAnimationSet GetShowAnimations(UIElement element)

if (collection is null)
{
element.SetValue(ShowAnimationsProperty, collection = new());
element.SetValue(ShowAnimationsProperty, collection = new ImplicitAnimationSet());
}

return collection;
Expand All @@ -80,7 +80,7 @@ public static ImplicitAnimationSet GetHideAnimations(UIElement element)

if (collection is null)
{
element.SetValue(HideAnimationsProperty, collection = new());
element.SetValue(HideAnimationsProperty, collection = new ImplicitAnimationSet());
}

return collection;
Expand All @@ -107,7 +107,7 @@ public static ImplicitAnimationSet GetAnimations(UIElement element)

if (collection is null)
{
element.SetValue(AnimationsProperty, collection = new());
element.SetValue(AnimationsProperty, collection = new ImplicitAnimationSet());
}

return collection;
Expand Down Expand Up @@ -145,15 +145,21 @@ static void OnAnimationsChanged(object sender, EventArgs e)
oldCollection.AnimationsChanged -= OnAnimationsChanged;
}

if (d is UIElement element &&
e.NewValue is ImplicitAnimationSet collection)
if (d is UIElement element)
{
collection.ParentReference = new(element);
collection.AnimationsChanged -= OnAnimationsChanged;
collection.AnimationsChanged += OnAnimationsChanged;
if (e.NewValue is ImplicitAnimationSet collection)
{
collection.ParentReference = new(element);
collection.AnimationsChanged -= OnAnimationsChanged;
collection.AnimationsChanged += OnAnimationsChanged;

ElementCompositionPreview.SetIsTranslationEnabled(element, true);
ElementCompositionPreview.SetImplicitShowAnimation(element, collection.GetCompositionAnimationGroup(element));
ElementCompositionPreview.SetIsTranslationEnabled(element, true);
ElementCompositionPreview.SetImplicitShowAnimation(element, collection.GetCompositionAnimationGroup(element));
}
else
{
ElementCompositionPreview.SetImplicitShowAnimation(element, null);
}
}
}

Expand All @@ -179,15 +185,21 @@ static void OnAnimationsChanged(object sender, EventArgs e)
oldCollection.AnimationsChanged -= OnAnimationsChanged;
}

if (d is UIElement element &&
e.NewValue is ImplicitAnimationSet collection)
if (d is UIElement element)
{
collection.ParentReference = new(element);
collection.AnimationsChanged -= OnAnimationsChanged;
collection.AnimationsChanged += OnAnimationsChanged;
if (e.NewValue is ImplicitAnimationSet collection)
{
collection.ParentReference = new(element);
collection.AnimationsChanged -= OnAnimationsChanged;
collection.AnimationsChanged += OnAnimationsChanged;

ElementCompositionPreview.SetIsTranslationEnabled(element, true);
ElementCompositionPreview.SetImplicitHideAnimation(element, collection.GetCompositionAnimationGroup(element));
ElementCompositionPreview.SetIsTranslationEnabled(element, true);
ElementCompositionPreview.SetImplicitHideAnimation(element, collection.GetCompositionAnimationGroup(element));
}
else
{
ElementCompositionPreview.SetImplicitHideAnimation(element, null);
}
}
}

Expand All @@ -213,15 +225,21 @@ static void OnAnimationsChanged(object sender, EventArgs e)
oldCollection.AnimationsChanged -= OnAnimationsChanged;
}

if (d is UIElement element &&
e.NewValue is ImplicitAnimationSet collection)
if (d is UIElement element)
{
collection.ParentReference = new(element);
collection.AnimationsChanged -= OnAnimationsChanged;
collection.AnimationsChanged += OnAnimationsChanged;
if (e.NewValue is ImplicitAnimationSet collection)
{
collection.ParentReference = new(element);
collection.AnimationsChanged -= OnAnimationsChanged;
collection.AnimationsChanged += OnAnimationsChanged;

ElementCompositionPreview.SetIsTranslationEnabled(element, true);
ElementCompositionPreview.GetElementVisual(element).ImplicitAnimations = collection.GetImplicitAnimationCollection(element);
ElementCompositionPreview.SetIsTranslationEnabled(element, true);
ElementCompositionPreview.GetElementVisual(element).ImplicitAnimations = collection.GetImplicitAnimationCollection(element);
}
else
{
ElementCompositionPreview.GetElementVisual(element).ImplicitAnimations = null;
}
}
}
}
Expand Down

0 comments on commit fef65a5

Please sign in to comment.