Skip to content

Commit d433d2e

Browse files
committed
feat: Implemented new grid succinct syntax.
1 parent ec2dbcf commit d433d2e

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.Reflection.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ private string GetGlobalizedTypeName(string? fullTargetType)
6464
return fullTargetType;
6565
}
6666

67+
private string GetGlobalizedTypeName(XamlType type)
68+
{
69+
var fullTypeName = type.Name;
70+
var knownType = FindType(type);
71+
if (knownType == null && type.PreferredXamlNamespace.StartsWith("using:"))
72+
{
73+
fullTypeName = type.PreferredXamlNamespace.TrimStart("using:") + "." + type.Name;
74+
}
75+
if (knownType != null)
76+
{
77+
// Override the using with the type that was found in the list of loaded assemblies
78+
fullTypeName = knownType.ToDisplayString();
79+
}
80+
81+
return GetGlobalizedTypeName(fullTypeName);
82+
}
83+
6784
private bool IsType(XamlType xamlType, XamlType? baseType)
6885
{
6986
if (xamlType == baseType)

src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5062,7 +5062,47 @@ Func<XamlMemberDefinition, bool> propertyPredicate
50625062
{
50635063
if (IsInitializableCollection(member.Member))
50645064
{
5065-
writer.AppendLineInvariant("// Empty collection");
5065+
// New grid succinct syntax.
5066+
if (member.Owner?.Type.Name == "Grid" &&
5067+
member.Owner?.Type.PreferredXamlNamespace == XamlConstants.PresentationXamlXmlNamespace &&
5068+
(member.Member.Name == "ColumnDefinitions" || member.Member.Name == "RowDefinitions") &&
5069+
member.Member.PreferredXamlNamespace == XamlConstants.PresentationXamlXmlNamespace &&
5070+
member.Value is string definitions)
5071+
{
5072+
using (writer.BlockInvariant($"{fullValueSetter} = "))
5073+
{
5074+
var propertyName = member.Member.Name == "ColumnDefinitions"
5075+
? "Width"
5076+
: "Height";
5077+
var definitionType = new XamlType(
5078+
unknownTypeNamespace: XamlConstants.PresentationXamlXmlNamespace,
5079+
unknownTypeName: member.Member.Name == "ColumnDefinitions"
5080+
? "ColumnDefinition"
5081+
: "RowDefinition",
5082+
list: new List<XamlType>(),
5083+
xamlSchemaContext: new XamlSchemaContext());
5084+
5085+
var values = definitions
5086+
.Split(',')
5087+
.Select(static definition => definition.Trim())
5088+
.ToArray();
5089+
5090+
foreach (var value in values)
5091+
{
5092+
using (writer.BlockInvariant("new {0}", GetGlobalizedTypeName(definitionType)))
5093+
{
5094+
writer.AppendLineInvariant("{0} = {1}", propertyName, BuildGridLength(value));
5095+
}
5096+
5097+
writer.AppendLineInvariant(",");
5098+
}
5099+
}
5100+
writer.AppendLineInvariant(",");
5101+
}
5102+
else
5103+
{
5104+
writer.AppendLineInvariant("// Empty collection");
5105+
}
50665106
}
50675107
else
50685108
{

0 commit comments

Comments
 (0)