Merge pull request #11754 from electron/menu-events

Add and document menu events
This commit is contained in:
shelley vohr 2018-01-29 12:40:57 -05:00 committed by GitHub
commit 12d4f984f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 69 additions and 6 deletions

View file

@ -23,9 +23,13 @@ Menu::Menu(v8::Isolate* isolate, v8::Local<v8::Object> 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<v8::FunctionTemplate> prototype) {

View file

@ -18,7 +18,8 @@ namespace atom {
namespace api {
class Menu : public mate::TrackableObject<Menu>,
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<Menu>,
std::unique_ptr<AtomMenuModel> 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);