Skip to content

Commit

Permalink
chore: Cleanup DependencyPropertyValuePrecedences
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Feb 28, 2024
1 parent f208d4e commit 8961524
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void ReadSetValue_Precedence()
Binder_GeneratedAttached_Attached.SetMyValue(SUT, 42);
Assert.AreEqual(42, Binder_GeneratedAttached_Attached.GetMyValue(SUT));

SUT.SetValue(Binder_GeneratedAttached_Attached.MyValueProperty, 43, DependencyPropertyValuePrecedences.ImplicitStyle);
SUT.SetValue(Binder_GeneratedAttached_Attached.MyValueProperty, 43, DependencyPropertyValuePrecedences.DefaultStyle);
Assert.AreEqual(42, Binder_GeneratedAttached_Attached.GetMyValue(SUT));
}

Expand All @@ -65,7 +65,7 @@ public void ReadSetValue2_Precedence()
Assert.AreEqual(42, Binder_GeneratedAttached_Attached.GetMyValue2(SUT));
Assert.AreEqual(1, SUT.Value2ChangedCallback);

SUT.SetValue(Binder_GeneratedAttached_Attached.MyValue2Property, 43, DependencyPropertyValuePrecedences.ImplicitStyle);
SUT.SetValue(Binder_GeneratedAttached_Attached.MyValue2Property, 43, DependencyPropertyValuePrecedences.DefaultStyle);
Assert.AreEqual(42, Binder_GeneratedAttached_Attached.GetMyValue2(SUT));
Assert.AreEqual(1, SUT.Value2ChangedCallback);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI.Tests/BinderTests/Given_Binder.LocalCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void When_Update_Lower_Precedence()
SUT.SetValue(BinderLocalCache_Data.MyValueProperty, 42);
Assert.AreEqual(42, SUT.MyValue);

SUT.SetValue(BinderLocalCache_Data.MyValueProperty, 43, DependencyPropertyValuePrecedences.ImplicitStyle);
SUT.SetValue(BinderLocalCache_Data.MyValueProperty, 43, DependencyPropertyValuePrecedences.DefaultStyle);
Assert.AreEqual(42, SUT.MyValue);
}

Expand Down
20 changes: 10 additions & 10 deletions src/Uno.UI.Tests/DependencyProperty/Given_DependencyProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public void When_Two_Precedences_Set_Then_Only_Highest_Returned()
var testProperty = DependencyProperty.Register(nameof(When_Two_Precedences_Set_Then_Only_Highest_Returned), typeof(string), typeof(MockDependencyObject), new PropertyMetadata("42"));

SUT.SetValue(testProperty, "Not42");
SUT.SetValue(testProperty, "ALowPriorityValue", DependencyPropertyValuePrecedences.ImplicitStyle);
SUT.SetValue(testProperty, "ALowPriorityValue", DependencyPropertyValuePrecedences.DefaultStyle);

Assert.AreEqual("Not42", SUT.GetValue(testProperty));
}
Expand Down Expand Up @@ -1622,7 +1622,7 @@ public void When_Set_Within_Style_Application()
(o, e) =>
{
e.BypassesPropagation.Should().BeFalse();
e.NewPrecedence.Should().Be(DependencyPropertyValuePrecedences.ImplicitStyle);
e.NewPrecedence.Should().Be(DependencyPropertyValuePrecedences.DefaultStyle);
button.Content = "Frogurt";
});

Expand Down Expand Up @@ -1675,7 +1675,7 @@ public void When_Set_Style_Property_PrecedenceInContext()
(dependencyObject, args) =>
{
args.NewPrecedence.Should().Be(DependencyPropertyValuePrecedences.Local);
args.NewValue.Should().Be(DependencyPropertyValuePrecedences.ExplicitStyle);
args.NewValue.Should().Be(DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle);
});

using var _ = new AssertionScope();
Expand All @@ -1693,7 +1693,7 @@ public void When_Set_Style_Property_PrecedenceInContext()

// Local value is cleared, fallback to style value
sut.BorderThickness.Should().Be(new Thickness(10d), "After removing local value");
sut.Tag.Should().Be(DependencyPropertyValuePrecedences.ExplicitStyle, "After applying style");
sut.Tag.Should().Be(DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle, "After applying style");

registration2.Dispose();

Expand Down Expand Up @@ -1745,7 +1745,7 @@ public void When_Set_Style_Property_PrecedenceInContext_No_Local()
(dependencyObject, args) =>
{
args.NewPrecedence.Should().Be(DependencyPropertyValuePrecedences.Local);
args.NewValue.Should().Be(DependencyPropertyValuePrecedences.ExplicitStyle);
args.NewValue.Should().Be(DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle);
});

