Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C] fix Specificity for VSM #16404

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Controls/src/Core/SetterSpecificity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public SetterSpecificity(int style, int id, int @class, int type) : this(0, 0, 0

public int CompareTo(SetterSpecificity other)
{
//VSM setters somehow supersedes Styles
if (Vsm != other.Vsm)
return Vsm.CompareTo(other.Vsm);

//everything coming from Style has lower priority than something that does not
if (Style != other.Style && Style == 0)
return 1;
Expand All @@ -74,8 +78,6 @@ public int CompareTo(SetterSpecificity other)
if (Style != other.Style)
return Style.CompareTo(other.Style);

if (Vsm != other.Vsm)
return Vsm.CompareTo(other.Vsm);
if (Manual != other.Manual)
return Manual.CompareTo(other.Manual);
if (DynamicResource != other.DynamicResource)
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/src/Core/VisualStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public static bool GoToState(VisualElement visualElement, string name)
var groups = (VisualStateGroupList)visualElement.GetValue(VisualStateGroupsProperty);
var context = visualElement.GetContext(VisualStateGroupsProperty);
var vsgSpecificity = context.Values.Keys.Last();
if (vsgSpecificity == SetterSpecificity.DefaultValue)
vsgSpecificity = new SetterSpecificity();
groups.Specificity = vsgSpecificity;
var specificity = new SetterSpecificity(1, 0, 0, 0, vsgSpecificity.Style, vsgSpecificity.Id, vsgSpecificity.Class, vsgSpecificity.Type);

Expand Down
36 changes: 1 addition & 35 deletions src/Controls/tests/Core.UnitTests/VisualStateManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,40 +490,6 @@ public void UnapplyingVSMShouldUnapplySetters()
Assert.False(label.TextColor == Colors.HotPink); //setter should be unapplied
}

[Fact]
//https://github.com/dotnet/maui/issues/6856
public void VSMInStyleShouldHaveStylePriority()
{
var label = new Label { TextColor = Colors.HotPink };//Setting the color manually should prevents style override
var SelectedStateName = "Selected";

Assert.Equal(label.TextColor, Colors.HotPink);

label.Style = new Style(typeof(Label))
{
Setters = {
new Setter { Property = Label.TextColorProperty, Value = Colors.AliceBlue },
new Setter {
Property = VisualStateManager.VisualStateGroupsProperty,
Value = new VisualStateGroupList {
new VisualStateGroup {
States = {
new VisualState {
Name = SelectedStateName,
Setters = { new Setter { Property = Label.TextColorProperty, Value=Colors.OrangeRed} }
},
}
}
}
},
}
};

Assert.Equal(label.TextColor, Colors.HotPink); //textcolor from Style isn't applied
VisualStateManager.GoToState(label, SelectedStateName);
Assert.Equal(label.TextColor, Colors.HotPink); //textcolor Style's VSM isn't applied
}

[Theory(Skip = "This test was created to check performance characteristics; leaving it in because it may be useful again.")]
[InlineData(1, 10)]
[InlineData(1, 10000)]
Expand Down Expand Up @@ -569,4 +535,4 @@ public void ValidatePerformance(int groups, int states)

}
}
}
}
45 changes: 45 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui11204.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://schemas.microsoft.com/dotnet/2021/maui/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui11204">

<ContentPage.Resources>
<Style TargetType="Border" Class="BorderStyle"
ApplyToDerivedTypes="true">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="State1">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Blue" />
<Setter Property="StrokeThickness" Value="2" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ContentPage.Resources>
<StackLayout>
<Border x:Name="border"
BackgroundColor="FloralWhite">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="State1">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Blue" />
<Setter Property="StrokeThickness" Value="2" />
</VisualState.Setters>
</VisualState>

</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Border>
<Border x:Name="borderWithStyleClass"
BackgroundColor="HotPink"
StyleClass="BorderStyle"/>
</StackLayout>
</ContentPage>
47 changes: 47 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui11204.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Devices;
using Microsoft.Maui.Platform;
using NUnit.Framework;


namespace Microsoft.Maui.Controls.Xaml.UnitTests
{
public partial class Maui11204 : ContentPage
{
public Maui11204() => InitializeComponent();
public Maui11204(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
class Tests
{
[SetUp] public void Setup() => AppInfo.SetCurrent(new MockAppInfo());
[TearDown] public void TearDown() => AppInfo.SetCurrent(null);

[Test]
public void VSMSetterOverrideManualValues([Values(false, true)] bool useCompiledXaml)
{
var page = new Maui11204(useCompiledXaml);
Assert.AreEqual(Colors.FloralWhite, page.border.BackgroundColor);
VisualStateManager.GoToState(page.border, "State1");
Assert.AreEqual(2, page.border.StrokeThickness);
Assert.AreEqual(Colors.Blue, page.border.BackgroundColor);
}

[Test]
public void StyleVSMSetterOverrideManualValues([Values(false, true)] bool useCompiledXaml)
{
var page = new Maui11204(useCompiledXaml);
Assert.AreEqual(Colors.HotPink, page.borderWithStyleClass.BackgroundColor);
VisualStateManager.GoToState(page.borderWithStyleClass, "State1");
Assert.AreEqual(2, page.borderWithStyleClass.StrokeThickness);
Assert.AreEqual(Colors.Blue, page.borderWithStyleClass.BackgroundColor);
}
}
}
}
Loading