fix: keep references to active menus created by api Menu (#19427)

Without this such menus would be destroyed by js garbage collector even
when they are still displayed.
This commit is contained in:
CezaryKulakowski 2019-08-06 23:55:00 +02:00 committed by Shelley Vohr
parent da2401ff39
commit 50cc54e50b

View file

@ -4,6 +4,8 @@
#include "shell/browser/api/atom_api_menu.h"
#include <map>
#include "native_mate/constructor.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
@ -14,6 +16,13 @@
#include "shell/common/native_mate_converters/string16_converter.h"
#include "shell/common/node_includes.h"
namespace {
// We need this map to keep references to currently opened menus.
// Without this menus would be destroyed by js garbage collector
// even when they are still displayed.
std::map<uint32_t, v8::Global<v8::Object>> g_menus;
} // unnamed namespace
namespace electron {
namespace api {
@ -200,10 +209,12 @@ bool Menu::WorksWhenHiddenAt(int index) const {
}
void Menu::OnMenuWillClose() {
g_menus.erase(weak_map_id());
Emit("menu-will-close");
}
void Menu::OnMenuWillShow() {
g_menus[weak_map_id()] = v8::Global<v8::Object>(isolate(), GetWrapper());
Emit("menu-will-show");
}