Browse Source

Hackfix for menu access keys.

Added a very hacky fix for #7777: if we key a keydown in an open `MenuItem` then manually pass the access key to the popup's `MenuItemAccessKeyHandler`, shortcutting the standard access key handling.

Fixes #7777
fixes/7777-menu-accelerator
Steven Kirk 4 years ago
parent
commit
0621e9fb3b
  1. 9
      src/Avalonia.Controls/MenuItem.cs
  2. 2
      src/Avalonia.Controls/MenuItemAccessKeyHandler.cs

9
src/Avalonia.Controls/MenuItem.cs

@ -433,7 +433,14 @@ namespace Avalonia.Controls
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
// Don't handle here: let event bubble up to menu.
// HACK: Fix for #7777 - Menu keyboard focus incorrect when handling menu mnemonics.
// If we implement something like UWP's access key scopes then this code should be removed.
if (IsSubMenuOpen &&
_popup?.Host is IInputRoot popupRoot &&
popupRoot.AccessKeyHandler is MenuItemAccessKeyHandler handler)
{
handler.OnTextInput(this, new TextInputEventArgs { Text = e.Key.ToString() });
}
}
/// <inheritdoc/>

2
src/Avalonia.Controls/MenuItemAccessKeyHandler.cs

@ -84,7 +84,7 @@ namespace Avalonia.Controls
/// </summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event args.</param>
protected virtual void OnTextInput(object? sender, TextInputEventArgs e)
protected internal virtual void OnTextInput(object? sender, TextInputEventArgs e)
{
if (!string.IsNullOrWhiteSpace(e.Text))
{

Loading…
Cancel
Save