Skip to content

Commit abb3d6b

Browse files
committed
Added automation peer for TokenizingTextBox
1 parent 2c04c96 commit abb3d6b

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Input/TokenizingTextBox/TokenizingTextBox.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
65
using System.Collections.ObjectModel;
76
using System.Linq;
87
using System.Threading.Tasks;
98
using Microsoft.Toolkit.Uwp.Deferred;
9+
using Microsoft.Toolkit.Uwp.UI.Automation.Peers;
1010
using Microsoft.Toolkit.Uwp.UI.Helpers;
1111
using Windows.System;
1212
using Windows.UI.Core;
1313
using Windows.UI.Xaml;
14+
using Windows.UI.Xaml.Automation.Peers;
1415
using Windows.UI.Xaml.Controls;
1516
using Windows.UI.Xaml.Input;
1617

@@ -486,6 +487,15 @@ protected void UpdateCurrentTextEdit(ITokenStringContainer edit)
486487
Text = edit.Text; // Update our text property.
487488
}
488489

490+
/// <summary>
491+
/// Creates AutomationPeer (<see cref="UIElement.OnCreateAutomationPeer"/>)
492+
/// </summary>
493+
/// <returns>An automation peer for this <see cref="TokenizingTextBox"/>.</returns>
494+
protected override AutomationPeer OnCreateAutomationPeer()
495+
{
496+
return new TokenizingTextBoxAutomationPeer(this);
497+
}
498+
489499
/// <summary>
490500
/// Remove the specified token from the list.
491501
/// </summary>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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.Controls;
10+
11+
namespace Microsoft.Toolkit.Uwp.UI.Automation.Peers
12+
{
13+
/// <summary>
14+
/// Defines a framework element automation peer for the <see cref="TokenizingTextBox"/> control.
15+
/// </summary>
16+
public class TokenizingTextBoxAutomationPeer : ListViewBaseAutomationPeer
17+
{
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="TokenizingTextBoxAutomationPeer"/> class.
20+
/// </summary>
21+
/// <param name="owner">
22+
/// The <see cref="TokenizingTextBox" /> that is associated with this <see cref="T:Microsoft.Toolkit.Uwp.UI.Automation.Peers.TokenizingTextBoxAutomationPeer" />.
23+
/// </param>
24+
public TokenizingTextBoxAutomationPeer(TokenizingTextBox owner)
25+
: base(owner)
26+
{
27+
}
28+
29+
private TokenizingTextBox OwningTokenizingTextBox
30+
{
31+
get
32+
{
33+
return Owner as TokenizingTextBox;
34+
}
35+
}
36+
37+
/// <summary>
38+
/// Called by GetClassName that gets a human readable name that, in addition to AutomationControlType,
39+
/// differentiates the control represented by this AutomationPeer.
40+
/// </summary>
41+
/// <returns>The string that contains the name.</returns>
42+
protected override string GetClassNameCore()
43+
{
44+
return Owner.GetType().Name;
45+
}
46+
47+
/// <summary>
48+
/// Called by GetName.
49+
/// </summary>
50+
/// <returns>
51+
/// Returns the first of these that is not null or empty:
52+
/// - Value returned by the base implementation
53+
/// - Name of the owning Carousel
54+
/// - Carousel class name
55+
/// </returns>
56+
protected override string GetNameCore()
57+
{
58+
string name = this.OwningTokenizingTextBox.Name;
59+
if (!string.IsNullOrEmpty(name))
60+
{
61+
return name;
62+
}
63+
64+
name = AutomationProperties.GetName(this.OwningTokenizingTextBox);
65+
if (!string.IsNullOrEmpty(name))
66+
{
67+
return name;
68+
}
69+
70+
return base.GetNameCore();
71+
}
72+
73+
/// <summary>
74+
/// Gets the control pattern that is associated with the specified Windows.UI.Xaml.Automation.Peers.PatternInterface.
75+
/// </summary>
76+
/// <param name="patternInterface">A value from the Windows.UI.Xaml.Automation.Peers.PatternInterface enumeration.</param>
77+
/// <returns>The object that supports the specified pattern, or null if unsupported.</returns>
78+
protected override object GetPatternCore(PatternInterface patternInterface)
79+
{
80+
switch (patternInterface)
81+
{
82+
case PatternInterface.Selection:
83+
return this;
84+
}
85+
86+
return base.GetPatternCore(patternInterface);
87+
}
88+
89+
/// <summary>
90+
/// Gets the collection of elements that are represented in the UI Automation tree as immediate
91+
/// child elements of the automation peer.
92+
/// </summary>
93+
/// <returns>The children elements.</returns>
94+
protected override IList<AutomationPeer> GetChildrenCore()
95+
{
96+
TokenizingTextBox owner = this.OwningTokenizingTextBox;
97+
98+
ItemCollection items = owner.Items;
99+
if (items.Count <= 0)
100+
{
101+
return null;
102+
}
103+
104+
List<AutomationPeer> peers = new List<AutomationPeer>(items.Count);
105+
for (int i = 0; i < items.Count; i++)
106+
{
107+
if (owner.ContainerFromIndex(i) is TokenizingTextBoxItem element)
108+
{
109+
peers.Add(FromElement(element) ?? CreatePeerForElement(element));
110+
}
111+
}
112+
113+
return peers;
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)