Skip to content

Commit d262481

Browse files
authored
Fixed codegen to produce correct WinUI3 code (#520)
1 parent 70c4a4e commit d262481

File tree

3 files changed

+69
-29
lines changed

3 files changed

+69
-29
lines changed

source/UIDataCodeGen/CodeGen/CSharp/CSharpInstantiatorGenerator.cs

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ sealed class CSharpInstantiatorGenerator : InstantiatorGeneratorBase
2626
{
2727
readonly Stringifier _s;
2828
readonly string _winUiNamespace;
29-
readonly string _winUi3CastHack;
3029

3130
CSharpInstantiatorGenerator(
3231
CodegenConfiguration configuration,
@@ -41,15 +40,10 @@ sealed class CSharpInstantiatorGenerator : InstantiatorGeneratorBase
4140
if (AnimatedVisualSourceInfo.WinUIVersion.Major >= 3)
4241
{
4342
_winUiNamespace = "Microsoft.UI";
44-
45-
// This is a hack that is required to use Win2D with WinUI3 as of August 2020. It
46-
// will not be necessary when an official Win2D for WinUI3 is released.
47-
_winUi3CastHack = "(IGeometrySource2D)(object)";
4843
}
4944
else
5045
{
5146
_winUiNamespace = "Windows.UI";
52-
_winUi3CastHack = string.Empty;
5347
}
5448
}
5549

@@ -95,7 +89,15 @@ protected override void WriteImplementationFileStart(CodeBuilder builder)
9589
if (SourceInfo.UsesCanvasGeometry)
9690
{
9791
// Windows.Graphics is needed for IGeometrySource2D.
98-
namespaces.Add("Windows.Graphics");
92+
if (SourceInfo.WinUIVersion.Major < 3)
93+
{
94+
namespaces.Add("Windows.Graphics");
95+
}
96+
else
97+
{
98+
namespaces.Add("Microsoft.Graphics");
99+
}
100+
99101
namespaces.Add("Microsoft.Graphics.Canvas.Geometry");
100102
}
101103

@@ -472,7 +474,15 @@ void WriteIDynamicAnimatedVisualSource(CodeBuilder builder)
472474
builder.WriteLine("bool _isImageLoadingAsynchronous;");
473475
builder.WriteLine("bool _isTryCreateAnimatedVisualCalled;");
474476
builder.WriteLine("bool _isImageLoadingStarted;");
475-
builder.WriteLine("EventRegistrationTokenTable<TypedEventHandler<IDynamicAnimatedVisualSource, object>> _animatedVisualInvalidatedEventTokenTable;");
477+
478+
if (AnimatedVisualSourceInfo.WinUIVersion.Major >= 3)
479+
{
480+
builder.WriteLine("HashSet<TypedEventHandler<IDynamicAnimatedVisualSource, object>> _animatedVisualInvalidatedEventTokenTable = new HashSet<TypedEventHandler<IDynamicAnimatedVisualSource, object>>();");
481+
}
482+
else
483+
{
484+
builder.WriteLine("EventRegistrationTokenTable<TypedEventHandler<IDynamicAnimatedVisualSource, object>> _animatedVisualInvalidatedEventTokenTable;");
485+
}
476486

477487
// Declare the variables to hold the LoadedImageSurfaces.
478488
foreach (var n in SourceInfo.LoadedImageSurfaces)
@@ -587,10 +597,13 @@ void WriteIDynamicAnimatedVisualSource(CodeBuilder builder)
587597
builder.WriteLine();
588598

589599
// Generate the method that get or create the EventRegistrationTokenTable.
590-
builder.WriteLine("EventRegistrationTokenTable<TypedEventHandler<IDynamicAnimatedVisualSource, object>> GetAnimatedVisualInvalidatedEventRegistrationTokenTable()");
591-
builder.OpenScope();
592-
builder.WriteLine("return EventRegistrationTokenTable<TypedEventHandler<IDynamicAnimatedVisualSource, object>>.GetOrCreateEventRegistrationTokenTable(ref _animatedVisualInvalidatedEventTokenTable);");
593-
builder.CloseScope();
600+
if (AnimatedVisualSourceInfo.WinUIVersion.Major < 3)
601+
{
602+
builder.WriteLine("EventRegistrationTokenTable<TypedEventHandler<IDynamicAnimatedVisualSource, object>> GetAnimatedVisualInvalidatedEventRegistrationTokenTable()");
603+
builder.OpenScope();
604+
builder.WriteLine("return EventRegistrationTokenTable<TypedEventHandler<IDynamicAnimatedVisualSource, object>>.GetOrCreateEventRegistrationTokenTable(ref _animatedVisualInvalidatedEventTokenTable);");
605+
builder.CloseScope();
606+
}
594607
}
595608

596609
/// <summary>
@@ -860,7 +873,7 @@ protected override void WriteCanvasGeometryCombinationFactory(CodeBuilder builde
860873
/// <inheritdoc/>
861874
protected override void WriteCanvasGeometryEllipseFactory(CodeBuilder builder, CanvasGeometry.Ellipse obj, string typeName, string fieldName)
862875
{
863-
builder.WriteLine($"var result = {FieldAssignment(fieldName)}{_winUi3CastHack}CanvasGeometry.CreateEllipse(");
876+
builder.WriteLine($"var result = {FieldAssignment(fieldName)}CanvasGeometry.CreateEllipse(");
864877
builder.Indent();
865878
builder.WriteLine($"null,");
866879
builder.WriteLine($"{_s.Float(obj.X)}, {_s.Float(obj.Y)}, {_s.Float(obj.RadiusX)}, {_s.Float(obj.RadiusY)});");
@@ -870,7 +883,7 @@ protected override void WriteCanvasGeometryEllipseFactory(CodeBuilder builder, C
870883
/// <inheritdoc/>
871884
protected override void WriteCanvasGeometryGroupFactory(CodeBuilder builder, CanvasGeometry.Group obj, string typeName, string fieldName)
872885
{
873-
builder.WriteLine($"var result = {FieldAssignment(fieldName)}{_winUi3CastHack}CanvasGeometry.CreateGroup(");
886+
builder.WriteLine($"var result = {FieldAssignment(fieldName)}CanvasGeometry.CreateGroup(");
874887
builder.Indent();
875888
builder.WriteLine($"null,");
876889
builder.WriteLine($"new CanvasGeometry[] {{ {string.Join(", ", obj.Geometries.Select(g => CallFactoryFor(g))) } }},");
@@ -911,14 +924,14 @@ protected override void WriteCanvasGeometryPathFactory(CodeBuilder builder, Canv
911924
}
912925
}
913926

914-
builder.WriteLine($"result = {FieldAssignment(fieldName)}{_winUi3CastHack}CanvasGeometry.CreatePath(builder);");
927+
builder.WriteLine($"result = {FieldAssignment(fieldName)}CanvasGeometry.CreatePath(builder);");
915928
builder.CloseScope();
916929
}
917930

918931
/// <inheritdoc/>
919932
protected override void WriteCanvasGeometryRoundedRectangleFactory(CodeBuilder builder, CanvasGeometry.RoundedRectangle obj, string typeName, string fieldName)
920933
{
921-
builder.WriteLine($"var result = {FieldAssignment(fieldName)}{_winUi3CastHack}CanvasGeometry.CreateRoundedRectangle(");
934+
builder.WriteLine($"var result = {FieldAssignment(fieldName)}CanvasGeometry.CreateRoundedRectangle(");
922935
builder.Indent();
923936
builder.WriteLine("null,");
924937
builder.WriteLine($"{_s.Float(obj.X)},");
@@ -978,11 +991,28 @@ void WriteAnimatedVisualInvalidatedEvent(CodeBuilder builder)
978991
builder.OpenScope();
979992
builder.WriteLine("add");
980993
builder.OpenScope();
981-
builder.WriteLine("return GetAnimatedVisualInvalidatedEventRegistrationTokenTable().AddEventHandler(value);");
994+
if (AnimatedVisualSourceInfo.WinUIVersion.Major >= 3)
995+
{
996+
builder.WriteLine("_animatedVisualInvalidatedEventTokenTable.Add(value);");
997+
}
998+
else
999+
{
1000+
builder.WriteLine("return GetAnimatedVisualInvalidatedEventRegistrationTokenTable().AddEventHandler(value);");
1001+
}
1002+
9821003
builder.CloseScope();
9831004
builder.WriteLine("remove");
9841005
builder.OpenScope();
985-
builder.WriteLine("GetAnimatedVisualInvalidatedEventRegistrationTokenTable().RemoveEventHandler(value);");
1006+
1007+
if (AnimatedVisualSourceInfo.WinUIVersion.Major >= 3)
1008+
{
1009+
builder.WriteLine("_animatedVisualInvalidatedEventTokenTable.Remove(value);");
1010+
}
1011+
else
1012+
{
1013+
builder.WriteLine("GetAnimatedVisualInvalidatedEventRegistrationTokenTable().RemoveEventHandler(value);");
1014+
}
1015+
9861016
builder.CloseScope();
9871017
builder.CloseScope();
9881018
builder.WriteLine();
@@ -1036,7 +1066,15 @@ void WriteHandleLoadCompleted(CodeBuilder builder)
10361066
// the previous result is now invalidated.
10371067
builder.WriteLine("if (_isImageLoadingAsynchronous)");
10381068
builder.OpenScope();
1039-
builder.WriteLine("_animatedVisualInvalidatedEventTokenTable?.InvocationList?.Invoke(this, null);");
1069+
if (AnimatedVisualSourceInfo.WinUIVersion.Major >= 3)
1070+
{
1071+
builder.WriteLine("foreach (var v in _animatedVisualInvalidatedEventTokenTable) v.Invoke(this, null);");
1072+
}
1073+
else
1074+
{
1075+
builder.WriteLine("_animatedVisualInvalidatedEventTokenTable?.InvocationList?.Invoke(this, null);");
1076+
}
1077+
10401078
builder.CloseScope();
10411079
builder.CloseScope();
10421080
builder.CloseScope();

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.7");
117+
public bool ImplementCreateAndDestroyMethods => WinUIVersion >= Version.Parse("2.7") && WinUIVersion < Version.Parse("3.0");
118118
}
119119
}

source/UIDataCodeGen/CodeGen/Cppwinrt/CppwinrtInstantiatorGenerator.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ sealed class CppwinrtInstantiatorGenerator : InstantiatorGeneratorBase
3333
// that contains the TryCreateAnimatedVisual method.
3434
readonly string _sourceClassName;
3535
readonly string _wuc;
36+
readonly string _winNamespace;
3637
readonly string _winUINamespace;
3738

3839
// The fully qualified name of the AnimatedVisual type that is returned
@@ -97,6 +98,7 @@ public static CppwinrtCodegenResult CreateFactoryCode(CodegenConfiguration confi
9798
_isIDynamic = SourceInfo.LoadedImageSurfaces.Any();
9899

99100
_winUINamespace = SourceInfo.WinUIVersion.Major >= 3 ? "Microsoft::UI" : "Windows::UI";
101+
_winNamespace = SourceInfo.WinUIVersion.Major >= 3 ? "Microsoft" : "Windows";
100102
_wuc = $"{_winUINamespace}::Composition";
101103
_sourceClassName = SourceInfo.ClassName;
102104

@@ -152,7 +154,7 @@ string GenerateIdlText()
152154

153155
if (_isIDynamic)
154156
{
155-
builder.WriteLine(", Windows.UI.Xaml.Data.INotifyPropertyChanged");
157+
builder.WriteLine($", {_winNamespace}.UI.Xaml.Data.INotifyPropertyChanged");
156158
}
157159

158160
foreach (var additionalInterface in SourceInfo.AdditionalInterfaces.Select(n => n.NormalizedQualifiedName))
@@ -450,22 +452,22 @@ void WriteIDynamicAnimatedVisualSourceHeaderText(HeaderBuilder builder)
450452
priv.WriteLine("Microsoft::UI::Xaml::Controls::IDynamicAnimatedVisualSource,");
451453
priv.WriteLine("Windows::Foundation::IInspectable>> m_IDynamicAnimatedVisualSourceEvent{};");
452454
priv.UnIndent();
453-
priv.WriteLine("winrt::event<Windows::UI::Xaml::Data::PropertyChangedEventHandler> m_PropertyChanged{};");
455+
priv.WriteLine($"winrt::event<{_winUINamespace}::Xaml::Data::PropertyChangedEventHandler> m_PropertyChanged{{}};");
454456

455457
foreach (var n in SourceInfo.LoadedImageSurfaces)
456458
{
457-
priv.WriteLine($"winrt::Windows::UI::Xaml::Media::{n.TypeName} {n.FieldName}{{ nullptr }};");
459+
priv.WriteLine($"winrt::{_winUINamespace}::Xaml::Media::{n.TypeName} {n.FieldName}{{ nullptr }};");
458460
}
459461

460462
priv.WriteLine("void EnsureImageLoadingStarted();");
461463
priv.WriteLine("void HandleLoadCompleted(");
462464
priv.Indent();
463-
priv.WriteLine("winrt::Windows::UI::Xaml::Media::LoadedImageSurface sender,");
464-
priv.WriteLine("winrt::Windows::UI::Xaml::Media::LoadedImageSourceLoadCompletedEventArgs e);");
465+
priv.WriteLine($"winrt::{_winUINamespace}::Xaml::Media::LoadedImageSurface sender,");
466+
priv.WriteLine($"winrt::{_winUINamespace}::Xaml::Media::LoadedImageSourceLoadCompletedEventArgs e);");
465467
priv.UnIndent();
466468

467469
// INotifyPropertyChanged implementation.
468-
pub.WriteLine("winrt::event_token PropertyChanged(winrt::Windows::UI::Xaml::Data::PropertyChangedEventHandler const& handler);");
470+
pub.WriteLine($"winrt::event_token PropertyChanged(winrt::{_winUINamespace}::Xaml::Data::PropertyChangedEventHandler const& handler);");
469471
pub.WriteLine("void PropertyChanged(winrt::event_token const& token) noexcept;");
470472

471473
// IDynamicAnimatedVisualSource implementation.
@@ -1147,7 +1149,7 @@ void WriteIDynamicAnimatedVisualSource(CodeBuilder builder)
11471149
builder.WriteLine("if (!m_isTryCreateAnimatedVisualCalled && m_isImageLoadingAsynchronous != value)");
11481150
builder.OpenScope();
11491151
builder.WriteLine("m_isImageLoadingAsynchronous = value;");
1150-
builder.WriteLine($"m_PropertyChanged(*this, Windows::UI::Xaml::Data::PropertyChangedEventArgs(L\"IsImageLoadingAsynchronous\"));");
1152+
builder.WriteLine($"m_PropertyChanged(*this, {_winUINamespace}::Xaml::Data::PropertyChangedEventArgs(L\"IsImageLoadingAsynchronous\"));");
11511153
builder.CloseScope();
11521154
builder.CloseScope();
11531155
builder.WriteLine();
@@ -1165,7 +1167,7 @@ void WriteIDynamicAnimatedVisualSource(CodeBuilder builder)
11651167
WriteEventImpl(
11661168
builder,
11671169
"PropertyChanged",
1168-
"Windows::UI::Xaml::Data::PropertyChangedEventHandler",
1170+
$"{_winUINamespace}::Xaml::Data::PropertyChangedEventHandler",
11691171
"m_PropertyChanged");
11701172

11711173
// Generate the AnimatedVisualInvalidated event implementation.
@@ -1251,7 +1253,7 @@ void WriteHandleLoadCompleted(CodeBuilder builder)
12511253
builder.WriteLine("if (m_loadCompletedEventCount == c_loadedImageSurfaceCount)");
12521254
builder.OpenScope();
12531255
builder.WriteLine("m_isImageLoadingCompleted = true;");
1254-
builder.WriteLine("m_PropertyChanged(*this, Windows::UI::Xaml::Data::PropertyChangedEventArgs(L\"IsImageLoadingCompleted\"));");
1256+
builder.WriteLine($"m_PropertyChanged(*this, {_winUINamespace}::Xaml::Data::PropertyChangedEventArgs(L\"IsImageLoadingCompleted\"));");
12551257

12561258
// If asynchronouse image loading is enabled notify via IDynamicAnimatedVisualSource that
12571259
// the previous result is now invalidated.

0 commit comments

Comments
 (0)