-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Added automation peer class for expander #3504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b2e65e2
Added automation peer class for expander
jamesmcroft 9b17e3d
Updated new file headers
jamesmcroft 439cf3d
Merge branch 'master' into jamesmcroft/3502-expander-automation
jamesmcroft 43df5ba
Moved expander automation peer into its own namespace
jamesmcroft e5e70be
Merge branch 'master' into jamesmcroft/3502-expander-automation
jamesmcroft 5560452
Merge branch 'master' into jamesmcroft/3502-expander-automation
Rosuavio 41b28b2
Merge branch 'master' into jamesmcroft/3502-expander-automation
michael-hawker b74914a
Merge branch 'master' into jamesmcroft/3502-expander-automation
Rosuavio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
Microsoft.Toolkit.Uwp.UI.Controls/Expander/ExpanderAutomationPeer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Microsoft.Toolkit.Uwp.UI.Controls; | ||
| using Windows.UI.Xaml.Automation; | ||
| using Windows.UI.Xaml.Automation.Peers; | ||
| using Windows.UI.Xaml.Automation.Provider; | ||
|
|
||
| namespace Microsoft.Toolkit.Uwp.UI.Automation.Peers | ||
| { | ||
| /// <summary> | ||
| /// Defines a framework element automation peer for the <see cref="Expander"/> control. | ||
| /// </summary> | ||
| public class ExpanderAutomationPeer : FrameworkElementAutomationPeer, IToggleProvider | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ExpanderAutomationPeer"/> class. | ||
| /// </summary> | ||
| /// <param name="owner"> | ||
| /// The <see cref="Expander" /> that is associated with this <see cref="T:Windows.UI.Xaml.Automation.Peers.ExpanderAutomationPeer" />. | ||
| /// </param> | ||
| public ExpanderAutomationPeer(Expander owner) | ||
| : base(owner) | ||
| { | ||
| } | ||
|
|
||
| /// <summary>Gets the toggle state of the control.</summary> | ||
| /// <returns>The toggle state of the control, as a value of the enumeration.</returns> | ||
| public ToggleState ToggleState => OwningExpander.IsExpanded ? ToggleState.On : ToggleState.Off; | ||
|
|
||
| private Expander OwningExpander | ||
| { | ||
| get | ||
| { | ||
| return Owner as Expander; | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Cycles through the toggle states of a control.</summary> | ||
| public void Toggle() | ||
| { | ||
| if (!IsEnabled()) | ||
| { | ||
| throw new ElementNotEnabledException(); | ||
| } | ||
|
|
||
| OwningExpander.IsExpanded = !OwningExpander.IsExpanded; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the control type for the element that is associated with the UI Automation peer. | ||
| /// </summary> | ||
| /// <returns>The control type.</returns> | ||
| protected override AutomationControlType GetAutomationControlTypeCore() | ||
| { | ||
| return AutomationControlType.Custom; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Called by GetClassName that gets a human readable name that, in addition to AutomationControlType, | ||
| /// differentiates the control represented by this AutomationPeer. | ||
| /// </summary> | ||
| /// <returns>The string that contains the name.</returns> | ||
| protected override string GetClassNameCore() | ||
| { | ||
| return Owner.GetType().Name; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Called by GetName. | ||
| /// </summary> | ||
| /// <returns> | ||
| /// Returns the first of these that is not null or empty: | ||
| /// - Value returned by the base implementation | ||
| /// - Name of the owning Expander | ||
| /// - Expander class name | ||
| /// </returns> | ||
| protected override string GetNameCore() | ||
| { | ||
| string name = base.GetNameCore(); | ||
| if (!string.IsNullOrEmpty(name)) | ||
| { | ||
| return name; | ||
| } | ||
|
|
||
| if (this.OwningExpander != null) | ||
| { | ||
| name = this.OwningExpander.Name; | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(name)) | ||
| { | ||
| name = this.GetClassName(); | ||
| } | ||
|
|
||
| return name; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the control pattern that is associated with the specified Windows.UI.Xaml.Automation.Peers.PatternInterface. | ||
| /// </summary> | ||
| /// <param name="patternInterface">A value from the Windows.UI.Xaml.Automation.Peers.PatternInterface enumeration.</param> | ||
| /// <returns>The object that supports the specified pattern, or null if unsupported.</returns> | ||
| protected override object GetPatternCore(PatternInterface patternInterface) | ||
| { | ||
| switch (patternInterface) | ||
| { | ||
| case PatternInterface.Toggle: | ||
| return this; | ||
| } | ||
|
|
||
| return base.GetPatternCore(patternInterface); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jamesmcroft I missed we should have used the
IExpandCollapseProviderin addition or instead of maybe? Thoughts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me take a look, I don't see why not though. Would you want this to be inclusive of
IToggleProvideror in favor of?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jamesmcroft good question. I don't know if it's a problem to provide multiple interfaces, probably best to include both if possible? I know some other code will probably look for one over the other, so I imagine having both would allow it to be used by the most other code parts looking for certain types of peers. @chingucoding @ranjeshj any thoughts?
Would it make sense to add unit tests like in #3544 as well?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would argue that having both interfaces only makes sense if you expose both patterns in
GetPatternCore. So at least implement what you are providing as part ofGetPatternCore. For what it's worth, the WinUI expander control only specifies the ExpandCollapsePattern, so I am inclined to say that that pattern/interface is sufficient.Yes, I think testing that peer and the exposed functions of the peer should be done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can have multiple patterns supported for sure. ExpandCollapse pattern made sense for Expander.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jamesmcroft if you've got some cycles still to continue helping with this, mind submitting a follow-on PR with the tweaks? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michael-hawker I'll get another PR open for these changes 😄