diff --git a/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.SourceGenerators/DependencyPropertyGenerator.Execute.cs b/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.SourceGenerators/DependencyPropertyGenerator.Execute.cs index 99f7c427..1c2208cf 100644 --- a/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.SourceGenerators/DependencyPropertyGenerator.Execute.cs +++ b/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.SourceGenerators/DependencyPropertyGenerator.Execute.cs @@ -554,8 +554,8 @@ static string GetExpressionWithTrailingSpace(Accessibility accessibility) if (propertyInfo.IsLocalCachingEnabled) { writer.WriteLine($$""" - get => field; - set + {{GetExpressionWithTrailingSpace(propertyInfo.GetterAccessibility)}}get => field; + {{GetExpressionWithTrailingSpace(propertyInfo.SetterAccessibility)}}set { On{{propertyInfo.PropertyName}}Set(ref value); @@ -602,7 +602,7 @@ static string GetExpressionWithTrailingSpace(Accessibility accessibility) // would introduce a lot of overhead. If callers really do want to have a callback being invoked, they can implement // the one wired up to the property metadata directly. We can still invoke the ones only using the new value, though. writer.WriteLine($$""" - get + {{GetExpressionWithTrailingSpace(propertyInfo.GetterAccessibility)}}get { object? __boxedValue = GetValue({{propertyInfo.PropertyName}}Property); @@ -610,7 +610,7 @@ static string GetExpressionWithTrailingSpace(Accessibility accessibility) return __boxedValue; } - set + {{GetExpressionWithTrailingSpace(propertyInfo.SetterAccessibility)}}set { On{{propertyInfo.PropertyName}}Set(ref value); @@ -624,7 +624,7 @@ static string GetExpressionWithTrailingSpace(Accessibility accessibility) { // Same as above but with the extra typed hook for both accessors writer.WriteLine($$""" - get + {{GetExpressionWithTrailingSpace(propertyInfo.GetterAccessibility)}}get { object? __boxedValue = GetValue({{propertyInfo.PropertyName}}Property); @@ -636,7 +636,7 @@ static string GetExpressionWithTrailingSpace(Accessibility accessibility) return __unboxedValue; } - set + {{GetExpressionWithTrailingSpace(propertyInfo.SetterAccessibility)}}set { On{{propertyInfo.PropertyName}}Set(ref value); diff --git a/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/Test_DependencyPropertyGenerator.cs b/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/Test_DependencyPropertyGenerator.cs index a84b3f2d..f531672f 100644 --- a/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/Test_DependencyPropertyGenerator.cs +++ b/components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/Test_DependencyPropertyGenerator.cs @@ -15,8 +15,8 @@ public partial class Test_DependencyPropertyGenerator public void SingleProperty_Int32_WithLocalCache() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -141,8 +141,8 @@ public partial int Number public void SingleProperty_Int32_WithLocalCache_WithCallback() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -321,8 +321,8 @@ file sealed class PropertyChangedUnsafeAccessors public void SingleProperty_Int32_WithLocalCache_WithDefaultValue() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -447,8 +447,8 @@ public partial int Number public void SingleProperty_Int32_WithNoCaching() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -563,8 +563,8 @@ public partial int Number public void SingleProperty_Int32_WithNoCaching_UnsetValue() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -679,8 +679,8 @@ public partial int Number public void SingleProperty_Int32_WithNoCaching_WithCallback() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -849,8 +849,8 @@ file sealed class PropertyChangedUnsafeAccessors public void SingleProperty_Int32_WithNoCaching_WithDefaultValue() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -965,8 +965,8 @@ public partial int Number public void SingleProperty_Int32_WithNoCaching_WithDefaultValue_WithCallback() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -1135,8 +1135,8 @@ file sealed class PropertyChangedUnsafeAccessors public void SingleProperty_Int32_WithNoCaching_WithSharedCallback() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -1305,8 +1305,8 @@ file sealed class PropertyChangedUnsafeAccessors public void SingleProperty_Int32_WithNoCaching_WithBothCallbacks() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -1484,8 +1484,8 @@ file sealed class PropertyChangedUnsafeAccessors public void SingleProperty_String_WithLocalCache() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -1610,8 +1610,8 @@ public partial string? Name public void SingleProperty_String_WithNoCaching() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -1726,8 +1726,8 @@ public partial string? Name public void SingleProperty_String_WithNoCaching_Required() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -1842,8 +1842,8 @@ public required partial string Name public void SingleProperty_String_WithNoCaching_New() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -1963,8 +1963,8 @@ partial class MyControl public void SingleProperty_String_WithNoCaching_Virtual() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -2081,8 +2081,8 @@ public virtual partial string Name public void SingleProperty_String_WithNoCaching_Override(string modifiers) { string source = $$""" - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -2198,12 +2198,128 @@ partial class MyControl CSharpGeneratorTest.VerifySources(source, ("MyNamespace.MyControl.g.cs", result), languageVersion: LanguageVersion.Preview); } + [TestMethod] + public void SingleProperty_String_WithNoCaching_CustomAccessibility() + { + string source = $$""" + using CommunityToolkit.WinUI; + using Windows.UI.Xaml; + + namespace MyNamespace; + + public partial class MyControl : DependencyObject + { + [GeneratedDependencyProperty] + internal partial string Name { protected get; private set; } + } + """; + + string result = $$""" + // + #pragma warning disable + #nullable enable + + namespace MyNamespace + { + /// + partial class MyControl + { + /// + /// The backing instance for . + /// + [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", )] + public static readonly global::Windows.UI.Xaml.DependencyProperty NameProperty = global::Windows.UI.Xaml.DependencyProperty.Register( + name: "Name", + propertyType: typeof(string), + ownerType: typeof(MyControl), + typeMetadata: null); + + /// + [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", )] + [global::System.Diagnostics.DebuggerNonUserCode] + [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + internal partial string Name + { + protected get + { + object? __boxedValue = GetValue(NameProperty); + + OnNameGet(ref __boxedValue); + + string __unboxedValue = (string)__boxedValue; + + OnNameGet(ref __unboxedValue); + + return __unboxedValue; + } + private set + { + OnNameSet(ref value); + + object? __boxedValue = value; + + OnNameSet(ref __boxedValue); + + SetValue(NameProperty, __boxedValue); + + OnNameChanged(value); + } + } + + /// Executes the logic for when the accessor is invoked + /// The raw property value that has been retrieved from . + /// This method is invoked on the boxed value retrieved via on . + [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", )] + partial void OnNameGet(ref object propertyValue); + + /// Executes the logic for when the accessor is invoked + /// The unboxed property value that has been retrieved from . + /// This method is invoked on the unboxed value retrieved via on . + [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", )] + partial void OnNameGet(ref string propertyValue); + + /// Executes the logic for when the accessor is invoked + /// The boxed property value that has been produced before assigning to . + /// This method is invoked on the boxed value that is about to be passed to on . + [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", )] + partial void OnNameSet(ref object propertyValue); + + /// Executes the logic for when the accessor is invoked + /// The property value that is being assigned to . + /// This method is invoked on the raw value being assigned to , before is used. + [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", )] + partial void OnNameSet(ref string propertyValue); + + /// Executes the logic for when has just changed. + /// The new property value that has been set. + /// This method is invoked right after the value of is changed. + [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", )] + partial void OnNameChanged(string newValue); + + /// Executes the logic for when has just changed. + /// Event data that is issued by any event that tracks changes to the effective value of this property. + /// This method is invoked by the infrastructure, after the value of is changed. + [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", )] + partial void OnNamePropertyChanged(global::Windows.UI.Xaml.DependencyPropertyChangedEventArgs e); + + /// Executes the logic for when any dependency property has just changed. + /// Event data that is issued by any event that tracks changes to the effective value of this property. + /// This method is invoked by the infrastructure, after the value of any dependency property has just changed. + [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", )] + partial void OnPropertyChanged(global::Windows.UI.Xaml.DependencyPropertyChangedEventArgs e); + } + } + """; + + CSharpGeneratorTest.VerifySources(source, ("MyNamespace.MyControl.g.cs", result), languageVersion: LanguageVersion.Preview); + } + [TestMethod] public void MultipleProperties_WithNoCaching_CorrectSpacing() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -2399,8 +2515,8 @@ public partial string? LastName public void MultipleProperties_WithNoCaching_WithJustOnePropertyCallback() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -2650,8 +2766,8 @@ file sealed class PropertyChangedUnsafeAccessors public void MultipleProperties_WithNoCaching_WithSharedPropertyCallback() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -2915,8 +3031,8 @@ file sealed class PropertyChangedUnsafeAccessors public void MultipleProperties_WithNoCaching_WithMixedPropertyCallbacks() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -3194,8 +3310,8 @@ file sealed class PropertyChangedUnsafeAccessors public void MultipleProperties_WithNoCaching_WithMixedPropertyCallbacks2() { const string source = """ - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -3485,8 +3601,8 @@ file sealed class PropertyChangedUnsafeAccessors public void SingleProperty_Int32_WithNoCaching_WithDefaultValueCallback(string returnType) { string source = $$""" - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -3608,8 +3724,8 @@ public partial int Number public void SingleProperty_NullableOfInt32_WithNoCaching_WithDefaultValueCallback(string returnType) { string source = $$""" - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; namespace MyNamespace; @@ -3731,8 +3847,8 @@ public partial int? Number public void SingleProperty_String_WithNoCaching_WithDefaultValueCallback(string returnType) { string source = $$""" - using Windows.UI.Xaml; using CommunityToolkit.WinUI; + using Windows.UI.Xaml; #nullable enable @@ -3912,10 +4028,10 @@ public void SingleProperty_MultipleTypes_WithNoCaching_DefaultValueIsOptimized( string source = $$""" using System; using System.Collections.Generic; + using CommunityToolkit.WinUI; using Windows.Foundation; using Windows.Foundation.Numerics; using Windows.UI.Xaml; - using CommunityToolkit.WinUI; #nullable enable