mac: Pass useDefaultAccelerator to getAcceleratorForCommandId

This commit is contained in:
Cheng Zhao 2016-07-02 11:47:40 +09:00 committed by Kevin Sawicki
parent 77cdc2c4a7
commit 6381f44f26
17 changed files with 102 additions and 76 deletions

View file

@ -11,8 +11,8 @@
#include "base/mac/scoped_nsobject.h"
#include "base/strings/string16.h"
namespace ui {
class MenuModel;
namespace atom {
class AtomMenuModel;
}
// A controller for the cross-platform menu model. The menu that's created
@ -23,24 +23,20 @@ class MenuModel;
// as it only maintains weak references.
@interface AtomMenuController : NSObject<NSMenuDelegate> {
@protected
ui::MenuModel* model_; // weak
atom::AtomMenuModel* model_; // weak
base::scoped_nsobject<NSMenu> menu_;
BOOL isMenuOpen_;
BOOL useDefaultAccelerator_;
}
@property(nonatomic, assign) ui::MenuModel* model;
// NIB-based initializer. This does not create a menu. Clients can set the
// properties of the object and the menu will be created upon the first call to
// |-menu|. Note that the menu will be immutable after creation.
- (id)init;
@property(nonatomic, assign) atom::AtomMenuModel* model;
// Builds a NSMenu from the pre-built model (must not be nil). Changes made
// to the contents of the model after calling this will not be noticed.
- (id)initWithModel:(ui::MenuModel*)model;
- (id)initWithModel:(atom::AtomMenuModel*)model useDefaultAccelerator:(BOOL)use;
// Populate current NSMenu with |model|.
- (void)populateWithModel:(ui::MenuModel*)model;
- (void)populateWithModel:(atom::AtomMenuModel*)model;
// Programmatically close the constructed menu.
- (void)cancel;

View file

@ -48,15 +48,11 @@ Role kRolesMap[] = {
@synthesize model = model_;
- (id)init {
if ((self = [super init]))
[self menu];
return self;
}
- (id)initWithModel:(ui::MenuModel*)model {
- (id)initWithModel:(atom::AtomMenuModel*)model useDefaultAccelerator:(BOOL)use {
if ((self = [super init])) {
model_ = model;
isMenuOpen_ = NO;
useDefaultAccelerator_ = use;
[self menu];
}
return self;
@ -73,7 +69,7 @@ Role kRolesMap[] = {
[super dealloc];
}
- (void)populateWithModel:(ui::MenuModel*)model {
- (void)populateWithModel:(atom::AtomMenuModel*)model {
if (!menu_)
return;
@ -82,7 +78,7 @@ Role kRolesMap[] = {
const int count = model->GetItemCount();
for (int index = 0; index < count; index++) {
if (model->GetTypeAt(index) == ui::MenuModel::TYPE_SEPARATOR)
if (model->GetTypeAt(index) == atom::AtomMenuModel::TYPE_SEPARATOR)
[self addSeparatorToMenu:menu_ atIndex:index];
else
[self addItemToMenu:menu_ atIndex:index fromModel:model];
@ -99,12 +95,12 @@ Role kRolesMap[] = {
// Creates a NSMenu from the given model. If the model has submenus, this can
// be invoked recursively.
- (NSMenu*)menuFromModel:(ui::MenuModel*)model {
- (NSMenu*)menuFromModel:(atom::AtomMenuModel*)model {
NSMenu* menu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
const int count = model->GetItemCount();
for (int index = 0; index < count; index++) {
if (model->GetTypeAt(index) == ui::MenuModel::TYPE_SEPARATOR)
if (model->GetTypeAt(index) == atom::AtomMenuModel::TYPE_SEPARATOR)
[self addSeparatorToMenu:menu atIndex:index];
else
[self addItemToMenu:menu atIndex:index fromModel:model];
@ -126,9 +122,7 @@ Role kRolesMap[] = {
// associated with the entry in the model identified by |modelIndex|.
- (void)addItemToMenu:(NSMenu*)menu
atIndex:(NSInteger)index
fromModel:(ui::MenuModel*)ui_model {
atom::AtomMenuModel* model = static_cast<atom::AtomMenuModel*>(ui_model);
fromModel:(atom::AtomMenuModel*)model {
base::string16 label16 = model->GetLabelAt(index);
NSString* label = l10n_util::FixUpWindowsStyleLabel(label16);
base::scoped_nsobject<NSMenuItem> item(
@ -141,12 +135,13 @@ Role kRolesMap[] = {
if (model->GetIconAt(index, &icon) && !icon.IsEmpty())
[item setImage:icon.ToNSImage()];
ui::MenuModel::ItemType type = model->GetTypeAt(index);
if (type == ui::MenuModel::TYPE_SUBMENU) {
atom::AtomMenuModel::ItemType type = model->GetTypeAt(index);
if (type == atom::AtomMenuModel::TYPE_SUBMENU) {
// Recursively build a submenu from the sub-model at this index.
[item setTarget:nil];
[item setAction:nil];
ui::MenuModel* submenuModel = model->GetSubmenuModelAt(index);
atom::AtomMenuModel* submenuModel = static_cast<atom::AtomMenuModel*>(
model->GetSubmenuModelAt(index));
NSMenu* submenu = [self menuFromModel:submenuModel];
[submenu setTitle:[item title]];
[item setSubmenu:submenu];
@ -170,7 +165,8 @@ Role kRolesMap[] = {
NSValue* modelObject = [NSValue valueWithPointer:model];
[item setRepresentedObject:modelObject]; // Retains |modelObject|.
ui::Accelerator accelerator;
if (model->GetAcceleratorAt(index, &accelerator)) {
if (model->GetAcceleratorAtWithParams(
index, useDefaultAccelerator_, &accelerator)) {
const ui::PlatformAcceleratorCocoa* platformAccelerator =
static_cast<const ui::PlatformAcceleratorCocoa*>(
accelerator.platform_accelerator());
@ -206,8 +202,8 @@ Role kRolesMap[] = {
return NO;
NSInteger modelIndex = [item tag];
ui::MenuModel* model =
static_cast<ui::MenuModel*>(
atom::AtomMenuModel* model =
static_cast<atom::AtomMenuModel*>(
[[(id)item representedObject] pointerValue]);
DCHECK(model);
if (model) {
@ -234,8 +230,8 @@ Role kRolesMap[] = {
// item chosen.
- (void)itemSelected:(id)sender {
NSInteger modelIndex = [sender tag];
ui::MenuModel* model =
static_cast<ui::MenuModel*>(
atom::AtomMenuModel* model =
static_cast<atom::AtomMenuModel*>(
[[sender representedObject] pointerValue]);
DCHECK(model);
if (model) {