From: Cheng Zhao Date: Sun, 16 Nov 2014 12:24:29 +0000 (+0800) Subject: cocoa: Enable modifying initialized menu X-Git-Tag: v0.19.3~1^2~21 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=49ac363eef94a198205f39d1ef1efaab9ffb59f2;p=electron.git cocoa: Enable modifying initialized menu --- diff --git a/atom/browser/ui/cocoa/atom_menu_controller.h b/atom/browser/ui/cocoa/atom_menu_controller.h index cc03f151..da10e1b0 100644 --- a/atom/browser/ui/cocoa/atom_menu_controller.h +++ b/atom/browser/ui/cocoa/atom_menu_controller.h @@ -39,6 +39,9 @@ class MenuModel; // to the contents of the model after calling this will not be noticed. - (id)initWithModel:(ui::MenuModel*)model; +// Populate current NSMenu with |model|. +- (void)populateWithModel:(ui::MenuModel*)model; + // Programmatically close the constructed menu. - (void)cancel; diff --git a/atom/browser/ui/cocoa/atom_menu_controller.mm b/atom/browser/ui/cocoa/atom_menu_controller.mm index 24f6a1f5..5013f5a9 100644 --- a/atom/browser/ui/cocoa/atom_menu_controller.mm +++ b/atom/browser/ui/cocoa/atom_menu_controller.mm @@ -93,6 +93,22 @@ int EventFlagsFromNSEvent(NSEvent* event) { [super dealloc]; } +- (void)populateWithModel:(ui::MenuModel*)model { + if (!menu_) + return; + + model_ = model; + [menu_ removeAllItems]; + + const int count = model->GetItemCount(); + for (int index = 0; index < count; index++) { + if (model->GetTypeAt(index) == ui::MenuModel::TYPE_SEPARATOR) + [self addSeparatorToMenu:menu_ atIndex:index]; + else + [self addItemToMenu:menu_ atIndex:index fromModel:model]; + } +} + - (void)cancel { if (isMenuOpen_) { [menu_ cancelTracking];