Skip to content

Commit abb4661

Browse files
committed
Use typed constant instead
1 parent 09d923c commit abb4661

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

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

Lines changed: 8 additions & 8 deletions
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), DefaultValue = "")]
10+
[DependencyProperty<string>("OutlineIconData", nameof(OnOutlineIconPropertyChanged), DefaultValue = "")]
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 = 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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Collections.Generic;
99
using System.Collections.Immutable;
1010
using System.Data.SqlTypes;
11+
using System.Linq;
1112
using System.Text;
1213

1314
namespace Files.Core.SourceGenerator
@@ -65,6 +66,7 @@ private string EmitSyntaxTree(INamedTypeSymbol typeSymbol, ImmutableArray<Attrib
6566
var fieldName = $"{propertyName}Property";
6667
var isSetterPrivate = false;
6768
var defaultValue = "global::Microsoft.UI.Xaml.DependencyProperty.UnsetValue";
69+
TypedConstant typedDefaultValueConstant = default;
6870
var isNullable = false;
6971

7072
// Get values from the attribute properties
@@ -75,14 +77,27 @@ private string EmitSyntaxTree(INamedTypeSymbol typeSymbol, ImmutableArray<Attrib
7577
switch (namedArgument.Key)
7678
{
7779
case "IsSetterPrivate": isSetterPrivate = (bool)value; break;
78-
case "DefaultValue": defaultValue = (string)value; break;
80+
case "DefaultValue": defaultValue = value.ToString(); typedDefaultValueConstant = namedArgument.Value; break;
7981
case "IsNullable": isNullable = (bool)value; break;
8082
}
8183
}
8284
}
8385

86+
ExpressionSyntax defaultValueExpression;
87+
88+
if (string.IsNullOrEmpty(defaultValue))
89+
defaultValueExpression = SyntaxFactory.ParseExpression("global::Microsoft.UI.Xaml.DependencyProperty.UnsetValue");
90+
else if (defaultValue.Equals("True", StringComparison.OrdinalIgnoreCase))
91+
defaultValueExpression = SyntaxFactory.ParseExpression("(bool)true");
92+
else if (defaultValue.Equals("False", StringComparison.OrdinalIgnoreCase))
93+
defaultValueExpression = SyntaxFactory.ParseExpression("(bool)false");
94+
else if (typedDefaultValueConstant.Kind is TypedConstantKind.Enum or TypedConstantKind.Primitive && typedDefaultValueConstant.Type is { } defaultValueTypeSymbol)
95+
defaultValueExpression = SyntaxFactory.ParseExpression($"({defaultValueTypeSymbol.Name}){defaultValue}");
96+
else
97+
defaultValueExpression = SyntaxFactory.ParseExpression("global::Microsoft.UI.Xaml.DependencyProperty.UnsetValue");
98+
8499
// Emit "new PropertyMetadata(...)"
85-
var dpPropertyMetadata = EmitPMObjectCreationExpression(SyntaxFactory.ParseExpression(defaultValue));
100+
var dpPropertyMetadata = EmitPMObjectCreationExpression(defaultValueExpression);
86101

87102
// Append callback method to PropertyMetadata
88103
if (!string.IsNullOrEmpty(callbackMethodName))

src/Files.Shared/Attributes/DependencyPropertyAttribute.cs

Lines changed: 1 addition & 1 deletion
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)