Skip to content

Commit

Permalink
Improved accessibility (#7)
Browse files Browse the repository at this point in the history
* Update

* update
  • Loading branch information
IeuanWalker authored Apr 6, 2023
1 parent 254f07f commit 525fb34
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 22 deletions.
3 changes: 2 additions & 1 deletion Demo/Controls/Examples/CustomButton.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<ControlTemplate>
<stateButton:StateButton Padding="20,20"
BackgroundColor="#0071FF"
Clicked="StateButton_Clicked">
Clicked="StateButton_Clicked"
SemanticProperties.Description="{TemplateBinding Text}">
<stateButton:StateButton.StrokeShape>
<RoundRectangle CornerRadius="25" />
</stateButton:StateButton.StrokeShape>
Expand Down
6 changes: 4 additions & 2 deletions Demo/Controls/Examples/Example6.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:stateButton="clr-namespace:IeuanWalker.Maui.StateButton;assembly=IeuanWalker.Maui.StateButton">
<ContentView.ControlTemplate>
<ControlTemplate>
<stateButton:StateButton Grid.Column="0"
<stateButton:StateButton Grid.Column="0"
BackgroundColor="#7B66FF"
Clicked="StateButton_Clicked"
ClickedCommand="{TemplateBinding ClickedCommand}"
Expand All @@ -17,7 +17,9 @@
PressedCommandParameter="{TemplateBinding PressedCommandParameter}"
Released="StateButton_Released"
ReleasedCommand="{TemplateBinding ReleasedCommand}"
ReleasedCommandParameter="{TemplateBinding ReleasedCommandParameter}">
ReleasedCommandParameter="{TemplateBinding ReleasedCommandParameter}"
SemanticProperties.Description="{TemplateBinding Text}"
SemanticProperties.Hint="{TemplateBinding AccessibilityHint}">
<stateButton:StateButton.StrokeShape>
<RoundRectangle CornerRadius="5" />
</stateButton:StateButton.StrokeShape>
Expand Down
6 changes: 6 additions & 0 deletions Demo/Controls/Examples/Example6.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public string Text
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public static readonly BindableProperty AccessibilityHintProperty = BindableProperty.Create(nameof(AccessibilityHint), typeof(string), typeof(CustomButton), string.Empty);
public string AccessibilityHint
{
get => (string)GetValue(AccessibilityHintProperty);
set => SetValue(AccessibilityHintProperty, value);
}

/// <summary>
/// Backing BindableProperty for the <see cref="ClickedCommand"/> property.
Expand Down
18 changes: 14 additions & 4 deletions Demo/Pages/AccessibilityTestPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@

<Label Style="{StaticResource sectionHeading}" Text="Standard button" />
<Button Text="Test" />
<controls:Example6 SemanticProperties.Description="Test" Text="Test" />
<controls:Example6 Text="Test" />

<Label Style="{StaticResource sectionHeading}" Text="With hint" />
<Button SemanticProperties.Hint="Reload list of contacts" Text="Reload" />
<controls:Example6 SemanticProperties.Description="Reload"
SemanticProperties.Hint="Reload list of contacts"
<Button SemanticProperties.Description="Reload"
SemanticProperties.Hint="Reload list of contacts"
Text="Reload" />
<controls:Example6 AccessibilityHint="Reload list of contacts" Text="Reload" />

<Label Style="{StaticResource sectionHeading}" Text="With hint and events" />
<Button Clicked="Button_Clicked"
SemanticProperties.Hint="Reload list of contacts"
Text="Reload" />
<controls:Example6 AccessibilityHint="Reload list of contacts"
Clicked="Button_Clicked"
Pressed="Example6_Pressed"
Released="Example6_Released"
Text="Reload" />
</VerticalStackLayout>
</ContentPage.Content>
Expand Down
9 changes: 7 additions & 2 deletions Demo/Pages/AccessibilityTestPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ public AccessibilityTestPage()
InitializeComponent();
}

async void StateButton_Clicked(object sender, EventArgs e)
async void Button_Clicked(object sender, EventArgs e)
{
await DisplayAlert("Clicked", string.Empty, "OK");
}

async void StateButton_Released(object sender, EventArgs e)
async void Example6_Pressed(object sender, EventArgs e)
{
await DisplayAlert("Pressed", string.Empty, "OK");
}

async void Example6_Released(object sender, EventArgs e)
{
await DisplayAlert("Release", string.Empty, "OK");
}
Expand Down
2 changes: 2 additions & 0 deletions Scr/Platform/CustomContentView.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public CustomContentView(IBorderView virtualView)
AddGestureRecognizer(new UITapGestureRecognizer(_stateButton.InvokeClicked));
}

public override bool CanBecomeFocused => true;

public override void TouchesMoved(NSSet touches, UIEvent? evt)
{
_stateButton.InvokeReleased();
Expand Down
15 changes: 2 additions & 13 deletions Scr/Platform/CustomContentViewGroup.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public CustomContentViewGroup(Context context, IBorderView virtualView) : base(c
{
_stateButton = (StateButton)virtualView;

//! important - this is what makes the switch accessible via keyboard navigation
Focusable = true;
Clickable = true;
Click += (sender, e) => _stateButton.InvokeClicked();

Touch += (sender, te) =>
{
Expand Down Expand Up @@ -63,17 +63,6 @@ public CustomContentViewGroup(Context context, IBorderView virtualView) : base(c

public override ICharSequence? AccessibilityClassNameFormatted => new String("android.widget.Button");

public override void OnInitializeAccessibilityNodeInfo(AccessibilityNodeInfo? info)
{
if (info is not null)
{
info.Focusable = true;
info.Clickable = true;
}

base.OnInitializeAccessibilityNodeInfo(info);
}

public override bool OnKeyUp([GeneratedEnum] Keycode keyCode, KeyEvent? e)
{
if (keyCode == Keycode.Space || keyCode == Keycode.Enter)
Expand Down
1 change: 1 addition & 0 deletions Scr/StateButton.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<Border x:Class="IeuanWalker.Maui.StateButton.StateButton"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
AutomationProperties.IsInAccessibleTree="True"
BackgroundColor="Transparent"
Stroke="Transparent" />

0 comments on commit 525fb34

Please sign in to comment.