From e351303d1f5c83a93f68649c8d9e5a6338470704 Mon Sep 17 00:00:00 2001 From: Betta_Fish <96322503+zxbmmmmmmmmm@users.noreply.github.com> Date: Fri, 21 Nov 2025 17:20:52 +0800 Subject: [PATCH] feat: Allow stopping composition animation (#20074) * feat: Allow stopping composition animation * docs: Add XML comment * fix: Fix nullability * feat: Add StopAnimationGroup * docs: Add XML comments --------- Co-authored-by: Julien Lebosquain --- .../Composition/CompositionObject.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/Avalonia.Base/Rendering/Composition/CompositionObject.cs b/src/Avalonia.Base/Rendering/Composition/CompositionObject.cs index 9392c6a617..708cb49128 100644 --- a/src/Avalonia.Base/Rendering/Composition/CompositionObject.cs +++ b/src/Avalonia.Base/Rendering/Composition/CompositionObject.cs @@ -65,6 +65,18 @@ namespace Avalonia.Rendering.Composition throw new ArgumentException("Unknown property " + propertyName); } + /// Disconnects an animation from the specified property and stops the animation. + /// The name of the property to disconnect the animation from. + public void StopAnimation(string propertyName) + { + if (propertyName is null) + throw new ArgumentNullException(nameof(propertyName)); + if (Server is not ServerObject srv) + return; + var prop = srv.GetCompositionProperty(propertyName) ?? throw new ArgumentException("Unknown property " + propertyName); + srv.Animations?.RemoveAnimationForProperty(prop); + } + /// /// Starts an animation group. /// The StartAnimationGroup method on CompositionObject lets you start CompositionAnimationGroup. @@ -126,6 +138,27 @@ namespace Avalonia.Rendering.Composition throw new ArgumentException(); } + /// Stops an animation group. + /// The animation group to stop. + public void StopAnimationGroup(ICompositionAnimationBase grp) + { + if (grp is CompositionAnimation animation) + { + if (animation.Target == null) + throw new ArgumentException("Animation Target can't be null"); + StopAnimation(animation.Target); + } + else if (grp is CompositionAnimationGroup group) + { + foreach (var a in group.Animations) + { + if (a.Target == null) + throw new ArgumentException("Animation Target can't be null"); + StopAnimation(a.Target); + } + } + } + protected void RegisterForSerialization() { if (Server == null)