misc:Refactor the WHAnimationHelper class structure to clearly define its responsibilities.

This commit is contained in:
望尘空忧
2025-08-10 00:00:30 +08:00
parent bb7873ef66
commit c801c4eec4
3 changed files with 50 additions and 4 deletions

View File

@@ -1,17 +1,18 @@
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Ursa.Controls; using Ursa.Controls;
using Ursa.Helpers;
namespace Ursa.Demo.Pages; namespace Ursa.Demo.Pages;
public partial class NavMenuDemo : UserControl public partial class NavMenuDemo : UserControl
{ {
private readonly WHAnimationHelper _animationHelper; private readonly NavMenuAnimationHelper _animationHelper;
public NavMenuDemo() public NavMenuDemo()
{ {
InitializeComponent(); InitializeComponent();
_animationHelper = new WHAnimationHelper(menu, NavMenu.IsHorizontalCollapsedProperty); _animationHelper = new NavMenuAnimationHelper(menu);
} }
protected override void OnLoaded(RoutedEventArgs e) protected override void OnLoaded(RoutedEventArgs e)

View File

@@ -0,0 +1,46 @@
using Avalonia;
using Avalonia.Animation;
using Avalonia.Animation.Easings;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.Styling;
using Ursa.Controls;
namespace Ursa.Helpers;
public class NavMenuAnimationHelper(NavMenu control) : WHAnimationHelper(control, NavMenu.IsHorizontalCollapsedProperty)
{
protected override Animation CreateAnimation(Size oldValue, Size newValue)
{
if (oldValue.Width > newValue.Width)
{
newValue = newValue.WithWidth(newValue.Width + 20);
}
return new Animation
{
Duration = TimeSpan.FromMilliseconds(300),
Easing = new CubicEaseInOut(),
FillMode = FillMode.None,
Children =
{
new KeyFrame
{
Cue = new Cue(0.0),
Setters =
{
new Setter(Layoutable.WidthProperty, oldValue.Width)
}
},
new KeyFrame
{
Cue = new Cue(1.0),
Setters =
{
new Setter(Layoutable.WidthProperty, newValue.Width)
}
}
}
};
}
}

View File

@@ -7,7 +7,7 @@ using Avalonia.Styling;
using Avalonia.Threading; using Avalonia.Threading;
using Avalonia.VisualTree; using Avalonia.VisualTree;
namespace Ursa.Controls; namespace Ursa.Helpers;
public class WHAnimationHelper(Control control, AvaloniaProperty property) public class WHAnimationHelper(Control control, AvaloniaProperty property)
{ {
@@ -41,7 +41,6 @@ public class WHAnimationHelper(Control control, AvaloniaProperty property)
var oldValue = control.DesiredSize; var oldValue = control.DesiredSize;
control.UpdateLayout(); control.UpdateLayout();
var newValue = control.DesiredSize; var newValue = control.DesiredSize;
newValue = newValue.WithWidth(newValue.Width + 20);
control.InvalidateArrange(); control.InvalidateArrange();
var animation = CreateAnimation(oldValue, newValue); var animation = CreateAnimation(oldValue, newValue);
animation.RunAsync(control, _cancellationTokenSource.Token); animation.RunAsync(control, _cancellationTokenSource.Token);