Skip to content

Commit 4aed54a

Browse files
kubafloPureWeen
authored andcommitted
[Android] VoiceOver on Toolbar Item (#29596)
<!-- Please let the below note in for people that find this PR --> > [!NOTE] > Are you waiting for the changes in this PR to be merged? > It would be very helpful if you could [test the resulting artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from this PR and let us know in a comment if this change resolves your issue. Thank you! <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Issues Fixed Fixes #29573 Fixes #23623
1 parent 6a85136 commit 4aed54a

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

src/Controls/src/Core/Platform/Android/Extensions/ToolbarExtensions.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Android.Views;
1212
using AndroidX.AppCompat.Graphics.Drawable;
1313
using AndroidX.AppCompat.Widget;
14+
using AndroidX.Core.View;
15+
using AndroidX.Core.View.Accessibility;
1416
using Microsoft.Maui.Graphics;
1517
using Microsoft.Maui.Primitives;
1618
using AGraphics = Android.Graphics;
@@ -399,6 +401,56 @@ static void UpdateMenuItem(AToolbar toolbar,
399401
textView.SetTextColor(tintColor.MultiplyAlpha(0.302f).ToPlatform());
400402
}
401403
}
404+
405+
SetSemanticProperties(item, toolbar.FindViewById(menuitem.ItemId));
406+
}
407+
408+
static void SetSemanticProperties(ToolbarItem menuItem, AView? view)
409+
{
410+
if (view == null)
411+
return;
412+
413+
var semantics = SemanticProperties.UpdateSemantics(menuItem, null);
414+
var desc = semantics?.Description;
415+
var hint = semantics?.Hint;
416+
417+
// Only apply delegate if we have meaningful accessibility information
418+
if (!string.IsNullOrWhiteSpace(desc) || !string.IsNullOrWhiteSpace(hint))
419+
{
420+
view.ImportantForAccessibility = ImportantForAccessibility.Yes;
421+
ViewCompat.SetAccessibilityDelegate(view, new AccessibilityDelegateCompatImpl(desc, hint));
422+
}
423+
else
424+
{
425+
// Remove any previously set delegate if no accessibility info is present
426+
ViewCompat.SetAccessibilityDelegate(view, null);
427+
}
428+
}
429+
430+
class AccessibilityDelegateCompatImpl : AccessibilityDelegateCompat
431+
{
432+
private readonly string? _desc;
433+
private readonly string? _hint;
434+
435+
public AccessibilityDelegateCompatImpl(string? desc, string? hint)
436+
{
437+
_desc = desc;
438+
_hint = hint;
439+
}
440+
441+
public override void OnInitializeAccessibilityNodeInfo(AView? host, AccessibilityNodeInfoCompat? info)
442+
{
443+
base.OnInitializeAccessibilityNodeInfo(host, info);
444+
445+
if (host == null || info == null)
446+
return;
447+
448+
if (!string.IsNullOrWhiteSpace(_desc))
449+
info.ContentDescription = _desc;
450+
451+
if (!string.IsNullOrWhiteSpace(_hint))
452+
info.HintText = _hint;
453+
}
402454
}
403455

404456
internal static void UpdateMenuItemIcon(this IMauiContext mauiContext, IMenuItem menuItem, ToolbarItem toolBarItem, Color? tintColor)

0 commit comments

Comments
 (0)