From 844b8e2182accc794714ed4973007bdda2977ae6 Mon Sep 17 00:00:00 2001 From: Carl de Billy Date: Tue, 16 Apr 2024 05:29:29 +0200 Subject: [PATCH 1/2] feat: New DependencyPropertyHelper class --- .../Given_DependencyPropertyHelper.cs | 259 ++++++++++++++++++ src/Uno.UI/UI/Xaml/DependencyObjectStore.cs | 2 - .../Xaml/Internal/DependencyPropertyHelper.cs | 205 ++++++++++++++ 3 files changed, 464 insertions(+), 2 deletions(-) create mode 100644 src/Uno.UI.RuntimeTests/Tests/Uno_Helpers/Given_DependencyPropertyHelper.cs create mode 100644 src/Uno.UI/UI/Xaml/Internal/DependencyPropertyHelper.cs diff --git a/src/Uno.UI.RuntimeTests/Tests/Uno_Helpers/Given_DependencyPropertyHelper.cs b/src/Uno.UI.RuntimeTests/Tests/Uno_Helpers/Given_DependencyPropertyHelper.cs new file mode 100644 index 000000000000..9d0a6bdcc847 --- /dev/null +++ b/src/Uno.UI.RuntimeTests/Tests/Uno_Helpers/Given_DependencyPropertyHelper.cs @@ -0,0 +1,259 @@ +#if HAS_UNO // DependencyPropertyHelper is only available on Uno +#nullable enable + +using System; +using FluentAssertions; +using FluentAssertions.Execution; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Uno.UI.Xaml.Core; + +namespace Uno.UI.RuntimeTests.Tests.Uno_Helpers; + +[TestClass] +[RunsOnUIThread] +public partial class Given_DependencyPropertyHelper +{ + [TestMethod] + public void When_GetDefaultValue() + { + // Arrange + var property = TestClass.TestProperty; + + // Act + var defaultValue = DependencyPropertyHelper.GetDefaultValue(property); + + // Assert + defaultValue.Should().Be("TestValue"); + } + + [TestMethod] + public void When_GetDependencyPropertyByName_OwnerType() + { + // Arrange + var propertyName = "TestProperty"; + + // Act + var property1 = DependencyPropertyHelper.GetDependencyPropertyByName(typeof(TestClass), propertyName); + var property2 = DependencyPropertyHelper.GetDependencyPropertyByName(typeof(DerivedTestClass), propertyName); + + // Assert + property1.Should().Be(TestClass.TestProperty); + property2.Should().Be(TestClass.TestProperty); + } + + [TestMethod] + public void When_GetDependencyPropertyByName_Property() + { + // Arrange + var propertyName = "TestProperty"; + + // Act + var property1 = DependencyPropertyHelper.GetDependencyPropertyByName(propertyName); + var property2 = DependencyPropertyHelper.GetDependencyPropertyByName(propertyName); + + // Assert + property1.Should().Be(TestClass.TestProperty); + property2.Should().Be(TestClass.TestProperty); + } + + [TestMethod] + public void When_GetDependencyPropertyByName_InvalidProperty() + { + // Arrange + var propertyName = "InvalidProperty"; + + // Act + var property = DependencyPropertyHelper.GetDependencyPropertyByName(typeof(TestClass), propertyName); + + // Assert + property.Should().BeNull(); + } + + [TestMethod] + public void When_GetDependencyPropertyByName_InvalidPropertyCasing() + { + // Arrange + var propertyName = "testProperty"; + + // Act + var property = DependencyPropertyHelper.GetDependencyPropertyByName(typeof(TestClass), propertyName); + + // Assert + property.Should().BeNull(); + } + + [TestMethod] + public void When_GetDependencyPropertiesForType() + { + // Arrange + var properties = DependencyPropertyHelper.GetDependencyPropertiesForType(); + + // Assert + properties.Should().Contain(TestClass.TestProperty); + } + + [TestMethod] + public void When_TryGetDependencyPropertiesForType() + { + // Arrange + var success = DependencyPropertyHelper.TryGetDependencyPropertiesForType(typeof(TestClass), out var properties); + + // Assert + success.Should().BeTrue(); + properties.Should().Contain(TestClass.TestProperty); + } + + [TestMethod] + public void When_TryGetDependencyPropertiesForType_Invalid() + { + // Arrange + var success = DependencyPropertyHelper.TryGetDependencyPropertiesForType(typeof(string), out var properties); + + // Assert + success.Should().BeFalse(); + properties.Should().BeNull(); + } + + [TestMethod] + public void When_GetPropertyType() + { + // Arrange + var property = TestClass.TestProperty; + + // Act + var propertyType = DependencyPropertyHelper.GetPropertyType(property); + + // Assert + propertyType.Should().Be(typeof(string)); + } + + [TestMethod] + public void When_GetPropertyDetails() + { + // Arrange + var property = TestClass.TestProperty; + + // Act + var (valueType, ownerType, name, isTypeNullable, isAttached, inInherited, defaultValue) = DependencyPropertyHelper.GetPropertyDetails(property); + + // Assert + using var _ = new AssertionScope(); + valueType.Should().Be(typeof(string)); + ownerType.Should().Be(typeof(TestClass)); + name.Should().Be("TestProperty"); + isTypeNullable.Should().BeTrue(); + isAttached.Should().BeFalse(); + inInherited.Should().BeFalse(); + defaultValue.Should().Be("TestValue"); + } + + [TestMethod] + public void When_GetPropertyDetails_DataContext() + { + // Arrange + var property = UIElement.DataContextProperty; + + // Act + var (valueType, _, name, isTypeNullable, isAttached, inInherited, defaultValue) = DependencyPropertyHelper.GetPropertyDetails(property); + + // Assert + using var _ = new AssertionScope(); + valueType.Should().Be(typeof(object)); + // ownerType is not checked here because it's different following the platform + name.Should().Be("DataContext"); + isTypeNullable.Should().BeTrue(); + isAttached.Should().BeFalse(); + inInherited.Should().BeTrue(); + defaultValue.Should().BeNull(); + } + + [TestMethod] + public void When_GetPropertyDetails_Attached() + { + // Arrange + var property = Grid.RowProperty; + + // Act + var (valueType, ownerType, name, isTypeNullable, isAttached, inInherited, defaultValue) = DependencyPropertyHelper.GetPropertyDetails(property); + + // Assert + using var _ = new AssertionScope(); + valueType.Should().Be(typeof(int)); + ownerType.Should().Be(typeof(Grid)); + name.Should().Be("Row"); + isTypeNullable.Should().BeFalse(); + isAttached.Should().BeTrue(); + inInherited.Should().BeFalse(); + defaultValue.Should().Be(0); + } + + [TestMethod] + public void When_GetProperties() + { + var properties = DependencyPropertyHelper.GetDependencyPropertiesForType(); + + properties.Should().Contain(DerivedTestClass.TestProperty); + } + + [TestMethod] + public void When_GetDefaultValue_Derived() + { + // Arrange + var property = DerivedTestClass.TestProperty; + + // Act + var defaultValue = DependencyPropertyHelper.GetDefaultValue(property); + + // Assert + defaultValue.Should().Be("TestValue"); + } + + [TestMethod] + public void When_GetDefaultUnsetValue_FromStyle() + { + // Arrange + var sut = new DerivedTestClass(); + sut.SetValue(TestClass.TestProperty, "Something"); + + // Act + var (unsetValue, precedence) = DependencyPropertyHelper.GetDefaultUnsetValue(sut, TestClass.TestProperty); + + // Assert + unsetValue.Should().Be("StyledTestValue"); + precedence.Should().Be(DependencyPropertyValuePrecedences.ExplicitStyle); + } + + [TestMethod] + public void When_GetDefaultUnsetValue() + { + // Arrange + var sut = new TestClass(); + sut.SetValue(TestClass.TestProperty, "Something"); + + // Act + var (unsetValue, precedence) = DependencyPropertyHelper.GetDefaultUnsetValue(sut, TestClass.TestProperty); + + // Assert + unsetValue.Should().Be("TestValue"); + precedence.Should().Be(DependencyPropertyValuePrecedences.DefaultValue); + } + + + private partial class TestClass : FrameworkElement // Not a DependencyObject because we don't want to deal with the generator here + { + public static readonly DependencyProperty TestProperty = DependencyProperty.Register("TestProperty", typeof(string), typeof(TestClass), new PropertyMetadata("TestValue")); + } + + private partial class DerivedTestClass : TestClass + { + public DerivedTestClass() + { + Style = new Style(typeof(DerivedTestClass)) + { + Setters = { new Setter(TestProperty, "StyledTestValue") } + }; + } + } +} +#endif diff --git a/src/Uno.UI/UI/Xaml/DependencyObjectStore.cs b/src/Uno.UI/UI/Xaml/DependencyObjectStore.cs index 5e1a36adaafc..19b06f97a100 100644 --- a/src/Uno.UI/UI/Xaml/DependencyObjectStore.cs +++ b/src/Uno.UI/UI/Xaml/DependencyObjectStore.cs @@ -447,9 +447,7 @@ public void SetValue(DependencyProperty property, object value) /// /// Clears the value for the specified dependency property on the specified instance. /// - /// The instance on which the property is attached /// The dependency property to get - /// The value precedence to assign public void ClearValue(DependencyProperty property) { SetValue(property, DependencyProperty.UnsetValue, DependencyPropertyValuePrecedences.Local); diff --git a/src/Uno.UI/UI/Xaml/Internal/DependencyPropertyHelper.cs b/src/Uno.UI/UI/Xaml/Internal/DependencyPropertyHelper.cs new file mode 100644 index 000000000000..13b18bf3d6d9 --- /dev/null +++ b/src/Uno.UI/UI/Xaml/Internal/DependencyPropertyHelper.cs @@ -0,0 +1,205 @@ +#nullable enable + +using Microsoft.UI.Xaml; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using Microsoft.UI.Xaml.Controls; +using Uno.Extensions; + +namespace Uno.UI.Xaml.Core; + +/// +/// The goal of this class is to provide a set of helper methods to work with from +/// external projects. +/// +/// +/// A small reflection is still required to access the class because those using it needs to know what they are doing. +/// +internal static class DependencyPropertyHelper +{ + /// + /// Get the default value of a for the owner type. + /// + /// + /// This is the value defined in the metadata of the property, which _may_ be overriden + /// using the internal DependencyProperty.OverrideMetadata() method on a per-type basis + /// -- that's why the type parameter is required. + /// + /// A classic example of that is the Button.VerticalAlignmentProperty which is overridden + /// to be VerticalAlignment.Center, while the default value is VerticalAlignment.Stretch + /// from the base class. + /// + /// This overload will return the default value for the owner type of the property - where + /// the value is originally defined. + /// + public static object? GetDefaultValue(DependencyProperty dependencyProperty) + => dependencyProperty.GetMetadata(dependencyProperty.OwnerType)?.DefaultValue; + + /// + /// Get a reference to by its name and owner type. + /// + /// + /// The name will match the name of the property as defined when the property is registered. + /// + /// There is usually NO "Property" suffix on that name since it's the name that is used in XAML. + /// + /// The name is case-sensitive. + /// + public static DependencyProperty? GetDependencyPropertyByName(Type ownerType, string propertyName) + => DependencyProperty.GetProperty(ownerType, propertyName); + + /// + /// Get a reference to by its name on the given type. + /// + /// + /// The name will match the name of the property as defined when the property is registered. + /// + /// There is usually NO "Property" suffix on that name since it's the name that is used in XAML. + /// + /// The name is case-sensitive. + /// + public static DependencyProperty? GetDependencyPropertyByName(string propertyName) + where T : DependencyObject + => GetDependencyPropertyByName(typeof(T), propertyName); + + /// + /// Get all the defined for a given type. + /// + public static IReadOnlyCollection? GetDependencyPropertiesForType() + where T : DependencyObject + => DependencyProperty.GetPropertiesForType(typeof(T)); + + /// + /// Try to get all the defined for a given type. + /// + /// False means it's not a dependency object + public static bool TryGetDependencyPropertiesForType( + Type forType, + [NotNullWhen(true)] out IReadOnlyCollection? properties) + { + // Check if type is a DependencyObject + if (!typeof(DependencyObject).IsAssignableFrom(forType)) + { + properties = null; + return false; + } + + properties = DependencyProperty.GetPropertiesForType(forType); + return true; + } + + /// + /// Get the value type of the property. + /// + public static Type GetPropertyType(DependencyProperty dependencyProperty) + => dependencyProperty.Type; + + /// + /// Get the name of the property. + /// + public static string GetPropertyName(DependencyProperty dependencyProperty) + => dependencyProperty.Name; + + /// + /// Get the owner type of the property + /// + /// + /// This is the property that defines the property, not the type that uses it. + /// It may also be overridden by a derived type. + /// + public static Type GetPropertyOwnerType(DependencyProperty dependencyProperty) + => dependencyProperty.OwnerType; + + /// + /// Get whether the property is an Attached Property. + /// + public static bool GetPropertyIsAttached(DependencyProperty dependencyProperty) + => dependencyProperty.IsAttached; + + /// + /// Get whether the property type is nullable - if the _null_ value is a valid value for the property. + /// + public static bool GetPropertyIsTypeNullable(DependencyProperty dependencyProperty) + => dependencyProperty.IsTypeNullable; + + /// + /// This method is used to get the default value of a property on a dependency object and give the precedence of that value. + /// + /// + /// This method won't check the local value of the property. It's basically what the property would be if it there was no local value. + /// + public static (object? value, DependencyPropertyValuePrecedences precedence) GetDefaultUnsetValue( + DependencyObject obj, + DependencyProperty dependencyProperty) + { + // 1st: Check assigned style value + if (obj is FrameworkElement fe && fe.TryGetValueFromStyle(dependencyProperty, out var valueFromStyle)) + { + // .TryGetValueFromStyle() will return false if the value is UnsetValue + return (valueFromStyle, DependencyPropertyValuePrecedences.ExplicitStyle); + } + + // 2nd: Check built-in style value, if any + if (obj is Control control && control.TryGetValueFromBuiltInStyle(dependencyProperty, out var valueFromImplicitStyle)) + { + // .TryGetValueFromBuiltInStyle() will return false if the value is UnsetValue + // NOTE: ExplicitStyle here actually means ExplicitOrImplicitStyle. This will be fixed with https://github.com/unoplatform/uno/pull/15684/ + return (valueFromImplicitStyle, DependencyPropertyValuePrecedences.ImplicitStyle); + } + + if(obj is IDependencyObjectStoreProvider { Store: { } store } && store.GetPropertyDetails(dependencyProperty) is { } details) + { + // 3rd: Check inherited value + var inheritedValue = details.GetInheritedValue(); + if(inheritedValue != DependencyProperty.UnsetValue) + { + return (details.GetInheritedValue(), DependencyPropertyValuePrecedences.Inheritance); + } + + // 4th: Check default value + var defaultValue = store.GetDefaultValue(dependencyProperty); + if(defaultValue != DependencyProperty.UnsetValue) + { + return (defaultValue, DependencyPropertyValuePrecedences.DefaultValue); + } + } + + // 5th: Return default value of the type (should not happen) + var propertyType = dependencyProperty.Type; + return (propertyType.IsValueType ? Activator.CreateInstance(propertyType) : null, DependencyPropertyValuePrecedences.DefaultValue); + } + + /// + /// Set the value of a property on an object for a given precedence. + /// + /// + /// You must know what you are doing when using this method as it can break the property system. + /// + public static void SetValueForPrecedence( + DependencyObject obj, + DependencyProperty dependencyProperty, + object value, + DependencyPropertyValuePrecedences precedence) + => obj.SetValue(dependencyProperty, value, precedence); + + /// + /// Get if the property value is inherited through the visual tree. + /// + public static bool GetPropertyIsInherited(DependencyProperty dependencyProperty) + => dependencyProperty.GetMetadata(dependencyProperty.OwnerType) is FrameworkPropertyMetadata metadata + && metadata.Options.HasFlag(FrameworkPropertyMetadataOptions.Inherits); + + /// + /// Get the multiple aspects of a given property at the same time. + /// + public static (Type ValueType, Type OwnerType, string Name, bool IsTypeNullable, bool IsAttached, bool IsInherited, object? defaultValue) GetPropertyDetails( + DependencyProperty property) + => (property.Type, + property.OwnerType, + property.Name, + property.IsTypeNullable, + property.IsAttached, + property.GetMetadata(property.OwnerType) is FrameworkPropertyMetadata metadata && metadata.Options.HasFlag(FrameworkPropertyMetadataOptions.Inherits), + property.GetMetadata(property.OwnerType)?.DefaultValue); +} From 25c3646aec707a5ee63ecc28f2945014d4100019 Mon Sep 17 00:00:00 2001 From: Carl de Billy Date: Thu, 12 Sep 2024 21:02:28 -0400 Subject: [PATCH 2/2] chore: Adjust method names --- .../Given_DependencyPropertyHelper.cs | 12 ++++---- .../Xaml/Internal/DependencyPropertyHelper.cs | 30 +++++++++++-------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/Uno.UI.RuntimeTests/Tests/Uno_Helpers/Given_DependencyPropertyHelper.cs b/src/Uno.UI.RuntimeTests/Tests/Uno_Helpers/Given_DependencyPropertyHelper.cs index 9d0a6bdcc847..5da739a50841 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Uno_Helpers/Given_DependencyPropertyHelper.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Uno_Helpers/Given_DependencyPropertyHelper.cs @@ -122,7 +122,7 @@ public void When_GetPropertyType() var property = TestClass.TestProperty; // Act - var propertyType = DependencyPropertyHelper.GetPropertyType(property); + var propertyType = DependencyPropertyHelper.GetValueType(property); // Assert propertyType.Should().Be(typeof(string)); @@ -135,7 +135,7 @@ public void When_GetPropertyDetails() var property = TestClass.TestProperty; // Act - var (valueType, ownerType, name, isTypeNullable, isAttached, inInherited, defaultValue) = DependencyPropertyHelper.GetPropertyDetails(property); + var (valueType, ownerType, name, isTypeNullable, isAttached, inInherited, defaultValue) = DependencyPropertyHelper.GetDetails(property); // Assert using var _ = new AssertionScope(); @@ -155,7 +155,7 @@ public void When_GetPropertyDetails_DataContext() var property = UIElement.DataContextProperty; // Act - var (valueType, _, name, isTypeNullable, isAttached, inInherited, defaultValue) = DependencyPropertyHelper.GetPropertyDetails(property); + var (valueType, _, name, isTypeNullable, isAttached, inInherited, defaultValue) = DependencyPropertyHelper.GetDetails(property); // Assert using var _ = new AssertionScope(); @@ -175,7 +175,7 @@ public void When_GetPropertyDetails_Attached() var property = Grid.RowProperty; // Act - var (valueType, ownerType, name, isTypeNullable, isAttached, inInherited, defaultValue) = DependencyPropertyHelper.GetPropertyDetails(property); + var (valueType, ownerType, name, isTypeNullable, isAttached, inInherited, defaultValue) = DependencyPropertyHelper.GetDetails(property); // Assert using var _ = new AssertionScope(); @@ -208,7 +208,7 @@ public void When_GetDefaultValue_Derived() // Assert defaultValue.Should().Be("TestValue"); } - + [TestMethod] public void When_GetDefaultUnsetValue_FromStyle() { @@ -223,7 +223,7 @@ public void When_GetDefaultUnsetValue_FromStyle() unsetValue.Should().Be("StyledTestValue"); precedence.Should().Be(DependencyPropertyValuePrecedences.ExplicitStyle); } - + [TestMethod] public void When_GetDefaultUnsetValue() { diff --git a/src/Uno.UI/UI/Xaml/Internal/DependencyPropertyHelper.cs b/src/Uno.UI/UI/Xaml/Internal/DependencyPropertyHelper.cs index 13b18bf3d6d9..1df1608380ea 100644 --- a/src/Uno.UI/UI/Xaml/Internal/DependencyPropertyHelper.cs +++ b/src/Uno.UI/UI/Xaml/Internal/DependencyPropertyHelper.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Microsoft.UI.Xaml.Controls; -using Uno.Extensions; namespace Uno.UI.Xaml.Core; @@ -92,13 +91,13 @@ public static bool TryGetDependencyPropertiesForType( /// /// Get the value type of the property. /// - public static Type GetPropertyType(DependencyProperty dependencyProperty) + public static Type GetValueType(DependencyProperty dependencyProperty) => dependencyProperty.Type; /// /// Get the name of the property. /// - public static string GetPropertyName(DependencyProperty dependencyProperty) + public static string GetName(DependencyProperty dependencyProperty) => dependencyProperty.Name; /// @@ -108,13 +107,13 @@ public static string GetPropertyName(DependencyProperty dependencyProperty) /// This is the property that defines the property, not the type that uses it. /// It may also be overridden by a derived type. /// - public static Type GetPropertyOwnerType(DependencyProperty dependencyProperty) + public static Type GetOwnerType(DependencyProperty dependencyProperty) => dependencyProperty.OwnerType; /// /// Get whether the property is an Attached Property. /// - public static bool GetPropertyIsAttached(DependencyProperty dependencyProperty) + public static bool GetIsAttached(DependencyProperty dependencyProperty) => dependencyProperty.IsAttached; /// @@ -148,18 +147,18 @@ public static (object? value, DependencyPropertyValuePrecedences precedence) Get return (valueFromImplicitStyle, DependencyPropertyValuePrecedences.ImplicitStyle); } - if(obj is IDependencyObjectStoreProvider { Store: { } store } && store.GetPropertyDetails(dependencyProperty) is { } details) + if (obj is IDependencyObjectStoreProvider { Store: { } store } && store.GetPropertyDetails(dependencyProperty) is { } details) { // 3rd: Check inherited value var inheritedValue = details.GetInheritedValue(); - if(inheritedValue != DependencyProperty.UnsetValue) + if (inheritedValue != DependencyProperty.UnsetValue) { return (details.GetInheritedValue(), DependencyPropertyValuePrecedences.Inheritance); } // 4th: Check default value var defaultValue = store.GetDefaultValue(dependencyProperty); - if(defaultValue != DependencyProperty.UnsetValue) + if (defaultValue != DependencyProperty.UnsetValue) { return (defaultValue, DependencyPropertyValuePrecedences.DefaultValue); } @@ -186,20 +185,25 @@ public static void SetValueForPrecedence( /// /// Get if the property value is inherited through the visual tree. /// - public static bool GetPropertyIsInherited(DependencyProperty dependencyProperty) + public static bool GetIsInherited(DependencyProperty dependencyProperty) => dependencyProperty.GetMetadata(dependencyProperty.OwnerType) is FrameworkPropertyMetadata metadata && metadata.Options.HasFlag(FrameworkPropertyMetadataOptions.Inherits); /// /// Get the multiple aspects of a given property at the same time. /// - public static (Type ValueType, Type OwnerType, string Name, bool IsTypeNullable, bool IsAttached, bool IsInherited, object? defaultValue) GetPropertyDetails( + public static (Type ValueType, Type OwnerType, string Name, bool IsTypeNullable, bool IsAttached, bool IsInherited, object? defaultValue) GetDetails( DependencyProperty property) - => (property.Type, + { + var propertyMetadata = property.GetMetadata(property.OwnerType); + + return (property.Type, property.OwnerType, property.Name, property.IsTypeNullable, property.IsAttached, - property.GetMetadata(property.OwnerType) is FrameworkPropertyMetadata metadata && metadata.Options.HasFlag(FrameworkPropertyMetadataOptions.Inherits), - property.GetMetadata(property.OwnerType)?.DefaultValue); + propertyMetadata is FrameworkPropertyMetadata metadata && + metadata.Options.HasFlag(FrameworkPropertyMetadataOptions.Inherits), + propertyMetadata?.DefaultValue); + } }