From 016617149f2047bd0d289e5d5610087030291051 Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Tue, 14 Jan 2025 15:02:19 -0500 Subject: [PATCH 1/2] perf(anim): Don't refresh triggers and transitions during parsing --- src/Uno.UI/UI/Xaml/VisualStateGroup.cs | 28 ++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Uno.UI/UI/Xaml/VisualStateGroup.cs b/src/Uno.UI/UI/Xaml/VisualStateGroup.cs index 654a33d05967..c19958dd8a4a 100644 --- a/src/Uno.UI/UI/Xaml/VisualStateGroup.cs +++ b/src/Uno.UI/UI/Xaml/VisualStateGroup.cs @@ -31,6 +31,7 @@ public sealed partial class VisualStateGroup : DependencyObject private readonly SerialDisposable _parentLoadedDisposable = new(); private (VisualState state, VisualTransition transition) _current; + private bool _pendingOnOwnerElementChanged; public event VisualStateChangedEventHandler CurrentStateChanging; @@ -159,11 +160,28 @@ private void OnParentChanged(object instance, object key, DependencyObjectParent } } - private void OnOwnerElementChanged() => + private void OnOwnerElementChanged() + { + if (this.GetParent() is IFrameworkElement fe + && fe.IsParsing) + { + _pendingOnOwnerElementChanged = true; + return; + } + ExecuteOnTriggers(t => t.OnOwnerElementChanged()); + } + + private void OnOwnerElementLoaded(object sender, RoutedEventArgs args) + { + if (_pendingOnOwnerElementChanged) + { + _pendingOnOwnerElementChanged = false; + OnOwnerElementChanged(); + } - private void OnOwnerElementLoaded(object sender, RoutedEventArgs args) => ExecuteOnTriggers(t => t.OnOwnerElementLoaded()); + } private void OnOwnerElementUnloaded(object sender, RoutedEventArgs args) => ExecuteOnTriggers(t => t.OnOwnerElementUnloaded()); @@ -500,6 +518,12 @@ static bool Match(VisualTransition transition, string from, string to) internal void RefreshStateTriggers(bool force = false) { + if (this.GetParent() is IFrameworkElement fe + && fe.IsParsing) + { + return; + } + var newState = GetActiveTrigger(); var oldState = CurrentState; if (newState == oldState) From 585aa584a17a7331cff7d135789b28c1dd725397 Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Wed, 15 Jan 2025 10:36:48 -0500 Subject: [PATCH 2/2] chore: Adjust test syntax --- src/Uno.UI/UI/Xaml/VisualStateGroup.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Uno.UI/UI/Xaml/VisualStateGroup.cs b/src/Uno.UI/UI/Xaml/VisualStateGroup.cs index c19958dd8a4a..10e3a0a6d974 100644 --- a/src/Uno.UI/UI/Xaml/VisualStateGroup.cs +++ b/src/Uno.UI/UI/Xaml/VisualStateGroup.cs @@ -162,8 +162,7 @@ private void OnParentChanged(object instance, object key, DependencyObjectParent private void OnOwnerElementChanged() { - if (this.GetParent() is IFrameworkElement fe - && fe.IsParsing) + if (this.GetParent() is IFrameworkElement { IsParsing: true }) { _pendingOnOwnerElementChanged = true; return; @@ -518,8 +517,7 @@ static bool Match(VisualTransition transition, string from, string to) internal void RefreshStateTriggers(bool force = false) { - if (this.GetParent() is IFrameworkElement fe - && fe.IsParsing) + if (this.GetParent() is IFrameworkElement { IsParsing: true }) { return; }