win: Correctly close popup menu

This commit is contained in:
Cheng Zhao 2017-12-21 15:26:32 +09:00
parent 459db92052
commit 088042841d
3 changed files with 23 additions and 7 deletions

View file

@ -48,7 +48,7 @@ void MenuViews::PopupAt(Window* window, int x, int y, int positioning_item) {
// Show the menu.
int32_t window_id = window->ID();
auto close_callback = base::Bind(
&MenuViews::ClosePopupAt, weak_factory_.GetWeakPtr(), window_id);
&MenuViews::OnClosed, weak_factory_.GetWeakPtr(), window_id);
menu_runners_[window_id] = std::unique_ptr<MenuRunner>(new MenuRunner(
model(), flags, close_callback));
menu_runners_[window_id]->RunMenuAt(
@ -60,12 +60,23 @@ void MenuViews::PopupAt(Window* window, int x, int y, int positioning_item) {
}
void MenuViews::ClosePopupAt(int32_t window_id) {
if (menu_runners_[window_id]) {
menu_runners_[window_id]->Cancel();
menu_runners_.erase(window_id);
auto runner = menu_runners_.find(window_id);
if (runner != menu_runners_.end()) {
// Close the runner for the window.
runner->second->Cancel();
} else if (window_id == -1) {
// Or just close all opened runners.
for (auto it = menu_runners_.begin(); it != menu_runners_.end();) {
// The iterator is invalidated after the call.
(it++)->second->Cancel();
}
}
}
void MenuViews::OnClosed(int32_t window_id) {
menu_runners_.erase(window_id);
}
// static
mate::WrappableBase* Menu::New(mate::Arguments* args) {
return new MenuViews(args->isolate(), args->GetThis());

View file

@ -25,6 +25,8 @@ class MenuViews : public Menu {
void ClosePopupAt(int32_t window_id) override;
private:
void OnClosed(int32_t window_id);
// window ID -> open context menu
std::map<int32_t, std::unique_ptr<views::MenuRunner>> menu_runners_;

View file

@ -94,10 +94,13 @@ Menu.prototype.popup = function (window, x, y, positioningItem) {
}
Menu.prototype.closePopup = function (window) {
if (!window || window.constructor !== BrowserWindow) {
window = BrowserWindow.getFocusedWindow()
if (window && window.constructor !== BrowserWindow) {
this.closePopupAt(window.id)
} else {
// Passing -1 (invalid) would make closePopupAt close the all menu runners
// belong to this menu.
this.closePopupAt(-1)
}
if (window) this.closePopupAt(window.id)
}
Menu.prototype.getMenuItemById = function (id) {