From 6957d5343ca03b882512e8d3438c06b47c4bb60e Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Wed, 29 May 2024 09:09:23 +0200 Subject: [PATCH 1/3] AccessibilityExtensions: Add missing braces --- .../Extensions/AccessibilityExtensions.cs | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Controls/src/Core/Platform/Windows/Extensions/AccessibilityExtensions.cs b/src/Controls/src/Core/Platform/Windows/Extensions/AccessibilityExtensions.cs index 41ea4aa7073d..afe9d8616c00 100644 --- a/src/Controls/src/Core/Platform/Windows/Extensions/AccessibilityExtensions.cs +++ b/src/Controls/src/Core/Platform/Windows/Extensions/AccessibilityExtensions.cs @@ -15,19 +15,27 @@ public static void SetAutomationPropertiesAutomationId(this FrameworkElement Con public static string SetAutomationPropertiesName(this FrameworkElement Control, Element Element, string _defaultAutomationPropertiesName = null) { if (Element == null) + { return _defaultAutomationPropertiesName; + } if (_defaultAutomationPropertiesName == null) + { _defaultAutomationPropertiesName = (string)Control.GetValue(NativeAutomationProperties.NameProperty); + } #pragma warning disable CS0618 // Type or member is obsolete var elemValue = (string)Element.GetValue(AutomationProperties.NameProperty); #pragma warning restore CS0618 // Type or member is obsolete if (!string.IsNullOrWhiteSpace(elemValue)) + { Control.SetValue(NativeAutomationProperties.NameProperty, elemValue); + } else + { Control.SetValue(NativeAutomationProperties.NameProperty, _defaultAutomationPropertiesName); + } return _defaultAutomationPropertiesName; } @@ -35,19 +43,27 @@ public static string SetAutomationPropertiesName(this FrameworkElement Control, public static AccessibilityView? SetAutomationPropertiesAccessibilityView(this FrameworkElement Control, Element Element, AccessibilityView? _defaultAutomationPropertiesAccessibilityView = null) { if (Element == null) + { return _defaultAutomationPropertiesAccessibilityView; + } if (!_defaultAutomationPropertiesAccessibilityView.HasValue) + { _defaultAutomationPropertiesAccessibilityView = (AccessibilityView)Control.GetValue(NativeAutomationProperties.AccessibilityViewProperty); + } var newValue = _defaultAutomationPropertiesAccessibilityView; var elemValue = (bool?)Element.GetValue(AutomationProperties.IsInAccessibleTreeProperty); if (elemValue == true) + { newValue = AccessibilityView.Content; + } else if (elemValue == false) + { newValue = AccessibilityView.Raw; + } Control.SetValue(NativeAutomationProperties.AccessibilityViewProperty, newValue); @@ -57,19 +73,27 @@ public static string SetAutomationPropertiesName(this FrameworkElement Control, public static string SetAutomationPropertiesHelpText(this FrameworkElement Control, Element Element, string _defaultAutomationPropertiesHelpText = null) { if (Element == null) + { return _defaultAutomationPropertiesHelpText; + } if (_defaultAutomationPropertiesHelpText == null) + { _defaultAutomationPropertiesHelpText = (string)Control.GetValue(NativeAutomationProperties.HelpTextProperty); + } #pragma warning disable CS0618 // Type or member is obsolete var elemValue = (string)Element.GetValue(AutomationProperties.HelpTextProperty); #pragma warning restore CS0618 // Type or member is obsolete if (!string.IsNullOrWhiteSpace(elemValue)) + { Control.SetValue(NativeAutomationProperties.HelpTextProperty, elemValue); + } else + { Control.SetValue(NativeAutomationProperties.HelpTextProperty, _defaultAutomationPropertiesHelpText); + } return _defaultAutomationPropertiesHelpText; } @@ -81,28 +105,38 @@ public static UIElement SetAutomationPropertiesLabeledBy( UIElement _defaultAutomationPropertiesLabeledBy = null) { if (Element == null) + { return _defaultAutomationPropertiesLabeledBy; + } // TODO Maui: this is a bit of a hack because Elements // currently don't implement IView but they should mauiContext ??= (Element as IView)?.Handler?.MauiContext; if (_defaultAutomationPropertiesLabeledBy == null) + { _defaultAutomationPropertiesLabeledBy = (UIElement)Control.GetValue(NativeAutomationProperties.LabeledByProperty); + } #pragma warning disable CS0618 // Type or member is obsolete var elemValue = (VisualElement)Element.GetValue(AutomationProperties.LabeledByProperty); #pragma warning restore CS0618 // Type or member is obsolete FrameworkElement nativeElement = null; if (mauiContext != null) + { nativeElement = (elemValue as IView)?.ToHandler(mauiContext)?.PlatformView as FrameworkElement; + } if (nativeElement != null) + { #pragma warning disable CS0618 // Type or member is obsolete Control.SetValue(AutomationProperties.LabeledByProperty, nativeElement); + } #pragma warning restore CS0618 // Type or member is obsolete else + { Control.SetValue(NativeAutomationProperties.LabeledByProperty, _defaultAutomationPropertiesLabeledBy); + } return _defaultAutomationPropertiesLabeledBy; } @@ -112,7 +146,9 @@ public static UIElement SetAutomationPropertiesLabeledBy( public static void SetBackButtonTitle(this PageControl Control, Element Element) { if (Element == null) + { return; + } var elemValue = ConcatenateNameAndHint(Element); @@ -130,11 +166,13 @@ static string ConcatenateNameAndHint(Element Element) #pragma warning restore CS0618 // Type or member is obsolete if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(hint)) + { separator = ""; - + } else + { separator = ". "; - + } return string.Join(separator, name, hint); From abf259c2b9f60f2560788675cfd98a62494af838 Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Wed, 29 May 2024 09:42:05 +0200 Subject: [PATCH 2/3] AccessibilityExtensions: Enable nullability and improve performance --- .../Extensions/AccessibilityExtensions.cs | 88 ++++++++++--------- 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/src/Controls/src/Core/Platform/Windows/Extensions/AccessibilityExtensions.cs b/src/Controls/src/Core/Platform/Windows/Extensions/AccessibilityExtensions.cs index afe9d8616c00..b32154f1a34e 100644 --- a/src/Controls/src/Core/Platform/Windows/Extensions/AccessibilityExtensions.cs +++ b/src/Controls/src/Core/Platform/Windows/Extensions/AccessibilityExtensions.cs @@ -1,4 +1,3 @@ -#nullable disable using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Automation.Peers; using NativeAutomationProperties = Microsoft.UI.Xaml.Automation.AutomationProperties; @@ -7,32 +6,32 @@ namespace Microsoft.Maui.Controls.Platform { public static class AccessibilityExtensions { - public static void SetAutomationPropertiesAutomationId(this FrameworkElement Control, string id) + public static void SetAutomationPropertiesAutomationId(this FrameworkElement Control, string? id) { Control.SetValue(NativeAutomationProperties.AutomationIdProperty, id); } - public static string SetAutomationPropertiesName(this FrameworkElement Control, Element Element, string _defaultAutomationPropertiesName = null) + public static string? SetAutomationPropertiesName(this FrameworkElement Control, Element? Element, string? _defaultAutomationPropertiesName = null) { - if (Element == null) + if (Element is null) { return _defaultAutomationPropertiesName; } - if (_defaultAutomationPropertiesName == null) + string? currentValue = null; + + if (_defaultAutomationPropertiesName is null) { - _defaultAutomationPropertiesName = (string)Control.GetValue(NativeAutomationProperties.NameProperty); + _defaultAutomationPropertiesName = currentValue = (string)Control.GetValue(NativeAutomationProperties.NameProperty); } #pragma warning disable CS0618 // Type or member is obsolete var elemValue = (string)Element.GetValue(AutomationProperties.NameProperty); #pragma warning restore CS0618 // Type or member is obsolete - if (!string.IsNullOrWhiteSpace(elemValue)) - { - Control.SetValue(NativeAutomationProperties.NameProperty, elemValue); - } - else + string newValue = !string.IsNullOrWhiteSpace(elemValue) ? elemValue : _defaultAutomationPropertiesName; + + if (currentValue is null || currentValue != newValue) { Control.SetValue(NativeAutomationProperties.NameProperty, _defaultAutomationPropertiesName); } @@ -40,16 +39,18 @@ public static string SetAutomationPropertiesName(this FrameworkElement Control, return _defaultAutomationPropertiesName; } - public static AccessibilityView? SetAutomationPropertiesAccessibilityView(this FrameworkElement Control, Element Element, AccessibilityView? _defaultAutomationPropertiesAccessibilityView = null) + public static AccessibilityView? SetAutomationPropertiesAccessibilityView(this FrameworkElement Control, Element? Element, AccessibilityView? _defaultAutomationPropertiesAccessibilityView = null) { - if (Element == null) + if (Element is null) { return _defaultAutomationPropertiesAccessibilityView; } + AccessibilityView? currentValue = null; + if (!_defaultAutomationPropertiesAccessibilityView.HasValue) { - _defaultAutomationPropertiesAccessibilityView = (AccessibilityView)Control.GetValue(NativeAutomationProperties.AccessibilityViewProperty); + _defaultAutomationPropertiesAccessibilityView = currentValue = (AccessibilityView)Control.GetValue(NativeAutomationProperties.AccessibilityViewProperty); } var newValue = _defaultAutomationPropertiesAccessibilityView; @@ -65,32 +66,35 @@ public static string SetAutomationPropertiesName(this FrameworkElement Control, newValue = AccessibilityView.Raw; } - Control.SetValue(NativeAutomationProperties.AccessibilityViewProperty, newValue); + if (currentValue is null || currentValue != newValue) + { + Control.SetValue(NativeAutomationProperties.AccessibilityViewProperty, newValue); + } return _defaultAutomationPropertiesAccessibilityView; } - public static string SetAutomationPropertiesHelpText(this FrameworkElement Control, Element Element, string _defaultAutomationPropertiesHelpText = null) + public static string? SetAutomationPropertiesHelpText(this FrameworkElement Control, Element? Element, string? _defaultAutomationPropertiesHelpText = null) { if (Element == null) { return _defaultAutomationPropertiesHelpText; } - if (_defaultAutomationPropertiesHelpText == null) + string? currentValue = null; + + if (_defaultAutomationPropertiesHelpText is null) { - _defaultAutomationPropertiesHelpText = (string)Control.GetValue(NativeAutomationProperties.HelpTextProperty); + _defaultAutomationPropertiesHelpText = currentValue = (string)Control.GetValue(NativeAutomationProperties.HelpTextProperty); } #pragma warning disable CS0618 // Type or member is obsolete var elemValue = (string)Element.GetValue(AutomationProperties.HelpTextProperty); #pragma warning restore CS0618 // Type or member is obsolete - if (!string.IsNullOrWhiteSpace(elemValue)) - { - Control.SetValue(NativeAutomationProperties.HelpTextProperty, elemValue); - } - else + string newValue = !string.IsNullOrWhiteSpace(elemValue) ? elemValue : _defaultAutomationPropertiesHelpText; + + if (currentValue is null || newValue != currentValue) { Control.SetValue(NativeAutomationProperties.HelpTextProperty, _defaultAutomationPropertiesHelpText); } @@ -98,13 +102,13 @@ public static string SetAutomationPropertiesHelpText(this FrameworkElement Contr return _defaultAutomationPropertiesHelpText; } - public static UIElement SetAutomationPropertiesLabeledBy( + public static UIElement? SetAutomationPropertiesLabeledBy( this FrameworkElement Control, - Element Element, - IMauiContext mauiContext, - UIElement _defaultAutomationPropertiesLabeledBy = null) + Element? Element, + IMauiContext? mauiContext, + UIElement? _defaultAutomationPropertiesLabeledBy = null) { - if (Element == null) + if (Element is null) { return _defaultAutomationPropertiesLabeledBy; } @@ -113,29 +117,29 @@ public static UIElement SetAutomationPropertiesLabeledBy( // currently don't implement IView but they should mauiContext ??= (Element as IView)?.Handler?.MauiContext; - if (_defaultAutomationPropertiesLabeledBy == null) + UIElement? currentValue = null; + + if (_defaultAutomationPropertiesLabeledBy is null) { - _defaultAutomationPropertiesLabeledBy = (UIElement)Control.GetValue(NativeAutomationProperties.LabeledByProperty); + _defaultAutomationPropertiesLabeledBy = currentValue = (UIElement)Control.GetValue(NativeAutomationProperties.LabeledByProperty); } #pragma warning disable CS0618 // Type or member is obsolete var elemValue = (VisualElement)Element.GetValue(AutomationProperties.LabeledByProperty); #pragma warning restore CS0618 // Type or member is obsolete - FrameworkElement nativeElement = null; + FrameworkElement? nativeElement = null; if (mauiContext != null) { nativeElement = (elemValue as IView)?.ToHandler(mauiContext)?.PlatformView as FrameworkElement; } - if (nativeElement != null) + UIElement? newValue = nativeElement is not null ? nativeElement : _defaultAutomationPropertiesLabeledBy; + + if (currentValue is null || newValue != currentValue) { #pragma warning disable CS0618 // Type or member is obsolete - Control.SetValue(AutomationProperties.LabeledByProperty, nativeElement); - } + Control.SetValue(AutomationProperties.LabeledByProperty, newValue); #pragma warning restore CS0618 // Type or member is obsolete - else - { - Control.SetValue(NativeAutomationProperties.LabeledByProperty, _defaultAutomationPropertiesLabeledBy); } return _defaultAutomationPropertiesLabeledBy; @@ -143,9 +147,9 @@ public static UIElement SetAutomationPropertiesLabeledBy( // TODO MAUI: This is not having any effect on anything I've tested yet. See if we need it // after we test the FP and NP w/ back button explicitly enabled. - public static void SetBackButtonTitle(this PageControl Control, Element Element) + public static void SetBackButtonTitle(this PageControl Control, Element? Element) { - if (Element == null) + if (Element is null) { return; } @@ -180,9 +184,9 @@ static string ConcatenateNameAndHint(Element Element) public static void SetAutomationProperties( this FrameworkElement frameworkElement, - Element element, - IMauiContext mauiContext, - string defaultName = null) + Element? element, + IMauiContext? mauiContext, + string? defaultName = null) { frameworkElement.SetAutomationPropertiesAutomationId(element?.AutomationId); frameworkElement.SetAutomationPropertiesName(element, defaultName); From 7fdc8829622c8cd40ef82360ebd48c690e6b84f9 Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Wed, 29 May 2024 10:43:47 +0200 Subject: [PATCH 3/3] Update shipped & unshipped API --- .../src/Core/PublicAPI/net-windows/PublicAPI.Shipped.txt | 7 ------- .../src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt | 7 +++++++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Shipped.txt b/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Shipped.txt index 8ceed62a1748..0dbfa8b75f24 100644 --- a/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Shipped.txt +++ b/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Shipped.txt @@ -2534,13 +2534,6 @@ ~static Microsoft.Maui.Controls.OnPlatform.implicit operator T(Microsoft.Maui.Controls.OnPlatform onPlatform) -> T ~static Microsoft.Maui.Controls.PanGestureRecognizer.CurrentId.get -> Microsoft.Maui.Controls.Internals.AutoId ~static Microsoft.Maui.Controls.Picker.ControlsPickerMapper -> Microsoft.Maui.IPropertyMapper -~static Microsoft.Maui.Controls.Platform.AccessibilityExtensions.SetAutomationProperties(this Microsoft.UI.Xaml.FrameworkElement frameworkElement, Microsoft.Maui.Controls.Element element, Microsoft.Maui.IMauiContext mauiContext, string defaultName = null) -> void -~static Microsoft.Maui.Controls.Platform.AccessibilityExtensions.SetAutomationPropertiesAccessibilityView(this Microsoft.UI.Xaml.FrameworkElement Control, Microsoft.Maui.Controls.Element Element, Microsoft.UI.Xaml.Automation.Peers.AccessibilityView? _defaultAutomationPropertiesAccessibilityView = null) -> Microsoft.UI.Xaml.Automation.Peers.AccessibilityView? -~static Microsoft.Maui.Controls.Platform.AccessibilityExtensions.SetAutomationPropertiesAutomationId(this Microsoft.UI.Xaml.FrameworkElement Control, string id) -> void -~static Microsoft.Maui.Controls.Platform.AccessibilityExtensions.SetAutomationPropertiesHelpText(this Microsoft.UI.Xaml.FrameworkElement Control, Microsoft.Maui.Controls.Element Element, string _defaultAutomationPropertiesHelpText = null) -> string -~static Microsoft.Maui.Controls.Platform.AccessibilityExtensions.SetAutomationPropertiesLabeledBy(this Microsoft.UI.Xaml.FrameworkElement Control, Microsoft.Maui.Controls.Element Element, Microsoft.Maui.IMauiContext mauiContext, Microsoft.UI.Xaml.UIElement _defaultAutomationPropertiesLabeledBy = null) -> Microsoft.UI.Xaml.UIElement -~static Microsoft.Maui.Controls.Platform.AccessibilityExtensions.SetAutomationPropertiesName(this Microsoft.UI.Xaml.FrameworkElement Control, Microsoft.Maui.Controls.Element Element, string _defaultAutomationPropertiesName = null) -> string -~static Microsoft.Maui.Controls.Platform.AccessibilityExtensions.SetBackButtonTitle(this Microsoft.Maui.Controls.Platform.PageControl Control, Microsoft.Maui.Controls.Element Element) -> void ~static Microsoft.Maui.Controls.Platform.AccessKeyHelper.UpdateAccessKey(Microsoft.UI.Xaml.FrameworkElement control, Microsoft.Maui.Controls.VisualElement element) -> void ~static Microsoft.Maui.Controls.Platform.BrushExtensions.ToBrush(this Microsoft.Maui.Controls.Brush brush) -> Microsoft.UI.Xaml.Media.Brush ~static Microsoft.Maui.Controls.Platform.FontExtensions.ApplyFont(this Microsoft.UI.Xaml.Controls.Control self, Microsoft.Maui.Font font, Microsoft.Maui.IFontManager fontManager) -> void diff --git a/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt index b5c3cd1ee776..3331c8dc9e4e 100644 --- a/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt @@ -68,6 +68,13 @@ Microsoft.Maui.Controls.PointerGestureRecognizer.PointerReleasedCommand.get -> S Microsoft.Maui.Controls.PointerGestureRecognizer.PointerReleasedCommand.set -> void Microsoft.Maui.Controls.PointerGestureRecognizer.PointerReleasedCommandParameter.get -> object! Microsoft.Maui.Controls.PointerGestureRecognizer.PointerReleasedCommandParameter.set -> void +static Microsoft.Maui.Controls.Platform.AccessibilityExtensions.SetAutomationProperties(this Microsoft.UI.Xaml.FrameworkElement! frameworkElement, Microsoft.Maui.Controls.Element? element, Microsoft.Maui.IMauiContext? mauiContext, string? defaultName = null) -> void +static Microsoft.Maui.Controls.Platform.AccessibilityExtensions.SetAutomationPropertiesAccessibilityView(this Microsoft.UI.Xaml.FrameworkElement! Control, Microsoft.Maui.Controls.Element? Element, Microsoft.UI.Xaml.Automation.Peers.AccessibilityView? _defaultAutomationPropertiesAccessibilityView = null) -> Microsoft.UI.Xaml.Automation.Peers.AccessibilityView? +static Microsoft.Maui.Controls.Platform.AccessibilityExtensions.SetAutomationPropertiesAutomationId(this Microsoft.UI.Xaml.FrameworkElement! Control, string? id) -> void +static Microsoft.Maui.Controls.Platform.AccessibilityExtensions.SetAutomationPropertiesHelpText(this Microsoft.UI.Xaml.FrameworkElement! Control, Microsoft.Maui.Controls.Element? Element, string? _defaultAutomationPropertiesHelpText = null) -> string? +static Microsoft.Maui.Controls.Platform.AccessibilityExtensions.SetAutomationPropertiesLabeledBy(this Microsoft.UI.Xaml.FrameworkElement! Control, Microsoft.Maui.Controls.Element? Element, Microsoft.Maui.IMauiContext? mauiContext, Microsoft.UI.Xaml.UIElement? _defaultAutomationPropertiesLabeledBy = null) -> Microsoft.UI.Xaml.UIElement? +static Microsoft.Maui.Controls.Platform.AccessibilityExtensions.SetAutomationPropertiesName(this Microsoft.UI.Xaml.FrameworkElement! Control, Microsoft.Maui.Controls.Element? Element, string? _defaultAutomationPropertiesName = null) -> string? +static Microsoft.Maui.Controls.Platform.AccessibilityExtensions.SetBackButtonTitle(this Microsoft.Maui.Controls.Platform.PageControl! Control, Microsoft.Maui.Controls.Element? Element) -> void static readonly Microsoft.Maui.Controls.KeyboardAccelerator.KeyProperty -> Microsoft.Maui.Controls.BindableProperty! static readonly Microsoft.Maui.Controls.KeyboardAccelerator.ModifiersProperty -> Microsoft.Maui.Controls.BindableProperty! static readonly Microsoft.Maui.Controls.DragGestureRecognizer.CanDragProperty -> Microsoft.Maui.Controls.BindableProperty!