From 0a9ba583c9fd3bdf76453f118c52ad6cfaa182b1 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 1 May 2026 17:28:33 +0200 Subject: [PATCH] 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 --- ILSpy.Tests/MainWindow/MainWindowTests.cs | 24 +++++++++++++++++++++++ ILSpy/Views/MainMenu.axaml.cs | 11 +++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/ILSpy.Tests/MainWindow/MainWindowTests.cs b/ILSpy.Tests/MainWindow/MainWindowTests.cs index b0484f9eb..3fdbf5804 100644 --- a/ILSpy.Tests/MainWindow/MainWindowTests.cs +++ b/ILSpy.Tests/MainWindow/MainWindowTests.cs @@ -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 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(); + window.Show(); + + var menu = window.GetVisualDescendants().OfType().First(); + await Waiters.WaitForAsync(() => + menu.Items.OfType().Any(m => (string?)m.Tag == nameof(Resources._File)) + && menu.Items.OfType().Single(m => (string?)m.Tag == nameof(Resources._File)) + .Items.OfType().Any()); + + var fileMenu = menu.Items.OfType().Single(m => (string?)m.Tag == nameof(Resources._File)); + var openItem = fileMenu.Items.OfType() + .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() { diff --git a/ILSpy/Views/MainMenu.axaml.cs b/ILSpy/Views/MainMenu.axaml.cs index 51a42f3d8..50bb80b5f 100644 --- a/ILSpy/Views/MainMenu.axaml.cs +++ b/ILSpy/Views/MainMenu.axaml.cs @@ -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);