mirror of https://github.com/icsharpcode/ILSpy.git
Browse Source
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 Codepull/3755/head
2 changed files with 75 additions and 1 deletions
@ -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"); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue