Browse Source

Show shortcut text on main-menu items via InputGesture

Assisted-by: Claude:claude-opus-4-7:Claude Code

Assisted-by: Claude:claude-opus-4-7:Claude Code
pull/3755/head
Siegfried Pammer 2 months ago
parent
commit
0a9ba583c9
  1. 24
      ILSpy.Tests/MainWindow/MainWindowTests.cs
  2. 11
      ILSpy/Views/MainMenu.axaml.cs

24
ILSpy.Tests/MainWindow/MainWindowTests.cs

@ -20,7 +20,10 @@ using System.Linq; @@ -20,7 +20,10 @@ using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using Avalonia.Controls;
using Avalonia.Headless.NUnit;
using Avalonia.Input;
using Avalonia.VisualTree;
using AwesomeAssertions;
@ -347,6 +350,27 @@ public class MainWindowTests @@ -347,6 +350,27 @@ public class MainWindowTests
savedFiles.Should().Contain(asm.FileName, "manually loaded assembly {0} must be persisted", asm.FileName);
}
[AvaloniaTest]
public async Task Main_Menu_Items_Display_Input_Gestures()
{
var window = AppComposition.Current.GetExport<MainWindow>();
window.Show();
var menu = window.GetVisualDescendants().OfType<Menu>().First();
await Waiters.WaitForAsync(() =>
menu.Items.OfType<MenuItem>().Any(m => (string?)m.Tag == nameof(Resources._File))
&& menu.Items.OfType<MenuItem>().Single(m => (string?)m.Tag == nameof(Resources._File))
.Items.OfType<MenuItem>().Any());
var fileMenu = menu.Items.OfType<MenuItem>().Single(m => (string?)m.Tag == nameof(Resources._File));
var openItem = fileMenu.Items.OfType<MenuItem>()
.Single(m => string.Equals(m.Header as string, Resources._Open, System.StringComparison.Ordinal));
openItem.InputGesture.Should().NotBeNull();
openItem.InputGesture!.Should().Be(KeyGesture.Parse("Ctrl+O"));
openItem.HotKey.Should().Be(KeyGesture.Parse("Ctrl+O"));
}
[AvaloniaTest]
public async Task Opening_Assembly_Adds_It_To_The_Tree()
{

11
ILSpy/Views/MainMenu.axaml.cs

@ -141,12 +141,15 @@ public partial class MainMenu : UserControl @@ -141,12 +141,15 @@ public partial class MainMenu : UserControl
IsEnabled = entry.Metadata?.IsEnabled ?? true,
};
// Wire up the keyboard accelerator if the export declared one. Setting
// MenuItem.HotKey both shows the gesture text in the menu and registers
// a window-scoped shortcut, which mirrors the WPF host's behaviour where
// the binding came from ApplicationCommands.Open etc.
// Wire up the keyboard accelerator if the export declared one. HotKey
// registers the window-scoped shortcut; InputGesture is what actually
// renders the gesture text on the right side of the menu item — both are
// needed to mirror the WPF host's "Ctrl+O" appearance + key handling.
if (TryParseGesture(entry.Metadata?.InputGestureText, out var gesture))
{
menuItem.HotKey = gesture;
menuItem.InputGesture = gesture;
}
if (command is IProvideParameterBinding parameterBinding)
menuItem.Bind(MenuItem.CommandParameterProperty, parameterBinding.ParameterBinding);

Loading…
Cancel
Save