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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static void UpdateSemanticNodeInfo(this View virtualView, AccessibilityNo
if (info == null)
return;

if (virtualView.TapGestureRecognizerNeedsDelegate())
if (virtualView.HasAccessibleTapGesture())
info.AddAction(AccessibilityNodeInfoCompat.AccessibilityActionCompat.ActionClick);
}

Expand Down Expand Up @@ -46,5 +46,9 @@ internal static void AddOrRemoveControlsAccessibilityDelegate(this View virtualV
}
}
}

internal static bool ControlsAccessibilityDelegateNeeded(this View virtualView)
=> virtualView.HasAccessibleTapGesture();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
using System.ComponentModel;
using Android.Content;
using Android.Views;
using AndroidX.AppCompat.Widget;
using AndroidX.Core.View;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Graphics;
using static Android.Views.View;
using AView = Android.Views.View;

namespace Microsoft.Maui.Controls.Platform
Expand All @@ -22,6 +24,8 @@ class GesturePlatformManager : IDisposable
bool _disposed;
bool _inputTransparent;
bool _isEnabled;
bool? _focusableDefaultValue;
bool? _clickableDefaultValue;
protected virtual VisualElement? Element => _handler?.VirtualView as VisualElement;

View? View => Element as View;
Expand Down Expand Up @@ -159,7 +163,7 @@ ScaleGestureDetector InitializeScaleDetector()

bool ViewHasPinchGestures()
{
if (View == null)
if (View is null)
return false;

int count = View.GestureRecognizers.Count;
Expand Down Expand Up @@ -210,14 +214,63 @@ void SetupGestures()
}

// Always unsubscribe first to avoid duplicates

platformView.Touch -= OnPlatformViewTouched;
platformView.KeyPress -= OnKeyPress;


if (shouldAddTouchEvent)
{
platformView.Touch += OnPlatformViewTouched;

// If we have a TapGestureRecognizer, we need to handle key presses
if (View.HasAccessibleTapGesture())
{
platformView.KeyPress += OnKeyPress;
_focusableDefaultValue ??= platformView.Focusable;
_clickableDefaultValue ??= platformView.Clickable;
platformView.Focusable = true;
platformView.Clickable = true;
}
}
else
{
_focusableDefaultValue = null;
_clickableDefaultValue = null;
}
}

void OnKeyPress(object? sender, KeyEventArgs e)
{
if (e.Event?.Action != KeyEventActions.Up)
{
e.Handled = false;
return;
}

if (View is null || sender is not AView platformView)
{
e.Handled = false;
return;
}

if (e.KeyCode.IsConfirmKey() &&
View.HasAccessibleTapGesture(out var tapGestureRecognizer) &&
e.Event.HasNoModifiers)
{
if (!platformView.Enabled)
{
e.Handled = true;
return;
}

if (!e.Event.IsCanceled)
tapGestureRecognizer.SendTapped(View, (v) => Point.Zero);
}

e.Handled = false;
}

void OnPlatformViewTouched(object? sender, AView.TouchEventArgs e)
{
if (_disposed)
Expand All @@ -236,8 +289,15 @@ void OnPlatformViewTouched(object? sender, AView.TouchEventArgs e)
void SetupElement(VisualElement? oldElement, VisualElement? newElement)
{
var platformView = Control;
if (platformView != null)
if (platformView is not null)
{
platformView.Focusable = _focusableDefaultValue ?? platformView.Focusable;
platformView.Clickable = _clickableDefaultValue ?? platformView.Clickable;
_focusableDefaultValue = null;
_clickableDefaultValue = null;
platformView.Touch -= OnPlatformViewTouched;
platformView.KeyPress -= OnKeyPress;
}

_handler = null;
if (oldElement != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ void LoadRecognizers()

if (PlatformView != null &&
_handler.VirtualView is View v &&
v.TapGestureRecognizerNeedsDelegate() &&
v.HasAccessibleTapGesture() &&
(PlatformView.AccessibilityTraits & UIAccessibilityTrait.Button) != UIAccessibilityTrait.Button)
{
PlatformView.AccessibilityTraits |= UIAccessibilityTrait.Button;
Expand Down
14 changes: 10 additions & 4 deletions src/Controls/src/Core/Platform/SemanticExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
namespace Microsoft.Maui.Controls.Platform
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.Maui.Controls.Platform
{
public static partial class SemanticExtensions
{
internal static bool ControlsAccessibilityDelegateNeeded(this View virtualView)
=> virtualView.TapGestureRecognizerNeedsDelegate();
internal static bool HasAccessibleTapGesture(this View virtualView) =>
HasAccessibleTapGesture(virtualView, out _);

internal static bool TapGestureRecognizerNeedsDelegate(this View virtualView)
internal static bool HasAccessibleTapGesture(
this View virtualView,
[NotNullWhen(true)] out TapGestureRecognizer? tapGestureRecognizer)
{
foreach (var gesture in virtualView.GestureRecognizers)
{
//Accessibility can't handle Tap Recognizers with > 1 tap
if (gesture is TapGestureRecognizer tgr && tgr.NumberOfTapsRequired == 1)
{
tapGestureRecognizer = tgr;
return (tgr.Buttons & ButtonsMask.Primary) == ButtonsMask.Primary;
}
}
tapGestureRecognizer = null;
return false;
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/Core/src/Platform/Android/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -806,5 +806,19 @@ void ShowSoftInput()

view.Post(ShowSoftInput);
}

internal static bool IsConfirmKey(this Keycode keyCode)
{
switch (keyCode)
{
case Keycode.DpadCenter:
case Keycode.Enter:
case Keycode.Space:
case Keycode.NumpadEnter:
return true;
default:
return false;
}
}
}
}
Loading