|
| 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 System.Collections.Generic; |
| 6 | +using Microsoft.Toolkit.Uwp.UI.Controls; |
| 7 | +using Windows.UI.Xaml.Automation; |
| 8 | +using Windows.UI.Xaml.Automation.Peers; |
| 9 | +using Windows.UI.Xaml.Automation.Provider; |
| 10 | +using Windows.UI.Xaml.Controls; |
| 11 | + |
| 12 | +namespace Microsoft.Toolkit.Uwp.UI.Automation.Peers |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Defines a framework element automation peer for the <see cref="Carousel"/> control. |
| 16 | + /// </summary> |
| 17 | + public class CarouselAutomationPeer : ItemsControlAutomationPeer, ISelectionProvider |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// Initializes a new instance of the <see cref="CarouselAutomationPeer"/> class. |
| 21 | + /// </summary> |
| 22 | + /// <param name="owner"> |
| 23 | + /// The <see cref="Carousel" /> that is associated with this <see cref="T:Windows.UI.Xaml.Automation.Peers.CarouselAutomationPeer" />. |
| 24 | + /// </param> |
| 25 | + public CarouselAutomationPeer(Carousel owner) |
| 26 | + : base(owner) |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary>Gets a value indicating whether the Microsoft UI Automation provider allows more than one child element to be selected concurrently.</summary> |
| 31 | + /// <returns>True if multiple selection is allowed; otherwise, false.</returns> |
| 32 | + public bool CanSelectMultiple => false; |
| 33 | + |
| 34 | + /// <summary>Gets a value indicating whether the UI Automation provider requires at least one child element to be selected.</summary> |
| 35 | + /// <returns>True if selection is required; otherwise, false.</returns> |
| 36 | + public bool IsSelectionRequired => true; |
| 37 | + |
| 38 | + private Carousel OwningCarousel |
| 39 | + { |
| 40 | + get |
| 41 | + { |
| 42 | + return Owner as Carousel; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary>Retrieves a UI Automation provider for each child element that is selected.</summary> |
| 47 | + /// <returns>An array of UI Automation providers.</returns> |
| 48 | + public IRawElementProviderSimple[] GetSelection() |
| 49 | + { |
| 50 | + return OwningCarousel.ContainerFromItem(this.OwningCarousel.SelectedItem) is CarouselItem selectedCarouselItem |
| 51 | + ? new[] { this.ProviderFromPeer(FromElement(selectedCarouselItem)) } |
| 52 | + : new IRawElementProviderSimple[] { }; |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Gets the control type for the element that is associated with the UI Automation peer. |
| 57 | + /// </summary> |
| 58 | + /// <returns>The control type.</returns> |
| 59 | + protected override AutomationControlType GetAutomationControlTypeCore() |
| 60 | + { |
| 61 | + return AutomationControlType.List; |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Called by GetClassName that gets a human readable name that, in addition to AutomationControlType, |
| 66 | + /// differentiates the control represented by this AutomationPeer. |
| 67 | + /// </summary> |
| 68 | + /// <returns>The string that contains the name.</returns> |
| 69 | + protected override string GetClassNameCore() |
| 70 | + { |
| 71 | + return Owner.GetType().Name; |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Called by GetName. |
| 76 | + /// </summary> |
| 77 | + /// <returns> |
| 78 | + /// Returns the first of these that is not null or empty: |
| 79 | + /// - Value returned by the base implementation |
| 80 | + /// - Name of the owning Carousel |
| 81 | + /// - Carousel class name |
| 82 | + /// </returns> |
| 83 | + protected override string GetNameCore() |
| 84 | + { |
| 85 | + string name = this.OwningCarousel.Name; |
| 86 | + if (!string.IsNullOrEmpty(name)) |
| 87 | + { |
| 88 | + return name; |
| 89 | + } |
| 90 | + |
| 91 | + name = AutomationProperties.GetName(this.OwningCarousel); |
| 92 | + if (!string.IsNullOrEmpty(name)) |
| 93 | + { |
| 94 | + return name; |
| 95 | + } |
| 96 | + |
| 97 | + return base.GetNameCore(); |
| 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.Selection: |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + return base.GetPatternCore(patternInterface); |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Gets the collection of elements that are represented in the UI Automation tree as immediate |
| 118 | + /// child elements of the automation peer. |
| 119 | + /// </summary> |
| 120 | + /// <returns>The children elements.</returns> |
| 121 | + protected override IList<AutomationPeer> GetChildrenCore() |
| 122 | + { |
| 123 | + Carousel owner = OwningCarousel; |
| 124 | + |
| 125 | + ItemCollection items = owner.Items; |
| 126 | + if (items.Count <= 0) |
| 127 | + { |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + List<AutomationPeer> peers = new List<AutomationPeer>(items.Count); |
| 132 | + for (int i = 0; i < items.Count; i++) |
| 133 | + { |
| 134 | + if (owner.ContainerFromIndex(i) is CarouselItem element) |
| 135 | + { |
| 136 | + peers.Add(FromElement(element) ?? CreatePeerForElement(element)); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return peers; |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments