diff --git a/components/SettingsControls/samples/ClickableSettingsCardSample.xaml b/components/SettingsControls/samples/ClickableSettingsCardSample.xaml index cfadad61..3fa46cec 100644 --- a/components/SettingsControls/samples/ClickableSettingsCardSample.xaml +++ b/components/SettingsControls/samples/ClickableSettingsCardSample.xaml @@ -1,4 +1,4 @@ - + - + ResourceKey="SystemColorHighlightColorBrush" /> diff --git a/components/SettingsControls/src/SettingsCard/SettingsCardAutomationPeer.cs b/components/SettingsControls/src/SettingsCard/SettingsCardAutomationPeer.cs index d6f159f3..dc049ca0 100644 --- a/components/SettingsControls/src/SettingsCard/SettingsCardAutomationPeer.cs +++ b/components/SettingsControls/src/SettingsCard/SettingsCardAutomationPeer.cs @@ -2,46 +2,67 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; -using System.Collections.Generic; -using System.Text; - namespace CommunityToolkit.WinUI.Controls; +/// +/// AutomationPeer for SettingsCard +/// +public class SettingsCardAutomationPeer : FrameworkElementAutomationPeer +{ + /// + /// Initializes a new instance of the class. + /// + /// SettingsCard + public SettingsCardAutomationPeer(SettingsCard owner) + : base(owner) + { + } + /// - /// AutomationPeer for SettingsCard + /// Gets the control type for the element that is associated with the UI Automation peer. /// - public class SettingsCardAutomationPeer : FrameworkElementAutomationPeer + /// The control type. + protected override AutomationControlType GetAutomationControlTypeCore() { - /// - /// Initializes a new instance of the class. - /// - /// SettingsCard - public SettingsCardAutomationPeer(SettingsCard owner) - : base(owner) + if (Owner is SettingsCard settingsCard && settingsCard.IsClickEnabled) { + return AutomationControlType.Button; } - - /// - /// Gets the control type for the element that is associated with the UI Automation peer. - /// - /// The control type. - protected override AutomationControlType GetAutomationControlTypeCore() + else { return AutomationControlType.Group; } + } - /// - /// Called by GetClassName that gets a human readable name that, in addition to AutomationControlType, - /// differentiates the control represented by this AutomationPeer. - /// - /// The string that contains the name. - protected override string GetClassNameCore() + /// + /// Called by GetClassName that gets a human readable name that, in addition to AutomationControlType, + /// differentiates the control represented by this AutomationPeer. + /// + /// The string that contains the name. + protected override string GetClassNameCore() + { + return Owner.GetType().Name; + } + + protected override string GetNameCore() + { + // We only want to announce the button card name if it is clickable, else it's just a regular card that does not receive focus + if (Owner is SettingsCard owner && owner.IsClickEnabled) { - string classNameCore = Owner.GetType().Name; -#if DEBUG_AUTOMATION - System.Diagnostics.Debug.WriteLine("SettingsCardAutomationPeer.GetClassNameCore returns " + classNameCore); -#endif - return classNameCore; + string name = AutomationProperties.GetName(owner); + if (!string.IsNullOrEmpty(name)) + { + return name; + } + else + { + if (owner.Header is string headerString && !string.IsNullOrEmpty(headerString)) + { + return headerString; + } + } } + + return base.GetNameCore(); } +} diff --git a/components/SettingsControls/src/SettingsExpander/SettingsExpander.cs b/components/SettingsControls/src/SettingsExpander/SettingsExpander.cs index 02503571..d969a16d 100644 --- a/components/SettingsControls/src/SettingsExpander/SettingsExpander.cs +++ b/components/SettingsControls/src/SettingsExpander/SettingsExpander.cs @@ -25,7 +25,7 @@ public SettingsExpander() protected override void OnApplyTemplate() { base.OnApplyTemplate(); - RegisterAutomation(); + SetAccessibleName(); if (_itemsRepeater != null) { @@ -43,11 +43,11 @@ protected override void OnApplyTemplate() } } - private void RegisterAutomation() + private void SetAccessibleName() { - if (Header is string headerString && headerString != string.Empty) + if (string.IsNullOrEmpty(AutomationProperties.GetName(this))) { - if (!string.IsNullOrEmpty(headerString) && string.IsNullOrEmpty(AutomationProperties.GetName(this))) + if (Header is string headerString && !string.IsNullOrEmpty(headerString)) { AutomationProperties.SetName(this, headerString); } diff --git a/components/SettingsControls/src/SettingsExpander/SettingsExpanderAutomationPeer.cs b/components/SettingsControls/src/SettingsExpander/SettingsExpanderAutomationPeer.cs index e4d9fdc7..d6cb35a6 100644 --- a/components/SettingsControls/src/SettingsExpander/SettingsExpanderAutomationPeer.cs +++ b/components/SettingsControls/src/SettingsExpander/SettingsExpanderAutomationPeer.cs @@ -39,11 +39,28 @@ protected override AutomationControlType GetAutomationControlTypeCore() /// The string that contains the name. protected override string GetClassNameCore() { - string classNameCore = Owner.GetType().Name; -#if DEBUG_AUTOMATION - System.Diagnostics.Debug.WriteLine("SettingsCardAutomationPeer.GetClassNameCore returns " + classNameCore); -#endif - return classNameCore; + return Owner.GetType().Name; + } + + protected override string GetNameCore() + { + string name = base.GetNameCore(); + + if (Owner is SettingsExpander owner) + { + if (!string.IsNullOrEmpty(AutomationProperties.GetName(owner))) + { + name = AutomationProperties.GetName(owner); + } + else + { + if (owner.Header is string headerString && !string.IsNullOrEmpty(headerString)) + { + name = headerString; + } + } + } + return name; } ///