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

[iOS] RadioButton a11y #10832

Merged
merged 10 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -43,6 +43,22 @@

</RadioButton>

<Label FontSize="Small" Text="The RadioButton below is the same as above, but also has a Semantic Description set on it for better accessibility."/>

<RadioButton GroupName="test"
SemanticProperties.Description="{Binding Source={x:Reference embeddedButton}, Path=Text}"
TextColor="Green"
TextTransform="Uppercase"
FontAttributes="Bold"
FontSize="12"
FontFamily="Arial">

<RadioButton.Content>
<Button x:Name="embeddedButton" Text="It's a button inside a button." TextColor="Green"></Button>
</RadioButton.Content>

</RadioButton>

<Label FontSize="Small" Text="A Content View which already has these properties set/bound should ignore the RadioButton properties."/>

<RadioButton GroupName="test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,21 @@ public partial class RadioButton : IRadioButton
Color IButtonStroke.StrokeColor => (Color)GetValue(BorderColorProperty);

int IButtonStroke.CornerRadius => (int)GetValue(CornerRadiusProperty);

Semantics _semantics;

Semantics IView.Semantics
{
get
{
if (ControlTemplate != null)
{
_semantics ??= new Semantics();
_semantics.Description = SemanticProperties.GetDescription(this) ?? this.ContentAsString();
}

return _semantics;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ protected override ContentView CreatePlatformView()
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} must be set to create a {nameof(ContentView)}");
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} cannot be null");

return new ContentView
{
CrossPlatformMeasure = VirtualView.CrossPlatformMeasure,
CrossPlatformArrange = VirtualView.CrossPlatformArrange
};
return new MauiRadioButton(VirtualView);
}

public override void SetVirtualView(IView view)
Expand Down
48 changes: 48 additions & 0 deletions src/Core/src/Platform/iOS/MauiRadioButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using CoreGraphics;
using Microsoft.Maui.Graphics;
using ObjCRuntime;
using UIKit;

namespace Microsoft.Maui.Platform;

internal class MauiRadioButton : ContentView
{
IRadioButton _radioButton;
UIAccessibilityTrait _accessibilityTraits;

internal MauiRadioButton(IRadioButton virtualView)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we follow the same pattern here as MauiCheckBox ? and just make IsChecked a property on MauiRadioButton ?

{
_radioButton = virtualView;
CrossPlatformMeasure = virtualView.CrossPlatformMeasure;
CrossPlatformArrange = virtualView.CrossPlatformArrange;
IsAccessibilityElement = true;
}

static UIKit.UIAccessibilityTrait? s_switchAccessibilityTraits;
UIKit.UIAccessibilityTrait SwitchAccessibilityTraits
{
get
{
if (s_switchAccessibilityTraits == null ||
s_switchAccessibilityTraits == UIKit.UIAccessibilityTrait.None)
{
s_switchAccessibilityTraits = new UIKit.UISwitch().AccessibilityTraits;
}

return s_switchAccessibilityTraits ?? UIKit.UIAccessibilityTrait.None;
}
}

public override UIAccessibilityTrait AccessibilityTraits
{
get => _accessibilityTraits |= SwitchAccessibilityTraits;
set => _accessibilityTraits = value | SwitchAccessibilityTraits;
}

public override string? AccessibilityValue
{
get => _radioButton.IsChecked ? "1" : "0";
set { }
}
}