diff --git a/atom/browser/api/atom_api_menu.cc b/atom/browser/api/atom_api_menu.cc index da208b3659..651e2067e1 100644 --- a/atom/browser/api/atom_api_menu.cc +++ b/atom/browser/api/atom_api_menu.cc @@ -23,9 +23,13 @@ Menu::Menu(v8::Isolate* isolate, v8::Local wrapper) : model_(new AtomMenuModel(this)), parent_(nullptr) { InitWith(isolate, wrapper); + model_->AddObserver(this); } Menu::~Menu() { + if (model_) { + model_->RemoveObserver(this); + } } void Menu::AfterInit(v8::Isolate* isolate) { @@ -153,6 +157,14 @@ bool Menu::IsVisibleAt(int index) const { return model_->IsVisibleAt(index); } +void Menu::OnMenuWillClose() { + Emit("menu-will-close"); +} + +void Menu::OnMenuWillShow() { + Emit("menu-will-show"); +} + // static void Menu::BuildPrototype(v8::Isolate* isolate, v8::Local prototype) { diff --git a/atom/browser/api/atom_api_menu.h b/atom/browser/api/atom_api_menu.h index 574d218024..aafd8f8939 100644 --- a/atom/browser/api/atom_api_menu.h +++ b/atom/browser/api/atom_api_menu.h @@ -18,7 +18,8 @@ namespace atom { namespace api { class Menu : public mate::TrackableObject, - public AtomMenuModel::Delegate { + public AtomMenuModel::Delegate, + public AtomMenuModel::Observer { public: static mate::WrappableBase* New(mate::Arguments* args); @@ -60,6 +61,10 @@ class Menu : public mate::TrackableObject, std::unique_ptr model_; Menu* parent_; + // Observable: + void OnMenuWillClose() override; + void OnMenuWillShow() override; + private: void InsertItemAt(int index, int command_id, const base::string16& label); void InsertSeparatorAt(int index); diff --git a/atom/browser/ui/atom_menu_model.cc b/atom/browser/ui/atom_menu_model.cc index 1ec6b3c09f..835e8d3dc4 100644 --- a/atom/browser/ui/atom_menu_model.cc +++ b/atom/browser/ui/atom_menu_model.cc @@ -42,8 +42,16 @@ bool AtomMenuModel::GetAcceleratorAtWithParams( void AtomMenuModel::MenuWillClose() { ui::SimpleMenuModel::MenuWillClose(); - for (Observer& observer : observers_) - observer.MenuWillClose(); + for (Observer& observer : observers_) { + observer.OnMenuWillClose(); + } +} + +void AtomMenuModel::MenuWillShow() { + ui::SimpleMenuModel::MenuWillShow(); + for (Observer& observer : observers_) { + observer.OnMenuWillShow(); + } } AtomMenuModel* AtomMenuModel::GetSubmenuModelAt(int index) { diff --git a/atom/browser/ui/atom_menu_model.h b/atom/browser/ui/atom_menu_model.h index 7bf734b74c..f20cf2586d 100644 --- a/atom/browser/ui/atom_menu_model.h +++ b/atom/browser/ui/atom_menu_model.h @@ -36,8 +36,11 @@ class AtomMenuModel : public ui::SimpleMenuModel { public: virtual ~Observer() {} + // Notifies the menu will open. + virtual void OnMenuWillShow() {} + // Notifies the menu has been closed. - virtual void MenuWillClose() {} + virtual void OnMenuWillClose() {} }; explicit AtomMenuModel(Delegate* delegate); @@ -54,6 +57,7 @@ class AtomMenuModel : public ui::SimpleMenuModel { // ui::SimpleMenuModel: void MenuWillClose() override; + void MenuWillShow() override; using SimpleMenuModel::GetSubmenuModelAt; AtomMenuModel* GetSubmenuModelAt(int index); diff --git a/atom/browser/ui/tray_icon_cocoa.h b/atom/browser/ui/tray_icon_cocoa.h index 7680b6f30f..d0b124acab 100644 --- a/atom/browser/ui/tray_icon_cocoa.h +++ b/atom/browser/ui/tray_icon_cocoa.h @@ -35,7 +35,7 @@ class TrayIconCocoa : public TrayIcon, protected: // AtomMenuModel::Observer: - void MenuWillClose() override; + void OnMenuWillClose() override; private: // Atom custom view for NSStatusItem. diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index 6c2def356c..a358f676a8 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -461,7 +461,7 @@ gfx::Rect TrayIconCocoa::GetBounds() { return bounds; } -void TrayIconCocoa::MenuWillClose() { +void TrayIconCocoa::OnMenuWillClose() { [status_item_view_ setNeedsDisplay:YES]; } diff --git a/docs/api/menu.md b/docs/api/menu.md index d6323ac178..60491b26f2 100644 --- a/docs/api/menu.md +++ b/docs/api/menu.md @@ -99,6 +99,29 @@ Returns `MenuItem` the item with the specified `id` Inserts the `menuItem` to the `pos` position of the menu. +### Instance Events + +Objects created with `new Menu` emit the following events: + +**Note:** Some events are only available on specific operating systems and are +labeled as such. + +#### Event: 'menu-will-show' + +Returns: + +* `event` Event + +Emitted when `menu.popup()` is called. + +#### Event: 'menu-will-close' + +Returns: + +* `event` Event + +Emitted when a popup is closed either manually or with `menu.closePopup()`. + ### Instance Properties `menu` objects also have the following properties: diff --git a/spec/api-menu-spec.js b/spec/api-menu-spec.js index 08835f3ad8..3fa0050920 100644 --- a/spec/api-menu-spec.js +++ b/spec/api-menu-spec.js @@ -284,6 +284,17 @@ describe('Menu module', () => { return closeWindow(w).then(() => { w = null }) }) + it('should emit menu-will-show event', (done) => { + menu.on('menu-will-show', () => { done() }) + menu.popup(w) + }) + + it('should emit menu-will-close event', (done) => { + menu.on('menu-will-close', () => { done() }) + menu.popup(w) + menu.closePopup() + }) + it('returns immediately', () => { const { browserWindow, x, y } = menu.popup(w, {x: 100, y: 101})