From 35d3b8c59c55d7828fd917b94c5edf4d21474ebb Mon Sep 17 00:00:00 2001 From: Difegue Date: Tue, 24 Sep 2024 17:23:39 +0200 Subject: [PATCH 1/4] Add narration support to CardAction --- src/Wpf.Ui/Controls/CardAction/CardAction.cs | 9 ++- .../CardAction/CardActionAutomationPeer.cs | 77 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/Wpf.Ui/Controls/CardAction/CardActionAutomationPeer.cs diff --git a/src/Wpf.Ui/Controls/CardAction/CardAction.cs b/src/Wpf.Ui/Controls/CardAction/CardAction.cs index dc73e9189..9614b8388 100644 --- a/src/Wpf.Ui/Controls/CardAction/CardAction.cs +++ b/src/Wpf.Ui/Controls/CardAction/CardAction.cs @@ -4,6 +4,8 @@ // All Rights Reserved. // ReSharper disable once CheckNamespace +using System.Windows.Automation.Peers; + namespace Wpf.Ui.Controls; /// @@ -48,4 +50,9 @@ public IconElement? Icon get => (IconElement?)GetValue(IconProperty); set => SetValue(IconProperty, value); } -} + + protected override AutomationPeer OnCreateAutomationPeer() + { + return new CardActionAutomationPeer(this); + } +} \ No newline at end of file diff --git a/src/Wpf.Ui/Controls/CardAction/CardActionAutomationPeer.cs b/src/Wpf.Ui/Controls/CardAction/CardActionAutomationPeer.cs new file mode 100644 index 000000000..ffba11b5f --- /dev/null +++ b/src/Wpf.Ui/Controls/CardAction/CardActionAutomationPeer.cs @@ -0,0 +1,77 @@ +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. +// Copyright (C) Leszek Pomianowski and WPF UI Contributors. +// All Rights Reserved. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Automation; +using System.Windows.Automation.Peers; + +namespace Wpf.Ui.Controls; + +internal class CardActionAutomationPeer : FrameworkElementAutomationPeer +{ + private readonly CardAction _owner; + + public CardActionAutomationPeer(CardAction owner) + : base(owner) + { + _owner = owner; + } + + protected override string GetClassNameCore() + { + return "Button"; + } + + protected override AutomationControlType GetAutomationControlTypeCore() + { + return AutomationControlType.Button; + } + + public override object GetPattern(PatternInterface patternInterface) + { + if (patternInterface == PatternInterface.ItemContainer) + { + return this; + } + + return base.GetPattern(patternInterface); + } + + protected override AutomationPeer GetLabeledByCore() + { + if (_owner.Content is UIElement element) + { + return CreatePeerForElement(element); + } + + return base.GetLabeledByCore(); + } + + protected override string GetNameCore() + { + var result = base.GetNameCore() ?? string.Empty; + + if (result == string.Empty) + { + result = AutomationProperties.GetName(_owner); + } + + if (result == string.Empty && _owner.Content is DependencyObject d) + { + result = AutomationProperties.GetName(d); + } + + if (result == string.Empty && _owner.Content is string s) + { + result = s; + } + + return result; + } +} From 7969f43a3a8536bd67506fcf64bf8313f081fe6f Mon Sep 17 00:00:00 2001 From: Difegue Date: Tue, 24 Sep 2024 17:24:21 +0200 Subject: [PATCH 2/4] Fix screen readers trying to read IconElement in some cases --- src/Wpf.Ui/Controls/Button/Button.xaml | 12 ++++++++ src/Wpf.Ui/Controls/Menu/MenuItem.xaml | 42 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/Wpf.Ui/Controls/Button/Button.xaml b/src/Wpf.Ui/Controls/Button/Button.xaml index 7214b0114..83a1b0c7e 100644 --- a/src/Wpf.Ui/Controls/Button/Button.xaml +++ b/src/Wpf.Ui/Controls/Button/Button.xaml @@ -195,6 +195,18 @@ VerticalAlignment="Center" Content="{TemplateBinding Content}" TextElement.Foreground="{TemplateBinding Foreground}" /> + + + diff --git a/src/Wpf.Ui/Controls/Menu/MenuItem.xaml b/src/Wpf.Ui/Controls/Menu/MenuItem.xaml index 61a176453..da8cb5f73 100644 --- a/src/Wpf.Ui/Controls/Menu/MenuItem.xaml +++ b/src/Wpf.Ui/Controls/Menu/MenuItem.xaml @@ -74,6 +74,16 @@ ContentSource="Header" RecognizesAccessKey="True" TextElement.Foreground="{TemplateBinding Foreground}" /> + + + + @@ -264,6 +284,17 @@ FontSize="11" Foreground="{DynamicResource TextFillColorDisabledBrush}" Text="{TemplateBinding InputGestureText}" /> + + + @@ -334,6 +365,17 @@ FontSize="{TemplateBinding FontSize}" Symbol="ChevronRight20" /> + + + From 4491fa1fdde439e840beb1281b19b521947eb5b4 Mon Sep 17 00:00:00 2001 From: Difegue Date: Tue, 24 Sep 2024 18:16:28 +0200 Subject: [PATCH 3/4] Add narration support to NavigationViewItem --- .../NavigationView/NavigationViewItem.cs | 6 ++ .../NavigationViewItemAutomationPeer.cs | 72 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/Wpf.Ui/Controls/NavigationView/NavigationViewItemAutomationPeer.cs diff --git a/src/Wpf.Ui/Controls/NavigationView/NavigationViewItem.cs b/src/Wpf.Ui/Controls/NavigationView/NavigationViewItem.cs index e85c80677..3eaa41b40 100644 --- a/src/Wpf.Ui/Controls/NavigationView/NavigationViewItem.cs +++ b/src/Wpf.Ui/Controls/NavigationView/NavigationViewItem.cs @@ -8,6 +8,7 @@ using System.Collections; using System.Collections.ObjectModel; using System.Collections.Specialized; +using System.Windows.Automation.Peers; using System.Windows.Controls; using System.Windows.Input; @@ -438,6 +439,11 @@ protected override void OnMouseDown(MouseButtonEventArgs e) e.Handled = true; } + protected override AutomationPeer OnCreateAutomationPeer() + { + return new NavigationViewItemAutomationPeer(this); + } + private void OnMenuItems_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { SetValue(HasMenuItemsPropertyKey, MenuItems.Count > 0); diff --git a/src/Wpf.Ui/Controls/NavigationView/NavigationViewItemAutomationPeer.cs b/src/Wpf.Ui/Controls/NavigationView/NavigationViewItemAutomationPeer.cs new file mode 100644 index 000000000..7a78729c5 --- /dev/null +++ b/src/Wpf.Ui/Controls/NavigationView/NavigationViewItemAutomationPeer.cs @@ -0,0 +1,72 @@ +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. +// Copyright (C) Leszek Pomianowski and WPF UI Contributors. +// All Rights Reserved. + +using System.Windows.Automation; +using System.Windows.Automation.Peers; + +namespace Wpf.Ui.Controls; + +internal class NavigationViewItemAutomationPeer : FrameworkElementAutomationPeer +{ + private readonly NavigationViewItem _owner; + + public NavigationViewItemAutomationPeer(NavigationViewItem owner) + : base(owner) + { + _owner = owner; + } + + protected override string GetClassNameCore() + { + return "NavigationItem"; + } + + protected override AutomationControlType GetAutomationControlTypeCore() + { + return AutomationControlType.TabItem; + } + + public override object GetPattern(PatternInterface patternInterface) + { + if (patternInterface == PatternInterface.ItemContainer) + { + return this; + } + + return base.GetPattern(patternInterface); + } + + protected override AutomationPeer GetLabeledByCore() + { + if (_owner.Content is UIElement element) + { + return CreatePeerForElement(element); + } + + return base.GetLabeledByCore(); + } + + protected override string GetNameCore() + { + var result = base.GetNameCore() ?? string.Empty; + + if (result == string.Empty) + { + result = AutomationProperties.GetName(_owner); + } + + if (result == string.Empty && _owner.Content is DependencyObject d) + { + result = AutomationProperties.GetName(d); + } + + if (result == string.Empty && _owner.Content is string s) + { + result = s; + } + + return result; + } +} From a6ae4f1837e3e2656acdcb823b5eb23b03430293 Mon Sep 17 00:00:00 2001 From: Difegue Date: Wed, 2 Oct 2024 17:35:23 +0200 Subject: [PATCH 4/4] Button workaround appears to be unnecessary now --- src/Wpf.Ui/Controls/Button/Button.xaml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Wpf.Ui/Controls/Button/Button.xaml b/src/Wpf.Ui/Controls/Button/Button.xaml index 83a1b0c7e..7214b0114 100644 --- a/src/Wpf.Ui/Controls/Button/Button.xaml +++ b/src/Wpf.Ui/Controls/Button/Button.xaml @@ -195,18 +195,6 @@ VerticalAlignment="Center" Content="{TemplateBinding Content}" TextElement.Foreground="{TemplateBinding Foreground}" /> - - -