Skip to content

Commit

Permalink
fix: Fix inherited attached DP propagation and refactor few RegisterP…
Browse files Browse the repository at this point in the history
…ropertyChangedCallback callsites
  • Loading branch information
Youssef1313 committed Aug 17, 2024
1 parent 77d9c05 commit addc6b4
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void When_SimpleInheritance_Late()
root.Children.Add(child1);

MyAttachedPropType.SetMyProperty(root, 42);
Assert.AreEqual(0, MyAttachedPropType.GetMyProperty(child1));
Assert.AreEqual(42, MyAttachedPropType.GetMyProperty(child1));

MyAttachedPropType.SetMyProperty(root, 43);
Assert.AreEqual(43, MyAttachedPropType.GetMyProperty(child1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Brush BorderBrush

private static Brush GetBorderBrushDefaultValue() => SolidColorBrushHelper.Transparent;

[GeneratedDependencyProperty(ChangedCallback = false, Options = FrameworkPropertyMetadataOptions.ValueInheritsDataContext)]
[GeneratedDependencyProperty(ChangedCallback = true, ChangedCallbackName = nameof(OnPropertyChanged), Options = FrameworkPropertyMetadataOptions.ValueInheritsDataContext)]
public static DependencyProperty BorderBrushProperty { get; } = CreateBorderBrushProperty();

#endregion
Expand All @@ -31,7 +31,7 @@ public Thickness BorderThickness

private static Thickness GetBorderThicknessDefaultValue() => Thickness.Empty;

[GeneratedDependencyProperty(ChangedCallback = false)]
[GeneratedDependencyProperty(ChangedCallback = true, ChangedCallbackName = nameof(OnPropertyChanged))]
public static DependencyProperty BorderThicknessProperty { get; } = CreateBorderThicknessProperty();

#endregion
Expand All @@ -46,7 +46,7 @@ public Thickness Padding

private static Thickness GetPaddingDefaultValue() => Thickness.Empty;

[GeneratedDependencyProperty(ChangedCallback = false, Options = FrameworkPropertyMetadataOptions.AffectsMeasure)]
[GeneratedDependencyProperty(ChangedCallback = true, ChangedCallbackName = nameof(OnPropertyChanged), Options = FrameworkPropertyMetadataOptions.AffectsMeasure)]
public static DependencyProperty PaddingProperty { get; } = CreatePaddingProperty();

#endregion
Expand All @@ -61,13 +61,13 @@ public CornerRadius CornerRadius

private static CornerRadius GetCornerRadiusDefaultValue() => CornerRadius.None;

[GeneratedDependencyProperty(ChangedCallback = false)]
[GeneratedDependencyProperty(ChangedCallback = true, ChangedCallbackName = nameof(OnPropertyChanged))]
public static DependencyProperty CornerRadiusProperty { get; } = CreateCornerRadiusProperty();

#endregion

public static DependencyProperty LayoutProperty { get; } = DependencyProperty.Register(
"Layout", typeof(Layout), typeof(LayoutPanel), new FrameworkPropertyMetadata(default(Layout)));
"Layout", typeof(Layout), typeof(LayoutPanel), new FrameworkPropertyMetadata(default(Layout), propertyChangedCallback: (sender, args) => ((LayoutPanel)sender).OnPropertyChanged(args)));

#if __ANDROID__ || __MACOS__
new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ namespace Microsoft/* UWP don't rename */.UI.Xaml.Controls

partial class LayoutPanel
{

public LayoutPanel()
{
this.RegisterDisposablePropertyChangedCallback((i, s, e) => OnPropertyChanged(e));
}


void OnPropertyChanged(DependencyPropertyChangedEventArgs args)
private void OnPropertyChanged(DependencyPropertyChangedEventArgs args)
{
var dependencyProperty = args.Property;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public partial class UniformGridLayout : IFlowLayoutAlgorithmDelegates

public UniformGridLayout()
{
this.RegisterDisposablePropertyChangedCallback((i, s, e) => OnPropertyChanged(e));
//__RP_Marker_ClassById(RuntimeProfiler.ProfId_UniformGridLayout);
LayoutId = "UniformGridLayout";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ partial class UniformGridLayout
{
public static DependencyProperty ItemsJustificationProperty { get; } = DependencyProperty.Register(
"ItemsJustification", typeof(UniformGridLayoutItemsJustification), typeof(UniformGridLayout),
new FrameworkPropertyMetadata(UniformGridLayoutItemsJustification.Start));
new FrameworkPropertyMetadata(UniformGridLayoutItemsJustification.Start, propertyChangedCallback: (sender, args) => ((UniformGridLayout)sender).OnPropertyChanged(args)));

public UniformGridLayoutItemsJustification ItemsJustification
{
Expand All @@ -23,7 +23,7 @@ public UniformGridLayoutItemsJustification ItemsJustification

public static DependencyProperty ItemsStretchProperty { get; } = DependencyProperty.Register(
"ItemsStretch", typeof(UniformGridLayoutItemsStretch), typeof(UniformGridLayout),
new FrameworkPropertyMetadata(UniformGridLayoutItemsStretch.None));
new FrameworkPropertyMetadata(UniformGridLayoutItemsStretch.None, propertyChangedCallback: (sender, args) => ((UniformGridLayout)sender).OnPropertyChanged(args)));

public UniformGridLayoutItemsStretch ItemsStretch
{
Expand All @@ -32,7 +32,7 @@ public UniformGridLayoutItemsStretch ItemsStretch
}

public static DependencyProperty MaximumRowsOrColumnsProperty { get; } = DependencyProperty.Register(
"MaximumRowsOrColumns", typeof(int), typeof(UniformGridLayout), new FrameworkPropertyMetadata(-1));
"MaximumRowsOrColumns", typeof(int), typeof(UniformGridLayout), new FrameworkPropertyMetadata(-1, propertyChangedCallback: (sender, args) => ((UniformGridLayout)sender).OnPropertyChanged(args)));

public int MaximumRowsOrColumns
{
Expand All @@ -41,7 +41,7 @@ public int MaximumRowsOrColumns
}

public static DependencyProperty MinColumnSpacingProperty { get; } = DependencyProperty.Register(
"MinColumnSpacing", typeof(double), typeof(UniformGridLayout), new FrameworkPropertyMetadata(0d));
"MinColumnSpacing", typeof(double), typeof(UniformGridLayout), new FrameworkPropertyMetadata(0d, propertyChangedCallback: (sender, args) => ((UniformGridLayout)sender).OnPropertyChanged(args)));

public double MinColumnSpacing
{
Expand All @@ -50,7 +50,7 @@ public double MinColumnSpacing
}

public static DependencyProperty MinItemHeightProperty { get; } = DependencyProperty.Register(
"MinItemHeight", typeof(double), typeof(UniformGridLayout), new FrameworkPropertyMetadata(0d));
"MinItemHeight", typeof(double), typeof(UniformGridLayout), new FrameworkPropertyMetadata(0d, propertyChangedCallback: (sender, args) => ((UniformGridLayout)sender).OnPropertyChanged(args)));

public double MinItemHeight
{
Expand All @@ -59,7 +59,7 @@ public double MinItemHeight
}

public static DependencyProperty MinItemWidthProperty { get; } = DependencyProperty.Register(
"MinItemWidth", typeof(double), typeof(UniformGridLayout), new FrameworkPropertyMetadata(0d));
"MinItemWidth", typeof(double), typeof(UniformGridLayout), new FrameworkPropertyMetadata(0d, propertyChangedCallback: (sender, args) => ((UniformGridLayout)sender).OnPropertyChanged(args)));

