|
| 1 | +using AndroidX.Core.View; |
| 2 | +using System.ComponentModel; |
| 3 | +using Xamarin.Forms; |
| 4 | +using Xamarin.CommunityToolkit.Effects; |
| 5 | +using Effects = Xamarin.CommunityToolkit.Android.Effects; |
| 6 | +using AndroidX.Core.View.Accessibiity; |
| 7 | +using Android.Widget; |
| 8 | + |
| 9 | +[assembly: ExportEffect(typeof(Effects.SemanticEffectRouter), nameof(SemanticEffectRouter))] |
| 10 | + |
| 11 | +namespace Xamarin.CommunityToolkit.Android.Effects |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Android implementation of the <see cref="SemanticEffect" /> |
| 15 | + /// </summary> |
| 16 | + public class SemanticEffectRouter : SemanticEffectRouterBase<SemanticEffectRouter> |
| 17 | + { |
| 18 | + SemanticAccessibilityDelegate? semanticAccessibilityDelegate; |
| 19 | + |
| 20 | + protected override void Update(global::Android.Views.View view, SemanticEffectRouter effect) |
| 21 | + { |
| 22 | + var isHeading = SemanticEffect.GetHeadingLevel(Element) != CommunityToolkit.Effects.Semantic.HeadingLevel.None; |
| 23 | + ViewCompat.SetAccessibilityHeading(view, isHeading); |
| 24 | + var desc = SemanticEffect.GetDescription(Element); |
| 25 | + var hint = SemanticEffect.GetHint(Element); |
| 26 | + |
| 27 | + if (!string.IsNullOrEmpty(hint) || !string.IsNullOrEmpty(desc)) |
| 28 | + { |
| 29 | + if (semanticAccessibilityDelegate == null) |
| 30 | + { |
| 31 | + semanticAccessibilityDelegate = new SemanticAccessibilityDelegate(Element); |
| 32 | + ViewCompat.SetAccessibilityDelegate(view, semanticAccessibilityDelegate); |
| 33 | + } |
| 34 | + } |
| 35 | + else if (semanticAccessibilityDelegate != null) |
| 36 | + { |
| 37 | + semanticAccessibilityDelegate = null; |
| 38 | + ViewCompat.SetAccessibilityDelegate(view, null); |
| 39 | + } |
| 40 | + |
| 41 | + if (semanticAccessibilityDelegate != null) |
| 42 | + { |
| 43 | + semanticAccessibilityDelegate.Element = Element; |
| 44 | + view.ImportantForAccessibility = global::Android.Views.ImportantForAccessibility.Yes; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + protected override void OnElementPropertyChanged(PropertyChangedEventArgs args) |
| 49 | + { |
| 50 | + base.OnElementPropertyChanged(args); |
| 51 | + |
| 52 | + if (args.PropertyName == SemanticEffect.HeadingLevelProperty.PropertyName || |
| 53 | + args.PropertyName == SemanticEffect.DescriptionProperty.PropertyName || |
| 54 | + args.PropertyName == SemanticEffect.HintProperty.PropertyName) |
| 55 | + { |
| 56 | + Update(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + class SemanticAccessibilityDelegate : AccessibilityDelegateCompat |
| 61 | + { |
| 62 | + public Element Element { get; set; } |
| 63 | + |
| 64 | + public SemanticAccessibilityDelegate(Element element) |
| 65 | + { |
| 66 | + Element = element; |
| 67 | + } |
| 68 | + |
| 69 | + public override void OnInitializeAccessibilityNodeInfo(global::Android.Views.View host, AccessibilityNodeInfoCompat info) |
| 70 | + { |
| 71 | + base.OnInitializeAccessibilityNodeInfo(host, info); |
| 72 | + |
| 73 | + if (Element == null) |
| 74 | + return; |
| 75 | + |
| 76 | + if (info == null) |
| 77 | + return; |
| 78 | + |
| 79 | + var hint = SemanticEffect.GetHint(Element); |
| 80 | + if (!string.IsNullOrEmpty(hint)) |
| 81 | + { |
| 82 | + info.HintText = hint; |
| 83 | + |
| 84 | + if (host is EditText) |
| 85 | + info.ShowingHintText = false; |
| 86 | + } |
| 87 | + |
| 88 | + var desc = SemanticEffect.GetDescription(Element); |
| 89 | + if (!string.IsNullOrEmpty(desc)) |
| 90 | + { |
| 91 | + info.ContentDescription = desc; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments