Add our own MenuModel class

This commit is contained in:
Cheng Zhao 2015-08-10 12:39:05 +08:00
parent a3f3a35fd1
commit 4b9ff309ec
6 changed files with 81 additions and 11 deletions

View file

@ -20,7 +20,7 @@ namespace atom {
namespace api {
Menu::Menu()
: model_(new ui::SimpleMenuModel(this)),
: model_(new AtomMenuModel(this)),
parent_(NULL) {
}

View file

@ -8,9 +8,9 @@
#include <string>
#include "atom/browser/api/atom_api_window.h"
#include "atom/browser/ui/atom_menu_model.h"
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "ui/base/models/simple_menu_model.h"
#include "native_mate/wrappable.h"
namespace atom {
@ -18,7 +18,7 @@ namespace atom {
namespace api {
class Menu : public mate::Wrappable,
public ui::SimpleMenuModel::Delegate {
public AtomMenuModel::Delegate {
public:
static mate::Wrappable* Create();
@ -33,7 +33,7 @@ class Menu : public mate::Wrappable,
static void SendActionToFirstResponder(const std::string& action);
#endif
ui::SimpleMenuModel* model() const { return model_.get(); }
AtomMenuModel* model() const { return model_.get(); }
protected:
Menu();
@ -42,7 +42,7 @@ class Menu : public mate::Wrappable,
// mate::Wrappable:
void AfterInit(v8::Isolate* isolate) override;
// ui::SimpleMenuModel::Delegate implementations:
// ui::SimpleMenuModel::Delegate:
bool IsCommandIdChecked(int command_id) const override;
bool IsCommandIdEnabled(int command_id) const override;
bool IsCommandIdVisible(int command_id) const override;
@ -54,7 +54,7 @@ class Menu : public mate::Wrappable,
virtual void Popup(Window* window) = 0;
virtual void PopupAt(Window* window, int x, int y) = 0;
scoped_ptr<ui::SimpleMenuModel> model_;
scoped_ptr<AtomMenuModel> model_;
Menu* parent_;
private:
@ -102,9 +102,9 @@ class Menu : public mate::Wrappable,
namespace mate {
template<>
struct Converter<ui::SimpleMenuModel*> {
struct Converter<atom::AtomMenuModel*> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
ui::SimpleMenuModel** out) {
atom::AtomMenuModel** out) {
// null would be tranfered to NULL.
if (val->IsNull()) {
*out = nullptr;

View file

@ -0,0 +1,22 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/ui/atom_menu_model.h"
namespace atom {
AtomMenuModel::AtomMenuModel(Delegate* delegate)
: ui::SimpleMenuModel(delegate),
delegate_(delegate) {
}
AtomMenuModel::~AtomMenuModel() {
}
void AtomMenuModel::MenuClosed() {
ui::SimpleMenuModel::MenuClosed();
FOR_EACH_OBSERVER(Observer, observers_, MenuClosed());
}
} // namespace atom

View file

@ -0,0 +1,47 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_UI_ATOM_MENU_MODEL_H_
#define ATOM_BROWSER_UI_ATOM_MENU_MODEL_H_
#include "base/observer_list.h"
#include "ui/base/models/simple_menu_model.h"
namespace atom {
class AtomMenuModel : public ui::SimpleMenuModel {
public:
class Delegate : public ui::SimpleMenuModel::Delegate {
public:
virtual ~Delegate() {}
};
class Observer {
public:
virtual ~Observer() {}
// Notifies the menu has been closed.
virtual void MenuClosed() {}
};
explicit AtomMenuModel(Delegate* delegate);
virtual ~AtomMenuModel();
void AddObserver(Observer* obs) { observers_.AddObserver(obs); }
void RemoveObserver(Observer* obs) { observers_.RemoveObserver(obs); }
// ui::SimpleMenuModel:
void MenuClosed() override;
private:
Delegate* delegate_; // weak ref.
ObserverList<Observer> observers_;
DISALLOW_COPY_AND_ASSIGN(AtomMenuModel);
};
} // namespace atom
#endif // ATOM_BROWSER_UI_ATOM_MENU_MODEL_H_

View file

@ -5,12 +5,12 @@
#import "atom/browser/ui/cocoa/atom_menu_controller.h"
#include "atom/browser/ui/atom_menu_model.h"
#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/platform_accelerator_cocoa.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/events/cocoa/cocoa_event_utils.h"
#include "ui/gfx/image/image.h"
@ -120,8 +120,7 @@
[item setTarget:nil];
[item setAction:nil];
ui::MenuModel* submenuModel = model->GetSubmenuModelAt(index);
NSMenu* submenu =
[self menuFromModel:(ui::SimpleMenuModel*)submenuModel];
NSMenu* submenu = [self menuFromModel:submenuModel];
[submenu setTitle:[item title]];
[item setSubmenu:submenu];

View file

@ -166,6 +166,8 @@
'atom/browser/ui/accelerator_util.h',
'atom/browser/ui/accelerator_util_mac.mm',
'atom/browser/ui/accelerator_util_views.cc',
'atom/browser/ui/atom_menu_model.cc',
'atom/browser/ui/atom_menu_model.h',
'atom/browser/ui/cocoa/atom_menu_controller.h',
'atom/browser/ui/cocoa/atom_menu_controller.mm',
'atom/browser/ui/cocoa/event_processing_window.h',