Skip to content

Commit

Permalink
fix: keep references to active menus created by api Menu (#19427)
Browse files Browse the repository at this point in the history
Without this such menus would be destroyed by js garbage collector even
when they are still displayed.
  • Loading branch information
CezaryKulakowski authored and codebytere committed Aug 6, 2019
1 parent da2401f commit 50cc54e
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions shell/browser/api/atom_api_menu.cc
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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");
}

Expand Down

0 comments on commit 50cc54e

Please sign in to comment.