Browse Source

Let the native menu derive item enabled-state from CanExecute

The menu builder hard-set each NativeMenuItem's IsEnabled from static
export metadata (always true), overriding the state that assigning Command
would otherwise derive from the command's CanExecute. The inline
NativeMenuBar on Windows/Linux re-derives from the command and greys out
OS-gated items (e.g. Open from GAC, which is Windows-only), but the macOS
native menu reads NativeMenuItem.IsEnabled directly -- so those items
stayed enabled there even though invoking them is a no-op. Drop the
explicit IsEnabled so every command-backed item follows its CanExecute on
all platforms.

Assisted-by: Claude:claude-opus-4-8:Claude Code
pull/3755/head
Siegfried Pammer 1 month ago
parent
commit
d2df519ba8
  1. 70
      ILSpy.Tests/Views/MainMenuTests.cs
  2. 6
      ILSpy/Views/MainMenu.axaml.cs

70
ILSpy.Tests/Views/MainMenuTests.cs

@ -0,0 +1,70 @@ @@ -0,0 +1,70 @@
// Copyright (c) 2026 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Headless.NUnit;
using AwesomeAssertions;
using ILSpy;
using NUnit.Framework;
namespace ICSharpCode.ILSpy.Tests.Views;
/// <summary>
/// The main menu is built as a NativeMenu (projected into the AppKit menu bar on macOS, rendered
/// inline by NativeMenuBar on Windows/Linux). A menu item's enabled state must follow its
/// command's CanExecute, not a hard-coded flag -- otherwise OS-gated commands like "Open from GAC"
/// (Windows-only) stay clickable on the macOS native menu even though invoking them is a no-op.
/// </summary>
[TestFixture]
public class MainMenuTests
{
static NativeMenuItem? Find(NativeMenu menu, Func<NativeMenuItem, bool> predicate)
{
foreach (var item in menu.Items.OfType<NativeMenuItem>())
{
if (predicate(item))
return item;
if (item.Menu is { } submenu && Find(submenu, predicate) is { } hit)
return hit;
}
return null;
}
[AvaloniaTest]
public void OpenFromGac_item_enabled_state_follows_the_command()
{
var window = new Window();
MainMenu.Attach(window);
var menu = NativeMenu.GetMenu(window);
menu.Should().NotBeNull("the composition host is up in tests, so Attach builds the menu");
var gac = Find(menu!, i => i.Header?.Contains("GAC", StringComparison.OrdinalIgnoreCase) == true);
gac.Should().NotBeNull("the File menu must contain an 'Open from GAC' item");
gac!.IsEnabled.Should().Be(OperatingSystem.IsWindows(),
"the GAC is Windows-only, so the item must reflect the command's CanExecute (disabled off "
+ "Windows) instead of a hard-coded enabled flag that the macOS native menu would show wrongly");
}
}

6
ILSpy/Views/MainMenu.axaml.cs

@ -229,10 +229,14 @@ public static class MainMenu @@ -229,10 +229,14 @@ public static class MainMenu
else
{
var command = entry.CreateExport().Value;
// No explicit IsEnabled: assigning Command lets NativeMenuItem track the
// command's CanExecute, so OS-gated commands (e.g. Open from GAC, which is
// Windows-only) grey out correctly. A hard-coded IsEnabled would override
// that, and the macOS native menu reads it directly -- leaving the item
// wrongly enabled there even though invoking it is a no-op.
var menuItem = new NativeMenuItem {
Header = ResourceHelper.GetString(entry.Metadata?.Header),
Command = command,
IsEnabled = entry.Metadata?.IsEnabled ?? true,
};
// NativeMenuItem.Icon is Bitmap (macOS NSImage has no vector form),

Loading…
Cancel
Save