Skip to content

Commit fa9c8ef

Browse files
authored
fix(macos): respect disabled state for menu items (#46)
- 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.
1 parent 3e8dd20 commit fa9c8ef

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/platform/macos/menu_macos.mm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ - (void)menuItemClicked:(id)sender {
124124
if (!menu_item)
125125
return;
126126

127+
// Ignore clicks on disabled items as an extra safety net.
128+
if (![menu_item isEnabled])
129+
return;
130+
127131
// Call the block if it exists
128132
if (_clickedBlock) {
129133
// Get the MenuItemId from the menu item's associated object
@@ -162,6 +166,14 @@ - (void)menuWillOpen:(NSMenu*)menu {
162166
}
163167
}
164168

169+
- (BOOL)menu:(NSMenu*)menu validateMenuItem:(NSMenuItem*)item {
170+
// Respect the programmatically set enabled state on NSMenuItem.
171+
// Without this override, NSMenu's default autoenablesItems (YES)
172+
// would re-enable all items whose target responds to the action,
173+
// overriding explicit setEnabled:NO calls.
174+
return [item isEnabled];
175+
}
176+
165177
- (void)menuDidClose:(NSMenu*)menu {
166178
@try {
167179
if (!menu)
@@ -509,6 +521,8 @@ - (void)menuDidClose:(NSMenu*)menu {
509521
Impl(MenuId id, NSMenu* menu)
510522
: id_(id), ns_menu_(menu), delegate_([[NSMenuDelegateImpl alloc] init]) {
511523
[ns_menu_ setDelegate:delegate_];
524+
// Disable auto-enabling so explicit setEnabled: calls are respected.
525+
[ns_menu_ setAutoenablesItems:NO];
512526
}
513527

514528
~Impl() {

0 commit comments

Comments
 (0)