-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -166,6 +167,16 @@ protected internal override void OnElementContextUninitialized(AttachedShadowEle | |
_container.Children.Remove(context.SpriteVisual); | ||
} | ||
|
||
context.SpriteVisual?.StopAnimation("Size"); | ||
|
||
context.Element.LayoutUpdated -= Element_LayoutUpdated; | ||
|
||
if (context.VisibilityToken != null) | ||
{ | ||
context.Element.UnregisterPropertyChangedCallback(UIElement.VisibilityProperty, context.VisibilityToken.Value); | ||
context.VisibilityToken = null; | ||
} | ||
|
||
base.OnElementContextUninitialized(context); | ||
} | ||
|
||
|
@@ -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/> | ||
|
@@ -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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please tell me there was a document describing how to do this? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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); | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 😄