Skip to content

Optimize Visual size sync through RelativeSizeAdjustment over expression animations #4397

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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.Numerics;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Hosting;

Expand Down Expand Up @@ -48,10 +50,10 @@ public static void SetVisualFactory(UIElement element, AttachedVisualFactoryBase
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance for the current event.</param>
private static async void OnVisualFactoryPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var element = (UIElement)d;
var attachedVisual = await ((AttachedVisualFactoryBase)e.NewValue).GetAttachedVisualAsync(element);
UIElement element = (UIElement)d;
Visual attachedVisual = await ((AttachedVisualFactoryBase)e.NewValue).GetAttachedVisualAsync(element);

attachedVisual.BindSize(element);
attachedVisual.RelativeSizeAdjustment = Vector2.One;

ElementCompositionPreview.SetElementChildVisual(element, attachedVisual);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ namespace Microsoft.Toolkit.Uwp.UI.Media
internal static class CompositionObjectExtensions
{
/// <summary>
/// Starts an <see cref="ExpressionAnimation"/> to keep the size of the source <see cref="CompositionObject"/> in sync with the target <see cref="UIElement"/>
/// Starts an <see cref="ExpressionAnimation"/> to keep the size of the source <see cref="Visual"/> in sync with the target <see cref="UIElement"/>
/// </summary>
/// <param name="source">The <see cref="CompositionObject"/> to start the animation on</param>
/// <param name="source">The <see cref="Visual"/> to start the animation on</param>
/// <param name="target">The target <see cref="UIElement"/> to read the size updates from</param>
public static void BindSize(this CompositionObject source, UIElement target)
public static void BindSize(this Visual source, UIElement target)
{
var visual = ElementCompositionPreview.GetElementVisual(target);
var bindSizeAnimation = source.Compositor.CreateExpressionAnimation($"{nameof(visual)}.Size");
Expand Down
12 changes: 10 additions & 2 deletions Microsoft.Toolkit.Uwp.UI.Media/Pipelines/PipelineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Numerics;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.UI.Animations;
using Windows.Graphics.Effects;
Expand Down Expand Up @@ -188,15 +189,22 @@ public async Task<CompositionBrush> BuildAsync()
/// <returns>A <see cref="Task{T}"/> that returns the final <see cref="SpriteVisual"/> instance to use</returns>
public async Task<SpriteVisual> AttachAsync(UIElement target, UIElement reference = null)
{
var visual = Window.Current.Compositor.CreateSpriteVisual();
SpriteVisual visual = Window.Current.Compositor.CreateSpriteVisual();

visual.Brush = await BuildAsync();

ElementCompositionPreview.SetElementChildVisual(target, visual);

if (reference != null)
{
visual.BindSize(reference);
if (reference == target)
{
visual.RelativeSizeAdjustment = Vector2.One;
}
else
{
visual.BindSize(reference);
}
}

return visual;
Expand Down