Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,8 @@ public partial class {{defaultTestClassName}}
/// <summary>
/// BindableProperty for the <see cref = "Text"/> property.
/// </summary>
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Text", typeof(string), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, (Microsoft.Maui.Controls.BindingMode)0, null, null, null, null, __{{defaultTestClassName}}BindablePropertyInitHelpers.CreateDefaultText);
public partial string Text { get => __{{defaultTestClassName}}BindablePropertyInitHelpers.IsInitializingText ? field : (string)GetValue(TextProperty); set => SetValue(TextProperty, value); }
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Text", typeof(string), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), "Initial Value", (Microsoft.Maui.Controls.BindingMode)0, null, null, null, null, null);
public partial string Text { get => false ? field : (string)GetValue(TextProperty); set => SetValue(TextProperty, value); }

/// <summary>
/// BindableProperty for the <see cref = "CustomDuration"/> property.
Expand All @@ -610,20 +610,11 @@ public partial class {{defaultTestClassName}}

file static class __{{defaultTestClassName}}BindablePropertyInitHelpers
{
public static volatile bool IsInitializingText = false;
public static object CreateDefaultText(global::Microsoft.Maui.Controls.BindableObject bindable)
{
IsInitializingText = true;
var defaultValue = ((TestView)bindable).Text;
IsInitializingText = false;
return defaultValue;
}

public static volatile bool IsInitializingCustomDuration = false;
public static object CreateDefaultCustomDuration(global::Microsoft.Maui.Controls.BindableObject bindable)
{
IsInitializingCustomDuration = true;
var defaultValue = ((TestView)bindable).CustomDuration;
var defaultValue = (({{defaultTestClassName}})bindable).CustomDuration;
IsInitializingCustomDuration = false;
return defaultValue;
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,8 @@ public partial class {{defaultTestClassName}}<T, U>
/// <summary>
/// BindableProperty for the <see cref = "Value"/> property.
/// </summary>
public static readonly global::Microsoft.Maui.Controls.BindableProperty ValueProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Value", typeof(T), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}<T, U>), null, (Microsoft.Maui.Controls.BindingMode)0, null, null, null, null, __{{defaultTestClassName}}BindablePropertyInitHelpers.CreateDefaultValue);
public partial T? Value { get => __{{defaultTestClassName}}BindablePropertyInitHelpers.IsInitializingValue ? field : (T? )GetValue(ValueProperty); set => SetValue(ValueProperty, value); }

[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
private static class __{{defaultTestClassName}}BindablePropertyInitHelpers
{
public static volatile bool IsInitializingValue = false;
public static object CreateDefaultValue(global::Microsoft.Maui.Controls.BindableObject bindable)
{
IsInitializingValue = true;
var defaultValue = (({{defaultTestClassName}}<T, U>)bindable).Value;
IsInitializingValue = false;
return defaultValue;
}
}
public static readonly global::Microsoft.Maui.Controls.BindableProperty ValueProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Value", typeof(T), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}<T, U>), default(T), (Microsoft.Maui.Controls.BindingMode)0, null, null, null, null, null);
public partial T? Value { get => false ? field : (T? )GetValue(ValueProperty); set => SetValue(ValueProperty, value); }
}
""";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ static void GenerateReadOnlyBindableProperty(StringBuilder sb, in BindableProper
.Append(GetFormattedReturnType(nonNullableReturnType))
.Append("), typeof(")
.Append(info.DeclaringType)
.Append("), null, ")
.Append("), ").Append(info.ResolvedInitializerExpression ?? "null").Append(", ")
.Append(info.DefaultBindingMode)
.Append(", ")
.Append(info.ValidateValueMethodName)
Expand Down Expand Up @@ -304,7 +304,7 @@ static void GenerateBindableProperty(StringBuilder sb, in BindablePropertyModel
.Append(GetFormattedReturnType(nonNullableReturnType))
.Append("), typeof(")
.Append(info.DeclaringType)
.Append("), null, ")
.Append("), ").Append(info.ResolvedInitializerExpression ?? "null").Append(", ")
.Append(info.DefaultBindingMode)
.Append(", ")
.Append(info.ValidateValueMethodName)
Expand Down Expand Up @@ -408,8 +408,16 @@ static BindablePropertySemanticValues SemanticTransform(GeneratorAttributeSyntax

var propertyAccessibility = GetPropertyAccessibility(propertySymbol);

// Resolve initializer expression to a fully qualified string for use as defaultValue
string? resolvedInitializer = null;
if (hasInitializer && propertyDeclarationSyntax.Initializer is not null)
{
resolvedInitializer = Helpers.InitializerExpressionResolver.TryResolve(
propertyDeclarationSyntax.Initializer.Value, semanticModel);
}

var attributeData = context.Attributes[0];
bindablePropertyModels[0] = CreateBindablePropertyModel(attributeData, propertySymbol.ContainingType, propertySymbol.Name, returnType, doesContainNewKeyword, isReadOnlyBindableProperty, setterAccessibility, hasInitializer, propertyAccessibility);
bindablePropertyModels[0] = CreateBindablePropertyModel(attributeData, propertySymbol.ContainingType, propertySymbol.Name, returnType, doesContainNewKeyword, isReadOnlyBindableProperty, setterAccessibility, hasInitializer, propertyAccessibility, resolvedInitializer);

return new(propertyInfo, ImmutableArray.Create(bindablePropertyModels));
}
Expand Down Expand Up @@ -517,7 +525,7 @@ static string GetGenericTypeParameters(INamedTypeSymbol typeSymbol)
return sb.ToString();
}

static BindablePropertyModel CreateBindablePropertyModel(in AttributeData attributeData, in INamedTypeSymbol declaringType, in string propertyName, in ITypeSymbol returnType, in bool doesContainNewKeyword, in bool isReadOnly, in string? setterAccessibility, in bool hasInitializer, in string? propertyAccessibility)
static BindablePropertyModel CreateBindablePropertyModel(in AttributeData attributeData, in INamedTypeSymbol declaringType, in string propertyName, in ITypeSymbol returnType, in bool doesContainNewKeyword, in bool isReadOnly, in string? setterAccessibility, in bool hasInitializer, in string? propertyAccessibility, in string? resolvedInitializerExpression)
{
if (attributeData.AttributeClass is null)
{
Expand All @@ -532,7 +540,10 @@ static BindablePropertyModel CreateBindablePropertyModel(in AttributeData attrib
var validateValueMethodName = attributeData.GetNamedMethodGroupArgumentsAttributeValueByNameAsString(nameof(BindablePropertyModel.ValidateValueMethodName));
var newKeywordText = doesContainNewKeyword ? "new " : string.Empty;

return new BindablePropertyModel(propertyName, returnType, declaringType, defaultBindingMode, validateValueMethodName, propertyChangedMethodName, propertyChangingMethodName, coerceValueMethodName, defaultValueCreatorMethodName, newKeywordText, isReadOnly, setterAccessibility, hasInitializer, propertyAccessibility);
// Only use the resolved initializer when there's no explicit DefaultValueCreatorMethodName
var effectiveResolvedInitializer = defaultValueCreatorMethodName is "null" ? resolvedInitializerExpression : null;

return new BindablePropertyModel(propertyName, returnType, declaringType, defaultBindingMode, validateValueMethodName, propertyChangedMethodName, propertyChangingMethodName, coerceValueMethodName, defaultValueCreatorMethodName, newKeywordText, isReadOnly, setterAccessibility, hasInitializer, propertyAccessibility, effectiveResolvedInitializer);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Loading
Loading