From 7cc04035c4c15d126662f529f6f7e5eb608e9183 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 6 May 2013 20:27:09 +0800 Subject: [PATCH] Add Menu API. (WIP) --- atom.gyp | 4 +++ browser/api/atom_api_menu.cc | 62 ++++++++++++++++++++++++++++++++ browser/api/atom_api_menu.h | 48 +++++++++++++++++++++++++ browser/api/atom_api_menu_mac.h | 32 +++++++++++++++++ browser/api/atom_api_menu_mac.mm | 27 ++++++++++++++ common/api/atom_extensions.h | 1 + 6 files changed, 174 insertions(+) create mode 100644 browser/api/atom_api_menu.cc create mode 100644 browser/api/atom_api_menu.h create mode 100644 browser/api/atom_api_menu_mac.h create mode 100644 browser/api/atom_api_menu_mac.mm diff --git a/atom.gyp b/atom.gyp index cebb03c290..ee92b68aa8 100644 --- a/atom.gyp +++ b/atom.gyp @@ -35,6 +35,10 @@ 'browser/api/atom_api_event.h', 'browser/api/atom_api_event_emitter.cc', 'browser/api/atom_api_event_emitter.h', + 'browser/api/atom_api_menu.cc', + 'browser/api/atom_api_menu.h', + 'browser/api/atom_api_menu_mac.h', + 'browser/api/atom_api_menu_mac.mm', 'browser/api/atom_api_window.cc', 'browser/api/atom_api_window.h', 'browser/api/atom_browser_bindings.cc', diff --git a/browser/api/atom_api_menu.cc b/browser/api/atom_api_menu.cc new file mode 100644 index 0000000000..3c82c58f71 --- /dev/null +++ b/browser/api/atom_api_menu.cc @@ -0,0 +1,62 @@ +// Copyright (c) 2013 GitHub, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "browser/api/atom_api_menu.h" + +namespace atom { + +namespace api { + +Menu::Menu(v8::Handle wrapper) + : EventEmitter(wrapper), + model_(new ui::SimpleMenuModel(this)) { +} + +Menu::~Menu() { +} + +bool Menu::IsCommandIdChecked(int command_id) const { + return false; +} + +bool Menu::IsCommandIdEnabled(int command_id) const { + return true; +} + +bool Menu::GetAcceleratorForCommandId(int command_id, + ui::Accelerator* accelerator) { + return false; +} + +void Menu::ExecuteCommand(int command_id, int event_flags) { +} + +// static +v8::Handle Menu::New(const v8::Arguments &args) { + v8::HandleScope scope; + + if (!args.IsConstructCall()) + return node::ThrowError("Require constructor call"); + + Menu::Create(args.This()); + + return args.This(); +} + +// static +void Menu::Initialize(v8::Handle target) { + v8::HandleScope scope; + + v8::Local t(v8::FunctionTemplate::New(Menu::New)); + t->InstanceTemplate()->SetInternalFieldCount(1); + t->SetClassName(v8::String::NewSymbol("Menu")); + + target->Set(v8::String::NewSymbol("Menu"), t->GetFunction()); +} + +} // namespace api + +} // namespace atom + +NODE_MODULE(atom_browser_menu, atom::api::Menu::Initialize) diff --git a/browser/api/atom_api_menu.h b/browser/api/atom_api_menu.h new file mode 100644 index 0000000000..aaabc92e2d --- /dev/null +++ b/browser/api/atom_api_menu.h @@ -0,0 +1,48 @@ +// Copyright (c) 2013 GitHub, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef ATOM_BROWSER_API_ATOM_API_MENU_H_ +#define ATOM_BROWSER_API_ATOM_API_MENU_H_ + +#include "base/memory/scoped_ptr.h" +#include "browser/api/atom_api_event_emitter.h" +#include "ui/base/models/simple_menu_model.h" + +namespace atom { + +namespace api { + +class Menu : public EventEmitter, + public ui::SimpleMenuModel::Delegate { + public: + virtual ~Menu(); + + static Menu* Create(v8::Handle wrapper); + + static void Initialize(v8::Handle target); + + protected: + explicit Menu(v8::Handle wrapper); + + // ui::SimpleMenuModel::Delegate implementations: + virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; + virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; + virtual bool GetAcceleratorForCommandId( + int command_id, + ui::Accelerator* accelerator) OVERRIDE; + virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; + + scoped_ptr model_; + + private: + static v8::Handle New(const v8::Arguments &args); + + DISALLOW_COPY_AND_ASSIGN(Menu); +}; + +} // namespace api + +} // namespace atom + +#endif // ATOM_BROWSER_API_ATOM_API_MENU_H_ diff --git a/browser/api/atom_api_menu_mac.h b/browser/api/atom_api_menu_mac.h new file mode 100644 index 0000000000..1181ddaeb7 --- /dev/null +++ b/browser/api/atom_api_menu_mac.h @@ -0,0 +1,32 @@ +// Copyright (c) 2013 GitHub, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef ATOM_BROWSER_API_ATOM_API_MENU_MAC_H_ +#define ATOM_BROWSER_API_ATOM_API_MENU_MAC_H_ + +#include "browser/api/atom_api_menu.h" + +#import "chrome/browser/ui/cocoa/menu_controller.h" + +namespace atom { + +namespace api { + +class MenuMac : public Menu { + public: + explicit MenuMac(v8::Handle wrapper); + virtual ~MenuMac(); + + protected: + scoped_nsobject controller_; + + private: + DISALLOW_COPY_AND_ASSIGN(MenuMac); +}; + +} // namespace api + +} // namespace atom + +#endif // ATOM_BROWSER_API_ATOM_API_MENU_MAC_H_ diff --git a/browser/api/atom_api_menu_mac.mm b/browser/api/atom_api_menu_mac.mm new file mode 100644 index 0000000000..1668903838 --- /dev/null +++ b/browser/api/atom_api_menu_mac.mm @@ -0,0 +1,27 @@ +// Copyright (c) 2013 GitHub, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "browser/api/atom_api_menu_mac.h" + +namespace atom { + +namespace api { + +MenuMac::MenuMac(v8::Handle wrapper) + : Menu(wrapper), + controller_([[MenuController alloc] initWithModel:model_.get() + useWithPopUpButtonCell:NO]) { +} + +MenuMac::~MenuMac() { +} + +// static +Menu* Menu::Create(v8::Handle wrapper) { + return new MenuMac(wrapper); +} + +} // namespace api + +} // namespace atom diff --git a/common/api/atom_extensions.h b/common/api/atom_extensions.h index fdff420122..ac4059fb1d 100644 --- a/common/api/atom_extensions.h +++ b/common/api/atom_extensions.h @@ -12,6 +12,7 @@ NODE_EXT_LIST_START NODE_EXT_LIST_ITEM(atom_browser_app) NODE_EXT_LIST_ITEM(atom_browser_dialog) NODE_EXT_LIST_ITEM(atom_browser_ipc) +NODE_EXT_LIST_ITEM(atom_browser_menu) NODE_EXT_LIST_ITEM(atom_browser_window) // Module names start with `atom_renderer_` can only be used by renderer