public double MinItemWidth
{
Expand All @@ -68,7 +68,7 @@ public double MinItemWidth
}

public static DependencyProperty MinRowSpacingProperty { get; } = DependencyProperty.Register(
"MinRowSpacing", typeof(double), typeof(UniformGridLayout), new FrameworkPropertyMetadata(0d));
"MinRowSpacing", typeof(double), typeof(UniformGridLayout), new FrameworkPropertyMetadata(0d, propertyChangedCallback: (sender, args) => ((UniformGridLayout)sender).OnPropertyChanged(args)));

public double MinRowSpacing
{
Expand All @@ -78,7 +78,7 @@ public double MinRowSpacing

public static DependencyProperty OrientationProperty { get; } = DependencyProperty.Register(
"Orientation", typeof(Orientation), typeof(UniformGridLayout),
new FrameworkPropertyMetadata(Orientation.Horizontal));
new FrameworkPropertyMetadata(Orientation.Horizontal, propertyChangedCallback: (sender, args) => ((UniformGridLayout)sender).OnPropertyChanged(args)));

public Orientation Orientation
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ public int ItemHeight
DependencyProperty.Register(
nameof(Items), typeof(IList<object>),
typeof(Primitives.LoopingSelector),
new FrameworkPropertyMetadata(default(IList<object>)));
new FrameworkPropertyMetadata(default(IList<object>), propertyChangedCallback: (sender, args) => ((LoopingSelector)sender).OnPropertyChanged(args)));


