mac: Correctly close popup menu

This commit is contained in:
Cheng Zhao 2018-01-01 15:39:43 +09:00
parent 5f4b62b6c8
commit d7bc127c60
3 changed files with 16 additions and 13 deletions

View file

@ -33,7 +33,7 @@ class MenuMac : public Menu {
private: private:
friend class Menu; friend class Menu;
static void SendActionToFirstResponder(const std::string& action); void OnClosed(int32_t window_id);
scoped_nsobject<AtomMenuController> menu_controller_; scoped_nsobject<AtomMenuController> menu_controller_;

View file

@ -108,14 +108,24 @@ void MenuMac::PopupOnUI(const base::WeakPtr<NativeWindow>& native_window,
} }
void MenuMac::ClosePopupAt(int32_t window_id) { void MenuMac::ClosePopupAt(int32_t window_id) {
auto it = popup_controllers_.find(window_id); auto controller = popup_controllers_.find(window_id);
if (it != popup_controllers_.end()) { if (controller != popup_controllers_.end()) {
popup_controllers_.erase(it); // Close the controller for the window.
[controller->second cancel];
} else if (window_id == -1) { } 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 // static
void Menu::SetApplicationMenu(Menu* base_menu) { void Menu::SetApplicationMenu(Menu* base_menu) {
MenuMac* menu = static_cast<MenuMac*>(base_menu); MenuMac* menu = static_cast<MenuMac*>(base_menu);

View file

@ -12,12 +12,9 @@
#include "ui/base/accelerators/accelerator.h" #include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/platform_accelerator_cocoa.h" #include "ui/base/accelerators/platform_accelerator_cocoa.h"
#include "ui/base/l10n/l10n_util_mac.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/events/cocoa/cocoa_event_utils.h"
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
using content::BrowserThread;
namespace { namespace {
struct Role { struct Role {
@ -334,11 +331,7 @@ static base::scoped_nsobject<NSMenu> recentDocumentsMenuSwap_;
if (isMenuOpen_) { if (isMenuOpen_) {
isMenuOpen_ = NO; isMenuOpen_ = NO;
model_->MenuWillClose(); model_->MenuWillClose();
closeCallback.Run();
// 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);
} }
} }