From d2653e819293c9145bfb256ff05950c3b54ec1fa Mon Sep 17 00:00:00 2001 From: Zhuo Lu Date: Wed, 16 May 2018 18:19:28 -0700 Subject: [PATCH] Fix: Prevent menu update while it's open (#12809) * Update application menu on default runloop Menu change should be prevented while the menu is open * Fix code style * Memory safety --- atom/browser/api/atom_api_menu_mac.mm | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/atom/browser/api/atom_api_menu_mac.mm b/atom/browser/api/atom_api_menu_mac.mm index c2516f4528aa..2b63b8a6cefe 100644 --- a/atom/browser/api/atom_api_menu_mac.mm +++ b/atom/browser/api/atom_api_menu_mac.mm @@ -16,6 +16,12 @@ using content::BrowserThread; +namespace { + +static scoped_nsobject applicationMenu_; + +} // namespace + namespace atom { namespace api { @@ -132,7 +138,18 @@ void Menu::SetApplicationMenu(Menu* base_menu) { base::scoped_nsobject menu_controller([ [AtomMenuController alloc] initWithModel:menu->model_.get() useDefaultAccelerator:YES]); - [NSApp setMainMenu:[menu_controller menu]]; + + NSRunLoop* currentRunLoop = [NSRunLoop currentRunLoop]; + [currentRunLoop cancelPerformSelector:@selector(setMainMenu:) + target:NSApp + argument:applicationMenu_]; + applicationMenu_.reset([[menu_controller menu] retain]); + [[NSRunLoop currentRunLoop] + performSelector:@selector(setMainMenu:) + target:NSApp + argument:applicationMenu_ + order:0 + modes:[NSArray arrayWithObject:NSDefaultRunLoopMode]]; // Ensure the menu_controller_ is destroyed after main menu is set. menu_controller.swap(menu->menu_controller_);