Browse Source

Added AccessibilityView and ControlType.

- `AutomationProperties.AccessibilityView`: from UWP - control's element's visibility in automation tree
- `ControlTypeOverride`: from UWP proposal - overrides the automation control type specified in peer
ui-automation-test
Steven Kirk 4 years ago
parent
commit
49ef4b3f28
  1. 91
      src/Avalonia.Controls/Automation/AutomationProperties.cs
  2. 9
      src/Avalonia.Controls/Automation/Peers/AutomationPeer.cs
  3. 11
      src/Avalonia.Controls/Automation/Peers/ControlAutomationPeer.cs

91
src/Avalonia.Controls/Automation/AutomationProperties.cs

@ -1,8 +1,30 @@
using System;
using Avalonia.Automation.Peers;
using Avalonia.Controls;
namespace Avalonia.Automation
{
/// <summary>
/// Declares how a control should included in different views of the automation tree.
/// </summary>
public enum AccessibilityView
{
/// <summary>
/// The control is included in the Raw view of the automation tree.
/// </summary>
Raw,
/// <summary>
/// The control is included in the Control view of the automation tree.
/// </summary>
Control,
/// <summary>
/// The control is included in the Content view of the automation tree.
/// </summary>
Content,
}
public static class AutomationProperties
{
internal const int AutomationPositionInSetDefault = -1;
@ -16,6 +38,15 @@ namespace Avalonia.Automation
"AcceleratorKey",
typeof(AutomationProperties));
/// <summary>
/// Defines the AutomationProperties.AccessibilityView attached property.
/// </summary>
public static readonly AttachedProperty<AccessibilityView> AccessibilityViewProperty =
AvaloniaProperty.RegisterAttached<StyledElement, AccessibilityView>(
"AccessibilityView",
typeof(AutomationProperties),
defaultValue: AccessibilityView.Content);
/// <summary>
/// Defines the AutomationProperties.AccessKey attached property
/// </summary>
@ -32,6 +63,14 @@ namespace Avalonia.Automation
"AutomationId",
typeof(AutomationProperties));
/// <summary>
/// Defines the AutomationProperties.ControlTypeOverride attached property.
/// </summary>
public static readonly AttachedProperty<AutomationControlType?> ControlTypeOverrideProperty =
AvaloniaProperty.RegisterAttached<StyledElement, AutomationControlType?>(
"ControlTypeOverride",
typeof(AutomationProperties));
/// <summary>
/// Defines the AutomationProperties.HelpText attached property.
/// </summary>
@ -171,6 +210,32 @@ namespace Avalonia.Automation
return ((string)element.GetValue(AcceleratorKeyProperty));
}
/// <summary>
/// Helper for setting AccessibilityView property on a StyledElement.
/// </summary>
public static void SetAccessibilityView(StyledElement element, AccessibilityView value)
{
if (element == null)
{
throw new ArgumentNullException(nameof(element));
}
element.SetValue(AccessibilityViewProperty, value);
}
/// <summary>
/// Helper for reading AccessibilityView property from a StyledElement.
/// </summary>
public static AccessibilityView GetAccessibilityView(StyledElement element)
{
if (element == null)
{
throw new ArgumentNullException(nameof(element));
}
return element.GetValue(AccessibilityViewProperty);
}
/// <summary>
/// Helper for setting AccessKey property on a StyledElement.
/// </summary>
@ -223,6 +288,32 @@ namespace Avalonia.Automation
return element.GetValue(AutomationIdProperty);
}
/// <summary>
/// Helper for setting ControlTypeOverride property on a StyledElement.
/// </summary>
public static void SetControlTypeOverride(StyledElement element, AutomationControlType? value)
{
if (element == null)
{
throw new ArgumentNullException(nameof(element));
}
element.SetValue(ControlTypeOverrideProperty, value);
}
/// <summary>
/// Helper for reading ControlTypeOverride property from a StyledElement.
/// </summary>
public static AutomationControlType? GetControlTypeOverride(StyledElement element)
{
if (element == null)
{
throw new ArgumentNullException(nameof(element));
}
return element.GetValue(ControlTypeOverrideProperty);
}
/// <summary>
/// Helper for setting HelpText property on a StyledElement.
/// </summary>

9
src/Avalonia.Controls/Automation/Peers/AutomationPeer.cs

@ -74,7 +74,7 @@ namespace Avalonia.Automation.Peers
/// <summary>
/// Gets the control type for the element that is associated with the UI Automation peer.
/// </summary>
public AutomationControlType GetAutomationControlType() => GetAutomationControlTypeCore();
public AutomationControlType GetAutomationControlType() => GetControlTypeOverrideCore();
/// <summary>
/// Gets the automation ID of the element that is associated with the UI Automation peer.
@ -230,7 +230,12 @@ namespace Avalonia.Automation.Peers
protected abstract bool IsKeyboardFocusableCore();
protected abstract void SetFocusCore();
protected abstract bool ShowContextMenuCore();
protected virtual AutomationControlType GetControlTypeOverrideCore()
{
return GetAutomationControlTypeCore();
}
protected virtual object? GetProviderCore(Type providerType)
{
return providerType.IsAssignableFrom(this.GetType()) ? this : null;

11
src/Avalonia.Controls/Automation/Peers/ControlAutomationPeer.cs

@ -148,17 +148,22 @@ namespace Avalonia.Automation.Peers
protected override string? GetAcceleratorKeyCore() => AutomationProperties.GetAcceleratorKey(Owner);
protected override string? GetAccessKeyCore() => AutomationProperties.GetAccessKey(Owner);
protected override AutomationControlType GetAutomationControlTypeCore() => AutomationControlType.Custom;
protected override string? GetAutomationIdCore() => AutomationProperties.GetAutomationId(Owner) ?? Owner.Name;
protected override Rect GetBoundingRectangleCore() => GetBounds(Owner.TransformedBounds);
protected override AutomationControlType GetAutomationControlTypeCore() => AutomationControlType.Custom;
protected override string GetClassNameCore() => Owner.GetType().Name;
protected override bool HasKeyboardFocusCore() => Owner.IsFocused;
protected override bool IsContentElementCore() => true;
protected override bool IsControlElementCore() => true;
protected override bool IsContentElementCore() => AutomationProperties.GetAccessibilityView(Owner) >= AccessibilityView.Content;
protected override bool IsControlElementCore() => AutomationProperties.GetAccessibilityView(Owner) >= AccessibilityView.Control;
protected override bool IsEnabledCore() => Owner.IsEnabled;
protected override bool IsKeyboardFocusableCore() => Owner.Focusable;
protected override void SetFocusCore() => Owner.Focus();
protected override AutomationControlType GetControlTypeOverrideCore()
{
return AutomationProperties.GetControlTypeOverride(Owner) ?? GetAutomationControlTypeCore();
}
private static Rect GetBounds(TransformedBounds? bounds)
{
return bounds?.Bounds.TransformToAABB(bounds!.Value.Transform) ?? default;

Loading…
Cancel
Save