Skip to content
Merged
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,16 @@ Conventions for contributors:

### Fixed

- **Elements using `.Keyframes()`, `.ScrollLinked()`, an implicit transition such as
`.OpacityTransition()`, `.WithTransitions()` or `.InteractionStates()` skip unchanged renders
again (issue #1702; follow-up to #1536, spec 014).** Each render rebuilds these declarations with
new arrays and WinUI objects, and the skip check compared those by reference, so such an element
always ran a full update. `.ScrollLinked()` also restarted its expressions on every parent render.
The declarations now compare by value: keyframes, scroll expressions with the same `ScrollViewer`,
transition durations and settings, and solid brushes by color and opacity. A changed or removed
declaration still updates. Turning Windows' "Animation effects" setting on or off now refreshes
every element in every Reactor host, so implicit transitions on unchanged elements follow it too.

- **`WindowLevel.AlwaysOnTop` refused by Windows is now reapplied (spec 054, issue #1707).** Windows
can decline a runtime topmost request from an app that doesn't own the foreground, for example
while the workstation is locked. The call reports success but the window never becomes topmost, and
Expand Down
13 changes: 8 additions & 5 deletions docs/_pipeline/templates/animation.md.dt
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ Animation and transition configuration changes are also checked on each render,
even when the element's other properties stay the same. For example, changing
only `.LayoutAnimation()`'s duration updates the layout animation on the next render.

Value-equivalent plain-data configurations such as `.Animate()` and
`.LayoutAnimation()` can still skip updates. Native objects and arrays use
reference equality: rebuilding `.OpacityTransition()`, `.WithTransitions()`,
`.Keyframes()`, or `.ScrollLinked()` with fresh objects or arrays prevents
skipping, even when the settings match.
Unchanged declarations still skip updates, even though each render rebuilds
them. `.Keyframes()` and `.ScrollLinked()` compare their keyframes and
expressions, implicit transitions such as `.OpacityTransition()` compare their
duration (and axes, for scale and translation), `.WithTransitions()` compares
each theme transition's settings, and `.InteractionStates()` compares solid
brushes by color and opacity. Other brushes, a `.ScrollLinked()` driven by a
different `ScrollViewer`, a continuum navigation transition, and a transition
type Reactor doesn't know still count as a change.

## Reference

Expand Down
13 changes: 8 additions & 5 deletions docs/guide/animation.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ Animation and transition configuration changes are also checked on each render,
even when the element's other properties stay the same. For example, changing
only `.LayoutAnimation()`'s duration updates the layout animation on the next render.

Value-equivalent plain-data configurations such as `.Animate()` and
`.LayoutAnimation()` can still skip updates. Native objects and arrays use
reference equality: rebuilding `.OpacityTransition()`, `.WithTransitions()`,
`.Keyframes()`, or `.ScrollLinked()` with fresh objects or arrays prevents
skipping, even when the settings match.
Unchanged declarations still skip updates, even though each render rebuilds
them. `.Keyframes()` and `.ScrollLinked()` compare their keyframes and
expressions, implicit transitions such as `.OpacityTransition()` compare their
duration (and axes, for scale and translation), `.WithTransitions()` compares
each theme transition's settings, and `.InteractionStates()` compares solid
brushes by color and opacity. Other brushes, a `.ScrollLinked()` driven by a
different `ScrollViewer`, a continuum navigation transition, and a transition
type Reactor doesn't know still count as a change.

## Reference

Expand Down
118 changes: 118 additions & 0 deletions src/Reactor/Animation/AnimationEquality.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System.Runtime.CompilerServices;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media.Animation;
using XamlTransition = Microsoft.UI.Xaml.Media.Animation.Transition;

namespace Microsoft.UI.Reactor.Animation;

/// <summary>
/// Value equality for animation declarations that modifiers rebuild on every render (issue #1702).
/// Element skip checks compare these, so two renders that declare the same animation compare
/// equal even though each allocated fresh arrays and WinUI objects. Unknown WinUI types fall
/// back to reference equality. Reading WinUI objects requires the UI thread, like reconcile.
/// </summary>
internal static class AnimationEquality
{
internal static bool SequenceEqual<T>(T[]? a, T[]? b)
{
if (ReferenceEquals(a, b)) return true;
if (a is null || b is null || a.Length != b.Length) return false;
var comparer = EqualityComparer<T>.Default;
for (int i = 0; i < a.Length; i++)
if (!comparer.Equals(a[i], b[i])) return false;
return true;
}

internal static int SequenceHash<T>(T[]? items)
{
if (items is null) return 0;
var hash = new HashCode();
foreach (var item in items) hash.Add(item);
return hash.ToHashCode();
}

/// <summary>Equality for the implicit transitions behind <c>.OpacityTransition()</c> and friends.</summary>
internal static bool ImplicitTransitionEqual(object? a, object? b)
{
if (ReferenceEquals(a, b)) return true;
return (a, b) switch
{
(ScalarTransition sa, ScalarTransition sb) => sa.Duration == sb.Duration,
(Vector3Transition va, Vector3Transition vb) => va.Duration == vb.Duration && va.Components == vb.Components,
(BrushTransition ba, BrushTransition bb) => ba.Duration == bb.Duration,
_ => false,
};
}

internal static bool ThemeTransitionsEqual(XamlTransition[]? a, XamlTransition[]? b)
{
if (ReferenceEquals(a, b)) return true;
if (a is null || b is null || a.Length != b.Length) return false;
for (int i = 0; i < a.Length; i++)
if (!ThemeTransitionEqual(a[i], b[i])) return false;
return true;
}

/// <summary>
/// Equality for WinUI's theme transitions, by type and settable properties. A transition type
/// missing from this table compares by reference, which only costs a skipped update.
/// </summary>
private static bool ThemeTransitionEqual(XamlTransition? a, XamlTransition? b)
{
if (ReferenceEquals(a, b)) return true;
return (a, b) switch
{
(EntranceThemeTransition ea, EntranceThemeTransition eb) =>
ea.FromHorizontalOffset == eb.FromHorizontalOffset && ea.FromVerticalOffset == eb.FromVerticalOffset
Comment thread
azchohfi marked this conversation as resolved.
Resolved
Comment thread
azchohfi marked this conversation as resolved.
Resolved
&& ea.IsStaggeringEnabled == eb.IsStaggeringEnabled,
(ContentThemeTransition ca, ContentThemeTransition cb) =>
ca.HorizontalOffset == cb.HorizontalOffset && ca.VerticalOffset == cb.VerticalOffset,
Comment thread
azchohfi marked this conversation as resolved.
Resolved
Comment thread
azchohfi marked this conversation as resolved.
Resolved
(PopupThemeTransition pa, PopupThemeTransition pb) =>
pa.FromHorizontalOffset == pb.FromHorizontalOffset && pa.FromVerticalOffset == pb.FromVerticalOffset,
Comment thread
azchohfi marked this conversation as resolved.
Resolved
Comment thread
azchohfi marked this conversation as resolved.
Resolved
(RepositionThemeTransition ra, RepositionThemeTransition rb) => ra.IsStaggeringEnabled == rb.IsStaggeringEnabled,
(EdgeUIThemeTransition ea, EdgeUIThemeTransition eb) => ea.Edge == eb.Edge,
(PaneThemeTransition pa, PaneThemeTransition pb) => pa.Edge == pb.Edge,
(NavigationThemeTransition na, NavigationThemeTransition nb) =>
NavigationInfoEqual(na.DefaultNavigationTransitionInfo, nb.DefaultNavigationTransitionInfo),
(AddDeleteThemeTransition, AddDeleteThemeTransition) => true,
(ReorderThemeTransition, ReorderThemeTransition) => true,
_ => false,
};
}

/// <summary>
/// Navigation transition infos compare by type and setting. A continuum transition names
/// an exit element, so it compares by reference.
/// </summary>
private static bool NavigationInfoEqual(NavigationTransitionInfo? a, NavigationTransitionInfo? b)
{
if (ReferenceEquals(a, b)) return true;
return (a, b) switch
{
(SlideNavigationTransitionInfo sa, SlideNavigationTransitionInfo sb) => sa.Effect == sb.Effect,
(CommonNavigationTransitionInfo ca, CommonNavigationTransitionInfo cb) => ca.IsStaggeringEnabled == cb.IsStaggeringEnabled,
(DrillInNavigationTransitionInfo, DrillInNavigationTransitionInfo) => true,
(EntranceNavigationTransitionInfo, EntranceNavigationTransitionInfo) => true,
(SuppressNavigationTransitionInfo, SuppressNavigationTransitionInfo) => true,
_ => false,
};
}

/// <summary>Hash for a value compared by reference.</summary>
internal static int IdentityHash(object? value) => value is null ? 0 : RuntimeHelpers.GetHashCode(value);
}

/// <summary>
/// An array compared by its elements, for array-valued animation declarations held in
/// <see cref="Core.AnimationExtras"/>.
/// </summary>
internal readonly struct ValueArray<T>(T[]? items) : IEquatable<ValueArray<T>>
{
internal T[]? Items { get; } = items;

public bool Equals(ValueArray<T> other) => AnimationEquality.SequenceEqual(Items, other.Items);

public override bool Equals(object? obj) => obj is ValueArray<T> other && Equals(other);

public override int GetHashCode() => AnimationEquality.SequenceHash(Items);
}
22 changes: 21 additions & 1 deletion src/Reactor/Animation/InteractionStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,27 @@ public record InteractionStateValues(
// Direct property set (pre-cached brush swap, ~1us)
Brush? Background = null,
Brush? Foreground = null,
Brush? BorderBrush = null);
Brush? BorderBrush = null)
{
/// <summary>
/// Compares solid brushes by color and opacity, so brushes rebuilt on every render compare
/// equal (issue #1702). Other brushes compare by reference.
/// </summary>
/// <remarks>Reads the brushes, so call it on the UI thread that created them.</remarks>
public virtual bool Equals(InteractionStateValues? other)
=> ReferenceEquals(this, other)
|| (other is not null && EqualityContract == other.EqualityContract
&& Opacity == other.Opacity && Scale == other.Scale && ScaleV == other.ScaleV
&& Translation == other.Translation && Rotation == other.Rotation
&& BrushHelper.BrushesEqual(Background, other.Background)
&& BrushHelper.BrushesEqual(Foreground, other.Foreground)
&& BrushHelper.BrushesEqual(BorderBrush, other.BorderBrush));

/// <summary>Hashes the brushes' presence only, without reading the WinUI brushes.</summary>
public override int GetHashCode()
=> HashCode.Combine(EqualityContract, Opacity, Scale, ScaleV, Translation, Rotation,
HashCode.Combine(Background is null, Foreground is null, BorderBrush is null));
}

/// <summary>
/// Configuration for the .InteractionStates() modifier.
Expand Down
13 changes: 13 additions & 0 deletions src/Reactor/Animation/KeyframeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ public record KeyframeAnimationDef
public TimeSpan Duration { get; init; } = TimeSpan.FromMilliseconds(300);
public bool Loop { get; init; }
public KeyframeDef[] Keyframes { get; init; } = [];

/// <summary>
/// Compares the keyframes by value, so a definition rebuilt on every render compares equal
/// (issue #1702).
/// </summary>
public virtual bool Equals(KeyframeAnimationDef? other)
=> ReferenceEquals(this, other)
|| (other is not null && EqualityContract == other.EqualityContract
&& Duration == other.Duration && Loop == other.Loop
&& AnimationEquality.SequenceEqual(Keyframes, other.Keyframes));

public override int GetHashCode()
=> HashCode.Combine(EqualityContract, Duration, Loop, AnimationEquality.SequenceHash(Keyframes));
}

