From 754126e5f7ad79dc71d686efcae6a0cf85ff69a9 Mon Sep 17 00:00:00 2001 From: polarboy Date: Fri, 9 Aug 2024 12:16:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=20Collapse=20=E5=8A=A8?= =?UTF-8?q?=E7=94=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 完成 Collapse 动画 --- src/AtomUI.Base/MotionScene/CollapseMotion.cs | 6 ++- src/AtomUI.Controls/Collapse/Collapse.cs | 21 ++++++-- src/AtomUI.Controls/Collapse/CollapseItem.cs | 51 +++++++++++-------- 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/src/AtomUI.Base/MotionScene/CollapseMotion.cs b/src/AtomUI.Base/MotionScene/CollapseMotion.cs index 1304bbd74..df57e37a5 100644 --- a/src/AtomUI.Base/MotionScene/CollapseMotion.cs +++ b/src/AtomUI.Base/MotionScene/CollapseMotion.cs @@ -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); + } } } } diff --git a/src/AtomUI.Controls/Collapse/Collapse.cs b/src/AtomUI.Controls/Collapse/Collapse.cs index 84e121094..f1c3ccbc5 100644 --- a/src/AtomUI.Controls/Collapse/Collapse.cs +++ b/src/AtomUI.Controls/Collapse/Collapse.cs @@ -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); + } + } } } } diff --git a/src/AtomUI.Controls/Collapse/CollapseItem.cs b/src/AtomUI.Controls/Collapse/CollapseItem.cs index 5353c71c7..040abb997 100644 --- a/src/AtomUI.Controls/Collapse/CollapseItem.cs +++ b/src/AtomUI.Controls/Collapse/CollapseItem.cs @@ -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()