fix: display empty menu item for non-visible submenus (#16832)

* fix: display empty menu item for nonvisible submenus

* use Chromium UI string ID
This commit is contained in:
Shelley Vohr 2019-02-08 13:19:01 -08:00 committed by Samuel Attard
parent 062778c031
commit ccf46a57bc

View file

@ -17,6 +17,7 @@
#include "ui/base/l10n/l10n_util_mac.h" #include "ui/base/l10n/l10n_util_mac.h"
#include "ui/events/cocoa/cocoa_event_utils.h" #include "ui/events/cocoa/cocoa_event_utils.h"
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
#include "ui/strings/grit/ui_strings.h"
using content::BrowserThread; using content::BrowserThread;
@ -58,6 +59,29 @@ Role kRolesMap[] = {
{@selector(clearRecentDocuments:), "clearrecentdocuments"}, {@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:@""]);
NSString* empty_menu_title =
l10n_util::GetNSString(IDS_APP_MENU_EMPTY_SUBMENU);
[submenu addItemWithTitle:empty_menu_title action:NULL keyEquivalent:@""];
[[submenu itemAtIndex:0] setEnabled:NO];
return submenu.autorelease();
}
} // namespace } // namespace
// Menu item is located for ease of removing it from the parent owner // Menu item is located for ease of removing it from the parent owner
@ -227,7 +251,9 @@ static base::scoped_nsobject<NSMenu> recentDocumentsMenuSwap_;
[item setAction:nil]; [item setAction:nil];
atom::AtomMenuModel* submenuModel = atom::AtomMenuModel* submenuModel =
static_cast<atom::AtomMenuModel*>(model->GetSubmenuModelAt(index)); static_cast<atom::AtomMenuModel*>(model->GetSubmenuModelAt(index));
NSMenu* submenu = [self menuFromModel:submenuModel]; NSMenu* submenu = MenuHasVisibleItems(submenuModel)
? [self menuFromModel:submenuModel]
: MakeEmptySubmenu();
[submenu setTitle:[item title]]; [submenu setTitle:[item title]];
[item setSubmenu:submenu]; [item setSubmenu:submenu];