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

Fix #4214 - Update AttachedDropShadow visual on layout/visibility #4230

Merged
merged 1 commit into from
Sep 9, 2021
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
72 changes: 68 additions & 4 deletions Microsoft.Toolkit.Uwp.UI/Shadows/AttachedDropShadow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Linq;
using System.Numerics;
using Windows.Foundation;
Expand Down Expand Up @@ -166,6 +167,16 @@ protected internal override void OnElementContextUninitialized(AttachedShadowEle
_container.Children.Remove(context.SpriteVisual);
}

context.SpriteVisual?.StopAnimation("Size");
Copy link
Member Author

Choose a reason for hiding this comment

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

@Sergio0694 any other clean-up I need to do beyond this to clean-up the composition animation?

Copy link
Member

Choose a reason for hiding this comment

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

Don't think so, the actual objects would just be disposed normally, we're not doing any specific cleanup in other places of the animation package either, it's mostly just fire and forget as usual. Stopping these animations is the only important bit because otherwise the composition layer will just keep them always active even if the XAML object is gone, but other than that we should be good 😄


context.Element.LayoutUpdated -= Element_LayoutUpdated;

if (context.VisibilityToken != null)
{
context.Element.UnregisterPropertyChangedCallback(UIElement.VisibilityProperty, context.VisibilityToken.Value);
context.VisibilityToken = null;
}

base.OnElementContextUninitialized(context);
}

Expand All @@ -176,6 +187,50 @@ protected override void SetElementChildVisual(AttachedShadowElementContext conte
{
_container.Children.InsertAtTop(context.SpriteVisual);
}

// Handles size changing and other elements around it updating.
context.Element.LayoutUpdated -= Element_LayoutUpdated;
context.Element.LayoutUpdated += Element_LayoutUpdated;

if (context.VisibilityToken != null)
{
context.Element.UnregisterPropertyChangedCallback(UIElement.VisibilityProperty, context.VisibilityToken.Value);
context.VisibilityToken = null;
}

context.VisibilityToken = context.Element.RegisterPropertyChangedCallback(UIElement.VisibilityProperty, Element_VisibilityChanged);
}

private void Element_LayoutUpdated(object sender, object e)
{
// Update other shadows to account for layout changes
CastToElement_SizeChanged(null, null);
}

private void Element_VisibilityChanged(DependencyObject sender, DependencyProperty dp)
{
if (sender is FrameworkElement element)
{
var context = GetElementContext(element);

if (element.Visibility == Visibility.Collapsed)
{
if (_container != null && _container.Children.Contains(context.SpriteVisual))
{
_container.Children.Remove(context.SpriteVisual);
}
}
else
{
if (_container != null && !_container.Children.Contains(context.SpriteVisual))
{
_container.Children.InsertAtTop(context.SpriteVisual);
}
}
}

// Update other shadows to account for layout changes
CastToElement_SizeChanged(null, null);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -255,12 +310,24 @@ protected override CompositionBrush GetShadowMask(AttachedShadowElementContext c
}

// Position our shadow in the correct spot to match the corresponding element.
context.SpriteVisual.Size = context.Element.RenderSize.ToVector2();
context.SpriteVisual.Offset = context.Element.CoordinatesFrom(CastTo).ToVector3();

BindSizeAndScale(context.SpriteVisual, context.Element);

return mask;
}

private static void BindSizeAndScale(CompositionObject source, UIElement target)
{
var visual = ElementCompositionPreview.GetElementVisual(target);
var bindSizeAnimation = source.Compositor.CreateExpressionAnimation($"{nameof(visual)}.Size * {nameof(visual)}.Scale.XY");
Copy link
Contributor

Choose a reason for hiding this comment

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

Please tell me there was a document describing how to do this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Kind of? https://docs.microsoft.com/uwp/api/Windows.UI.Composition.ExpressionAnimation

I just asked @Sergio0694 😋. We have helpers in the Toolkit for this, but they're in the Animations package and we don't want to include references to that everywhere.

I also just copied this from the Media package from another internal method and tweaked it... But yeah it is a bit of black magic.


bindSizeAnimation.SetReferenceParameter(nameof(visual), visual);

// Start the animation
source.StartAnimation("Size", bindSizeAnimation);
}

private void CustomMaskedElement_Loaded(object sender, RoutedEventArgs e)
{
var context = GetElementContext(sender as FrameworkElement);
Expand All @@ -274,9 +341,6 @@ private void CustomMaskedElement_Loaded(object sender, RoutedEventArgs e)
/// <inheritdoc/>
protected internal override void OnSizeChanged(AttachedShadowElementContext context, Size newSize, Size previousSize)
{
var sizeAsVec2 = newSize.ToVector2();

context.SpriteVisual.Size = sizeAsVec2;
context.SpriteVisual.Offset = context.Element.CoordinatesFrom(CastTo).ToVector3();

UpdateShadowClip(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public sealed class AttachedShadowElementContext

private Dictionary<string, object> _resources;

internal long? VisibilityToken { get; set; }

/// <summary>
/// Gets a value indicating whether or not this <see cref="AttachedShadowElementContext"/> has been initialized.
/// </summary>
Expand Down