From 5405e97602d799481ec3fcef764ccbe240868bba Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Thu, 1 Dec 2022 17:49:54 +0100 Subject: [PATCH] feat: Address Rule CA1304 --- src/Avalonia.Base/Input/AccessKeyHandler.cs | 7 ++++--- src/Avalonia.Base/Input/KeyGesture.cs | 3 ++- src/Avalonia.Controls/MenuItemAccessKeyHandler.cs | 10 +++++----- src/Avalonia.Remote.Protocol/MetsysBson.cs | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Avalonia.Base/Input/AccessKeyHandler.cs b/src/Avalonia.Base/Input/AccessKeyHandler.cs index 5695d27153..ec8da92ad2 100644 --- a/src/Avalonia.Base/Input/AccessKeyHandler.cs +++ b/src/Avalonia.Base/Input/AccessKeyHandler.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using Avalonia.Interactivity; using Avalonia.VisualTree; @@ -113,7 +114,7 @@ namespace Avalonia.Input _registered.Remove(existing); } - _registered.Add(Tuple.Create(accessKey.ToString().ToUpper(), element)); + _registered.Add(Tuple.Create(accessKey.ToString(CultureInfo.InvariantCulture).ToUpperInvariant(), element)); } /// @@ -180,9 +181,9 @@ namespace Avalonia.Input { // If any other key is pressed with the Alt key held down, or the main menu is open, // find all controls who have registered that access key. - var text = e.Key.ToString().ToUpper(); + var text = e.Key.ToString(); var matches = _registered - .Where(x => x.Item1 == text && ((Visual)x.Item2).IsEffectivelyVisible) + .Where(x => string.Equals( x.Item1 , text, StringComparison.OrdinalIgnoreCase) && ((Visual)x.Item2).IsEffectivelyVisible) .Select(x => x.Item2); // If the menu is open, only match controls in the menu's visual tree. diff --git a/src/Avalonia.Base/Input/KeyGesture.cs b/src/Avalonia.Base/Input/KeyGesture.cs index 1a6372d346..56edc76f22 100644 --- a/src/Avalonia.Base/Input/KeyGesture.cs +++ b/src/Avalonia.Base/Input/KeyGesture.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Text; using Avalonia.Utilities; @@ -143,7 +144,7 @@ namespace Avalonia.Input // TODO: Move that to external key parser private static Key ParseKey(string key) { - if (s_keySynonyms.TryGetValue(key.ToLower(), out Key rv)) + if (s_keySynonyms.TryGetValue(key.ToLower(CultureInfo.InvariantCulture), out Key rv)) return rv; return EnumHelper.Parse(key, true); diff --git a/src/Avalonia.Controls/MenuItemAccessKeyHandler.cs b/src/Avalonia.Controls/MenuItemAccessKeyHandler.cs index 4cc6a20082..5f50ce64df 100644 --- a/src/Avalonia.Controls/MenuItemAccessKeyHandler.cs +++ b/src/Avalonia.Controls/MenuItemAccessKeyHandler.cs @@ -14,7 +14,7 @@ namespace Avalonia.Controls /// /// The registered access keys. /// - private readonly List> _registered = new List>(); + private readonly List<(string AccessKey, IInputElement Element)> _registered = new(); /// /// The window to which the handler belongs. @@ -59,12 +59,12 @@ namespace Avalonia.Controls { var existing = _registered.FirstOrDefault(x => x.Item2 == element); - if (existing != null) + if (existing != default) { _registered.Remove(existing); } - _registered.Add(Tuple.Create(accessKey.ToString().ToUpper(), element)); + _registered.Add((accessKey.ToString().ToUpperInvariant(), element)); } /// @@ -88,9 +88,9 @@ namespace Avalonia.Controls { if (!string.IsNullOrWhiteSpace(e.Text)) { - var text = e.Text.ToUpper(); + var text = e.Text.ToUpperInvariant(); var focus = _registered - .FirstOrDefault(x => x.Item1 == text && x.Item2.IsEffectivelyVisible)?.Item2; + .FirstOrDefault(x => x.Element.IsEffectivelyVisible && x.Item1 == text).Element; focus?.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent)); diff --git a/src/Avalonia.Remote.Protocol/MetsysBson.cs b/src/Avalonia.Remote.Protocol/MetsysBson.cs index 6abece6bf3..8aaab933e9 100644 --- a/src/Avalonia.Remote.Protocol/MetsysBson.cs +++ b/src/Avalonia.Remote.Protocol/MetsysBson.cs @@ -562,7 +562,7 @@ namespace Metsys.Bson { if (_string == null && Value != null) { - _string = BitConverter.ToString(Value).Replace("-", string.Empty).ToLower(); + _string = BitConverter.ToString(Value).Replace("-", string.Empty).ToLowerInvariant(); } return _string;