Skip to content

Commit

Permalink
[C] fix Specificity for VSM
Browse files Browse the repository at this point in the history
- fixes #11224
  • Loading branch information
StephaneDelcroix committed Jul 27, 2023
1 parent 0047ca2 commit 7abf7dc
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
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
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);
}
}
}
}

0 comments on commit 7abf7dc

Please sign in to comment.