Skip to content

Commit

Permalink
Merge pull request #3895 from MahApps/fix/GH-3894-Accessibility-Windo…
Browse files Browse the repository at this point in the history
…wCommands

Fix Accessibility Insights issues with WindowCommands
  • Loading branch information
punker76 committed Aug 3, 2020
2 parents 38b0201 + 80c0d5c commit 7dbf841
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
65 changes: 65 additions & 0 deletions src/MahApps.Metro/Automation/Peers/WindowCommandsAutomationPeer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 System.Windows;
using System.Windows.Automation.Peers;
using JetBrains.Annotations;
using MahApps.Metro.Controls;

namespace MahApps.Metro.Automation.Peers
{
public class WindowCommandsAutomationPeer : FrameworkElementAutomationPeer
{
public WindowCommandsAutomationPeer([NotNull] WindowCommands owner)
: base(owner)
{
}

/// <inheritdoc />
protected override string GetClassNameCore()
{
return "WindowCommands";
}

/// <inheritdoc />
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.ToolBar;
}

/// <inheritdoc />
protected override string GetNameCore()
{
string nameCore = base.GetNameCore();

if (string.IsNullOrEmpty(nameCore))
{
nameCore = ((WindowCommands)this.Owner).Name;
}

if (string.IsNullOrEmpty(nameCore))
{
nameCore = this.GetClassNameCore();
}

return nameCore;
}

/// <inheritdoc />
protected override bool IsOffscreenCore()
{
return !((WindowCommands)this.Owner).HasItems || base.IsOffscreenCore();
}

protected override Point GetClickablePointCore()
{
if (!((WindowCommands)this.Owner).HasItems)
{
return new Point(double.NaN, double.NaN);
}

return base.GetClickablePointCore();
}
}
}
27 changes: 25 additions & 2 deletions src/MahApps.Metro/Controls/MetroWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Input;
Expand Down Expand Up @@ -129,8 +130,30 @@ public class MetroWindow : Window
public static readonly DependencyProperty IconTemplateProperty = DependencyProperty.Register(nameof(IconTemplate), typeof(DataTemplate), typeof(MetroWindow), new PropertyMetadata(null));
public static readonly DependencyProperty TitleTemplateProperty = DependencyProperty.Register(nameof(TitleTemplate), typeof(DataTemplate), typeof(MetroWindow), new PropertyMetadata(null));

public static readonly DependencyProperty LeftWindowCommandsProperty = DependencyProperty.Register(nameof(LeftWindowCommands), typeof(WindowCommands), typeof(MetroWindow), new PropertyMetadata(null, UpdateLogicalChilds));
public static readonly DependencyProperty RightWindowCommandsProperty = DependencyProperty.Register(nameof(RightWindowCommands), typeof(WindowCommands), typeof(MetroWindow), new PropertyMetadata(null, UpdateLogicalChilds));
public static readonly DependencyProperty LeftWindowCommandsProperty = DependencyProperty.Register(nameof(LeftWindowCommands), typeof(WindowCommands), typeof(MetroWindow), new PropertyMetadata(null, OnLeftWindowCommandsPropertyChanged));

private static void OnLeftWindowCommandsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is WindowCommands windowCommands)
{
AutomationProperties.SetName(windowCommands, nameof(LeftWindowCommands));
}

UpdateLogicalChilds(d, e);
}

public static readonly DependencyProperty RightWindowCommandsProperty = DependencyProperty.Register(nameof(RightWindowCommands), typeof(WindowCommands), typeof(MetroWindow), new PropertyMetadata(null, OnRightWindowCommandsPropertyChanged));

private static void OnRightWindowCommandsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is WindowCommands windowCommands)
{
AutomationProperties.SetName(windowCommands, nameof(RightWindowCommands));
}

UpdateLogicalChilds(d, e);
}

public static readonly DependencyProperty WindowButtonCommandsProperty = DependencyProperty.Register(nameof(WindowButtonCommands), typeof(WindowButtonCommands), typeof(MetroWindow), new PropertyMetadata(null, UpdateLogicalChilds));

public static readonly DependencyProperty LeftWindowCommandsOverlayBehaviorProperty = DependencyProperty.Register(nameof(LeftWindowCommandsOverlayBehavior), typeof(WindowCommandsOverlayBehavior), typeof(MetroWindow), new PropertyMetadata(WindowCommandsOverlayBehavior.Never, OnShowTitleBarPropertyChangedCallback));
Expand Down
10 changes: 10 additions & 0 deletions src/MahApps.Metro/Controls/WindowCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Data;
using ControlzEx;
using ControlzEx.Theming;
using MahApps.Metro.Automation.Peers;
using MahApps.Metro.ValueBoxes;

namespace MahApps.Metro.Controls
Expand Down Expand Up @@ -358,5 +360,13 @@ private void WindowCommandsLoaded(object sender, RoutedEventArgs e)
this.SetValue(ParentWindowPropertyKey, window);
}
}

/// <summary>
/// Creates AutomationPeer (<see cref="UIElement.OnCreateAutomationPeer"/>)
/// </summary>
protected override AutomationPeer OnCreateAutomationPeer()
{
return new WindowCommandsAutomationPeer(this);
}
}
}
1 change: 1 addition & 0 deletions src/MahApps.Metro/Themes/WindowCommands.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
<ControlTemplate x:Key="MahApps.Templates.WindowCommands" TargetType="Controls:WindowCommands">
<DockPanel>
<ToggleButton x:Name="PART_ToggleButton"
AutomationProperties.Name="OverflowToggleButton"
ClickMode="Press"
DockPanel.Dock="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(DockPanel.Dock), Mode=OneWay}"
Foreground="{TemplateBinding Foreground}"
Expand Down

0 comments on commit 7dbf841

Please sign in to comment.