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

Without this such menus would be destroyed by js garbage collector even
when they are still displayed.

Co-authored-by: CezaryKulakowski <50166166+CezaryKulakowski@users.noreply.github.com>
This commit is contained in:
Shelley Vohr
2020-02-12 00:00:03 +00:00
committed by GitHub
parent da65c881e2
commit cb9b3b29af

View File

@@ -4,6 +4,7 @@
#include "shell/browser/api/atom_api_menu.h"
#include <map>
#include <utility>
#include "native_mate/constructor.h"
@@ -16,6 +17,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 {
@@ -202,10 +210,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");
}