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