From e543fe702cd1ab9db41eb100473495eb5f87de7b Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 3 Oct 2019 07:25:14 +0200 Subject: [PATCH] fix: recentDocuments menu role on macOS (#20398) --- .../browser/ui/cocoa/atom_menu_controller.mm | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/shell/browser/ui/cocoa/atom_menu_controller.mm b/shell/browser/ui/cocoa/atom_menu_controller.mm index 692aae13c877..ef1a6090055b 100644 --- a/shell/browser/ui/cocoa/atom_menu_controller.mm +++ b/shell/browser/ui/cocoa/atom_menu_controller.mm @@ -126,11 +126,13 @@ static base::scoped_nsobject recentDocumentsMenuSwap_; if (!menu_) return; + // Locate & retain the recent documents menu item if (!recentDocumentsMenuItem_) { - // Locate & retain the recent documents menu item - recentDocumentsMenuItem_.reset( - [[[[[NSApp mainMenu] itemWithTitle:@"Electron"] submenu] - itemWithTitle:@"Open Recent"] retain]); + 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 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 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]); }