From d7bc127c6065f3206284269f6b886546f4cdb91e Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 1 Jan 2018 15:39:43 +0900 Subject: [PATCH] mac: Correctly close popup menu --- atom/browser/api/atom_api_menu_mac.h | 2 +- atom/browser/api/atom_api_menu_mac.mm | 18 ++++++++++++++---- atom/browser/ui/cocoa/atom_menu_controller.mm | 9 +-------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/atom/browser/api/atom_api_menu_mac.h b/atom/browser/api/atom_api_menu_mac.h index ed897a7907a1..f680765423a9 100644 --- a/atom/browser/api/atom_api_menu_mac.h +++ b/atom/browser/api/atom_api_menu_mac.h @@ -33,7 +33,7 @@ class MenuMac : public Menu { private: friend class Menu; - static void SendActionToFirstResponder(const std::string& action); + void OnClosed(int32_t window_id); scoped_nsobject menu_controller_; diff --git a/atom/browser/api/atom_api_menu_mac.mm b/atom/browser/api/atom_api_menu_mac.mm index 2b5c337f518c..da4aa1c19dbd 100644 --- a/atom/browser/api/atom_api_menu_mac.mm +++ b/atom/browser/api/atom_api_menu_mac.mm @@ -108,14 +108,24 @@ void MenuMac::PopupOnUI(const base::WeakPtr& native_window, } void MenuMac::ClosePopupAt(int32_t window_id) { - auto it = popup_controllers_.find(window_id); - if (it != popup_controllers_.end()) { - popup_controllers_.erase(it); + auto controller = popup_controllers_.find(window_id); + if (controller != popup_controllers_.end()) { + // Close the controller for the window. + [controller->second cancel]; } else if (window_id == -1) { - popup_controllers_.clear(); + // Or just close all opened controllers. + for (auto it = popup_controllers_.begin(); + it != popup_controllers_.end();) { + // The iterator is invalidated after the call. + [(it++)->second cancel]; + } } } +void MenuMac::OnClosed(int32_t window_id) { + popup_controllers_.erase(window_id); +} + // static void Menu::SetApplicationMenu(Menu* base_menu) { MenuMac* menu = static_cast(base_menu); diff --git a/atom/browser/ui/cocoa/atom_menu_controller.mm b/atom/browser/ui/cocoa/atom_menu_controller.mm index a41604ca9577..cee7b639897b 100644 --- a/atom/browser/ui/cocoa/atom_menu_controller.mm +++ b/atom/browser/ui/cocoa/atom_menu_controller.mm @@ -12,12 +12,9 @@ #include "ui/base/accelerators/accelerator.h" #include "ui/base/accelerators/platform_accelerator_cocoa.h" #include "ui/base/l10n/l10n_util_mac.h" -#include "content/public/browser/browser_thread.h" #include "ui/events/cocoa/cocoa_event_utils.h" #include "ui/gfx/image/image.h" -using content::BrowserThread; - namespace { struct Role { @@ -334,11 +331,7 @@ static base::scoped_nsobject recentDocumentsMenuSwap_; if (isMenuOpen_) { isMenuOpen_ = NO; model_->MenuWillClose(); - - // Post async task so that itemSelected runs before the close callback - // deletes the controller from the map which deallocates it - if (!closeCallback.is_null()) - BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closeCallback); + closeCallback.Run(); } }