diff --git a/.editorconfig b/.editorconfig index 8869bb519d..41eed9f9d6 100644 --- a/.editorconfig +++ b/.editorconfig @@ -137,6 +137,8 @@ space_within_single_line_array_initializer_braces = true #Net Analyzer dotnet_analyzer_diagnostic.category-Performance.severity = none #error - Uncomment when all violations are fixed. +# CA1304: Specify CultureInfo +dotnet_diagnostic.CA1304.severity = warning # CA1802: Use literals where appropriate dotnet_diagnostic.CA1802.severity = warning # CA1820: Test for empty strings using string length diff --git a/src/Avalonia.Base/Input/AccessKeyHandler.cs b/src/Avalonia.Base/Input/AccessKeyHandler.cs index 5695d27153..59c66ed505 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; @@ -23,7 +24,7 @@ namespace Avalonia.Input /// /// 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. @@ -108,12 +109,12 @@ namespace Avalonia.Input { 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)); } /// @@ -143,7 +144,7 @@ namespace Avalonia.Input { // TODO: Use FocusScopes to store the current element and restore it when context menu is closed. // Save currently focused input element. - _restoreFocusElement = FocusManager.Instance?.Current; + _restoreFocusElement = FocusManager.Instance?.Current; // When Alt is pressed without a main menu, or with a closed main menu, show // access key markers in the window (i.e. "_File"). @@ -180,10 +181,11 @@ 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) - .Select(x => x.Item2); + .Where(x => string.Equals(x.AccessKey, text, StringComparison.OrdinalIgnoreCase) + && x.Element.IsEffectivelyVisible) + .Select(x => x.Element); // If the menu is open, only match controls in the menu's visual tree. if (menuIsOpen) @@ -194,7 +196,7 @@ namespace Avalonia.Input var match = matches.FirstOrDefault(); // If there was a match, raise the AccessKeyPressed event on it. - if (match != null) + if (match is not null) { match.RaiseEvent(new RoutedEventArgs(AccessKeyPressedEvent)); e.Handled = true; diff --git a/src/Avalonia.Base/Input/KeyGesture.cs b/src/Avalonia.Base/Input/KeyGesture.cs index b08f5d0ba5..c6618fd550 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..381f1799d4 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,10 @@ namespace Avalonia.Controls { if (!string.IsNullOrWhiteSpace(e.Text)) { - var text = e.Text.ToUpper(); + var text = e.Text; var focus = _registered - .FirstOrDefault(x => x.Item1 == text && x.Item2.IsEffectivelyVisible)?.Item2; + .FirstOrDefault(x => string.Equals(x.AccessKey, text, StringComparison.OrdinalIgnoreCase) + && x.Element.IsEffectivelyVisible).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;