using var _ = new AssertionScope();
Expand Down Expand Up @@ -1775,7 +1775,7 @@ public void When_Set_Style_Property_FromCallback_PrecedenceInContext_No_Local()
Border.BorderThicknessProperty,
(dependencyObject, args) =>
{
args.NewPrecedence.Should().Be(DependencyPropertyValuePrecedences.ExplicitStyle);
args.NewPrecedence.Should().Be(DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle);
args.NewValue.Should().Be(new Thickness(10d));
sut.Child = new Rectangle();
});
Expand Down Expand Up @@ -1874,12 +1874,12 @@ public void When_Set_With_Both_Style_And_LocalValue()
#if !NETFX_CORE // this part of the test not possible on UWP - extensive check for Uno
sut.GetValueForEachPrecedences(MyDependencyObject.PropAProperty).Select(v => v.value)
.Should().HaveElementAt((int)DependencyPropertyValuePrecedences.DefaultValue, null)
.And.HaveElementAt((int)DependencyPropertyValuePrecedences.ExplicitStyle, "StyleValue")
.And.HaveElementAt((int)DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle, "StyleValue")
.And.HaveElementAt((int)DependencyPropertyValuePrecedences.Local, "LocalValue");

sut.GetValueForEachPrecedences(MyDependencyObject.PropBProperty).Select(v => v.value)
.Should().HaveElementAt((int)DependencyPropertyValuePrecedences.DefaultValue, null)
.And.HaveElementAt((int)DependencyPropertyValuePrecedences.ExplicitStyle, "StyleValueForB")
.And.HaveElementAt((int)DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle, "StyleValueForB")
.And.HaveElementAt((int)DependencyPropertyValuePrecedences.Local, UnsetValue.Instance);
#endif

Expand All @@ -1904,12 +1904,12 @@ public void When_Set_With_Both_Style_And_LocalValue()
#if !NETFX_CORE // this part of the test not possible on UWP - extensive check for Uno
sut.GetValueForEachPrecedences(MyDependencyObject.PropAProperty).Select(v => v.value)
.Should().HaveElementAt((int)DependencyPropertyValuePrecedences.DefaultValue, null)
.And.HaveElementAt((int)DependencyPropertyValuePrecedences.ExplicitStyle, UnsetValue.Instance)
.And.HaveElementAt((int)DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle, UnsetValue.Instance)
.And.HaveElementAt((int)DependencyPropertyValuePrecedences.Local, UnsetValue.Instance);

sut.GetValueForEachPrecedences(MyDependencyObject.PropBProperty).Select(v => v.value)
.Should().HaveElementAt((int)DependencyPropertyValuePrecedences.DefaultValue, null)
.And.HaveElementAt((int)DependencyPropertyValuePrecedences.ExplicitStyle, UnsetValue.Instance)
.And.HaveElementAt((int)DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle, UnsetValue.Instance)
.And.HaveElementAt((int)DependencyPropertyValuePrecedences.Local, null); // because of the callback ;-)
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/Slider/Slider.mux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ protected override void OnApplyTemplate()
// TODO Uno specific: Set background of slider container to ensure touch events are captured, but allow it to be overwritten by bindings etc
if (_tpSliderContainer.Background == null)
{
_tpSliderContainer.SetValue(BackgroundProperty, SolidColorBrushHelper.Transparent, DependencyPropertyValuePrecedences.ImplicitStyle);
_tpSliderContainer.SetValue(BackgroundProperty, SolidColorBrushHelper.Transparent, DependencyPropertyValuePrecedences.DefaultStyle);
}

