diff --git a/src/GraphicsControls/Controls/GraphicsView.android.cs b/src/GraphicsControls/Controls/GraphicsView.android.cs index 4e9cd04..529d2e8 100644 --- a/src/GraphicsControls/Controls/GraphicsView.android.cs +++ b/src/GraphicsControls/Controls/GraphicsView.android.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Graphics.Android; using Android.Content; using Android.Views; @@ -36,9 +37,20 @@ namespace GraphicsControls.Android e.NewElement.Invalidated += OnDrawInvalidated; SetNativeControl(new NativeGraphicsView(Context)); Control.Drawable = Element; + + UpdateAutomationProperties(); } } + protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) + { + base.OnElementPropertyChanged(sender, e); + + if (e.PropertyName == AutomationProperties.HelpTextProperty.PropertyName || + e.PropertyName == AutomationProperties.NameProperty.PropertyName) + UpdateAutomationProperties(); + } + protected override void OnAttachedToWindow() { base.OnAttachedToWindow(); @@ -96,5 +108,35 @@ namespace GraphicsControls.Android { Control?.InvalidateDrawable(); } + + void UpdateAutomationProperties() + { + if (Control == null) + return; + + var defaultContentDescription = Control.ContentDescription; + + string value = ConcatenateNameAndHelpText(Element); + + var contentDescription = !string.IsNullOrWhiteSpace(value) ? value : defaultContentDescription; + + if (string.IsNullOrWhiteSpace(contentDescription) && Element is Element element) + contentDescription = element.AutomationId; + + Control.ContentDescription = contentDescription; + } + + internal static string ConcatenateNameAndHelpText(BindableObject Element) + { + var name = (string)Element.GetValue(AutomationProperties.NameProperty); + var helpText = (string)Element.GetValue(AutomationProperties.HelpTextProperty); + + if (string.IsNullOrWhiteSpace(name)) + return helpText; + if (string.IsNullOrWhiteSpace(helpText)) + return name; + + return $"{name}. {helpText}"; + } } } \ No newline at end of file diff --git a/src/GraphicsControls/Controls/GraphicsView.ios.cs b/src/GraphicsControls/Controls/GraphicsView.ios.cs index d4ca5d6..b02e68e 100644 --- a/src/GraphicsControls/Controls/GraphicsView.ios.cs +++ b/src/GraphicsControls/Controls/GraphicsView.ios.cs @@ -49,6 +49,8 @@ namespace GraphicsControls.iOS Control.Drawable = Element; UpdateContent(); + UpdateAutomationHelpText(); + UpdateAutomationName(); } } @@ -58,6 +60,10 @@ namespace GraphicsControls.iOS if (e.PropertyName == ContentView.ContentProperty.PropertyName) UpdateContent(); + else if (e.PropertyName == AutomationProperties.HelpTextProperty.PropertyName) + UpdateAutomationHelpText(); + else if (e.PropertyName == AutomationProperties.NameProperty.PropertyName) + UpdateAutomationName(); Control?.InvalidateDrawable(); } @@ -173,5 +179,15 @@ namespace GraphicsControls.iOS var content = Subviews[0]; BringSubviewToFront(content); } + + void UpdateAutomationHelpText() + { + Control?.SetAccessibilityHint(Element); + } + + void UpdateAutomationName() + { + Control?.SetAccessibilityLabel(Element); + } } } \ No newline at end of file diff --git a/src/GraphicsControls/Controls/GraphicsView.macos.cs b/src/GraphicsControls/Controls/GraphicsView.macos.cs index 6a11a6a..94551d4 100644 --- a/src/GraphicsControls/Controls/GraphicsView.macos.cs +++ b/src/GraphicsControls/Controls/GraphicsView.macos.cs @@ -47,6 +47,8 @@ namespace GraphicsControls.Mac Control.Drawable = Element; UpdateContent(); + UpdateAutomationHelpText(); + UpdateAutomationName(); } } @@ -56,6 +58,10 @@ namespace GraphicsControls.Mac if (e.PropertyName == ContentView.ContentProperty.PropertyName) UpdateContent(); + else if (e.PropertyName == AutomationProperties.HelpTextProperty.PropertyName) + UpdateAutomationHelpText(); + else if (e.PropertyName == AutomationProperties.NameProperty.PropertyName) + UpdateAutomationName(); Control?.InvalidateDrawable(); } @@ -143,5 +149,15 @@ namespace GraphicsControls.Mac content.RemoveFromSuperview(); AddSubview(content); } + + void UpdateAutomationHelpText() + { + Control?.SetAccessibilityHint(Element); + } + + void UpdateAutomationName() + { + Control?.SetAccessibilityLabel(Element); + } } } \ No newline at end of file