Skip to content

Commit

Permalink
optimize item animation
Browse files Browse the repository at this point in the history
  • Loading branch information
huynhsontung committed Dec 23, 2021
1 parent 4972992 commit b608148
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions Indirect/Controls/ThreadItemControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using NeoSmart.Unicode;
using System.Numerics;
using Windows.UI.Xaml.Hosting;
using Windows.UI.Composition;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

Expand All @@ -25,6 +26,7 @@ namespace Indirect.Controls
internal sealed partial class ThreadItemControl : UserControl
{
private static MainViewModel ViewModel => ((App)Application.Current).ViewModel;
private bool _visible;

public static readonly DependencyProperty ItemProperty = DependencyProperty.Register(
nameof(Item),
Expand All @@ -51,15 +53,19 @@ private static void OnItemSourceChanged(DependencyObject d, DependencyPropertyCh
public ThreadItemControl()
{
this.InitializeComponent();
Opacity = 0;
MainContentControl.SizeChanged += MainContentControl_SizeChanged;
MainContentControl.EffectiveViewportChanged += MainContentControl_EffectiveViewportChanged;
}

private void MainContentControl_EffectiveViewportChanged(FrameworkElement sender, EffectiveViewportChangedEventArgs args)
{
_visible = args.BringIntoViewDistanceY - sender.ActualHeight <= 0;
}

private void MainContentControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (Item == null) return;
if (Item == null || !_visible) return;

Opacity = 1;
var itemType = Item.Source.ItemType;
if (itemType == DirectItemType.ActionLog || itemType == DirectItemType.Like) return;

Expand All @@ -68,12 +74,22 @@ private void MainContentControl_SizeChanged(object sender, SizeChangedEventArgs
var prev = e.PreviousSize.ToVector2();
var next = e.NewSize.ToVector2();

var batch = Window.Current.Compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
var anim = Window.Current.Compositor.CreateVector3KeyFrameAnimation();
anim.InsertKeyFrame(0, new Vector3(prev / next, 1));
anim.InsertKeyFrame(1, Vector3.One);

var content = ((ContentControl)sender).ContentTemplateRoot;
var panel = ElementCompositionPreview.GetElementVisual(content);

if (!Item.IsInitialized)
{
var scalarAnim = Window.Current.Compositor.CreateScalarKeyFrameAnimation();
scalarAnim.InsertKeyFrame(0, 0);
scalarAnim.InsertKeyFrame(1, 1);
panel.StartAnimation("Opacity", scalarAnim);
}

panel.CenterPoint = new Vector3(Item.FromMe ? next.X : 0, 0, 0);
panel.StartAnimation("Scale", anim);

Expand All @@ -86,6 +102,8 @@ private void MainContentControl_SizeChanged(object sender, SizeChangedEventArgs
var text = ElementCompositionPreview.GetElementVisual(content.FindDescendant<TextBlock>());
text.StartAnimation("Scale", factor);
}

batch.End();
}

public void OnItemClick()
Expand Down

0 comments on commit b608148

Please sign in to comment.