Skip to content

[Accessibility] Add Accessibility Traits to CollectionView items on iOS/Catalyst #27971

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

Merged
merged 8 commits into from
Mar 17, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Foundation;
using ObjCRuntime;
using UIKit;
using Microsoft.Maui.Controls.Platform;

namespace Microsoft.Maui.Controls.Handlers.Items
{
Expand Down Expand Up @@ -149,6 +150,7 @@ internal void UpdateSelectionMode()
}

UpdatePlatformSelection();
CollectionView.UpdateAccessibilityTraits(ItemsView);
}

void SynchronizePlatformSelectionWithSelectedItems()
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/src/Core/Handlers/Items/iOS/TemplatedCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Graphics;
using UIKit;
using Microsoft.Maui.Controls.Platform;

namespace Microsoft.Maui.Controls.Handlers.Items
{
Expand Down Expand Up @@ -202,6 +203,7 @@ public void Bind(DataTemplate template, object bindingContext, ItemsView itemsVi
}

CurrentTemplate = itemTemplate;
this.UpdateAccessibilityTraits(itemsView);
}

void SetRenderer(IPlatformViewHandler renderer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Foundation;
using ObjCRuntime;
using UIKit;
using Microsoft.Maui.Controls.Platform;

namespace Microsoft.Maui.Controls.Handlers.Items2
{
Expand Down Expand Up @@ -149,6 +150,7 @@ internal void UpdateSelectionMode()
}

UpdatePlatformSelection();
CollectionView.UpdateAccessibilityTraits(ItemsView);
}

void SynchronizePlatformSelectionWithSelectedItems()
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/src/Core/Handlers/Items2/iOS/TemplatedCell2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ void BindVirtualView(View virtualView, object bindingContext, ItemsView itemsVie
{
view.SetValueFromRenderer(BindableObject.BindingContextProperty, bindingContext);
}

this.UpdateAccessibilityTraits(itemsView);
}

bool IsUsingVSMForSelectionColor(View view)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using UIKit;

namespace Microsoft.Maui.Controls.Platform;

internal static class AcessibilityExtensions
{
internal static void UpdateAccessibilityTraits(this UICollectionView collectionView, SelectableItemsView itemsView)
{
foreach (var subview in collectionView.Subviews)
{
if (subview is UICollectionViewCell cell)
{
cell.UpdateAccessibilityTraits(itemsView);
}
}
}

internal static void UpdateAccessibilityTraits(this UICollectionViewCell cell, ItemsView itemsView)
{
var selectionMode = (itemsView as CollectionView)?.SelectionMode;
if (cell.ContentView is not null
&& cell.ContentView.Subviews.Length > 0
&& selectionMode is not null)
{
var firstChild = cell.ContentView.Subviews[0];

// if the first child is a control, changing the accessibility traits from an entry to a button could be confusing.
if (firstChild is UIControl)
{
return;
}

if (selectionMode != SelectionMode.None)
{
firstChild.AccessibilityTraits |= UIAccessibilityTrait.Button;
}
else
{
firstChild.AccessibilityTraits &= ~UIAccessibilityTrait.Button;
}
}
}
}
72 changes: 72 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue26817.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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:local="clr-namespace:Maui.Controls.Sample"
x:Class="Maui.Controls.Sample.Issues.Issue26817">
<VerticalStackLayout>
<Label Text="Border" Padding="0,10,10,10"/>
<local:CollectionView2 SelectionMode="Single" x:Name="border">
<CollectionView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Item 1</x:String>
<x:String>Item 2</x:String>
</x:Array>
</CollectionView.ItemsSource>
<CollectionView.ItemTemplate>
<DataTemplate>
<Border BackgroundColor="LightGray" Padding="10" Margin="5" SemanticProperties.Description="{Binding}">
<VerticalStackLayout>
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center"/>
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" />
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center"/>
</VerticalStackLayout>
</Border>
</DataTemplate>
</CollectionView.ItemTemplate>
</local:CollectionView2>

