From a9a8f0cf48683714a380439285325cce23ab33d8 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Mon, 19 Aug 2024 14:56:39 +0300 Subject: [PATCH 1/2] refactor: DP system simplifications --- src/Uno.UI/DataBinding/BindingExpression.cs | 3 +- src/Uno.UI/UI/Xaml/Controls/Button/Button.cs | 13 --- .../Xaml/Controls/ProgressBar/ProgressBar.cs | 5 - .../Controls/ScrollViewer/ScrollViewer.cs | 15 --- .../UI/Xaml/Controls/TextBox/TextBox.cs | 2 +- src/Uno.UI/UI/Xaml/DependencyObjectStore.cs | 13 +-- src/Uno.UI/UI/Xaml/DependencyProperty.cs | 100 ++++++------------ .../UI/Xaml/DependencyPropertyDetails.cs | 10 +- .../UI/Xaml/FrameworkPropertyMetadata.cs | 19 ++-- src/Uno.UI/UI/Xaml/PropertyMetadata.cs | 64 ++--------- .../UI/Xaml/PropertyMetadataDictionary.cs | 64 ----------- src/Uno.UI/UI/Xaml/Shapes/Ellipse.cs | 5 - src/Uno.UI/UI/Xaml/Shapes/Rectangle.cs | 5 - 13 files changed, 60 insertions(+), 258 deletions(-) delete mode 100644 src/Uno.UI/UI/Xaml/PropertyMetadataDictionary.cs diff --git a/src/Uno.UI/DataBinding/BindingExpression.cs b/src/Uno.UI/DataBinding/BindingExpression.cs index c43ab24d1c53..1b7d4ebd06ad 100644 --- a/src/Uno.UI/DataBinding/BindingExpression.cs +++ b/src/Uno.UI/DataBinding/BindingExpression.cs @@ -369,7 +369,8 @@ private void ApplyFallbackValue(bool useTypeDefaultValue = true) } else if (useTypeDefaultValue && TargetPropertyDetails != null) { - SetTargetValue(TargetPropertyDetails.Property.GetMetadata(_view.Target?.GetType()).DefaultValue); + var viewTarget = _view.Target; + SetTargetValue(TargetPropertyDetails.Property.GetDefaultValue(viewTarget as UIElement, viewTarget?.GetType())); } } diff --git a/src/Uno.UI/UI/Xaml/Controls/Button/Button.cs b/src/Uno.UI/UI/Xaml/Controls/Button/Button.cs index f1ee31e3baa1..ee5460c650d7 100644 --- a/src/Uno.UI/UI/Xaml/Controls/Button/Button.cs +++ b/src/Uno.UI/UI/Xaml/Controls/Button/Button.cs @@ -5,19 +5,6 @@ namespace Microsoft.UI.Xaml.Controls { public partial class Button : ButtonBase { - static Button() - { - HorizontalContentAlignmentProperty.OverrideMetadata( - typeof(Button), - new FrameworkPropertyMetadata(HorizontalAlignment.Center) - ); - - VerticalContentAlignmentProperty.OverrideMetadata( - typeof(Button), - new FrameworkPropertyMetadata(VerticalAlignment.Center) - ); - } - /// /// Initializes a new instance of the Button class. /// diff --git a/src/Uno.UI/UI/Xaml/Controls/ProgressBar/ProgressBar.cs b/src/Uno.UI/UI/Xaml/Controls/ProgressBar/ProgressBar.cs index 39d2fa685894..21204846b50d 100644 --- a/src/Uno.UI/UI/Xaml/Controls/ProgressBar/ProgressBar.cs +++ b/src/Uno.UI/UI/Xaml/Controls/ProgressBar/ProgressBar.cs @@ -11,11 +11,6 @@ public partial class ProgressBar : RangeBase private FrameworkElement _determinateRoot; private FrameworkElement _progressBarIndicator; - static ProgressBar() - { - MaximumProperty.OverrideMetadata(typeof(ProgressBar), new FrameworkPropertyMetadata(100d)); - } - public ProgressBar() { TemplateSettings = new ProgressBarTemplateSettings(); diff --git a/src/Uno.UI/UI/Xaml/Controls/ScrollViewer/ScrollViewer.cs b/src/Uno.UI/UI/Xaml/Controls/ScrollViewer/ScrollViewer.cs index 8a7785be60e0..34abc21c4a2a 100644 --- a/src/Uno.UI/UI/Xaml/Controls/ScrollViewer/ScrollViewer.cs +++ b/src/Uno.UI/UI/Xaml/Controls/ScrollViewer/ScrollViewer.cs @@ -115,21 +115,6 @@ public static class ScrollBarsSeparator internal event SizeChangedEventHandler? ExtentSizeChanged; - static ScrollViewer() - { -#if !IS_UNIT_TESTS - HorizontalContentAlignmentProperty.OverrideMetadata( - typeof(ScrollViewer), - new FrameworkPropertyMetadata(HorizontalAlignment.Stretch) - ); - - VerticalContentAlignmentProperty.OverrideMetadata( - typeof(ScrollViewer), - new FrameworkPropertyMetadata(VerticalAlignment.Top) - ); -#endif - } - public ScrollViewer() { DefaultStyleKey = typeof(ScrollViewer); diff --git a/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs index 1b2c8fabb492..fd39a342eecf 100644 --- a/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs +++ b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs @@ -205,7 +205,7 @@ private void InitializeProperties() OnIsSpellCheckEnabledChanged(IsSpellCheckEnabled); OnTextAlignmentChanged(TextAlignment); OnTextWrappingChanged(); - OnFocusStateChanged((FocusState)FocusStateProperty.GetMetadata(GetType()).DefaultValue, FocusState, initial: true); + OnFocusStateChanged(FocusState.Unfocused, FocusState, initial: true); OnTextCharacterCasingChanged(CharacterCasing); UpdateDescriptionVisibility(true); var buttonRef = _deleteButton?.GetTarget(); diff --git a/src/Uno.UI/UI/Xaml/DependencyObjectStore.cs b/src/Uno.UI/UI/Xaml/DependencyObjectStore.cs index 017d28b3017f..6dfaf3a2f210 100644 --- a/src/Uno.UI/UI/Xaml/DependencyObjectStore.cs +++ b/src/Uno.UI/UI/Xaml/DependencyObjectStore.cs @@ -627,10 +627,7 @@ private static void TryClearBinding(object? value, DependencyPropertyDetails pro private void TryUpdateInheritedAttachedProperty(DependencyProperty property, DependencyPropertyDetails propertyDetails) { - if ( - property.IsAttached - && propertyDetails.Metadata is FrameworkPropertyMetadata fm - && fm.Options.HasInherits()) + if (property.IsAttached && property.IsInherited) { // Add inheritable attached properties to the inherited forwarded // properties, so they can be automatically propagated when a child @@ -651,14 +648,14 @@ private void ApplyCoercion(DependencyObject actualInstanceAlias, DependencyPrope return; } - var coerceValueCallback = propertyDetails.Metadata.CoerceValueCallback; + var coerceValueCallback = propertyDetails.Property.Metadata.CoerceValueCallback; if (coerceValueCallback == null) { // No coercion to remove or to apply. return; } - var options = (propertyDetails.Metadata as FrameworkPropertyMetadata)?.Options ?? FrameworkPropertyMetadataOptions.Default; + var options = (propertyDetails.Property.Metadata as FrameworkPropertyMetadata)?.Options ?? FrameworkPropertyMetadataOptions.Default; if (Equals(previousValue, baseValue) && ((options & FrameworkPropertyMetadataOptions.CoerceOnlyWhenChanged) != 0)) { @@ -1872,7 +1869,7 @@ private void InvokeCallbacks( ) { //var propertyChangedParams = new PropertyChangedParams(property, previousValue, newValue); - var propertyMetadata = propertyDetails.Metadata; + var propertyMetadata = property.Metadata; // We can reuse the weak reference, otherwise capture the weak reference to this instance. var instanceRef = _originalObjectRef ?? ThisWeakReference; @@ -2041,7 +2038,7 @@ DependencyPropertyDetails propertyDetails { if (value != null && value != DependencyProperty.UnsetValue - && ((propertyDetails.Metadata as FrameworkPropertyMetadata)?.Options.HasAutoConvert() ?? false)) + && ((propertyDetails.Property.Metadata as FrameworkPropertyMetadata)?.Options.HasAutoConvert() ?? false)) { if (value?.GetType() != propertyDetails.Property.Type) { diff --git a/src/Uno.UI/UI/Xaml/DependencyProperty.cs b/src/Uno.UI/UI/Xaml/DependencyProperty.cs index 3f581a87f5db..65eb6c42d969 100644 --- a/src/Uno.UI/UI/Xaml/DependencyProperty.cs +++ b/src/Uno.UI/UI/Xaml/DependencyProperty.cs @@ -6,6 +6,10 @@ using System.Text; using System.Threading; using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Controls.Primitives; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Shapes; using Uno; using Uno.Extensions; using Uno.UI; @@ -42,7 +46,6 @@ public sealed partial class DependencyProperty private readonly static FrameworkPropertiesForTypeDictionary _getFrameworkPropertiesForType = new FrameworkPropertiesForTypeDictionary(); private readonly PropertyMetadata _ownerTypeMetadata; // For perf consideration, we keep direct ref the metadata for the owner type - private readonly PropertyMetadataDictionary _metadata = new PropertyMetadataDictionary(); private readonly Flags _flags; private string _name; @@ -85,7 +88,6 @@ private DependencyProperty(string name, Type propertyType, Type ownerType, Prope _uniqueId = Interlocked.Increment(ref _globalId); _ownerTypeMetadata = defaultMetadata ?? new FrameworkPropertyMetadata(null); - _metadata.Add(_ownerType, _ownerTypeMetadata); } // This is our equivalent of WinUI's ValidateXXX methods in PropertySystem.cpp, e.g, CDependencyObject::ValidateFloatValue @@ -109,6 +111,8 @@ internal void ValidateValue(object value) /// internal int UniqueId => _uniqueId; + internal PropertyMetadata Metadata => _ownerTypeMetadata; + /// /// Determines if the property storage should be backed by a /// @@ -236,79 +240,22 @@ internal int CachedHashCode /// A property metadata object. public PropertyMetadata GetMetadata(Type forType) { + // NOTE: WinUI always allocates a and returns fresh PropertyMetadata and only sets DefaultValue on the returned instance, and nothing else. + // For now, we have internal usages that requires the full info to be returned, so we return the complete info and even the same instance. + if (forType == _ownerType) { return _ownerTypeMetadata; } - PropertyMetadata metadata = null; - if (!_metadata.TryGetValue(forType, out metadata)) - { - if ( - !IsTypeDependencyObject(forType) -#if !UNO_REFERENCE_API - // Perf: On generic API the Panel.Children are UIElement, so avoid costly check - // This check must be removed when Panel.Children will support only - // UIElement as its elements. See #103492 - && !forType.Is<_View>() -#endif - && OwnerType != typeof(AttachedDependencyObject) - ) - { - throw new ArgumentException($"'{forType}' type must derive from DependencyObject.", nameof(forType)); - } - - var baseType = forType.IsSubclassOf(_ownerType) - ? forType.BaseType - : _ownerType; - - ForceInitializeTypeConstructor(forType); - - metadata = _metadata.FindOrCreate(forType, baseType, this); - } - - return metadata; - } - - private static bool IsTypeDependencyObject(Type forType) => typeof(DependencyObject).IsAssignableFrom(forType); - - internal void OverrideMetadata(Type forType, PropertyMetadata typeMetadata) - { - ForceInitializeTypeConstructor(forType); - - if (forType == null) - { - throw new ArgumentNullException(nameof(forType), "Value cannot be null."); - } - - if (typeMetadata == null) - { - throw new ArgumentNullException(nameof(typeMetadata)); - } - - if (!(typeof(DependencyObject).IsAssignableFrom(forType))) - { - throw new ArgumentException($"'{forType}' type must derive from DependencyObject.", nameof(forType)); - } - - if (_metadata.ContainsKey(forType)) - { - throw new ArgumentException($"PropertyMetadata is already registered for type '{forType}'.", nameof(forType)); - } + var defaultValueForType = GetDefaultValue(null, forType); - if (_metadata.ContainsValue(typeMetadata)) + if (!DependencyObjectStore.AreDifferent(_ownerTypeMetadata.DefaultValue, defaultValueForType)) { - throw new ArgumentException($"Metadata is already associated with a type and property. A new one must be created.", nameof(typeMetadata)); - } - - var baseMetadata = GetMetadata(forType.BaseType); - if (!baseMetadata.GetType().IsAssignableFrom(typeMetadata.GetType())) - { - throw new ArgumentException($"Metadata override and base metadata must be of the same type or derived type.", nameof(typeMetadata)); + return _ownerTypeMetadata; } - typeMetadata.Merge(baseMetadata, this); - _metadata.Add(forType, typeMetadata); + return _ownerTypeMetadata.CloneWithOverwrittenDefaultValue(defaultValueForType); } internal Type OwnerType @@ -519,7 +466,7 @@ private static DependencyProperty[] InternalGetFrameworkPropertiesForType(Type t foreach (var prop in GetPropertiesForType(type)) { - var propertyOptions = (prop.GetMetadata(type) as FrameworkPropertyMetadata)?.Options; + var propertyOptions = (prop.Metadata as FrameworkPropertyMetadata)?.Options; if (propertyOptions != null && (propertyOptions & options) != 0) { @@ -530,6 +477,25 @@ private static DependencyProperty[] InternalGetFrameworkPropertiesForType(Type t return output.ToArray(); } + internal object GetDefaultValue(UIElement referenceObject, Type forType) + { + if (referenceObject?.GetDefaultValue2(this, out var defaultValue) == true) + { + return defaultValue; + } + + if (this == Shape.StretchProperty) + { + if (forType == typeof(Rectangle) || forType == typeof(Ellipse)) + { + return Stretch.Fill; + } + } + + // TODO: Handle DependencyProperty.CreateDefaultValueCallback when implemented. + return _ownerTypeMetadata.DefaultValue; + } + public override int GetHashCode() => CachedHashCode; [Flags] diff --git a/src/Uno.UI/UI/Xaml/DependencyPropertyDetails.cs b/src/Uno.UI/UI/Xaml/DependencyPropertyDetails.cs index b6ac32107e3b..76ae9fef2d28 100644 --- a/src/Uno.UI/UI/Xaml/DependencyPropertyDetails.cs +++ b/src/Uno.UI/UI/Xaml/DependencyPropertyDetails.cs @@ -23,7 +23,6 @@ internal class DependencyPropertyDetails : IEnumerable, IEnumerable, ID private object? _fastLocalValue; private BindingExpression? _binding; private object?[]? _stack; - private PropertyMetadata? _metadata; private object? _defaultValue; private Flags _flags; private DependencyPropertyCallbackManager? _callbackManager; @@ -73,8 +72,6 @@ public void Dispose() public DependencyProperty Property { get; } - public PropertyMetadata Metadata => _metadata ??= Property.GetMetadata(_dependencyObjectType); - /// /// Constructor /// @@ -84,7 +81,7 @@ internal DependencyPropertyDetails(DependencyProperty property, Type dependencyO Property = property; _dependencyObjectType = dependencyObjectType; - GetPropertyInheritanceConfiguration(isTemplatedParentOrDataContext, out var hasInherits, out var hasValueInherits, out var hasValueDoesNotInherits); + GetPropertyInheritanceConfiguration(property, isTemplatedParentOrDataContext, out var hasInherits, out var hasValueInherits, out var hasValueDoesNotInherits); _flags |= property.HasWeakStorage ? Flags.WeakStorage : Flags.None; _flags |= hasValueInherits ? Flags.ValueInherits : Flags.None; @@ -99,6 +96,7 @@ internal static void ContinueLocalCanDefeatAnimations() => _localCanDefeatAnimationSuppressed--; private void GetPropertyInheritanceConfiguration( + DependencyProperty property, bool isTemplatedParentOrDataContext, out bool hasInherits, out bool hasValueInherits, @@ -113,7 +111,7 @@ private void GetPropertyInheritanceConfiguration( return; } - if (Metadata is FrameworkPropertyMetadata propertyMetadata) + if (property.Metadata is FrameworkPropertyMetadata propertyMetadata) { hasValueInherits = propertyMetadata.Options.HasValueInheritsDataContext(); hasValueDoesNotInherit = propertyMetadata.Options.HasValueDoesNotInheritDataContext(); @@ -130,7 +128,7 @@ private void GetPropertyInheritanceConfiguration( { if (!HasDefaultValueSet) { - _defaultValue = Metadata.DefaultValue; + _defaultValue = Property.GetMetadata(_dependencyObjectType).DefaultValue; // Ensures that the default value of non-nullable properties is not null if (_defaultValue == null && !Property.IsTypeNullable) diff --git a/src/Uno.UI/UI/Xaml/FrameworkPropertyMetadata.cs b/src/Uno.UI/UI/Xaml/FrameworkPropertyMetadata.cs index 3057de76ac3e..b9f55d43a413 100644 --- a/src/Uno.UI/UI/Xaml/FrameworkPropertyMetadata.cs +++ b/src/Uno.UI/UI/Xaml/FrameworkPropertyMetadata.cs @@ -144,7 +144,7 @@ public UpdateSourceTrigger DefaultUpdateSourceTrigger { get { - if (this == TextBox.TextProperty.GetMetadata(typeof(TextBox))) + if (this == TextBox.TextProperty.Metadata) { return UpdateSourceTrigger.Explicit; } @@ -153,17 +153,6 @@ public UpdateSourceTrigger DefaultUpdateSourceTrigger } } - protected internal override void Merge(PropertyMetadata baseMetadata, DependencyProperty dp) - { - base.Merge(baseMetadata, dp); - - if (baseMetadata is FrameworkPropertyMetadata baseFrameworkMetadata) - { - // Merge options flags - Options |= baseFrameworkMetadata.Options; - } - } - /// /// Temporary flag to opt-in for automatic parent propagation. /// @@ -185,5 +174,11 @@ public bool HasWeakStorage ? Options |= FrameworkPropertyMetadataOptions.WeakStorage : Options &= ~FrameworkPropertyMetadataOptions.WeakStorage; } + + internal override PropertyMetadata CloneWithOverwrittenDefaultValue(object newDefaultValue) + { + // This should clone CreateDefaultValueCallback when it's supported + return new FrameworkPropertyMetadata(newDefaultValue, Options, PropertyChangedCallback, CoerceValueCallback, BackingFieldUpdateCallback); + } } } diff --git a/src/Uno.UI/UI/Xaml/PropertyMetadata.cs b/src/Uno.UI/UI/Xaml/PropertyMetadata.cs index 61f734665e8c..d5df41fd7308 100644 --- a/src/Uno.UI/UI/Xaml/PropertyMetadata.cs +++ b/src/Uno.UI/UI/Xaml/PropertyMetadata.cs @@ -32,12 +32,6 @@ namespace Microsoft.UI.Xaml /// public partial class PropertyMetadata { - private bool _isDefaultValueSet; - private object _defaultValue; - - private bool _isCoerceValueCallbackSet; - private CoerceValueCallback _coerceValueCallback; - internal PropertyMetadata() { } public PropertyMetadata( @@ -125,62 +119,14 @@ BackingFieldUpdateCallback backingFieldUpdateCallback BackingFieldUpdateCallback = backingFieldUpdateCallback; } - public object DefaultValue - { - get => _defaultValue; - internal set - { - _defaultValue = value; - _isDefaultValueSet = true; - } - } + public object DefaultValue { get; } public PropertyChangedCallback PropertyChangedCallback { get; internal set; } - internal CoerceValueCallback CoerceValueCallback - { - get => _coerceValueCallback; - set - { - _coerceValueCallback = value; - _isCoerceValueCallbackSet = true; - } - } + internal CoerceValueCallback CoerceValueCallback { get; set; } internal BackingFieldUpdateCallback BackingFieldUpdateCallback { get; set; } - internal protected virtual void Merge(PropertyMetadata baseMetadata, DependencyProperty dp) - { - // The supplied metadata is merged with the property metadata for - // the dependency property as it exists on the base owner. Any - // characteristics that were specified in the original base - // metadata will persist; only those characteristics that were - // specifically changed in the new metadata will override the - // characteristics of the base metadata. Some characteristics such - // as DefaultValue are replaced if specified in the new metadata. - // Others, such as PropertyChangedCallback, are combined. - // Ultimately, the merge behavior depends on the property metadata - // type being used for the override, so the behavior described here - // is for the existing property metadata classes used by WPF - // dependency properties. For details, see Dependency Property - // Metadata and Framework Property Metadata. - // Source: https://msdn.microsoft.com/en-us/library/ms597491(v=vs.110).aspx - - if (!_isCoerceValueCallbackSet) - { - CoerceValueCallback = baseMetadata.CoerceValueCallback; - } - - if (!_isDefaultValueSet) - { - DefaultValue = baseMetadata.DefaultValue; - } - - // Merge PropertyChangedCallback delegates - PropertyChangedCallback = baseMetadata.PropertyChangedCallback + PropertyChangedCallback; - BackingFieldUpdateCallback = baseMetadata.BackingFieldUpdateCallback + BackingFieldUpdateCallback; - } - internal void MergePropertyChangedCallback(PropertyChangedCallback callback) { PropertyChangedCallback += callback; @@ -197,5 +143,11 @@ internal void RaiseBackingFieldUpdate(DependencyObject source, object newValue) { BackingFieldUpdateCallback?.Invoke(source, newValue); } + + internal virtual PropertyMetadata CloneWithOverwrittenDefaultValue(object newDefaultValue) + { + // This should clone CreateDefaultValueCallback when it's supported + return new PropertyMetadata(newDefaultValue, PropertyChangedCallback, CoerceValueCallback, BackingFieldUpdateCallback); + } } } diff --git a/src/Uno.UI/UI/Xaml/PropertyMetadataDictionary.cs b/src/Uno.UI/UI/Xaml/PropertyMetadataDictionary.cs deleted file mode 100644 index 35079d6c9038..000000000000 --- a/src/Uno.UI/UI/Xaml/PropertyMetadataDictionary.cs +++ /dev/null @@ -1,64 +0,0 @@ -#nullable enable - -using System; -using System.Collections; -using System.Linq; -using System.Text; -using Microsoft.UI.Xaml; -using Uno.Extensions; -using System.Diagnostics; -using System.Runtime.CompilerServices; -using Uno; -using System.Threading; -using Uno.Collections; - -namespace Microsoft.UI.Xaml -{ - internal class PropertyMetadataDictionary - { - /// - /// This implementation of PropertyMetadataDictionary uses HashTable because it's generally faster than - /// Dictionary`ref/ref. - /// - /// - /// This is created per dependency property instance. So there is a hashtable for each DP, and it's likely that all these instances live for the lifetime of the app. - /// So we don't use pooling to not cause pool exhaustion by renting without returning. - /// - private readonly HashtableEx _table = new HashtableEx(usePooling: false); - - internal void Add(Type ownerType, PropertyMetadata ownerTypeMetadata) - => _table.Add(ownerType, ownerTypeMetadata); - - internal bool TryGetValue(Type ownerType, out PropertyMetadata? metadata) - { - if (_table.TryGetValue(ownerType, out var value)) - { - metadata = (PropertyMetadata)value!; - return true; - } - - metadata = null; - return false; - } - - internal bool ContainsKey(Type ownerType) - => _table.ContainsKey(ownerType); - - internal bool ContainsValue(PropertyMetadata typeMetadata) - => _table.ContainsValue(typeMetadata); - - internal PropertyMetadata FindOrCreate(Type ownerType, Type baseType, DependencyProperty property) - { - if (_table.TryGetValue(ownerType, out var value)) - { - return (PropertyMetadata)value!; - } - - var metadata = property.GetMetadata(baseType); - - _table[ownerType] = metadata; - - return metadata; - } - } -} diff --git a/src/Uno.UI/UI/Xaml/Shapes/Ellipse.cs b/src/Uno.UI/UI/Xaml/Shapes/Ellipse.cs index ed0fd711d5b9..cbce160c9a9d 100644 --- a/src/Uno.UI/UI/Xaml/Shapes/Ellipse.cs +++ b/src/Uno.UI/UI/Xaml/Shapes/Ellipse.cs @@ -4,11 +4,6 @@ namespace Microsoft.UI.Xaml.Shapes { public partial class Ellipse : Shape { - static Ellipse() - { - StretchProperty.OverrideMetadata(typeof(Ellipse), new FrameworkPropertyMetadata(defaultValue: Media.Stretch.Fill)); - } - #if __IOS__ || __MACOS__ || __SKIA__ || __ANDROID__ || __WASM__ protected override Size MeasureOverride(Size availableSize) => MeasureRelativeShape(availableSize); #endif diff --git a/src/Uno.UI/UI/Xaml/Shapes/Rectangle.cs b/src/Uno.UI/UI/Xaml/Shapes/Rectangle.cs index 6d9da099b17e..6996548e52b3 100644 --- a/src/Uno.UI/UI/Xaml/Shapes/Rectangle.cs +++ b/src/Uno.UI/UI/Xaml/Shapes/Rectangle.cs @@ -4,11 +4,6 @@ namespace Microsoft.UI.Xaml.Shapes { public partial class Rectangle : Shape { - static Rectangle() - { - StretchProperty.OverrideMetadata(typeof(Rectangle), new FrameworkPropertyMetadata(defaultValue: Media.Stretch.Fill)); - } - #if __IOS__ || __MACOS__ || __SKIA__ || __ANDROID__ || __WASM__ /// protected override Size MeasureOverride(Size availableSize) From e2b8083d61a12ef9627014671df5dc3242e3b514 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Mon, 19 Aug 2024 16:33:05 +0300 Subject: [PATCH 2/2] chore: Remove no longer relevant tests --- .../BinderTests/Given_Binder.LocalCache.cs | 18 -- .../Given_DependencyProperty.cs | 187 ------------------ 2 files changed, 205 deletions(-) diff --git a/src/Uno.UI.Tests/BinderTests/Given_Binder.LocalCache.cs b/src/Uno.UI.Tests/BinderTests/Given_Binder.LocalCache.cs index 26c43df42747..77d7193dafa0 100644 --- a/src/Uno.UI.Tests/BinderTests/Given_Binder.LocalCache.cs +++ b/src/Uno.UI.Tests/BinderTests/Given_Binder.LocalCache.cs @@ -107,16 +107,6 @@ public void When_Coerce_And_Coerce_False() Assert.AreEqual(true, SUT.IsEnabled); } - - [TestMethod] - public void When_Coerce_And_MetadataOverride() - { - var SUT = new BinderLocalCache_Data_IsEnabled_Inherited(); - Assert.AreEqual(false, SUT.IsEnabled); - - SUT.SetValue(BinderLocalCache_Data_IsEnabled.IsEnabledProperty, true); - Assert.AreEqual(true, SUT.IsEnabled); - } } public partial class BinderLocalCache_Data : DependencyObject @@ -158,12 +148,4 @@ public void SuppressIsEnabled(bool suppress) this.CoerceValue(IsEnabledProperty); } } - - public partial class BinderLocalCache_Data_IsEnabled_Inherited : BinderLocalCache_Data_IsEnabled - { - static BinderLocalCache_Data_IsEnabled_Inherited() - { - IsEnabledProperty.OverrideMetadata(typeof(BinderLocalCache_Data_IsEnabled_Inherited), new FrameworkPropertyMetadata(defaultValue: false)); - } - } } diff --git a/src/Uno.UI.Tests/DependencyProperty/Given_DependencyProperty.cs b/src/Uno.UI.Tests/DependencyProperty/Given_DependencyProperty.cs index 8cb0e8363161..0e335bfc5b2e 100644 --- a/src/Uno.UI.Tests/DependencyProperty/Given_DependencyProperty.cs +++ b/src/Uno.UI.Tests/DependencyProperty/Given_DependencyProperty.cs @@ -980,41 +980,6 @@ private object Custom(object dependencyObject, object baseValue, DependencyPrope #endregion - [TestMethod] - [ExpectedException(typeof(ArgumentException))] - public void When_OverrideMetadata_With_Metadata_Is_Not_Derived_From_BaseMetadata_Then_Fail() - { - var testProperty = DependencyProperty.Register( - nameof(When_OverrideMetadata_With_Metadata_Is_Not_Derived_From_BaseMetadata_Then_Fail), - typeof(string), - typeof(MockDependencyObject), - new FrameworkPropertyMetadata(null) - ); - - testProperty.OverrideMetadata(typeof(MockDependencyObject2), new PropertyMetadata(null)); - } - - [TestMethod] - [ExpectedException(typeof(ArgumentException))] - public void When_OverrideMetadata_With_ForType_Is_OwnerType_Then_Fail() - { - var testProperty = DependencyProperty.Register( - nameof(When_OverrideMetadata_With_ForType_Is_OwnerType_Then_Fail), - typeof(string), - typeof(MockDependencyObject), - null - ); - - testProperty.OverrideMetadata(typeof(MockDependencyObject), new PropertyMetadata("test")); - } - - [TestMethod] - [ExpectedException(typeof(ArgumentException))] - public void When_OverrideMetadata_With_Same_ForType_Twice_Then_Fail() - { - MyDependencyObject1.MyPropertyProperty.OverrideMetadata(typeof(MyDependencyObject2), new PropertyMetadata("test")); - } - [TestMethod] public void When_PropertyMetadata_Is_Null() { @@ -1049,54 +1014,6 @@ public void When_SetValue_With_Coercion_Precedence_Then_Fail() SUT.SetValue(testProperty, "test", DependencyPropertyValuePrecedences.Coercion); } - [TestMethod] - public void When_OverrideMetadata_DefaultValue() - { - var SUT1 = new MyDependencyObject1(); - var SUT2 = new MyDependencyObject2(); - var SUT3 = new MyDependencyObject3(); - - Assert.AreEqual("default1", MyDependencyObject1.MyPropertyProperty.GetMetadata(typeof(MyDependencyObject1)).DefaultValue); - Assert.AreEqual("default2", MyDependencyObject1.MyPropertyProperty.GetMetadata(typeof(MyDependencyObject2)).DefaultValue); - Assert.AreEqual("default3", MyDependencyObject1.MyPropertyProperty.GetMetadata(typeof(MyDependencyObject3)).DefaultValue); - - Assert.AreEqual("default1", SUT1.GetValue(MyDependencyObject1.MyPropertyProperty)); - Assert.AreEqual("default2", SUT2.GetValue(MyDependencyObject1.MyPropertyProperty)); - Assert.AreEqual("default3", SUT3.GetValue(MyDependencyObject1.MyPropertyProperty)); - } - - [TestMethod] - public void When_OverrideMetadata_CoerceValueCallback() - { - var SUT1 = new MyDependencyObject1(); - var SUT2 = new MyDependencyObject2(); - var SUT3 = new MyDependencyObject3(); - - // +1 CoerceValueCallback (1) - SUT1.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); - SUT2.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); - SUT3.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); - - // +1 CoerceValueCallback (2) - SUT1.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); - SUT2.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); - SUT3.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); - - // +1 CoerceValueCallback (3) - SUT1.SetValue(MyDependencyObject1.MyPropertyProperty, "B"); - SUT2.SetValue(MyDependencyObject1.MyPropertyProperty, "B"); - SUT3.SetValue(MyDependencyObject1.MyPropertyProperty, "B"); - - // +1 CoerceValueCallback (4) - SUT1.CoerceValue(MyDependencyObject1.MyPropertyProperty); - SUT2.CoerceValue(MyDependencyObject1.MyPropertyProperty); - SUT3.CoerceValue(MyDependencyObject1.MyPropertyProperty); - - Assert.AreEqual(4, SUT1.CoerceValueCallbackCount); - Assert.AreEqual(4, SUT2.CoerceValueCallbackCount); - Assert.AreEqual(4, SUT3.CoerceValueCallbackCount); - } - [TestMethod] public void When_SetValue_Inheritance_And_CoerceValue_Then_GetValue_Local_Is_UnsetValue() { @@ -1108,77 +1025,6 @@ public void When_SetValue_Inheritance_And_CoerceValue_Then_GetValue_Local_Is_Uns Assert.AreEqual(DependencyProperty.UnsetValue, SUT.GetValue(MyDependencyObject1.MyPropertyProperty, DependencyPropertyValuePrecedences.Local)); } - [TestMethod] - public void When_OverrideMetadata_PropertyChangedCallback() - { - var SUT1 = new MyDependencyObject1(); - var SUT2 = new MyDependencyObject2(); - var SUT3 = new MyDependencyObject3(); - - SUT1.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); // +1 PropertyChangedCallback (1) - SUT2.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); // +2 PropertyChangedCallback (2) - SUT3.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); // +3 PropertyChangedCallback (3) - - SUT1.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); // +0 PropertyChangedCallback (1) - SUT2.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); // +0 PropertyChangedCallback (2) - SUT3.SetValue(MyDependencyObject1.MyPropertyProperty, "A"); // +0 PropertyChangedCallback (3) - - SUT1.SetValue(MyDependencyObject1.MyPropertyProperty, "B"); // +1 PropertyChangedCallback (2) - SUT2.SetValue(MyDependencyObject1.MyPropertyProperty, "B"); // +2 PropertyChangedCallback (4) - SUT3.SetValue(MyDependencyObject1.MyPropertyProperty, "B"); // +3 PropertyChangedCallback (6) - - SUT1.CoerceValue(MyDependencyObject1.MyPropertyProperty); // +0 PropertyChangedCallback (2) - SUT1.CoerceValue(MyDependencyObject1.MyPropertyProperty); // +0 PropertyChangedCallback (4) - SUT1.CoerceValue(MyDependencyObject1.MyPropertyProperty); // +0 PropertyChangedCallback (6) - - var propertyChangedCallbacks1 = new[] - { - "changed1: coercion1: A", - "changed1: coercion1: B", - }; - - var propertyChangedCallbacks2 = new[] - { - "changed1: coercion2: A", - "changed2: coercion2: A", - "changed1: coercion2: B", - "changed2: coercion2: B", - }; - - var propertyChangedCallbacks3 = new[] - { - "changed1: coercion3: A", - "changed2: coercion3: A", - "changed3: coercion3: A", - "changed1: coercion3: B", - "changed2: coercion3: B", - "changed3: coercion3: B", - }; - - Assert.IsTrue(SUT1.PropertyChangedCallbacks.SequenceEqual(propertyChangedCallbacks1)); - Assert.IsTrue(SUT2.PropertyChangedCallbacks.SequenceEqual(propertyChangedCallbacks2)); - Assert.IsTrue(SUT3.PropertyChangedCallbacks.SequenceEqual(propertyChangedCallbacks3)); - } - - [TestMethod] - public void When_OverrideMetadata_FrameworkPropertyMetadata_Options() - { - var SUT1 = new MyDependencyObject1(); - var SUT2 = new MyDependencyObject2(); - var SUT3 = new MyDependencyObject3(); - - var metadata1 = MyDependencyObject1.MyPropertyProperty.GetMetadata(typeof(MyDependencyObject1)); - var metadata2 = MyDependencyObject1.MyPropertyProperty.GetMetadata(typeof(MyDependencyObject2)); - var metadata3 = MyDependencyObject1.MyPropertyProperty.GetMetadata(typeof(MyDependencyObject3)); - - Assert.IsNotInstanceOfType(metadata1, typeof(FrameworkPropertyMetadata)); - Assert.IsInstanceOfType(metadata2, typeof(FrameworkPropertyMetadata)); - Assert.IsInstanceOfType(metadata3, typeof(FrameworkPropertyMetadata)); - - Assert.AreEqual(FrameworkPropertyMetadataOptions.Default | FrameworkPropertyMetadataOptions.Inherits, (metadata2 as FrameworkPropertyMetadata).Options); - Assert.AreEqual(FrameworkPropertyMetadataOptions.Default | FrameworkPropertyMetadataOptions.Inherits, (metadata3 as FrameworkPropertyMetadata).Options); - } - [TestMethod] public void When_SetValue_TypeA_On_PropertyType_Of_TypeB_Then_GetValue_Of_TypeA() { @@ -2037,39 +1883,6 @@ public MyDependencyObject1() { } public int CoerceValueCallbackCount { get; set; } = 0; } - partial class MyDependencyObject2 : MyDependencyObject1 - { - static MyDependencyObject2() - { - var metadata = new FrameworkPropertyMetadata( - "default2", - FrameworkPropertyMetadataOptions.Inherits, - (s, e) => { (s as MyDependencyObject1).PropertyChangedCallbacks.Add("changed2: " + e.NewValue); }, - (s, baseValue, _) => { (s as MyDependencyObject1).CoerceValueCallbackCount++; return "coercion2: " + baseValue; } - ); - - MyPropertyProperty.OverrideMetadata(typeof(MyDependencyObject2), metadata); - } - - public MyDependencyObject2() { } - } - - partial class MyDependencyObject3 : MyDependencyObject2 - { - static MyDependencyObject3() - { - var metadata = new FrameworkPropertyMetadata( - "default3", - (s, e) => { (s as MyDependencyObject1).PropertyChangedCallbacks.Add("changed3: " + e.NewValue); }, - (s, baseValue, _) => { (s as MyDependencyObject1).CoerceValueCallbackCount++; return "coercion3: " + baseValue; } - ); - - MyPropertyProperty.OverrideMetadata(typeof(MyDependencyObject3), metadata); - } - - public MyDependencyObject3() { } - } - partial class NullablePropertyOwner : FrameworkElement {