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

Introduce SetSemanticFocus API via SemanticExtensions #1829

Merged
merged 7 commits into from
Jul 28, 2021
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 @@ -184,7 +184,20 @@
Text="Button in StackLayout with semantic description"
FontSize="14"/>
</VerticalStackLayout>

<Label
Text="Explore SemanticExtensions below"
TextColor="RoyalBlue"
FontAttributes="Bold"
FontSize="16"
Margin="0,10"/>
<Button
Text="Click to set semantic focus to label below"
FontSize="14"
Clicked="SetSemanticFocusButton_Clicked"/>
<Label
x:Name="semanticFocusLabel"
Text="Label receiving semantic focus"
FontSize="14"/>
<Label
Text="Explore AutomationProperties on controls within layouts below"
TextColor="RoyalBlue"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Maui.Controls.Sample.Pages.Base;
using Maui.Controls.Sample.Pages.Base;
using Microsoft.Maui;
using Microsoft.Maui.Controls.Xaml;

Expand Down Expand Up @@ -27,5 +26,10 @@ private async void PushButton_Clicked(object sender, System.EventArgs e)
{
await Navigation.PushAsync(new SemanticsPage());
}

private void SetSemanticFocusButton_Clicked(object sender, System.EventArgs e)
{
semanticFocusLabel.SetSemanticFocus();
}
}
}
18 changes: 18 additions & 0 deletions src/Core/src/Platform/Android/SemanticExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using Android.Text;
using Android.Views;
using Android.Views.Accessibility;

namespace Microsoft.Maui
{
public static partial class SemanticExtensions
{
public static void SetSemanticFocus(this IFrameworkElement element)
{
if (element?.Handler?.NativeView is not View view)
throw new NullReferenceException("Can't access view from a null handler");

view.SendAccessibilityEvent(EventTypes.ViewFocused);
}
}
}
11 changes: 11 additions & 0 deletions src/Core/src/Platform/Standard/SemanticExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Microsoft.Maui
{
public static partial class SemanticExtensions
{
/// <summary>
/// Force semantic screen reader focus to specified element
/// </summary>
/// <param name="element"></param>
public static void SetSemanticFocus(this IFrameworkElement element) { }
}
}
7 changes: 7 additions & 0 deletions src/Core/src/Platform/Windows/SemanticExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Microsoft.Maui
{
public static partial class SemanticExtensions
{
public static void SetSemanticFocus(this IFrameworkElement element) { }
}
}
49 changes: 32 additions & 17 deletions src/Core/src/Platform/iOS/MauiCheckBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class MauiCheckBox : UIButton

static UIImage? Checked;
static UIImage? Unchecked;
static UIAccessibilityTrait? s_switchAccessibilityTraits;
UIAccessibilityTrait _accessibilityTraits;

Color? _tintColor;
Expand All @@ -24,17 +23,6 @@ public class MauiCheckBox : UIButton

public EventHandler? CheckedChanged;

internal float MinimumViewSize
{
get { return _minimumViewSize; }
set
{
_minimumViewSize = value;
var xOffset = (value - DefaultSize + LineWidth) / 4;
ContentEdgeInsets = new UIEdgeInsets(0, xOffset, 0, 0);
}
}

public MauiCheckBox()
{
ContentMode = UIViewContentMode.Center;
Expand All @@ -43,10 +31,7 @@ public MauiCheckBox()
VerticalAlignment = UIControlContentVerticalAlignment.Center;
AdjustsImageWhenDisabled = false;
AdjustsImageWhenHighlighted = false;

TouchUpInside += OnTouchUpInside;
s_switchAccessibilityTraits ??= new UISwitch().AccessibilityTraits;
_accessibilityTraits = s_switchAccessibilityTraits.Value;
}

void OnTouchUpInside(object? sender, EventArgs e)
Expand All @@ -55,6 +40,17 @@ void OnTouchUpInside(object? sender, EventArgs e)
CheckedChanged?.Invoke(this, EventArgs.Empty);
}

internal float MinimumViewSize
{
get { return _minimumViewSize; }
set
{
_minimumViewSize = value;
var xOffset = (value - DefaultSize + LineWidth) / 4;
ContentEdgeInsets = new UIEdgeInsets(0, xOffset, 0, 0);
}
}

public bool IsChecked
{
get => _isChecked;
Expand Down Expand Up @@ -266,11 +262,30 @@ void UpdateDisplay()
SetNeedsDisplay();
}

static UIKit.UIAccessibilityTrait? s_switchAccessibilityTraits;
UIKit.UIAccessibilityTrait SwitchAccessibilityTraits
{
get
{
// Accessibility Traits are none if VO is off
// So we return None until we detect that it's been turned on
if (base.AccessibilityTraits == UIAccessibilityTrait.None)
return UIAccessibilityTrait.None;

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;
set => _accessibilityTraits = value;
get => _accessibilityTraits |= SwitchAccessibilityTraits;
set => _accessibilityTraits = value | SwitchAccessibilityTraits;
}

public override string? AccessibilityValue
Expand Down
20 changes: 20 additions & 0 deletions src/Core/src/Platform/iOS/SemanticExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Foundation;
using UIKit;
using System;

namespace Microsoft.Maui
{
public static partial class SemanticExtensions
{
public static void SetSemanticFocus(this IFrameworkElement element)
{
if (element?.Handler?.NativeView == null)
throw new NullReferenceException("Can't access view from a null handler");
Copy link
Member

Choose a reason for hiding this comment

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

This will appear if I set focus on say a ctor. That is technically valid and can be fixed later, but we could maybe have a "pending" property and set when the handler is created.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea, pending is just a really hard concept with announce because if you want to queue up an announce then you have to interrupt whatever the OS naturally wants to announce. In most (maybe all?) cases that would be considered bad practice.

So we're being hostile here intentionally for now :-)


if (element.Handler.NativeView is not NSObject nativeView)
return;

UIAccessibility.PostNotification(UIAccessibilityPostNotification.LayoutChanged, nativeView);
}
}
}