Skip to content

Commit

Permalink
fix: display empty menu item for nonvisible submenus
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Feb 8, 2019
1 parent 4989ecc commit 33bfd29
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions atom/browser/ui/cocoa/atom_menu_controller.mm
Expand Up @@ -58,6 +58,30 @@
{@selector(clearRecentDocuments:), "clearrecentdocuments"},
};

// Called when adding a submenu to the menu and checks if the submenu, via its
// |model|, has visible child items.
bool MenuHasVisibleItems(const atom::AtomMenuModel* model) {
int count = model->GetItemCount();
for (int index = 0; index < count; index++) {
if (model->IsVisibleAt(index))
return true;
}
return false;
}

// Called when an empty submenu is created. This inserts a menu item labeled
// "(empty)" into the submenu. Matches Windows behavior.
NSMenu* MakeEmptySubmenu() {
base::scoped_nsobject<NSMenu> submenu([[NSMenu alloc] initWithTitle:@""]);
base::string16 empty_menu_title = base::ASCIIToUTF16("(empty)");
NSString* empty_menu_label =
l10n_util::FixUpWindowsStyleLabel(empty_menu_title);

[submenu addItemWithTitle:empty_menu_label action:NULL keyEquivalent:@""];
[[submenu itemAtIndex:0] setEnabled:NO];
return submenu.autorelease();
}

} // namespace

// Menu item is located for ease of removing it from the parent owner
Expand Down Expand Up @@ -220,13 +244,16 @@ - (void)addItemToMenu:(NSMenu*)menu
NSMenu* submenu = [[NSMenu alloc] initWithTitle:label];
[item setSubmenu:submenu];
[NSApp setServicesMenu:submenu];
} else if (type == atom::AtomMenuModel::TYPE_SUBMENU) {
} else if (type == atom::AtomMenuModel::TYPE_SUBMENU &&
model->IsVisibleAt(index)) {
// Recursively build a submenu from the sub-model at this index.
[item setTarget:nil];
[item setAction:nil];
atom::AtomMenuModel* submenuModel =
static_cast<atom::AtomMenuModel*>(model->GetSubmenuModelAt(index));
NSMenu* submenu = [self menuFromModel:submenuModel];
NSMenu* submenu = MenuHasVisibleItems(submenuModel)
? [self menuFromModel:submenuModel]
: MakeEmptySubmenu();
[submenu setTitle:[item title]];
[item setSubmenu:submenu];

Expand Down

0 comments on commit 33bfd29

Please sign in to comment.