Skip to content

Commit 0b69c0c

Browse files
author
Simeon
authored
Add WinUI 2.6 animated icon support. (#412)
* Add support for WinUI 2.6 animated icon. This also removes the ability to set a custom interface in place of the WinUI interface, because nobody seems to need it now, and it adds a bunch of complexity.
1 parent fe7369a commit 0b69c0c

File tree

12 files changed

+337
-272
lines changed

12 files changed

+337
-272
lines changed

LottieGen/CommandLineOptions.cs

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,12 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using System.Text;
9-
10-
internal enum Lang
11-
{
12-
// Language wasn't recognized.
13-
Unknown,
14-
15-
// Language specified was ambigious.
16-
Ambiguous,
17-
18-
CSharp,
19-
Cx,
20-
Cppwinrt,
21-
LottieYaml,
22-
WinCompDgml,
23-
Stats,
24-
}
9+
using Microsoft.Toolkit.Uwp.UI.Lottie.UIData.CodeGen.CSharp;
2510

2611
sealed class CommandLineOptions
2712
{
2813
readonly List<string> _additionalInterfaces = new List<string>();
2914
readonly List<string> _languageStrings = new List<string>();
30-
string? _interfaceBaseName;
3115
Version? _winUIVersion;
3216

3317
internal IReadOnlyList<string> AdditionalInterfaces => _additionalInterfaces;
@@ -48,9 +32,7 @@ sealed class CommandLineOptions
4832

4933
internal string? InputFile { get; private set; }
5034

51-
internal string InterfaceBaseName => _interfaceBaseName ?? "Microsoft.UI.Xaml.Controls.IAnimatedVisual";
52-
53-
internal IReadOnlyList<Lang> Languages { get; private set; } = Array.Empty<Lang>();
35+
internal IReadOnlyList<Language> Languages { get; private set; } = Array.Empty<Language>();
5436

5537
internal uint? MinimumUapVersion { get; private set; }
5638

@@ -84,7 +66,7 @@ sealed class CommandLineOptions
8466
// for adding to generated code so that users can regenerate the code and know that
8567
// they got the set of options the same as a previous run. It does not include the
8668
// InputFile, OutputFolder, or Language options.
87-
internal string ToConfigurationCommandLine(string languageSwitch)
69+
internal string ToConfigurationCommandLine(Language languageSwitch)
8870
{
8971
var sb = new StringBuilder();
9072
sb.Append(ThisAssembly.AssemblyName);
@@ -117,8 +99,6 @@ internal string ToConfigurationCommandLine(string languageSwitch)
11799
sb.Append($" -{nameof(GenerateDependencyObject)}");
118100
}
119101

120-
sb.Append($" -{nameof(InterfaceBaseName)} {InterfaceBaseName}");
121-
122102
sb.Append($" -Language {languageSwitch}");
123103

124104
if (MinimumUapVersion.HasValue)
@@ -131,18 +111,28 @@ internal string ToConfigurationCommandLine(string languageSwitch)
131111
sb.Append($" -{nameof(Namespace)} {Namespace}");
132112
}
133113

134-
// The -Public switch is ignored for c++.
135-
if (Public &&
136-
!(languageSwitch.Equals("cppwinrt", StringComparison.OrdinalIgnoreCase) ||
137-
languageSwitch.Equals("cx", StringComparison.OrdinalIgnoreCase)))
114+
switch (languageSwitch)
138115
{
139-
sb.Append($" -{nameof(Public)}");
116+
case Language.Cx:
117+
case Language.Cppwinrt:
118+
// The -Public switch is ignored for c++.
119+
break;
120+
121+
default:
122+
sb.Append($" -{nameof(Public)}");
123+
break;
140124
}
141125

142-
// The -RootNamespace parameter is only used for cppwinrt.
143-
if (!string.IsNullOrWhiteSpace(RootNamespace) && languageSwitch.Equals("cppwinrt", StringComparison.OrdinalIgnoreCase))
126+
switch (languageSwitch)
144127
{
145-
sb.Append($" -{nameof(RootNamespace)} {RootNamespace}");
128+
case Language.Cppwinrt:
129+
// The -RootNamespace parameter is only used for cppwinrt.
130+
if (!string.IsNullOrWhiteSpace(RootNamespace))
131+
{
132+
sb.Append($" -{nameof(RootNamespace)} {RootNamespace}");
133+
}
134+
135+
break;
146136
}
147137

148138
if (StrictMode)
@@ -201,17 +191,17 @@ internal static CommandLineOptions ParseCommandLine(string[] args)
201191
result.ParseCommandLineStrings(args);
202192

203193
// Convert the language strings to language values.
204-
var languageTokenizer = new CommandlineTokenizer<Lang>(Lang.Ambiguous)
205-
.AddKeyword(Lang.CSharp)
206-
.AddKeyword(Lang.Cx, "cppcx")
207-
.AddKeyword(Lang.Cx)
208-
.AddKeyword(Lang.Cppwinrt)
209-
.AddKeyword(Lang.Cppwinrt, "winrtcpp")
210-
.AddKeyword(Lang.LottieYaml)
211-
.AddKeyword(Lang.WinCompDgml, "dgml")
212-
.AddKeyword(Lang.Stats);
213-
214-
var languages = new List<Lang>();
194+
var languageTokenizer = new CommandlineTokenizer<Language>(Language.Ambiguous)
195+
.AddKeyword(Language.CSharp)
196+
.AddKeyword(Language.Cx, "cppcx")
197+
.AddKeyword(Language.Cx)
198+
.AddKeyword(Language.Cppwinrt)
199+
.AddKeyword(Language.Cppwinrt, "winrtcpp")
200+
.AddKeyword(Language.LottieYaml)
201+
.AddKeyword(Language.WinCompDgml, "dgml")
202+
.AddKeyword(Language.Stats);
203+
204+
var languages = new List<Language>();
215205

216206
// Parse the language string.
217207
foreach (var languageString in result._languageStrings)
@@ -220,10 +210,10 @@ internal static CommandLineOptions ParseCommandLine(string[] args)
220210
languages.Add(language);
221211
switch (language)
222212
{
223-
case Lang.Unknown:
213+
case Language.Unknown:
224214
result.ErrorDescription = $"Unrecognized language: {languageString}";
225215
break;
226-
case Lang.Ambiguous:
216+
case Language.Ambiguous:
227217
result.ErrorDescription = $"Ambiguous language: {languageString}";
228218
break;
229219
}
@@ -342,16 +332,6 @@ void ParseCommandLineStrings(string[] args)
342332
InputFile = arg;
343333
previousKeyword = Keyword.None;
344334
break;
345-
case Keyword.Interface:
346-
if (_interfaceBaseName != null)
347-
{
348-
ErrorDescription = ArgumentSpecifiedMoreThanOnce("Interface base name");
349-
return;
350-
}
351-
352-
_interfaceBaseName = arg;
353-
previousKeyword = Keyword.None;
354-
break;
355335
case Keyword.Language:
356336
_languageStrings.Add(arg);
357337
previousKeyword = Keyword.None;
@@ -424,6 +404,7 @@ void ParseCommandLineStrings(string[] args)
424404

425405
previousKeyword = Keyword.None;
426406
break;
407+
427408
case Keyword.WinUIVersion:
428409
if (_winUIVersion != null)
429410
{

LottieGen/Language.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
enum Language
6+
{
7+
// Language wasn't recognized.
8+
Unknown,
9+
10+
// Language specified was ambigious.
11+
Ambiguous,
12+
13+
CSharp,
14+
Cx,
15+
Cppwinrt,
16+
LottieYaml,
17+
WinCompDgml,
18+
Stats,
19+
}

LottieGen/LottieJsonFileProcessor.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,18 @@ bool TryGenerateCode(LottieComposition lottieComposition)
144144

145145
var codeGenSucceeded = true;
146146

147-
var areBothCppwinrtAndCxRequested = _options.Languages.Where(l => l == Lang.Cppwinrt || l == Lang.Cx).Count() == 2;
147+
var areBothCppwinrtAndCxRequested = _options.Languages.Where(l => l == Language.Cppwinrt || l == Language.Cx).Count() == 2;
148148

149149
foreach (var lang in _options.Languages)
150150
{
151151
switch (lang)
152152
{
153-
case Lang.CSharp:
153+
case Language.CSharp:
154154
codeGenSucceeded &= TryGenerateCSharpCode(lottieComposition, $"{outputFileBase}.cs");
155155
_profiler.OnCodeGenFinished();
156156
break;
157157

158-
case Lang.Cx:
158+
case Language.Cx:
159159
{
160160
// If both cppwinrt and cx files were requested, add a differentiator to
161161
// the folder name for the cx files to make their names distinct from the
@@ -176,22 +176,22 @@ bool TryGenerateCode(LottieComposition lottieComposition)
176176
break;
177177
}
178178

179-
case Lang.Cppwinrt:
179+
case Language.Cppwinrt:
180180
codeGenSucceeded &= TryGenerateCppwinrtCode(lottieComposition, _outputFolder);
181181
_profiler.OnCodeGenFinished();
182182
break;
183183

184-
case Lang.LottieYaml:
184+
case Language.LottieYaml:
185185
codeGenSucceeded &= TryGenerateLottieYaml(lottieComposition, $"{outputFileBase}-Lottie.yaml");
186186
_profiler.OnSerializationFinished();
187187
break;
188188

189-
case Lang.WinCompDgml:
189+
case Language.WinCompDgml:
190190
codeGenSucceeded &= TryGenerateWincompDgml(lottieComposition, $"{outputFileBase}.dgml");
191191
_profiler.OnSerializationFinished();
192192
break;
193193

194-
case Lang.Stats:
194+
case Language.Stats:
195195
codeGenSucceeded &= TryGenerateStats(lottieComposition);
196196
break;
197197

@@ -434,15 +434,18 @@ bool TryGenerateWincompDgml(
434434
return result;
435435
}
436436

437-
bool TryGenerateCSharpCode(LottieComposition lottieComposition, string outputFilePath)
437+
bool TryGenerateCSharpCode(
438+
LottieComposition lottieComposition,
439+
string outputFilePath)
438440
{
439441
if (!TryEnsureTranslated(lottieComposition))
440442
{
441443
return false;
442444
}
443445

444446
var codegenResult =
445-
CSharpInstantiatorGenerator.CreateFactoryCode(CreateCodeGenConfiguration(lottieComposition, "CSharp"));
447+
CSharpInstantiatorGenerator.CreateFactoryCode(
448+
CreateCodeGenConfiguration(lottieComposition, Language.CSharp));
446449

447450
if (string.IsNullOrWhiteSpace(codegenResult.CsText))
448451
{
@@ -478,7 +481,7 @@ bool TryGenerateCppwinrtCode(LottieComposition lottieComposition, string outputF
478481
}
479482

480483
var codegenResult =
481-
CppwinrtInstantiatorGenerator.CreateFactoryCode(CreateCodeGenConfiguration(lottieComposition, "Cppwinrt"));
484+
CppwinrtInstantiatorGenerator.CreateFactoryCode(CreateCodeGenConfiguration(lottieComposition, Language.Cppwinrt));
482485

483486
if (string.IsNullOrWhiteSpace(codegenResult.CppText))
484487
{
@@ -532,7 +535,7 @@ bool TryGenerateCXCode(LottieComposition lottieComposition, string outputFolder)
532535
}
533536

534537
var codegenResult =
535-
CxInstantiatorGenerator.CreateFactoryCode(CreateCodeGenConfiguration(lottieComposition, "CX"));
538+
CxInstantiatorGenerator.CreateFactoryCode(CreateCodeGenConfiguration(lottieComposition, Language.Cx));
536539

537540
if (string.IsNullOrWhiteSpace(codegenResult.CppText))
538541
{
@@ -635,7 +638,7 @@ static bool TryEnsureDirectoryExists(string directoryPath)
635638

636639
CodegenConfiguration CreateCodeGenConfiguration(
637640
LottieComposition lottieComposition,
638-
string languageSwitch)
641+
Language languageSwitch)
639642
{
640643
if (_translationResults is null)
641644
{
@@ -644,7 +647,6 @@ CodegenConfiguration CreateCodeGenConfiguration(
644647

645648
var result = new CodegenConfiguration(
646649
className: _className,
647-
interfaceType: _options.InterfaceBaseName,
648650
additionalInterfaces: _options.AdditionalInterfaces,
649651
objectGraphs: _translationResults.Select(
650652
tr => ((CompositionObject?)tr.RootVisual!, tr.MinimumRequiredUapVersion)).ToArray(),
@@ -674,7 +676,7 @@ CodegenConfiguration CreateCodeGenConfiguration(
674676
// Returns lines that describe the invocation of this tool.
675677
// This information is passed to the code generator so that it can
676678
// be included in the generated output.
677-
IEnumerable<string> GetToolInvocationInfo(string languageSwitch)
679+
IEnumerable<string> GetToolInvocationInfo(Language languageSwitch)
678680
{
679681
var inputFile = new FileInfo(_sourceFilePath);
680682

0 commit comments

Comments
 (0)