Skip to content
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
10 changes: 5 additions & 5 deletions src/Controls/src/Core/VisualElement/VisualElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1579,13 +1579,13 @@ protected internal virtual void ChangeVisualState()
{
VisualStateManager.GoToState(this, VisualStateManager.CommonStates.Disabled);
}
else if (IsPointerOver)
{
VisualStateManager.GoToState(this, VisualStateManager.CommonStates.PointerOver);
}
else
{
VisualStateManager.GoToState(this, VisualStateManager.CommonStates.Normal);
bool isSelected = this.IsElementInSelectedState();
string targetState = isSelected ? VisualStateManager.CommonStates.Selected
: (IsPointerOver ? VisualStateManager.CommonStates.PointerOver : VisualStateManager.CommonStates.Normal);

VisualStateManager.GoToState(this, targetState);
}

if (IsEnabled)
Expand Down
14 changes: 14 additions & 0 deletions src/Controls/src/Core/VisualStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,20 @@ internal static bool HasVisualState(this VisualElement element, string name)

return false;
}

internal static bool IsElementInSelectedState(this VisualElement element)
{
var groups = VisualStateManager.GetVisualStateGroups(element);
foreach (var group in groups)
{
if (group.CurrentState?.Name == VisualStateManager.CommonStates.Selected)
{
return true;
}
}

return false;
}
}

internal class WatchAddList<T> : IList<T>
Expand Down
82 changes: 82 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue29484.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 29484, "CollectionView Selected state does not work on the selected item when combined with PointerOver", PlatformAffected.UWP | PlatformAffected.macOS)]
public class Issue29484 : TestContentPage
{
protected override void Init()
{
Style pointerOverSelectedStyle = CreatePointerOverSelectedItemStyle();
DataTemplate pointerOverSelectedItemTemplate = CreateItemTemplate(pointerOverSelectedStyle);

Grid grid = new Grid
{
HorizontalOptions = LayoutOptions.Center,
RowDefinitions =
{
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Star },
}
};

grid.Add(new Label { Text = "PointerOverAndSelectedState" }, 0, 0);
grid.Add(InitializeCollectionView(pointerOverSelectedItemTemplate), 0, 1);
Content = grid;
}

CollectionView InitializeCollectionView(DataTemplate itemTemplate)
{
return new CollectionView
{
AutomationId = "CollectionView",
ItemTemplate = itemTemplate,
SelectionMode = SelectionMode.Single,
ItemsSource = new List<string> { "Item 1", "Item 2", "Item 3", "Item 4" }
};
}

DataTemplate CreateItemTemplate(Style style)
{
return new DataTemplate(() =>
{
Label label = new Label { Style = style };
label.SetBinding(Label.TextProperty, ".");
return label;
});
}

Style CreatePointerOverSelectedItemStyle()
{
return new Style(typeof(Label))
{
Setters =
{
new Setter { Property = BackgroundColorProperty, Value = Colors.Transparent },
new Setter
{
Property = VisualStateManager.VisualStateGroupsProperty,
Value = CreateVisualState()
}
}
};
}

VisualStateGroupList CreateVisualState()
{
VisualStateGroupList groupList = new VisualStateGroupList();
VisualStateGroup commonStates = new VisualStateGroup { Name = "CommonStates" };

VisualState normalState = new VisualState { Name = "Normal" };
VisualState pointerOverState = new VisualState { Name = "PointerOver" };
VisualState selectedState = new VisualState { Name = "Selected" };

pointerOverState.Setters.Add(new Setter { Property = BackgroundColorProperty, Value = Colors.DarkTurquoise });
selectedState.Setters.Add(new Setter { Property = BackgroundColorProperty, Value = Colors.DarkBlue });

commonStates.States.Add(pointerOverState);
commonStates.States.Add(selectedState);
commonStates.States.Add(normalState);

groupList.Add(commonStates);
return groupList;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if TEST_FAILS_ON_ANDROID && TEST_FAILS_ON_IOS
// This test is disabled on mobile platforms because they do not support pointer hover states.
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue29484 : _IssuesUITest
{
public Issue29484(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "CollectionView Selected state does not work on the selected item when combined with PointerOver";

[Test]
[Category(UITestCategories.CollectionView)]
public void PointerOverWithSelectedStateShouldWork()
{
App.WaitForElement("CollectionView");
App.Tap("Item 2");
App.Tap("PointerOverAndSelectedState");
VerifyScreenshot();
}
}
#endif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading