Skip to content

Commit

Permalink
settingslayout animation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kikipoulet committed Sep 17, 2024
1 parent 037f7f4 commit 46f5ae4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 93 deletions.
4 changes: 2 additions & 2 deletions SukiUI/Controls/SettingsLayout.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@
<Style Selector="controls|SettingsLayout">
<Setter Property="Template">
<ControlTemplate>
<DockPanel MaxWidth="1400" SizeChanged="DockPanel_SizeChanged">
<DockPanel MaxWidth="1400" SizeChanged="DockPanel_SizeChanged">
<StackPanel Name="StackSummary"
Width="400"
Margin="0,25,0,0"
HorizontalAlignment="Left"
DockPanel.Dock="Left" />
<ScrollViewer Name="MyScroll" Classes="Stack">
<StackPanel Name="StackItems"
Margin="-10,0"
Margin="20,0"
theme:StackPanelExtensions.AnimatedScroll="True" />
</ScrollViewer>
</DockPanel>
Expand Down
111 changes: 20 additions & 91 deletions SukiUI/Controls/SettingsLayout.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Layout;

namespace SukiUI.Controls;

Expand Down Expand Up @@ -137,109 +139,34 @@ private void UpdateItems()
};
}

private Mutex mut = new Mutex();

private double LastDesiredSize = -1;

private async void DockPanel_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (isAnimatingWidth)

var stack = this.GetTemplateChildren().First(n => n.Name == "StackSummary");
var desiredSize = e.NewSize.Width > 1100 ? 400 : 0;

if(LastDesiredSize == desiredSize)
return;

var currentwidth = this.GetTemplateChildren().First(n => n.Name == "StackSummary").Width;
var desiredSize = e.NewSize.Width > 1000 ? 400 : 0;

if (desiredSize != currentwidth)
if ((currentwidth == 0 || currentwidth == 400))
await AnimateSummaryWidth(this.GetTemplateChildren().First(n => n.Name == "StackSummary").Width, desiredSize);
LastDesiredSize = desiredSize;

if (e.NewSize.Width <= 1000 && e.NewSize.Width > 850)
await AnimateItemsMargin(new Thickness(100, 0));
else if (e.NewSize.Width <= 850 && e.NewSize.Width > 700)
await AnimateItemsMargin(new Thickness(50, 0));
else
await AnimateItemsMargin(new Thickness(-10, 0));
if (stack.Width != desiredSize && (stack.Width == 0 || stack.Width == 400))
stack.Animate<double>(WidthProperty, stack.Width, desiredSize, TimeSpan.FromMilliseconds(800));


}

private bool isAnimatingWidth = false;
private bool isAnimatingMargin = false;
private bool isAnimatingScroll = false;

private async Task AnimateItemsMargin(Thickness desiredSize)
{
if (isAnimatingMargin)
return;

var myScroll = (ScrollViewer)this.GetTemplateChildren().First(n => n.Name == "MyScroll");
if (myScroll.Content is not StackPanel stackItems)
return;

if (stackItems.Margin.Left == desiredSize.Left)
return;

isAnimatingMargin = true;

var animationTask = new Animation
{
Duration = TimeSpan.FromMilliseconds(800),
FillMode = FillMode.Forward,
Easing = new CubicEaseInOut(),
IterationCount = new IterationCount(1),
PlaybackDirection = PlaybackDirection.Normal,
Children =
{
new KeyFrame()
{
Setters = { new Setter { Property = MarginProperty, Value = stackItems.Margin } },
KeyTime = TimeSpan.FromSeconds(0)
},
new KeyFrame()
{
Setters = { new Setter { Property = MarginProperty, Value = desiredSize } },
KeyTime = TimeSpan.FromMilliseconds(800)
}
}
}.RunAsync(stackItems);

var abortTask = Task.Run(async () =>
{
await Task.Delay(1000);
isAnimatingMargin = false;
});

await Task.WhenAll(animationTask, abortTask);
}

private async Task AnimateSummaryWidth(double current, double desiredSize)
{
isAnimatingWidth = true;

var animationTask = new Animation
{
Duration = TimeSpan.FromMilliseconds(800),
FillMode = FillMode.Forward,
Easing = new CubicEaseInOut(),
IterationCount = new IterationCount(1),
PlaybackDirection = PlaybackDirection.Normal,
Children =
{
new KeyFrame()
{
Setters = { new Setter { Property = WidthProperty, Value = current } },
KeyTime = TimeSpan.FromSeconds(0)
},
new KeyFrame()
{
Setters = { new Setter { Property = WidthProperty, Value = desiredSize } },
KeyTime = TimeSpan.FromMilliseconds(800)
}
}
}.RunAsync(this.GetTemplateChildren().First(n => n.Name == "StackSummary"));

var abortTask = Task.Run(async () =>
{
await Task.Delay(1000);
isAnimatingWidth = false;
});

await Task.WhenAll(animationTask, abortTask);
}


private async Task AnimateScroll(double desiredScroll)
{
Expand All @@ -262,7 +189,7 @@ private async Task AnimateScroll(double desiredScroll)
},
new KeyFrame()
{
Setters = { new Setter { Property = ScrollViewer.OffsetProperty, Value = new Vector(myscroll.Offset.X, desiredScroll) } },
Setters = { new Setter { Property = ScrollViewer.OffsetProperty, Value = new Vector(myscroll.Offset.X, desiredScroll -30) } },
KeyTime = TimeSpan.FromMilliseconds(800)
}
}
Expand All @@ -276,4 +203,6 @@ private async Task AnimateScroll(double desiredScroll)

await Task.WhenAll(animationTask, abortTask);
}


}

0 comments on commit 46f5ae4

Please sign in to comment.