Skip to content

Commit f2e8349

Browse files
committed
Use expression instead of string for default value acquisition
1 parent 09d923c commit f2e8349

File tree

3 files changed

+62
-53
lines changed

3 files changed

+62
-53
lines changed

src/Files.App.Controls/ThemedIcon/ThemedIcon.Properties.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
namespace Files.App.Controls
88
{
9-
[DependencyProperty<string>("FilledIconData", nameof(OnFilledIconPropertyChanged), DefaultValue = "string.Empty")]
10-
[DependencyProperty<string>("OutlineIconData", nameof(OnOutlineIconPropertyChanged), DefaultValue = "string.Empty")]
9+
[DependencyProperty<string>("FilledIconData", nameof(OnFilledIconPropertyChanged))]
10+
[DependencyProperty<string>("OutlineIconData", nameof(OnOutlineIconPropertyChanged))]
1111
[DependencyProperty<Brush>("Color", nameof(OnColorPropertyChanged))]
12-
[DependencyProperty<ThemedIconTypes>("IconType", nameof(OnIconTypePropertyChanged), DefaultValue = "ThemedIconTypes.Layered")]
13-
[DependencyProperty<ThemedIconColorType>("IconColorType", nameof(OnIconColorTypePropertyChanged), DefaultValue = "ThemedIconColorType.None")]
14-
[DependencyProperty<double>("IconSize", nameof(OnIconSizePropertyChanged), DefaultValue = "(double)16")]
15-
[DependencyProperty<bool>("IsToggled", nameof(OnIsToggledPropertyChanged), DefaultValue = "false")]
16-
[DependencyProperty<bool>("IsFilled", nameof(OnIsFilledPropertyChanged), DefaultValue = "false")]
17-
[DependencyProperty<bool>("IsHighContrast", nameof(OnIsHighContrastPropertyChanged), DefaultValue = "false")]
12+
[DependencyProperty<ThemedIconTypes>("IconType", nameof(OnIconTypePropertyChanged), DefaultValue = ThemedIconTypes.Layered)]
13+
[DependencyProperty<ThemedIconColorType>("IconColorType", nameof(OnIconColorTypePropertyChanged), DefaultValue = ThemedIconColorType.None)]
14+
[DependencyProperty<double>("IconSize", nameof(OnIconSizePropertyChanged), DefaultValue = (double)16)]
15+
[DependencyProperty<bool>("IsToggled", nameof(OnIsToggledPropertyChanged), DefaultValue = false)]
16+
[DependencyProperty<bool>("IsFilled", nameof(OnIsFilledPropertyChanged), DefaultValue = false)]
17+
[DependencyProperty<bool>("IsHighContrast", nameof(OnIsHighContrastPropertyChanged), DefaultValue = false)]
1818
[DependencyProperty<object>("Layers", nameof(OnLayersPropertyChanged))]
1919
public partial class ThemedIcon : Control
2020
{

src/Files.Core.SourceGenerator/Generators/DependencyPropertyGenerator.cs

+53-44
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
using System;
88
using System.Collections.Generic;
99
using System.Collections.Immutable;
10-
using System.Data.SqlTypes;
1110
using System.Text;
1211

1312
namespace Files.Core.SourceGenerator
1413
{
1514
/// <summary>
1615
/// Generates a set of dependency property and its backing field.
1716
/// </summary>
18-
[Generator]
17+
[Generator(LanguageNames.CSharp)]
1918
public sealed class DependencyPropertyGenerator : IIncrementalGenerator
2019
{
2120
/// <inheritdoc/>
@@ -65,30 +64,43 @@ private string EmitSyntaxTree(INamedTypeSymbol typeSymbol, ImmutableArray<Attrib
6564
var fieldName = $"{propertyName}Property";
6665
var isSetterPrivate = false;
6766
var defaultValue = "global::Microsoft.UI.Xaml.DependencyProperty.UnsetValue";
67+
ExpressionSyntax? defaultValueExpression = null;
6868
var isNullable = false;
6969

7070
// Get values from the attribute properties
71+
int index = 0;
7172
foreach (var namedArgument in attribute.NamedArguments)
7273
{
7374
if (namedArgument.Value.Value is { } value)
7475
{
7576
switch (namedArgument.Key)
7677
{
77-
case "IsSetterPrivate": isSetterPrivate = (bool)value; break;
78-
case "DefaultValue": defaultValue = (string)value; break;
79-
case "IsNullable": isNullable = (bool)value; break;
78+
case "IsSetterPrivate":
79+
isSetterPrivate = (bool)value;
80+
break;
81+
case "DefaultValue":
82+
defaultValueExpression = ((AttributeSyntax)attribute.ApplicationSyntaxReference!.GetSyntax()).ArgumentList!.Arguments[index].Expression;
83+
break;
84+
case "IsNullable":
85+
isNullable = (bool)value;
86+
break;
8087
}
8188
}
89+
90+
index++;
8291
}
8392

84-
// Emit "new PropertyMetadata(...)"
85-
var dpPropertyMetadata = EmitPMObjectCreationExpression(SyntaxFactory.ParseExpression(defaultValue));
93+
defaultValueExpression ??= SyntaxFactory.ParseExpression(defaultValue);
94+
95+
// Emit "new PropertyMetadata(...)" expression
96+
var dpPropertyMetadata = EmitPMObjectCreationExpression(defaultValueExpression);
8697

87-
// Append callback method to PropertyMetadata
98+
// Append callback to PropertyMetadata
8899
if (!string.IsNullOrEmpty(callbackMethodName))
89-
dpPropertyMetadata = EmitDPCallbackParenthesizedLambdaExpression(dpPropertyMetadata, callbackMethodName, type, isNullable, typeSymbol);
100+
dpPropertyMetadata = dpPropertyMetadata.AddArgumentListArguments(
101+
SyntaxFactory.Argument(EmitDPCallbackParenthesizedLambdaExpression(callbackMethodName, type, isNullable, typeSymbol)));
90102

91-
// Emit "DependencyProperty.Register(...)" invocation expression
103+
// Emit "DependencyProperty.Register(...)" expression
92104
var dpRegisteringExpression = EmitDPRegisterInvocationExpression(propertyName, type, typeSymbol, dpPropertyMetadata);
93105

94106
// Emit the backing DependencyProperty field with attributes
@@ -111,53 +123,50 @@ private string EmitSyntaxTree(INamedTypeSymbol typeSymbol, ImmutableArray<Attrib
111123
if (members.Count is 0)
112124
return string.Empty;
113125

114-
// Generate class lock
126+
// Generate class block
115127
var generatedClass = SourceGeneratorHelper.GetClassDeclaration(typeSymbol, members);
116128

117129
// Generate namespace block
118130
var generatedNamespace = SourceGeneratorHelper.GetFileScopedNamespaceDeclaration(typeSymbol, generatedClass);
119131

120-
// Generate file block (complication uint)
132+
// Generate complication uint
121133
var compilationUnit = SourceGeneratorHelper.GetCompilationUnit(generatedNamespace);
122134

123135
// Get full syntax tree and return as UTF8 string
124136
return SyntaxFactory.SyntaxTree(compilationUnit, encoding: Encoding.UTF8).GetText().ToString();
125137
}
126138

127-
private ObjectCreationExpressionSyntax EmitDPCallbackParenthesizedLambdaExpression(ObjectCreationExpressionSyntax expression, string callbackName, ITypeSymbol type, bool isNullable, ITypeSymbol classSymbol)
139+
private ParenthesizedLambdaExpressionSyntax EmitDPCallbackParenthesizedLambdaExpression(string callbackName, ITypeSymbol type, bool isNullable, ITypeSymbol classSymbol)
128140
{
129141
// (d, e) => ((class)d).callbackName((type)e.OldValue, (type)e.NewValue)
130-
return expression.AddArgumentListArguments(
131-
SyntaxFactory.Argument(
132-
SyntaxFactory.ParenthesizedLambdaExpression()
133-
.AddParameterListParameters(
134-
SyntaxFactory.Parameter(SyntaxFactory.Identifier("d")),
135-
SyntaxFactory.Parameter(SyntaxFactory.Identifier("e")))
136-
.WithExpressionBody(
137-
SyntaxFactory.InvocationExpression(
138-
SyntaxFactory.MemberAccessExpression(
139-
SyntaxKind.SimpleMemberAccessExpression,
140-
SyntaxFactory.ParenthesizedExpression(
141-
SyntaxFactory.CastExpression(
142-
classSymbol.GetTypeSyntax(false),
143-
SyntaxFactory.IdentifierName("d"))),
144-
SyntaxFactory.IdentifierName(callbackName)))
145-
.AddArgumentListArguments(
146-
SyntaxFactory.Argument(
147-
SyntaxFactory.CastExpression(
148-
type.GetTypeSyntax(isNullable),
149-
SyntaxFactory.MemberAccessExpression(
150-
SyntaxKind.SimpleMemberAccessExpression,
151-
SyntaxFactory.IdentifierName("e"),
152-
SyntaxFactory.IdentifierName("OldValue")))),
153-
SyntaxFactory.Argument(
154-
SyntaxFactory.CastExpression(
155-
type.GetTypeSyntax(isNullable),
156-
SyntaxFactory.MemberAccessExpression(
157-
SyntaxKind.SimpleMemberAccessExpression,
158-
SyntaxFactory.IdentifierName("e"),
159-
SyntaxFactory.IdentifierName("NewValue"))))
160-
))));
142+
return SyntaxFactory.ParenthesizedLambdaExpression()
143+
.AddParameterListParameters(
144+
SyntaxFactory.Parameter(SyntaxFactory.Identifier("d")),
145+
SyntaxFactory.Parameter(SyntaxFactory.Identifier("e")))
146+
.WithExpressionBody(
147+
SyntaxFactory.InvocationExpression(
148+
SyntaxFactory.MemberAccessExpression(
149+
SyntaxKind.SimpleMemberAccessExpression,
150+
SyntaxFactory.ParenthesizedExpression(
151+
SyntaxFactory.CastExpression(
152+
classSymbol.GetTypeSyntax(false),
153+
SyntaxFactory.IdentifierName("d"))),
154+
SyntaxFactory.IdentifierName(callbackName)))
155+
.AddArgumentListArguments(
156+
SyntaxFactory.Argument(
157+
SyntaxFactory.CastExpression(
158+
type.GetTypeSyntax(isNullable),
159+
SyntaxFactory.MemberAccessExpression(
160+
SyntaxKind.SimpleMemberAccessExpression,
161+
SyntaxFactory.IdentifierName("e"),
162+
SyntaxFactory.IdentifierName("OldValue")))),
163+
SyntaxFactory.Argument(
164+
SyntaxFactory.CastExpression(
165+
type.GetTypeSyntax(isNullable),
166+
SyntaxFactory.MemberAccessExpression(
167+
SyntaxKind.SimpleMemberAccessExpression,
168+
SyntaxFactory.IdentifierName("e"),
169+
SyntaxFactory.IdentifierName("NewValue"))))));
161170
}
162171

163172
private ObjectCreationExpressionSyntax EmitPMObjectCreationExpression(ExpressionSyntax defaultValueExpression)

src/Files.Shared/Attributes/DependencyPropertyAttribute.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ public DependencyPropertyAttribute(string name, string propertyChanged = "")
2222

2323
public bool IsNullable { get; init; }
2424

25-
public string DefaultValue { get; init; } = "DependencyProperty.UnsetValue";
25+
public object? DefaultValue { get; init; }
2626
}

0 commit comments

Comments
 (0)