fix: recentDocuments menu role on macOS (#20398)

This commit is contained in:
Shelley Vohr 2019-10-03 07:25:14 +02:00 committed by Cheng Zhao
parent 2470ef2e91
commit e543fe702c

View file

@ -126,11 +126,13 @@ static base::scoped_nsobject<NSMenu> recentDocumentsMenuSwap_;
if (!menu_)
return;
if (!recentDocumentsMenuItem_) {
// Locate & retain the recent documents menu item
recentDocumentsMenuItem_.reset(
[[[[[NSApp mainMenu] itemWithTitle:@"Electron"] submenu]
itemWithTitle:@"Open Recent"] retain]);
if (!recentDocumentsMenuItem_) {
base::string16 title = base::ASCIIToUTF16("Open Recent");
NSString* openTitle = l10n_util::FixUpWindowsStyleLabel(title);
recentDocumentsMenuItem_.reset([[[[[NSApp mainMenu]
itemWithTitle:@"Electron"] submenu] itemWithTitle:openTitle] retain]);
}
model_ = model;
@ -193,8 +195,17 @@ static base::scoped_nsobject<NSMenu> recentDocumentsMenuSwap_;
// Replaces the item's submenu instance with the singleton recent documents
// menu. Previously replaced menu items will be recovered.
- (void)replaceSubmenuShowingRecentDocuments:(NSMenuItem*)item {
NSMenu* recentDocumentsMenu =
[[[recentDocumentsMenuItem_ submenu] retain] autorelease];
NSMenu* recentDocumentsMenu = [recentDocumentsMenuItem_ submenu];
if (!recentDocumentsMenu) {
base::string16 title = base::ASCIIToUTF16("Clear Menu");
NSString* clearTitle = l10n_util::FixUpWindowsStyleLabel(title);
recentDocumentsMenu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
[recentDocumentsMenu
addItem:[[[NSMenuItem alloc]
initWithTitle:clearTitle
action:@selector(clearRecentDocuments:)
keyEquivalent:@""] autorelease]];
}
// Remove menu items in recent documents back to swap menu
[self moveMenuItems:recentDocumentsMenu to:recentDocumentsMenuSwap_];
@ -211,6 +222,9 @@ static base::scoped_nsobject<NSMenu> recentDocumentsMenuSwap_;
// Replace submenu
[item setSubmenu:recentDocumentsMenu];
DCHECK_EQ([item action], @selector(submenuAction:));
DCHECK_EQ([item target], recentDocumentsMenu);
// Remember the new menu item that carries the recent documents menu
recentDocumentsMenuItem_.reset([item retain]);
}