|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using Microsoft.Toolkit.Uwp.UI.Controls; |
| 6 | +using Windows.UI.Xaml.Automation; |
| 7 | +using Windows.UI.Xaml.Automation.Peers; |
| 8 | +using Windows.UI.Xaml.Automation.Provider; |
| 9 | + |
| 10 | +namespace Microsoft.Toolkit.Uwp.UI.Automation.Peers |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Defines a framework element automation peer for the <see cref="Expander"/> control. |
| 14 | + /// </summary> |
| 15 | + public class ExpanderAutomationPeer : FrameworkElementAutomationPeer, IToggleProvider |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Initializes a new instance of the <see cref="ExpanderAutomationPeer"/> class. |
| 19 | + /// </summary> |
| 20 | + /// <param name="owner"> |
| 21 | + /// The <see cref="Expander" /> that is associated with this <see cref="T:Windows.UI.Xaml.Automation.Peers.ExpanderAutomationPeer" />. |
| 22 | + /// </param> |
| 23 | + public ExpanderAutomationPeer(Expander owner) |
| 24 | + : base(owner) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary>Gets the toggle state of the control.</summary> |
| 29 | + /// <returns>The toggle state of the control, as a value of the enumeration.</returns> |
| 30 | + public ToggleState ToggleState => OwningExpander.IsExpanded ? ToggleState.On : ToggleState.Off; |
| 31 | + |
| 32 | + private Expander OwningExpander |
| 33 | + { |
| 34 | + get |
| 35 | + { |
| 36 | + return Owner as Expander; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary>Cycles through the toggle states of a control.</summary> |
| 41 | + public void Toggle() |
| 42 | + { |
| 43 | + if (!IsEnabled()) |
| 44 | + { |
| 45 | + throw new ElementNotEnabledException(); |
| 46 | + } |
| 47 | + |
| 48 | + OwningExpander.IsExpanded = !OwningExpander.IsExpanded; |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets the control type for the element that is associated with the UI Automation peer. |
| 53 | + /// </summary> |
| 54 | + /// <returns>The control type.</returns> |
| 55 | + protected override AutomationControlType GetAutomationControlTypeCore() |
| 56 | + { |
| 57 | + return AutomationControlType.Custom; |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Called by GetClassName that gets a human readable name that, in addition to AutomationControlType, |
| 62 | + /// differentiates the control represented by this AutomationPeer. |
| 63 | + /// </summary> |
| 64 | + /// <returns>The string that contains the name.</returns> |
| 65 | + protected override string GetClassNameCore() |
| 66 | + { |
| 67 | + return Owner.GetType().Name; |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Called by GetName. |
| 72 | + /// </summary> |
| 73 | + /// <returns> |
| 74 | + /// Returns the first of these that is not null or empty: |
| 75 | + /// - Value returned by the base implementation |
| 76 | + /// - Name of the owning Expander |
| 77 | + /// - Expander class name |
| 78 | + /// </returns> |
| 79 | + protected override string GetNameCore() |
| 80 | + { |
| 81 | + string name = base.GetNameCore(); |
| 82 | + if (!string.IsNullOrEmpty(name)) |
| 83 | + { |
| 84 | + return name; |
| 85 | + } |
| 86 | + |
| 87 | + if (this.OwningExpander != null) |
| 88 | + { |
| 89 | + name = this.OwningExpander.Name; |
| 90 | + } |
| 91 | + |
| 92 | + if (string.IsNullOrEmpty(name)) |
| 93 | + { |
| 94 | + name = this.GetClassName(); |
| 95 | + } |
| 96 | + |
| 97 | + return name; |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Gets the control pattern that is associated with the specified Windows.UI.Xaml.Automation.Peers.PatternInterface. |
| 102 | + /// </summary> |
| 103 | + /// <param name="patternInterface">A value from the Windows.UI.Xaml.Automation.Peers.PatternInterface enumeration.</param> |
| 104 | + /// <returns>The object that supports the specified pattern, or null if unsupported.</returns> |
| 105 | + protected override object GetPatternCore(PatternInterface patternInterface) |
| 106 | + { |
| 107 | + switch (patternInterface) |
| 108 | + { |
| 109 | + case PatternInterface.Toggle: |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + return base.GetPatternCore(patternInterface); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments