Add API to SendActionToFirstResponder.

It's important to bind application menu items to curtain actions of
first responder, like 'quit', 'minimize', 'copy' etc. This API gives
developers ability to do most of them in javascript.
This commit is contained in:
Cheng Zhao 2013-05-16 17:25:02 +08:00
parent 5d985aae09
commit faf7280d1f
5 changed files with 31 additions and 0 deletions

View file

@ -379,6 +379,8 @@ void Menu::Initialize(v8::Handle<v8::Object> target) {
target->Set(v8::String::NewSymbol("Menu"), t->GetFunction());
NODE_SET_METHOD(target, "setApplicationMenu", SetApplicationMenu);
NODE_SET_METHOD(
target, "sendActionToFirstResponder", SendActionToFirstResponder);
}
} // namespace api

View file

@ -69,6 +69,8 @@ class Menu : public EventEmitter,
static v8::Handle<v8::Value> Popup(const v8::Arguments &args);
static v8::Handle<v8::Value> SetApplicationMenu(const v8::Arguments &args);
static v8::Handle<v8::Value> SendActionToFirstResponder(
const v8::Arguments &args);
DISALLOW_COPY_AND_ASSIGN(Menu);
};

View file

@ -31,6 +31,9 @@ class MenuMac : public Menu {
// submenus and set their titles.
static void FixMenuTitles(NSMenu* menu);
// Fake sending an action from the application menu.
static void SendActionToFirstResponder(const std::string& action);
DISALLOW_COPY_AND_ASSIGN(MenuMac);
};

View file

@ -6,6 +6,7 @@
#include "base/message_loop.h"
#include "base/mac/scoped_sending_event.h"
#include "base/strings/sys_string_conversions.h"
#include "browser/native_window.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
@ -71,6 +72,14 @@ void MenuMac::FixMenuTitles(NSMenu* menu) {
}
}
// static
void MenuMac::SendActionToFirstResponder(const std::string& action) {
SEL selector = NSSelectorFromString(base::SysUTF8ToNSString(action));
[[NSApplication sharedApplication] sendAction:selector
to:nil
from:[NSApp mainMenu]];
}
// static
v8::Handle<v8::Value> Menu::SetApplicationMenu(const v8::Arguments &args) {
v8::HandleScope scope;
@ -94,6 +103,20 @@ v8::Handle<v8::Value> Menu::SetApplicationMenu(const v8::Arguments &args) {
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Menu::SendActionToFirstResponder(
const v8::Arguments &args) {
v8::HandleScope scope;
if (!args[0]->IsString())
return node::ThrowTypeError("Bad argument");
std::string action(*v8::String::Utf8Value(args[0]));
MenuMac::SendActionToFirstResponder(action);
return v8::Undefined();
}
// static
Menu* Menu::Create(v8::Handle<v8::Object> wrapper) {
return new MenuMac(wrapper);

View file

@ -28,5 +28,6 @@ Menu::appendSubMenu = (args...) -> @insertSubMenu -1, args...
Menu.setApplicationMenu = (menu) ->
throw new TypeError('Invalid menu') unless menu?.constructor is Menu
bindings.setApplicationMenu menu
Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder
module.exports = Menu