Skip to content

Commit 0443b1e

Browse files
authored
Fix codegen to generate IAnimatedVisual2 and IAnimatedVisualSource2 interfaces for WinUI3 (#551)
* Unify animation controller initialization code for remapped time scales * Fixed CreateSpriteShape helper codegen * Allow IAV2 and IAVS2 to be implemented for WinUI3
1 parent 1570c14 commit 0443b1e

File tree

7 files changed

+39
-39
lines changed

7 files changed

+39
-39
lines changed

source/LottieToWinComp/Animate.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,27 @@ public static void WithKeyFrame(
4343
Debug.Assert(scale <= 1, "Precondition");
4444
Debug.Assert(animation.KeyFrameCount > 0, "Precondition");
4545

46-
if (scale == 1.0 && offset == 0.0 && context.ObjectFactory.IsUapApiAvailable(nameof(AnimationController)) && context.RootProgressController is not null)
46+
var key = new ScaleAndOffset(scale, offset);
47+
var state = context.GetStateCache<StateCache>();
48+
49+
if (context.ObjectFactory.IsUapApiAvailable(nameof(AnimationController)))
4750
{
48-
// Special case when we can use root AnimationController (no time stretch, no time offset)
49-
compObject.StartAnimation(target, animation, context.RootProgressController!);
51+
if (!state.ProgressControllers.TryGetValue(key, out var controllerCached))
52+
{
53+
controllerCached = context.ObjectFactory.CreateAnimationControllerList();
54+
controllerCached.Pause();
55+
56+
var rootProgressAnimation = context.ObjectFactory.CreateExpressionAnimation(scale == 1.0 && offset == 0.0 ? ExpressionFactory.RootProgress : ExpressionFactory.ScaledAndOffsetRootProgress(scale, offset));
57+
rootProgressAnimation.SetReferenceParameter(ExpressionFactory.RootName, context.RootVisual!);
58+
controllerCached.StartAnimation("Progress", rootProgressAnimation);
59+
60+
state.ProgressControllers.Add(key, controllerCached);
61+
}
62+
63+
compObject.StartAnimation(target, animation, controllerCached);
5064
return;
5165
}
5266

53-
var state = context.GetStateCache<StateCache>();
54-
5567
// Start the animation ...
5668
compObject.StartAnimation(target, animation);
5769

@@ -61,7 +73,6 @@ public static void WithKeyFrame(
6173
controller!.Pause();
6274

6375
// Bind it to the root visual's Progress property, scaling and offsetting if necessary.
64-
var key = new ScaleAndOffset(scale, offset);
6576
if (!state.ProgressBindingAnimations.TryGetValue(key, out var bindingAnimation))
6677
{
6778
bindingAnimation = context.ObjectFactory.CreateExpressionAnimation(ExpressionFactory.ScaledAndOffsetRootProgress(scale, offset));
@@ -853,7 +864,7 @@ void InsertExpressionKeyFrame(TCA animation, float progress, CubicBezierFunction
853864
}
854865

855866
// A pair of doubles used as a key in a dictionary.
856-
sealed class ScaleAndOffset
867+
public sealed class ScaleAndOffset
857868
{
858869
internal ScaleAndOffset(double scale, double offset)
859870
{
@@ -877,6 +888,9 @@ sealed class StateCache
877888
{
878889
public Dictionary<ScaleAndOffset, ExpressionAnimation> ProgressBindingAnimations { get; }
879890
= new Dictionary<ScaleAndOffset, ExpressionAnimation>();
891+
892+
public Dictionary<ScaleAndOffset, AnimationController> ProgressControllers { get; }
893+
= new Dictionary<ScaleAndOffset, AnimationController>();
880894
}
881895
}
882896
}

source/LottieToWinComp/TranslationContext.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@ internal static TranslationResult TryTranslateLottieComposition(
150150
/// </summary>
151151
public ContainerVisual? RootVisual { get; private set; }
152152

153-
/// <summary>
154-
/// AnimationController that has Progress property bound to RootVisual.Progress property.
155-
/// </summary>
156-
public AnimationController? RootProgressController { get; private set; }
157-
158153
/// <summary>
159154
/// True iff theme property bindings are enabled.
160155
/// </summary>
@@ -228,16 +223,6 @@ void Translate()
228223
// Add the master progress property to the visual.
229224
RootVisual.Properties.InsertScalar(ProgressPropertyName, 0);
230225

231-
// AnimationController that has Progress value bound to RootVisual.Progress
232-
if (ObjectFactory.IsUapApiAvailable(nameof(AnimationController)))
233-
{
234-
RootProgressController = ObjectFactory.CreateAnimationControllerList();
235-
var rootProgressAnimation = context.ObjectFactory.CreateExpressionAnimation(ExpressionFactory.RootProgress);
236-
rootProgressAnimation.SetReferenceParameter(ExpressionFactory.RootName, RootVisual);
237-
RootProgressController.Pause();
238-
RootProgressController.StartAnimation("Progress", rootProgressAnimation);
239-
}
240-
241226
// Add the translations of each layer to the root visual. This will recursively
242227
// add the tranlation of the layers in precomps.
243228
var contentsChildren = RootVisual.Children;

source/UIDataCodeGen/CodeGen/CSharp/CSharpInstantiatorGenerator.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ void WriteIAnimatedVisualSource(CodeBuilder builder, bool implementDynamicAVS)
273273
builder.WriteLine($": {Interface_IAnimatedVisualSource.GetQualifiedName(_s)}");
274274
}
275275

276-
if (SourceInfo.WinUIVersion >= new Version(2, 6) && SourceInfo.WinUIVersion.Major < 3)
276+
if (SourceInfo.WinUIVersion >= new Version(2, 6))
277277
{
278278
builder.WriteLine($", {Interface_IAnimatedVisualSource2.GetQualifiedName(_s)}");
279279
}
@@ -685,15 +685,17 @@ void WriteSetPropertyImpl(
685685
protected override void WriteAnimatedVisualStart(CodeBuilder builder, IAnimatedVisualInfo info)
686686
{
687687
// Start the instantiator class.
688+
builder.WriteLine($"sealed class {info.ClassName}");
689+
builder.Indent();
690+
builder.WriteLine($": {Interface_IAnimatedVisual.GetQualifiedName(_s)}");
691+
688692
if (info.ImplementCreateAndDestroyMethods)
689693
{
690-
builder.WriteLine($"sealed class {info.ClassName} : {Interface_IAnimatedVisual2.GetQualifiedName(_s)}");
691-
}
692-
else
693-
{
694-
builder.WriteLine($"sealed class {info.ClassName} : {Interface_IAnimatedVisual.GetQualifiedName(_s)}");
694+
builder.WriteLine($", {Interface_IAnimatedVisual2.GetQualifiedName(_s)}");
695695
}
696696

697+
builder.UnIndent();
698+
697699
builder.OpenScope();
698700
}
699701

source/UIDataCodeGen/CodeGen/CodegenConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,6 @@ Version winUIVersion
114114
/// </summary>
115115
public Version WinUIVersion { get; set; }
116116

117-
public bool ImplementCreateAndDestroyMethods => WinUIVersion >= Version.Parse("2.8") && WinUIVersion < Version.Parse("3.0");
117+
public bool ImplementCreateAndDestroyMethods => WinUIVersion >= Version.Parse("2.8");
118118
}
119119
}

source/UIDataCodeGen/CodeGen/Cppwinrt/CppwinrtInstantiatorGenerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ string GenerateIdlText()
145145
else
146146
{
147147
builder.WriteLine($": [default] {Interface_IAnimatedVisualSource.NormalizedQualifiedName}");
148+
}
148149

149-
if (SourceInfo.WinUIVersion >= new Version(2, 6) && SourceInfo.WinUIVersion.Major < 3)
150-
{
151-
builder.WriteLine($", {Interface_IAnimatedVisualSource2.NormalizedQualifiedName}");
152-
}
150+
if (SourceInfo.WinUIVersion >= new Version(2, 6))
151+
{
152+
builder.WriteLine($", {Interface_IAnimatedVisualSource2.NormalizedQualifiedName}");
153153
}
154154

155155
if (_isIDynamic)

source/UIDataCodeGen/CodeGen/Cx/CxInstantiatorGenerator.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ public static CxCodegenResult CreateFactoryCode(CodegenConfiguration configurati
7171
_animatedVisualTypeName = Interface_IAnimatedVisual.GetQualifiedName(_s);
7272
_animatedVisualTypeName2 = Interface_IAnimatedVisual2.GetQualifiedName(_s);
7373

74-
// Temporary until IAnimatedVisualSource2 makes it into WinUI3.
75-
_isAnimatedIcon = SourceInfo.WinUIVersion >= new Version(2, 6) && SourceInfo.WinUIVersion.Major < 3;
74+
_isAnimatedIcon = SourceInfo.WinUIVersion >= new Version(2, 6);
7675
}
7776

7877
static string FieldAssignment(string fieldName) => fieldName is not null ? $"{fieldName} = " : string.Empty;
@@ -134,7 +133,7 @@ void WriteIAnimatedVisualSourceHeaderText(HeaderBuilder builder)
134133

135134
inherits.Add(Interface_IAnimatedVisualSource.GetQualifiedName(_s));
136135

137-
if (SourceInfo.WinUIVersion >= new Version(2, 6) && SourceInfo.WinUIVersion.Major < 3)
136+
if (SourceInfo.WinUIVersion >= new Version(2, 6))
138137
{
139138
inherits.Add(Interface_IAnimatedVisualSource2.GetQualifiedName(_s));
140139
}
@@ -372,7 +371,7 @@ void WriteIDynamicAnimatedVisualSourceHeaderText(HeaderBuilder builder)
372371

373372
inherits.Add("Microsoft::UI::Xaml::Controls::IDynamicAnimatedVisualSource");
374373

375-
if (SourceInfo.WinUIVersion >= new Version(2, 6) && SourceInfo.WinUIVersion.Major < 3)
374+
if (SourceInfo.WinUIVersion >= new Version(2, 6))
376375
{
377376
inherits.Add(Interface_IAnimatedVisualSource2.GetQualifiedName(_s));
378377
}

source/UIDataCodeGen/CodeGen/InstantiatorGeneratorBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,7 +2212,7 @@ void WriteCallHelperCreateSpriteShape(
22122212
{
22132213
b.WriteLine($"{ReferenceTypeName("CompositionSpriteShape")} CreateSpriteShape({ReferenceTypeName("CompositionGeometry")} geometry, {_s.TypeMatrix3x2} transformMatrix)");
22142214
b.OpenScope();
2215-
WriteCreateAssignment(b, node, $"_c{Deref}CreateSpriteShape(geometry)");
2215+
b.WriteLine($"{ConstVar} result = _c{Deref}CreateSpriteShape(geometry);");
22162216
WriteSetPropertyStatement(b, "TransformMatrix", "transformMatrix");
22172217
b.WriteLine("return result;");
22182218
b.CloseScope();
@@ -2244,7 +2244,7 @@ void WriteCallHelperCreateSpriteShapeWithFillBrush(
22442244
{
22452245
b.WriteLine($"{ReferenceTypeName("CompositionSpriteShape")} CreateSpriteShape({ReferenceTypeName("CompositionGeometry")} geometry, {_s.TypeMatrix3x2} transformMatrix, {ReferenceTypeName("CompositionBrush")} fillBrush)");
22462246
b.OpenScope();
2247-
WriteCreateAssignment(b, node, $"_c{Deref}CreateSpriteShape(geometry)");
2247+
b.WriteLine($"{ConstVar} result = _c{Deref}CreateSpriteShape(geometry);");
22482248
WriteSetPropertyStatement(b, "TransformMatrix", "transformMatrix");
22492249
WriteSetPropertyStatement(b, "FillBrush", "fillBrush");
22502250
b.WriteLine("return result;");

0 commit comments

Comments
 (0)