Skip to content

Commit b8ffdb5

Browse files
committed
Updated automation peers for chip box
1 parent 5b46555 commit b8ffdb5

File tree

3 files changed

+150
-1
lines changed

3 files changed

+150
-1
lines changed

src/MADE.UI.Controls.ChipBox/Chip.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace MADE.UI.Controls
66
using System.Windows.Input;
77
using MADE.UI.Extensions;
88
using Windows.UI.Xaml;
9+
using Windows.UI.Xaml.Automation.Peers;
910
using Windows.UI.Xaml.Controls;
1011

1112
/// <summary>
@@ -72,6 +73,15 @@ public bool CanRemove
7273
/// </summary>
7374
public Button RemoveButton { get; private set; }
7475

76+
/// <summary>
77+
/// Provides the class-specific <see cref="ChipAutomationPeer"/> implementation for the Microsoft UI Automation infrastructure.
78+
/// </summary>
79+
/// <returns>The class-specific <see cref="ChipAutomationPeer"/> instance.</returns>
80+
protected override AutomationPeer OnCreateAutomationPeer()
81+
{
82+
return new ChipAutomationPeer(this);
83+
}
84+
7585
/// <summary>
7686
/// Loads the relevant control template so that it's parts can be referenced.
7787
/// </summary>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// MADE Apps licenses this file to you under the MIT license.
2+
// See the LICENSE file in the project root for more information.
3+
4+
namespace MADE.UI.Controls
5+
{
6+
using Windows.UI.Xaml.Automation.Peers;
7+
using Windows.UI.Xaml.Automation.Provider;
8+
9+
/// <summary>
10+
/// Defines a framework element automation peer for the <see cref="Chip"/> control.
11+
/// </summary>
12+
public class ChipAutomationPeer : FrameworkElementAutomationPeer, IValueProvider
13+
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="ChipAutomationPeer"/> class.
16+
/// </summary>
17+
/// <param name="owner">
18+
/// The <see cref="Chip" /> that is associated with this <see cref="AutomationPeer"/>.
19+
/// </param>
20+
public ChipAutomationPeer(Chip owner)
21+
: base(owner)
22+
{
23+
}
24+
25+
/// <summary>Gets a value that indicates whether the value of a control is read-only.</summary>
26+
/// <returns>**true** if the value is read-only; **false** if it can be modified.</returns>
27+
public bool IsReadOnly => !OwningChip.CanRemove;
28+
29+
/// <summary>Gets the value of the control.</summary>
30+
/// <returns>The value of the control.</returns>
31+
public string Value => this.OwningChip.Content?.ToString() ?? string.Empty;
32+
33+
private Chip OwningChip => this.Owner as Chip;
34+
35+
/// <summary>Sets the value of a control.</summary>
36+
/// <param name="value">The value to set. The provider is responsible for converting the value to the appropriate data type.</param>
37+
public void SetValue(string value)
38+
{
39+
if (string.IsNullOrWhiteSpace(value))
40+
{
41+
return;
42+
}
43+
44+
OwningChip.Content = value;
45+
}
46+
47+
/// <summary>
48+
/// Gets the control type for the element that is associated with the UI Automation peer.
49+
/// </summary>
50+
/// <returns>The control type.</returns>
51+
protected override AutomationControlType GetAutomationControlTypeCore()
52+
{
53+
return AutomationControlType.ListItem;
54+
}
55+
56+
/// <summary>
57+
/// Provides the type name of the <see cref="ChipBox"/> when a Microsoft UI Automation client calls GetClassName or an equivalent Microsoft UI Automation client API.
58+
/// </summary>
59+
/// <returns>The type name of the <see cref="ChipBox"/>.</returns>
60+
protected override string GetClassNameCore()
61+
{
62+
return this.Owner.GetType().Name;
63+
}
64+
65+
/// <summary>
66+
/// Provides the name given to the <see cref="ChipBox"/> when a Microsoft UI Automation client calls GetName or an equivalent Microsoft UI Automation client API.
67+
/// </summary>
68+
/// <returns>The name of the <see cref="ChipBox"/>.</returns>
69+
protected override string GetNameCore()
70+
{
71+
string name = string.Empty;
72+
73+
if (string.IsNullOrEmpty(name))
74+
{
75+
name = base.GetNameCore();
76+
}
77+
78+
if (this.OwningChip != null)
79+
{
80+
name = this.OwningChip.Name;
81+
}
82+
83+
if (string.IsNullOrEmpty(name))
84+
{
85+
name = this.GetClassName();
86+
}
87+
88+
return name;
89+
}
90+
91+
/// <summary>
92+
/// Provides the interaction patterns associated with the <see cref="Chip"/> when a Microsoft UI Automation client calls GetPattern or an equivalent Microsoft UI Automation client API.
93+
/// </summary>
94+
/// <param name="patternInterface">A value from the PatternInterface enumeration.</param>
95+
/// <returns>This if it supports the pattern interface; otherwise, null.</returns>
96+
protected override object GetPatternCore(PatternInterface patternInterface)
97+
{
98+
switch (patternInterface)
99+
{
100+
case PatternInterface.Value:
101+
return this;
102+
}
103+
104+
return base.GetPatternCore(patternInterface);
105+
}
106+
}
107+
}

