From 0300e1d25cacf50cd4502f94ea47a54b0e4ce0b6 Mon Sep 17 00:00:00 2001 From: LiJianying Date: Sat, 30 May 2026 10:57:46 +0800 Subject: [PATCH] fix(macos): respect disabled state for menu items - Set autoenablesItems=NO on NSMenu so explicit setEnabled: calls are not overridden by AppKit's auto-validation. - Add isEnabled check in menuItemClicked: as an extra safety net to ignore clicks on disabled items. - Implement menu:validateMenuItem: on NSMenuDelegateImpl to return the item's actual isEnabled state. --- src/platform/macos/menu_macos.mm | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/platform/macos/menu_macos.mm b/src/platform/macos/menu_macos.mm index ffb280a..2f2a18f 100644 --- a/src/platform/macos/menu_macos.mm +++ b/src/platform/macos/menu_macos.mm @@ -124,6 +124,10 @@ - (void)menuItemClicked:(id)sender { if (!menu_item) return; + // Ignore clicks on disabled items as an extra safety net. + if (![menu_item isEnabled]) + return; + // Call the block if it exists if (_clickedBlock) { // Get the MenuItemId from the menu item's associated object @@ -162,6 +166,14 @@ - (void)menuWillOpen:(NSMenu*)menu { } } +- (BOOL)menu:(NSMenu*)menu validateMenuItem:(NSMenuItem*)item { + // Respect the programmatically set enabled state on NSMenuItem. + // Without this override, NSMenu's default autoenablesItems (YES) + // would re-enable all items whose target responds to the action, + // overriding explicit setEnabled:NO calls. + return [item isEnabled]; +} + - (void)menuDidClose:(NSMenu*)menu { @try { if (!menu) @@ -509,6 +521,8 @@ - (void)menuDidClose:(NSMenu*)menu { Impl(MenuId id, NSMenu* menu) : id_(id), ns_menu_(menu), delegate_([[NSMenuDelegateImpl alloc] init]) { [ns_menu_ setDelegate:delegate_]; + // Disable auto-enabling so explicit setEnabled: calls are respected. + [ns_menu_ setAutoenablesItems:NO]; } ~Impl() {