Skip to content
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

fix: replace hardcoded checked/ unchecked names with toggle pattern #1529

Merged
merged 3 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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.IO;
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;
sfoslund marked this conversation as resolved.
Show resolved Hide resolved
if (_radioButton == null)
{
throw new InvalidDataException("Invalid menu item header");
}
}

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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -133,24 +134,24 @@
<Button.ContextMenu>
<ContextMenu FlowDirection="LeftToRight" x:Name="cmHierarchySettings" Style="{StaticResource ctxMenuDefault}">
<MenuItem Header="{x:Static Properties:Resources.HierarchyControl_Menu_TreeView}">
<MenuItem x:Name="mniRaw" IsCheckable="False" Click="mniRaw_Click"
<cc:ToggleMenuItem x:Name="mniRaw" IsCheckable="False" Click="mniRaw_Click" Style="{StaticResource miBoldOnSelection}"
AutomationProperties.Name="{x:Static Properties:Resources.mniRawAutomationPropertiesName1}">
<MenuItem.Header>
<RadioButton x:Name="rbRaw" Content="{x:Static Properties:Resources.HierarchyControl_Raw}" Loaded="mniRaw_Loaded" Click="rbRaw_Click" Focusable="False" />
</MenuItem.Header>
</MenuItem>
<MenuItem x:Name="mniControl" IsCheckable="False" Click="mniControl_Click"
</cc:ToggleMenuItem>
<cc:ToggleMenuItem x:Name="mniControl" IsCheckable="False" Click="mniControl_Click" Style="{StaticResource miBoldOnSelection}"
AutomationProperties.Name="{x:Static Properties:Resources.mniControlAutomationPropertiesName}">
<MenuItem.Header>
<RadioButton x:Name="rbControl" Content="{x:Static Properties:Resources.HierarchyControl_Control}" Loaded="mniControl_Loaded" Click="rbControl_Click" Focusable="False" />
</MenuItem.Header>
</MenuItem>
<MenuItem x:Name="mniContent" IsCheckable="False" Click="mniContent_Click"
</cc:ToggleMenuItem>
<cc:ToggleMenuItem x:Name="mniContent" IsCheckable="False" Click="mniContent_Click" Style="{StaticResource miBoldOnSelection}"
AutomationProperties.Name="{x:Static Properties:Resources.mniContentAutomationPropertiesName1}">
<MenuItem.Header>
<RadioButton x:Name="rbContent" Content="{x:Static Properties:Resources.HierarchyControl_Content}" Loaded="mniContent_Loaded" Click="rbContent_Click" Focusable="False" />
</MenuItem.Header>
</MenuItem>
</cc:ToggleMenuItem>
</MenuItem>
<MenuItem Header="{x:Static Properties:Resources.HierarchyControl_Menu_ShowAncestry}" x:Name="mniShowAncestry" IsCheckable="True"
Loaded="mniShowAncestry_Loaded" Click="mniShowAncestry_Click"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand All @@ -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);
}

/// <summary>
Expand All @@ -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);
}

/// <summary>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions src/AccessibilityInsights.SharedUx/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1548,28 +1548,10 @@ URL: {0}</value>
<value>Couldn't save the event record file: {0}</value>
<comment>{0} is the error message of the Exception</comment>
</data>
<data name="HierarchyControl_ContentView_Checked" xml:space="preserve">
<value>Content view Checked</value>
</data>
<data name="HierarchyControl_ContentView_Unchecked" xml:space="preserve">
<value>Content view</value>
</data>
<data name="HierarchyControl_ControlView_Checked" xml:space="preserve">
<value>Control view Checked</value>
</data>
<data name="HierarchyControl_ControlView_Unchecked" xml:space="preserve">
<value>Control view</value>
</data>
<data name="HierarchyControl_LiveMode_InvokeToTestFormat" xml:space="preserve">
<value>Invoke to test {0} and descendants</value>
<comment>{0} is the glimpse of the current element</comment>
</data>
<data name="HierarchyControl_RawView_Checked" xml:space="preserve">
<value>Raw view Checked</value>
</data>
<data name="HierarchyControl_RawView_Unchecked" xml:space="preserve">
<value>Raw view</value>
</data>
<data name="PatternInfoControl_AvailablePatterns" xml:space="preserve">
<value>Available Patterns:</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -332,5 +333,15 @@ internal static DependencyObject GetParentElem<T>(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;
}
}
}
}