/// <summary>
Expand Down
16 changes: 15 additions & 1 deletion src/Reactor/Animation/ScrollAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,21 @@ public record ScrollExpression(string Property, string Expression);
/// <summary>
/// Configuration for scroll-linked expression animations.
/// </summary>
public record ScrollAnimationConfig(ScrollViewer ScrollViewer, ScrollExpression[] Expressions);
public record ScrollAnimationConfig(ScrollViewer ScrollViewer, ScrollExpression[] Expressions)
{
/// <summary>
/// Compares the same <see cref="ScrollViewer"/> and the expressions by value, so a
/// configuration rebuilt on every render compares equal (issue #1702).
/// </summary>
public virtual bool Equals(ScrollAnimationConfig? other)
=> ReferenceEquals(this, other)
|| (other is not null && EqualityContract == other.EqualityContract
&& ReferenceEquals(ScrollViewer, other.ScrollViewer)
&& AnimationEquality.SequenceEqual(Expressions, other.Expressions));

public override int GetHashCode()
=> HashCode.Combine(EqualityContract, AnimationEquality.IdentityHash(ScrollViewer), AnimationEquality.SequenceHash(Expressions));
}

/// <summary>
/// Fluent builder for constructing scroll-linked expression animations.
Expand Down
9 changes: 6 additions & 3 deletions src/Reactor/Core/ChildReconciler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ private static bool UpdateChildAt(
|| Reconciler.IsFlyoutDecorator(newEl)
|| GestureDispatch.HasGestureOrDragSlots(newEl.Modifiers)
|| GestureDispatch.HasGestureOrDragSlots(oldEl.Modifiers)
|| Reconciler.CallSiteChangedOnSkip(oldEl, newEl))
|| Reconciler.CallSiteChangedOnSkip(oldEl, newEl)
|| Reconciler.KeyframesRebuiltOnSkip(oldEl, newEl))
&& (existingControl ??= children.Get(i)) is FrameworkElement fe)
{
reconciler.RefreshSkippedChildState(fe, oldEl, newEl);
Expand Down Expand Up @@ -660,7 +661,8 @@ private static void ReconcileKeyedCore(
|| Reconciler.IsFlyoutDecorator(newEl)
|| GestureDispatch.HasGestureOrDragSlots(newEl.Modifiers)
|| GestureDispatch.HasGestureOrDragSlots(oldEl.Modifiers)
|| Reconciler.CallSiteChangedOnSkip(oldEl, newEl))
|| Reconciler.CallSiteChangedOnSkip(oldEl, newEl)
|| Reconciler.KeyframesRebuiltOnSkip(oldEl, newEl))
&& map.At(prefixLen) < childCount
&& (existingControl ??= map.ControlAt(prefixLen)) is FrameworkElement fe)
{
Expand Down Expand Up @@ -714,7 +716,8 @@ private static void ReconcileKeyedCore(
|| Reconciler.IsFlyoutDecorator(newEl)
|| GestureDispatch.HasGestureOrDragSlots(newEl.Modifiers)
|| GestureDispatch.HasGestureOrDragSlots(oldEl.Modifiers)
|| Reconciler.CallSiteChangedOnSkip(oldEl, newEl))
|| Reconciler.CallSiteChangedOnSkip(oldEl, newEl)
|| Reconciler.KeyframesRebuiltOnSkip(oldEl, newEl))
&& map.At(oldIdx) >= 0
&& map.At(oldIdx) < childCount
&& (existingControl ??= map.ControlAt(oldIdx)) is FrameworkElement fe)
Expand Down
Loading
Loading