Browse Source

fix: Address review

pull/9588/head
Giuseppe Lippolis 3 years ago
parent
commit
44c9f2578a
  1. 15
      src/Avalonia.Base/Input/AccessKeyHandler.cs
  2. 5
      src/Avalonia.Controls/MenuItemAccessKeyHandler.cs

15
src/Avalonia.Base/Input/AccessKeyHandler.cs

@ -24,7 +24,7 @@ namespace Avalonia.Input
/// <summary>
/// The registered access keys.
/// </summary>
private readonly List<Tuple<string, IInputElement>> _registered = new List<Tuple<string, IInputElement>>();
private readonly List<(string AccessKey, IInputElement Element)> _registered = new();
/// <summary>
/// The window to which the handler belongs.
@ -109,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(CultureInfo.InvariantCulture).ToUpperInvariant(), element));
_registered.Add((accessKey.ToString().ToUpperInvariant(), element));
}
/// <summary>
@ -144,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").
@ -183,8 +183,9 @@ namespace Avalonia.Input
// find all controls who have registered that access key.
var text = e.Key.ToString();
var matches = _registered
.Where(x => string.Equals( x.Item1 , text, StringComparison.OrdinalIgnoreCase) && ((Visual)x.Item2).IsEffectivelyVisible)
.Select(x => x.Item2);
.Where(x => x.Element.IsEffectivelyVisible
&& string.Equals(x.AccessKey , text, StringComparison.OrdinalIgnoreCase) )
.Select(x => x.Element);
// If the menu is open, only match controls in the menu's visual tree.
if (menuIsOpen)
@ -195,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;

5
src/Avalonia.Controls/MenuItemAccessKeyHandler.cs

@ -88,9 +88,10 @@ namespace Avalonia.Controls
{
if (!string.IsNullOrWhiteSpace(e.Text))
{
var text = e.Text.ToUpperInvariant();
var text = e.Text;
var focus = _registered
.FirstOrDefault(x => x.Element.IsEffectivelyVisible && x.Item1 == text).Element;
.FirstOrDefault(x => x.Element.IsEffectivelyVisible
&& string.Equals(x.AccessKey, text, StringComparison.OrdinalIgnoreCase)).Element;
focus?.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));

Loading…
Cancel
Save