public static DependencyProperty SelectedIndexProperty { get; } =
DependencyProperty.Register(
nameof(SelectedIndex), typeof(int),
typeof(Primitives.LoopingSelector),
new FrameworkPropertyMetadata(default(int)));
new FrameworkPropertyMetadata(default(int), propertyChangedCallback: (sender, args) => ((LoopingSelector)sender).OnPropertyChanged(args)));


public static DependencyProperty SelectedItemProperty { get; } =
Expand All @@ -101,6 +101,6 @@ public int ItemHeight
DependencyProperty.Register(
nameof(ShouldLoop), typeof(bool),
typeof(Primitives.LoopingSelector),
new FrameworkPropertyMetadata(default(bool)));
new FrameworkPropertyMetadata(default(bool), propertyChangedCallback: (sender, args) => ((LoopingSelector)sender).OnPropertyChanged(args)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ partial class LoopingSelector

internal LoopingSelector()
{
this.RegisterDisposablePropertyChangedCallback((i, s, e) => OnPropertyChanged(e));

_hasFocus = false;
_isSized = false;
_isSetupPending = true;
Expand Down
12 changes: 2 additions & 10 deletions src/Uno.UI/UI/Xaml/DependencyObjectStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1277,17 +1277,9 @@ private void CleanupInheritedProperties()
return (localProperty, propertyDetails);
}
}

// Then look for attached inheritable properties, only if a property details
// has been initialized. This avoids creating the details if the property has
// not been attached on a child, or if there's no property changed callback
// attached to a child.
else if (
property.IsAttached
&& _properties.FindPropertyDetails(property) is DependencyPropertyDetails attachedDetails
&& attachedDetails.HasInherits)
else if (property.IsAttached && property.IsInherited)
{
return (property, attachedDetails);
return (property, _properties.GetPropertyDetails(property));
}
}

Expand Down
18 changes: 17 additions & 1 deletion src/Uno.UI/UI/Xaml/DependencyProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ private DependencyProperty(string name, Type propertyType, Type ownerType, Prope
_flags |= attached ? Flags.IsAttached : Flags.None;
_flags |= typeof(DependencyObjectCollection).IsAssignableFrom(propertyType) ? Flags.IsDependencyObjectCollection : Flags.None;
_flags |= GetIsTypeNullable(propertyType) ? Flags.IsTypeNullable : Flags.None;
_flags |= (defaultMetadata as FrameworkPropertyMetadata)?.Options.HasWeakStorage() is true ? Flags.HasWeakStorage : Flags.None;
if (defaultMetadata is FrameworkPropertyMetadata frameworkMetadata)
{
_flags |= frameworkMetadata.Options.HasWeakStorage() ? Flags.HasWeakStorage : Flags.None;
_flags |= frameworkMetadata.Options.HasInherits() ? Flags.IsInherited : Flags.None;
}

_flags |= ownerType.Assembly.Equals(typeof(DependencyProperty).Assembly) ? Flags.IsUnoType : Flags.None;

if (ownerType == typeof(FrameworkElement))
Expand Down Expand Up @@ -336,6 +341,12 @@ internal string Name
internal bool IsAttached
=> (_flags & Flags.IsAttached) != 0;

/// <summary>
/// Determines if the property is an inherited property
/// </summary>
internal bool IsInherited
=> (_flags & Flags.IsInherited) != 0;

/// <summary>
/// Determines if the owner type is declared by Uno.UI
/// </summary>
Expand Down Expand Up @@ -555,6 +566,11 @@ private enum Flags
IsUnoType = (1 << 4),

ValidateNotNegativeAndNotNaN = (1 << 5),

/// <summary>
/// Set when the property is an inherited property
/// </summary>
IsInherited = (1 << 6),
}
}
}

0 comments on commit addc6b4

Please sign in to comment.