<Label Text="VerticalStackLayout" Padding="0,10,10,10"/>
<local:CollectionView2 SelectionMode="Single" x:Name="vsl">
<CollectionView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Item 1</x:String>
<x:String>Item 2</x:String>
</x:Array>
</CollectionView.ItemsSource>
<CollectionView.ItemTemplate>
<DataTemplate>
<VerticalStackLayout BackgroundColor="LightBlue" Padding="10" Margin="5" SemanticProperties.Description="{Binding}">
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center"/>
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" />
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center"/>
</VerticalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</local:CollectionView2>

<Label Text="Grid" Padding="0,10,10,10"/>
<local:CollectionView2 SelectionMode="Single" x:Name="grid">
<CollectionView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Item 1</x:String>
<x:String>Item 2</x:String>
</x:Array>
</CollectionView.ItemsSource>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid BackgroundColor="LightGreen" Padding="10" Margin="5" SemanticProperties.Description="{Binding}" RowDefinitions="*,*,*">
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" Grid.Row="0"/>
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" Grid.Row="1"/>
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" Grid.Row="2"/>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</local:CollectionView2>

<Button Text="Toggle SelectionMode" Clicked="Button_Clicked" AutomationId="ToggleSelectionModeButton" />
<Label x:Name="selectionLabel" />
<Label Text="Border Item is Button?" x:Name="Label1" />
<Label Text="VerticalStackLayout Item is Button?" x:Name="Label2" />
<Label Text="Grid Item is Button?" x:Name="Label3" />
</VerticalStackLayout>
</ContentPage>
56 changes: 56 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue26817.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 26817, "CollectionViewHandler2 assigns Accessibility Traits with SelectionMode correctly", PlatformAffected.macOS)]
public partial class Issue26817 : ContentPage
{
public Issue26817()
{
InitializeComponent();
}

int toggleCount;

private void Button_Clicked(object sender, EventArgs e)
{
var selectionMode = toggleCount switch
{
0 => SelectionMode.None,
1 => SelectionMode.Single,
2 => SelectionMode.Multiple,
_ => throw new NotImplementedException(),
};

border.SelectionMode = selectionMode;
vsl.SelectionMode = selectionMode;
grid.SelectionMode = selectionMode;
CheckAccessibilityTraits(border, Label1, "Border:");
CheckAccessibilityTraits(vsl, Label2, "VSL:");
CheckAccessibilityTraits(grid, Label3, "Grid:");

selectionLabel.Text = "selectionMode: " + selectionMode;

toggleCount = (toggleCount + 1) % 3;
}

public void CheckAccessibilityTraits(CollectionView collectionView, Label label, string prefix)
{
#if IOS || MACCATALYST
if (collectionView.Handler?.PlatformView is UIKit.UIView collectionViewWrapper)
{
if (collectionViewWrapper.Subviews.Length == 0 || collectionViewWrapper.Subviews[0] is not UIKit.UIView cv)
return;

foreach (var child in cv.Subviews)
{
if (child is UIKit.UICollectionViewCell cell && cell.ContentView.Subviews.FirstOrDefault() is UIKit.UIView firstChild)
{
bool isButton = (firstChild.AccessibilityTraits & UIKit.UIAccessibilityTrait.Button) == UIKit.UIAccessibilityTrait.Button;
label.Text = $"{prefix} Item is Button - {isButton}";
break;
}
}
}
#endif
}
}
}
72 changes: 72 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue26817_2.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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:local="clr-namespace:Maui.Controls.Sample"
x:Class="Maui.Controls.Sample.Issues.Issue26817_2">
<VerticalStackLayout>
<Label Text="Border" Padding="0,10,10,10"/>
<CollectionView SelectionMode="Single" x:Name="border">
<CollectionView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Item 1</x:String>
<x:String>Item 2</x:String>
</x:Array>
</CollectionView.ItemsSource>
<CollectionView.ItemTemplate>
<DataTemplate>
<Border BackgroundColor="LightGray" Padding="10" Margin="5" SemanticProperties.Description="{Binding}">
<VerticalStackLayout>
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center"/>
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" />
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center"/>
</VerticalStackLayout>
</Border>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

