From 661152f3eed43ef1c8fd837688b0201fc7340bf3 Mon Sep 17 00:00:00 2001 From: sfoslund Date: Thu, 12 Jan 2023 13:42:36 -0800 Subject: [PATCH 1/3] Replace hardcoded checked with toggle pattern in menu items --- ...CheckableTreeViewDataItemAutomationPeer.cs | 15 ++---- .../Controls/CustomControls/ToggleMenuItem.cs | 27 ++++++++++ .../ToggleMenuItemAutomationPeer.cs | 46 ++++++++++++++++ .../Controls/HierarchyControl.xaml | 13 ++--- .../Controls/HierarchyControl.xaml.cs | 15 ------ .../Properties/Resources.Designer.cs | 54 ------------------- .../Properties/Resources.resx | 18 ------- .../Utilities/ExtensionMethods.cs | 11 ++++ 8 files changed, 94 insertions(+), 105 deletions(-) create mode 100644 src/AccessibilityInsights.SharedUx/Controls/CustomControls/ToggleMenuItem.cs create mode 100644 src/AccessibilityInsights.SharedUx/Controls/CustomControls/ToggleMenuItemAutomationPeer.cs diff --git a/src/AccessibilityInsights.SharedUx/Controls/CustomControls/CheckableTreeViewDataItemAutomationPeer.cs b/src/AccessibilityInsights.SharedUx/Controls/CustomControls/CheckableTreeViewDataItemAutomationPeer.cs index f54b1bf80..8a6f513a2 100644 --- a/src/AccessibilityInsights.SharedUx/Controls/CustomControls/CheckableTreeViewDataItemAutomationPeer.cs +++ b/src/AccessibilityInsights.SharedUx/Controls/CustomControls/CheckableTreeViewDataItemAutomationPeer.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using AccessibilityInsights.SharedUx.Utilities; using AccessibilityInsights.SharedUx.ViewModels; using System.Windows.Automation; using System.Windows.Automation.Peers; @@ -32,24 +33,14 @@ public ToggleState ToggleState { get { - return ConvertToToggleState(_owner.IsChecked); + return ExtensionMethods.ConvertToToggleState(_owner.IsChecked); } } public void Toggle() { _owner.IsChecked = !_owner.IsChecked; - RaisePropertyChangedEvent(TogglePatternIdentifiers.ToggleStateProperty, ConvertToToggleState(!_owner.IsChecked), ConvertToToggleState(_owner.IsChecked)); - } - - private static ToggleState ConvertToToggleState(bool? value) - { - switch (value) - { - case (true): return ToggleState.On; - case (false): return ToggleState.Off; - default: return ToggleState.Indeterminate; - } + RaisePropertyChangedEvent(TogglePatternIdentifiers.ToggleStateProperty, ExtensionMethods.ConvertToToggleState(!_owner.IsChecked), ExtensionMethods.ConvertToToggleState(_owner.IsChecked)); } } } diff --git a/src/AccessibilityInsights.SharedUx/Controls/CustomControls/ToggleMenuItem.cs b/src/AccessibilityInsights.SharedUx/Controls/CustomControls/ToggleMenuItem.cs new file mode 100644 index 000000000..add3a368f --- /dev/null +++ b/src/AccessibilityInsights.SharedUx/Controls/CustomControls/ToggleMenuItem.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Windows; +using System.Windows.Automation.Peers; +using System.Windows.Controls; + +namespace AccessibilityInsights.SharedUx.Controls.CustomControls +{ + internal class ToggleMenuItem : MenuItem + { + protected override AutomationPeer OnCreateAutomationPeer() + { + return new ToggleMenuItemAutomationPeer(this); + } + + protected override DependencyObject GetContainerForItemOverride() + { + return new ToggleMenuItem(); + } + + protected override bool IsItemItsOwnContainerOverride(object item) + { + return item is ToggleMenuItem; + } + } +} diff --git a/src/AccessibilityInsights.SharedUx/Controls/CustomControls/ToggleMenuItemAutomationPeer.cs b/src/AccessibilityInsights.SharedUx/Controls/CustomControls/ToggleMenuItemAutomationPeer.cs new file mode 100644 index 000000000..8cba3971d --- /dev/null +++ b/src/AccessibilityInsights.SharedUx/Controls/CustomControls/ToggleMenuItemAutomationPeer.cs @@ -0,0 +1,46 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using AccessibilityInsights.SharedUx.Utilities; +using System.Windows.Automation; +using System.Windows.Automation.Peers; +using System.Windows.Automation.Provider; +using System.Windows.Controls; + +namespace AccessibilityInsights.SharedUx.Controls.CustomControls +{ + internal class ToggleMenuItemAutomationPeer : MenuItemAutomationPeer, IToggleProvider + { + private readonly RadioButton _radioButton; + + public ToggleMenuItemAutomationPeer(MenuItem item) + : base(item) + { + _radioButton = item.Header as RadioButton; + } + + public override object GetPattern(PatternInterface patternInterface) + { + if (patternInterface == PatternInterface.Toggle) + { + return this; + } + + return base.GetPattern(patternInterface); + } + + public ToggleState ToggleState + { + get + { + return ExtensionMethods.ConvertToToggleState(_radioButton.IsChecked); + } + } + + public void Toggle() + { + _radioButton.IsChecked = !_radioButton.IsChecked; + RaisePropertyChangedEvent(TogglePatternIdentifiers.ToggleStateProperty, ExtensionMethods.ConvertToToggleState(!_radioButton.IsChecked), ExtensionMethods.ConvertToToggleState(_radioButton.IsChecked)); + } + } +} diff --git a/src/AccessibilityInsights.SharedUx/Controls/HierarchyControl.xaml b/src/AccessibilityInsights.SharedUx/Controls/HierarchyControl.xaml index f40633f37..065b062bb 100644 --- a/src/AccessibilityInsights.SharedUx/Controls/HierarchyControl.xaml +++ b/src/AccessibilityInsights.SharedUx/Controls/HierarchyControl.xaml @@ -9,6 +9,7 @@ xmlns:controls="clr-namespace:AccessibilityInsights.CommonUxComponents.Controls;assembly=AccessibilityInsights.CommonUxComponents" xmlns:behaviors="clr-namespace:AccessibilityInsights.SharedUx.Behaviors" xmlns:local="clr-namespace:AccessibilityInsights.SharedUx.Controls" + xmlns:cc="clr-namespace:AccessibilityInsights.SharedUx.Controls.CustomControls" xmlns:converters="clr-namespace:AccessibilityInsights.SharedUx.Converters" xmlns:Properties="clr-namespace:AccessibilityInsights.SharedUx.Properties" mc:Ignorable="d" @@ -133,24 +134,24 @@ - - - + - - + - + diff --git a/src/AccessibilityInsights.SharedUx/Controls/HierarchyControl.xaml.cs b/src/AccessibilityInsights.SharedUx/Controls/HierarchyControl.xaml.cs index f9280bf9c..e9d7d8de7 100644 --- a/src/AccessibilityInsights.SharedUx/Controls/HierarchyControl.xaml.cs +++ b/src/AccessibilityInsights.SharedUx/Controls/HierarchyControl.xaml.cs @@ -508,11 +508,6 @@ private void mniRaw_Loaded(object sender, RoutedEventArgs e) this.mniRaw.IsEnabled = false; this.rbRaw.IsChecked = this.ElementContext.Element.TreeWalkerMode == TreeViewMode.Raw; } - bool isChecked = this.rbRaw.IsChecked.HasValue && this.rbRaw.IsChecked.Value; - this.mniRaw.SetValue(AutomationProperties.NameProperty, - isChecked ? - Properties.Resources.HierarchyControl_RawView_Checked : - Properties.Resources.HierarchyControl_RawView_Unchecked); } /// @@ -532,11 +527,6 @@ private void mniContent_Loaded(object sender, RoutedEventArgs e) this.mniContent.IsEnabled = false; this.rbContent.IsChecked = this.ElementContext.Element.TreeWalkerMode == TreeViewMode.Content; } - bool isChecked = this.rbContent.IsChecked.HasValue && this.rbContent.IsChecked.Value; - this.mniContent.SetValue(AutomationProperties.NameProperty, - isChecked ? - Properties.Resources.HierarchyControl_ContentView_Checked : - Properties.Resources.HierarchyControl_ContentView_Unchecked); } /// @@ -556,11 +546,6 @@ private void mniControl_Loaded(object sender, RoutedEventArgs e) this.mniControl.IsEnabled = false; this.rbControl.IsChecked = this.ElementContext.Element.TreeWalkerMode == TreeViewMode.Control; } - bool isChecked = this.rbControl.IsChecked.HasValue && this.rbControl.IsChecked.Value; - this.mniControl.SetValue(AutomationProperties.NameProperty, - isChecked ? - Properties.Resources.HierarchyControl_ControlView_Checked : - Properties.Resources.HierarchyControl_ControlView_Unchecked); } /// diff --git a/src/AccessibilityInsights.SharedUx/Properties/Resources.Designer.cs b/src/AccessibilityInsights.SharedUx/Properties/Resources.Designer.cs index ca45d3526..42b6211fb 100644 --- a/src/AccessibilityInsights.SharedUx/Properties/Resources.Designer.cs +++ b/src/AccessibilityInsights.SharedUx/Properties/Resources.Designer.cs @@ -2030,24 +2030,6 @@ public static string HierarchyControl_Content { } } - /// - /// Looks up a localized string similar to Content view Checked. - /// - public static string HierarchyControl_ContentView_Checked { - get { - return ResourceManager.GetString("HierarchyControl_ContentView_Checked", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Content view. - /// - public static string HierarchyControl_ContentView_Unchecked { - get { - return ResourceManager.GetString("HierarchyControl_ContentView_Unchecked", resourceCulture); - } - } - /// /// Looks up a localized string similar to _Control. /// @@ -2057,24 +2039,6 @@ public static string HierarchyControl_Control { } } - /// - /// Looks up a localized string similar to Control view Checked. - /// - public static string HierarchyControl_ControlView_Checked { - get { - return ResourceManager.GetString("HierarchyControl_ControlView_Checked", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Control view. - /// - public static string HierarchyControl_ControlView_Unchecked { - get { - return ResourceManager.GetString("HierarchyControl_ControlView_Unchecked", resourceCulture); - } - } - /// /// Looks up a localized string similar to Could not find the selected item, the bug filing is canceled.. /// @@ -2139,24 +2103,6 @@ public static string HierarchyControl_Raw { } } - /// - /// Looks up a localized string similar to Raw view Checked. - /// - public static string HierarchyControl_RawView_Checked { - get { - return ResourceManager.GetString("HierarchyControl_RawView_Checked", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Raw view. - /// - public static string HierarchyControl_RawView_Unchecked { - get { - return ResourceManager.GetString("HierarchyControl_RawView_Unchecked", resourceCulture); - } - } - /// /// Looks up a localized string similar to Search. /// diff --git a/src/AccessibilityInsights.SharedUx/Properties/Resources.resx b/src/AccessibilityInsights.SharedUx/Properties/Resources.resx index 5bec7b34d..0cde3a1fa 100644 --- a/src/AccessibilityInsights.SharedUx/Properties/Resources.resx +++ b/src/AccessibilityInsights.SharedUx/Properties/Resources.resx @@ -1548,28 +1548,10 @@ URL: {0} Couldn't save the event record file: {0} {0} is the error message of the Exception - - Content view Checked - - - Content view - - - Control view Checked - - - Control view - Invoke to test {0} and descendants {0} is the glimpse of the current element - - Raw view Checked - - - Raw view - Available Patterns: diff --git a/src/AccessibilityInsights.SharedUx/Utilities/ExtensionMethods.cs b/src/AccessibilityInsights.SharedUx/Utilities/ExtensionMethods.cs index 7dcebc537..1e254329f 100644 --- a/src/AccessibilityInsights.SharedUx/Utilities/ExtensionMethods.cs +++ b/src/AccessibilityInsights.SharedUx/Utilities/ExtensionMethods.cs @@ -21,6 +21,7 @@ using System.Text; using System.Text.RegularExpressions; using System.Windows; +using System.Windows.Automation; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Interop; @@ -332,5 +333,15 @@ internal static DependencyObject GetParentElem(this DependencyObject obj) } #pragma warning restore CA1031 // Do not catch general exception types } + + internal static ToggleState ConvertToToggleState(bool? value) + { + switch (value) + { + case (true): return ToggleState.On; + case (false): return ToggleState.Off; + default: return ToggleState.Indeterminate; + } + } } } From 2d0dfa5aec7768032ba8d0da369f46479a938af2 Mon Sep 17 00:00:00 2001 From: sfoslund Date: Thu, 12 Jan 2023 14:31:08 -0800 Subject: [PATCH 2/3] Fix style --- .../Controls/HierarchyControl.xaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/AccessibilityInsights.SharedUx/Controls/HierarchyControl.xaml b/src/AccessibilityInsights.SharedUx/Controls/HierarchyControl.xaml index 065b062bb..5e6916846 100644 --- a/src/AccessibilityInsights.SharedUx/Controls/HierarchyControl.xaml +++ b/src/AccessibilityInsights.SharedUx/Controls/HierarchyControl.xaml @@ -134,19 +134,19 @@ - - - From dad93bc1a0462ec7603bd6b205bd709c61dcf3a8 Mon Sep 17 00:00:00 2001 From: Sarah Oslund Date: Fri, 13 Jan 2023 10:23:57 -0800 Subject: [PATCH 3/3] Add exception on null radio button --- .../Controls/CustomControls/ToggleMenuItemAutomationPeer.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/AccessibilityInsights.SharedUx/Controls/CustomControls/ToggleMenuItemAutomationPeer.cs b/src/AccessibilityInsights.SharedUx/Controls/CustomControls/ToggleMenuItemAutomationPeer.cs index 8cba3971d..251477ac0 100644 --- a/src/AccessibilityInsights.SharedUx/Controls/CustomControls/ToggleMenuItemAutomationPeer.cs +++ b/src/AccessibilityInsights.SharedUx/Controls/CustomControls/ToggleMenuItemAutomationPeer.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using AccessibilityInsights.SharedUx.Utilities; +using System.IO; using System.Windows.Automation; using System.Windows.Automation.Peers; using System.Windows.Automation.Provider; @@ -17,6 +18,10 @@ public ToggleMenuItemAutomationPeer(MenuItem item) : base(item) { _radioButton = item.Header as RadioButton; + if (_radioButton == null) + { + throw new InvalidDataException("Invalid menu item header"); + } } public override object GetPattern(PatternInterface patternInterface)