From bb94f85f0eeb2f2d64c8a2e09203308427eec93c Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Tue, 2 Jun 2026 23:23:17 +0200 Subject: [PATCH] Fix macOS app menu by populating NativeMenu in place The Cocoa menu exporter samples Application.Current's NativeMenu once at startup and never watches the property afterwards, so the previous code's NativeMenu.SetMenu(Application.Current, freshMenu) was never observed: About and Check for Updates simply went missing from the app-named menu. Declare an empty NativeMenu in App.axaml so it exists before that sampling, then insert the Help items into that same instance at startup, which fires the exporter's re-export. Inert on Windows / Linux. Assisted-by: Claude:claude-opus-4-8:Claude Code --- ILSpy/App.axaml | 8 ++++++++ ILSpy/Views/MainMenu.axaml.cs | 20 ++++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/ILSpy/App.axaml b/ILSpy/App.axaml index b9be7ebd2..0241d76c4 100644 --- a/ILSpy/App.axaml +++ b/ILSpy/App.axaml @@ -19,6 +19,14 @@ RequestedThemeVariant="Default"> + + + + + diff --git a/ILSpy/Views/MainMenu.axaml.cs b/ILSpy/Views/MainMenu.axaml.cs index 687d02a71..2b764f84a 100644 --- a/ILSpy/Views/MainMenu.axaml.cs +++ b/ILSpy/Views/MainMenu.axaml.cs @@ -80,24 +80,28 @@ public static class MainMenu } // macOS convention puts About / Check for Updates under the bold app-named menu - // next to the Apple logo, not under a separate "Help" top-level. Avalonia's Cocoa - // menu exporter watches NativeMenu.MenuProperty on Application.Current as the - // channel for that menu; setting a NativeMenu there prepends our items above - // the auto-inserted Services / Hide / Quit block. We then remove _Help from the - // window menu so the items don't appear in both places. + // next to the Apple logo, not under a separate "Help" top-level. The Cocoa exporter + // samples Application.Current's NativeMenu ONCE at startup (before this window exists) + // and never watches the property afterwards -- so swapping in a fresh menu here would + // be ignored. Instead we populate the empty NativeMenu declared in App.axaml IN PLACE: + // the exporter subscribes to that instance's Items, so inserting fires a re-export. + // Items go at the top, above the Services / Hide / Quit block the exporter appended. + // We then remove _Help from the window menu so the items don't appear in both places. static void PromoteHelpToMacAppMenu(NativeMenu rootMenu, Dictionary topLevelByTag) { if (!topLevelByTag.TryGetValue("_Help", out var helpItem) || helpItem.Menu is null) return; if (Application.Current is null) return; - var appMenu = new NativeMenu(); + var appMenu = NativeMenu.GetMenu(Application.Current); + if (appMenu is null) + return; + var index = 0; foreach (var item in helpItem.Menu.Items.ToArray()) { helpItem.Menu.Items.Remove(item); - appMenu.Items.Add(item); + appMenu.Items.Insert(index++, item); } - NativeMenu.SetMenu(Application.Current, appMenu); rootMenu.Items.Remove(helpItem); topLevelByTag.Remove("_Help"); }