src/MADE.UI.Controls.ChipBox/ChipBoxAutomationPeer.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
namespace MADE.UI.Controls
55
{
66
using Windows.UI.Xaml.Automation.Peers;
7+
using Windows.UI.Xaml.Automation.Provider;
78

89
/// <summary>
910
/// Defines a framework element automation peer for the <see cref="ChipBox"/> control.
1011
/// </summary>
11-
public class ChipBoxAutomationPeer : FrameworkElementAutomationPeer
12+
public class ChipBoxAutomationPeer : FrameworkElementAutomationPeer, IValueProvider
1213
{
1314
/// <summary>
1415
/// Initializes a new instance of the <see cref="ChipBoxAutomationPeer"/> class.
@@ -21,8 +22,23 @@ public ChipBoxAutomationPeer(ChipBox owner)
2122
{
2223
}
2324

25+
/// <summary>Gets a value that indicates whether the value of a control is read-only.</summary>
26+
/// <returns>**true** if the value is read-only; **false** if it can be modified.</returns>
27+
public bool IsReadOnly => this.OwningChipBox.IsReadonly;
28+
29+
/// <summary>Gets the value of the control.</summary>
30+
/// <returns>The value of the control.</returns>
31+
public string Value => this.OwningChipBox.TextBox.Text;
32+
2433
private ChipBox OwningChipBox => this.Owner as ChipBox;
2534

35+
/// <summary>Sets the value of a control.</summary>
36+
/// <param name="value">The value to set. The provider is responsible for converting the value to the appropriate data type.</param>
37+
public void SetValue(string value)
38+
{
39+
this.OwningChipBox.TextBox.Text = value;
40+
}
41+
2642
/// <summary>
2743
/// Gets the control type for the element that is associated with the UI Automation peer.
2844
/// </summary>
@@ -66,5 +82,21 @@ protected override string GetNameCore()
6682

6783
return name;
6884
}
85+
86+
/// <summary>
87+
/// Provides the interaction patterns associated with the <see cref="Chip"/> when a Microsoft UI Automation client calls GetPattern or an equivalent Microsoft UI Automation client API.
88+
/// </summary>
89+
/// <param name="patternInterface">A value from the PatternInterface enumeration.</param>
90+
/// <returns>This if it supports the pattern interface; otherwise, null.</returns>
91+
protected override object GetPatternCore(PatternInterface patternInterface)
92+
{
93+
switch (patternInterface)
94+
{
95+
case PatternInterface.Value:
96+
return this;
97+
}
98+
99+
return base.GetPatternCore(patternInterface);
100+
}
69101
}
70102
}

0 commit comments

Comments
 (0)