完成 Collapse 动画

完成 Collapse 动画
This commit is contained in:
polarboy
2024-08-09 12:16:50 +08:00
parent 2a0698a7e3
commit 754126e5f7
3 changed files with 54 additions and 24 deletions
@@ -39,7 +39,11 @@ public class CollapseMotion : AbstractMotion
{
base.NotifyPreBuildTransition(config, motionTarget);
if (config.Property == MotionHeightProperty) {
config.StartValue = Math.Ceiling(motionTarget.DesiredSize.Height);
if (!double.IsNaN(motionTarget.Height)) {
config.StartValue = Math.Ceiling(motionTarget.Height);
} else {
config.StartValue = Math.Ceiling(motionTarget.DesiredSize.Height);
}
}
}
}
+18 -3
View File
@@ -197,7 +197,12 @@ public class Collapse : SelectingItemsControl
base.OnGotFocus(e);
if (e.NavigationMethod == NavigationMethod.Directional) {
e.Handled = UpdateSelectionFromEventSource(e.Source);
Control? containerFromEventSource = GetContainerFromEventSource(e.Source);
if (containerFromEventSource is CollapseItem collapseItem) {
if (!collapseItem.IsAnimating) {
e.Handled = UpdateSelectionFromEventSource(e.Source);
}
}
}
}
@@ -206,7 +211,12 @@ public class Collapse : SelectingItemsControl
base.OnPointerPressed(e);
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed && e.Pointer.Type == PointerType.Mouse) {
e.Handled = UpdateSelectionFromEventSource(e.Source);
Control? containerFromEventSource = GetContainerFromEventSource(e.Source);
if (containerFromEventSource is CollapseItem collapseItem) {
if (!collapseItem.IsAnimating) {
e.Handled = UpdateSelectionFromEventSource(e.Source);
}
}
}
}
@@ -217,7 +227,12 @@ public class Collapse : SelectingItemsControl
if (container != null
&& container.GetVisualsAt(e.GetPosition(container))
.Any(c => container == c || container.IsVisualAncestorOf(c))) {
e.Handled = UpdateSelectionFromEventSource(e.Source);
Control? containerFromEventSource = GetContainerFromEventSource(e.Source);
if (containerFromEventSource is CollapseItem collapseItem) {
if (!collapseItem.IsAnimating) {
e.Handled = UpdateSelectionFromEventSource(e.Source);
}
}
}
}
}
+31 -20
View File
@@ -9,10 +9,8 @@ using Avalonia.Automation.Peers;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Mixins;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Layout;
using Avalonia.Threading;
namespace AtomUI.Controls;
@@ -153,6 +151,9 @@ public class CollapseItem : HeaderedContentControl, ISelectable
}
private bool _animating = false;
private bool _enableAnimation = true;
internal bool IsAnimating => _animating;
protected override AutomationPeer OnCreateAutomationPeer() => new ListItemAutomationPeer(this);
@@ -180,7 +181,9 @@ public class CollapseItem : HeaderedContentControl, ISelectable
base.OnApplyTemplate(e);
TokenResourceBinder.CreateTokenBinding(this, MotionDurationProperty, GlobalResourceKey.MotionDurationSlow);
SetupIconButton();
_enableAnimation = false;
HandleSelectedChanged();
_enableAnimation = true;
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
@@ -213,7 +216,11 @@ public class CollapseItem : HeaderedContentControl, ISelectable
if (Presenter is null || _animating) {
return;
}
Presenter.IsVisible = true;
if (!_enableAnimation) {
return;
}
LayoutHelper.MeasureChild(Presenter, new Size(Bounds.Width, double.PositiveInfinity), new Thickness());
_animating = true;
var director = Director.Instance;
@@ -231,24 +238,28 @@ public class CollapseItem : HeaderedContentControl, ISelectable
private void CollapseItemContent()
{
Presenter!.IsVisible = false;
// if (Presenter is null || _animating) {
// return;
// }
// _animating = true;
// LayoutHelper.MeasureChild(Presenter, new Size(Bounds.Width, double.PositiveInfinity), new Thickness());
// var director = Director.Instance;
// var motion = new CollapseMotion();
// motion.ConfigureOpacity(MotionDuration);
// motion.ConfigureHeight(MotionDuration);
// var motionActor = new MotionActor(Presenter, motion);
// motionActor.DispatchInSceneLayer = false;
// motionActor.Completed += (sender, args) =>
// {
// _animating = false;
// Presenter.IsVisible = false;
// };
// director?.Schedule(motionActor);
if (Presenter is null || _animating) {
return;
}
if (!_enableAnimation) {
Presenter.IsVisible = false;
return;
}
_animating = true;
LayoutHelper.MeasureChild(Presenter, new Size(Bounds.Width, double.PositiveInfinity), new Thickness());
var director = Director.Instance;
var motion = new CollapseMotion();
motion.ConfigureOpacity(MotionDuration);
motion.ConfigureHeight(MotionDuration);
var motionActor = new MotionActor(Presenter, motion);
motionActor.DispatchInSceneLayer = false;
motionActor.Completed += (sender, args) =>
{
_animating = false;
Presenter.IsVisible = false;
};
director?.Schedule(motionActor);
}
private void SetupIconButton()