// Attach the event handlers
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UI/UI/Xaml/DependencyObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,9 @@ internal static DependencyPropertyValuePrecedences GetBaseValueSource(this Depen
{
var precedence = dependencyObject.GetCurrentHighestValuePrecedence(property);
// TODO: Bring this closer to CFrameworkElement::IsPropertySetByStyle from WinUI.
if (precedence is DependencyPropertyValuePrecedences.DefaultStyle or DependencyPropertyValuePrecedences.ImplicitStyle or DependencyPropertyValuePrecedences.ExplicitStyle)
if (precedence is DependencyPropertyValuePrecedences.DefaultStyle or DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle)
{
return DependencyPropertyValuePrecedences.ExplicitStyle;
return DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle;
}
else if (precedence == DependencyPropertyValuePrecedences.DefaultValue)
{
Expand Down
14 changes: 4 additions & 10 deletions src/Uno.UI/UI/Xaml/DependencyPropertyValuePrecedences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,20 @@ public enum DependencyPropertyValuePrecedences : int
TemplatedParent,

/// <summary>
/// Defined when setting a style from the style property
/// Defined when setting a style from the style property or resolving an implicit style
/// </summary>
ExplicitStyle,
ExplicitOrImplicitStyle,

/// <summary>
/// Values defined by an implicitly defined style
/// Values defined by a default style
/// </summary>
/// <remarks>On Uno, this is actually used for values set by the default style, to allow for them to correctly take precedence over inherited values.</remarks>
ImplicitStyle,
DefaultStyle,

/// <summary>
/// Defined by the inheritance of a FrameworkElement property of the same name
/// </summary>
Inheritance,

/// <summary>
/// Defined by the default style from Generic.xaml
/// </summary>
DefaultStyle,

/// <summary>
/// Defined on the dependency property metadata
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Documents/TextElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ void SetDefaultForeground(DependencyProperty foregroundProperty)
{
// Hyperlink doesn't appear to inherit foreground from the parent.
// So, we set this with ImplicitStyle precedence which is a higher precedence than Inheritance.
this.SetValue(foregroundProperty, DefaultTextForegroundBrush, DependencyPropertyValuePrecedences.ImplicitStyle);
this.SetValue(foregroundProperty, DefaultTextForegroundBrush, DependencyPropertyValuePrecedences.DefaultStyle);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UI/UI/Xaml/FrameworkElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ private void ApplyStyle()
{
var oldActiveStyle = _activeStyle;
UpdateActiveStyle();
OnStyleChanged(oldActiveStyle, _activeStyle, DependencyPropertyValuePrecedences.ExplicitStyle);
OnStyleChanged(oldActiveStyle, _activeStyle, DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle);
}

/// <summary>
Expand Down Expand Up @@ -678,7 +678,7 @@ private protected void ApplyDefaultStyle()

// Although this is the default style, we use the ImplicitStyle enum value (which is otherwise unused) to ensure that it takes precedence
//over inherited property values. UWP's precedence system is simpler than WPF's, from which the enum is derived.
OnStyleChanged(null, style, DependencyPropertyValuePrecedences.ImplicitStyle);
OnStyleChanged(null, style, DependencyPropertyValuePrecedences.DefaultStyle);
#if DEBUG
AppliedDefaultStyle = style;
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ namespace <#= mixin.NamespaceName #>

protected virtual void OnStyleChanged(Style oldValue, Style newValue)
{
OnStyleChanged(oldValue, newValue, DependencyPropertyValuePrecedences.ExplicitStyle);
OnStyleChanged(oldValue, newValue, DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle);
}

private void OnStyleChanged(Style oldStyle, Style newStyle, DependencyPropertyValuePrecedences precedence)
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/IFrameworkElementImplementation.iOS.tt
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ namespace <#= mixin.NamespaceName #>

protected virtual void OnStyleChanged(Style oldValue, Style newValue)
{
OnStyleChanged(oldValue, newValue, DependencyPropertyValuePrecedences.ExplicitStyle);
OnStyleChanged(oldValue, newValue, DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle);
}

private void OnStyleChanged(Style oldStyle, Style newStyle, DependencyPropertyValuePrecedences precedence)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ namespace <#= mixin.NamespaceName #>

protected virtual void OnStyleChanged(Style oldValue, Style newValue)
{
OnStyleChanged(oldValue, newValue, DependencyPropertyValuePrecedences.ExplicitStyle);
OnStyleChanged(oldValue, newValue, DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle);
}

private void OnStyleChanged(Style oldStyle, Style newStyle, DependencyPropertyValuePrecedences precedence)
Expand Down
6 changes: 3 additions & 3 deletions src/Uno.UI/UI/Xaml/Style/Style.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private bool TryGetAdjustedSetter(DependencyPropertyValuePrecedences baseValueSo
// In DependencyObject::EvaluateBaseValue (DependencyObject.cpp file), the value is updated to that returned from GetValueFromStyle
// Then, baseValueSource is updated from BaseValueSourceBuiltInStyle to BaseValueSourceStyle
// The OverrideLocalPrecedence call below is the equivalent of the baseValueSource update.
if (baseValueSource == DependencyPropertyValuePrecedences.ImplicitStyle &&
if (baseValueSource == DependencyPropertyValuePrecedences.DefaultStyle &&
dependencyObject is FrameworkElement fe &&
fe.GetActiveStyle() is { } activeStyle &&
// Make sure to only consider active style if it was explicit.
Expand All @@ -121,7 +121,7 @@ internal void ApplyTo(DependencyObject o, DependencyPropertyValuePrecedences pre
return;
}

Debug.Assert(precedence is DependencyPropertyValuePrecedences.ImplicitStyle or DependencyPropertyValuePrecedences.ExplicitStyle);
Debug.Assert(precedence is DependencyPropertyValuePrecedences.DefaultStyle or DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle);

IDisposable? localPrecedenceDisposable = null;

Expand All @@ -140,7 +140,7 @@ internal void ApplyTo(DependencyObject o, DependencyPropertyValuePrecedences pre
{
if (TryGetAdjustedSetter(precedence, o, _flattenedSetters[i], out var adjustedSetter))
{
using (o.OverrideLocalPrecedence(DependencyPropertyValuePrecedences.ExplicitStyle))
using (o.OverrideLocalPrecedence(DependencyPropertyValuePrecedences.ExplicitOrImplicitStyle))
{
adjustedSetter.ApplyTo(o);
}
Expand Down

0 comments on commit 8961524

Please sign in to comment.