<Label Text="VerticalStackLayout" Padding="0,10,10,10"/>
<CollectionView SelectionMode="Single" x:Name="vsl">
<CollectionView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Item 1</x:String>
<x:String>Item 2</x:String>
</x:Array>
</CollectionView.ItemsSource>
<CollectionView.ItemTemplate>
<DataTemplate>
<VerticalStackLayout BackgroundColor="LightBlue" Padding="10" Margin="5" SemanticProperties.Description="{Binding}">
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center"/>
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" />
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center"/>
</VerticalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

<Label Text="Grid" Padding="0,10,10,10"/>
<CollectionView SelectionMode="Single" x:Name="grid">
<CollectionView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Item 1</x:String>
<x:String>Item 2</x:String>
</x:Array>
</CollectionView.ItemsSource>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid BackgroundColor="LightGreen" Padding="10" Margin="5" SemanticProperties.Description="{Binding}" RowDefinitions="*,*,*">
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" Grid.Row="0"/>
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" Grid.Row="1"/>
<Label Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" Grid.Row="2"/>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

<Button Text="Toggle SelectionMode" Clicked="Button_Clicked" AutomationId="ToggleSelectionModeButton" />
<Label x:Name="selectionLabel" />
<Label Text="Border Item is Button?" x:Name="Label1" />
<Label Text="VerticalStackLayout Item is Button?" x:Name="Label2" />
<Label Text="Grid Item is Button?" x:Name="Label3" />
</VerticalStackLayout>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 26817_2, "CollectionViewHandler assigns Accessibility Traits with SelectionMode correctly", PlatformAffected.macOS)]
public partial class Issue26817_2 : ContentPage
{
public Issue26817_2()
{
InitializeComponent();
}

int toggleCount;

private void Button_Clicked(object sender, EventArgs e)
{
var selectionMode = toggleCount switch
{
0 => SelectionMode.None,
1 => SelectionMode.Single,
2 => SelectionMode.Multiple,
_ => throw new NotImplementedException(),
};

border.SelectionMode = selectionMode;
vsl.SelectionMode = selectionMode;
grid.SelectionMode = selectionMode;
CheckAccessibilityTraits(border, Label1, "Border:");
CheckAccessibilityTraits(vsl, Label2, "VSL:");
CheckAccessibilityTraits(grid, Label3, "Grid:");

selectionLabel.Text = "selectionMode: " + selectionMode;

toggleCount = (toggleCount + 1) % 3;
}

public void CheckAccessibilityTraits(CollectionView collectionView, Label label, string prefix)
{
#if IOS || MACCATALYST
if (collectionView.Handler?.PlatformView is UIKit.UIView collectionViewWrapper)
{
if (collectionViewWrapper.Subviews.Length == 0 || collectionViewWrapper.Subviews[0] is not UIKit.UIView cv)
return;

foreach (var child in cv.Subviews)
{
if (child is UIKit.UICollectionViewCell cell && cell.ContentView.Subviews.FirstOrDefault() is UIKit.UIView firstChild)
{
bool isButton = (firstChild.AccessibilityTraits & UIKit.UIAccessibilityTrait.Button) == UIKit.UIAccessibilityTrait.Button;
label.Text = $"{prefix} Item is Button - {isButton}";
break;
}
}
}
#endif
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,31 @@
#if MACCATALYST || IOS
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue26817 : _IssuesUITest
{
public Issue26817(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "CollectionViewHandler2 assigns Accessibility Traits with SelectionMode correctly";

[Test]
[Category(UITestCategories.CollectionView)]
public void AccessibilityTraitsSetCorrectly()
{
App.WaitForElement("ToggleSelectionModeButton").Click();
VerifyScreenshot(TestContext.CurrentContext.Test.MethodName + "None");
App.WaitForElement("ToggleSelectionModeButton").Click();
VerifyScreenshot(TestContext.CurrentContext.Test.MethodName + "Single");
App.WaitForElement("ToggleSelectionModeButton").Click();
VerifyScreenshot(TestContext.CurrentContext.Test.MethodName + "Multiple");
App.WaitForElement("ToggleSelectionModeButton").Click();
VerifyScreenshot(TestContext.CurrentContext.Test.MethodName + "None");
}
}
}
#endif
Loading
Loading