Add 'quit' and 'window-all-closed' events for app module.

This commit is contained in:
Cheng Zhao 2013-05-03 10:53:54 +08:00
parent 66404b5f15
commit 9b75019898
10 changed files with 123 additions and 15 deletions

View file

@ -11,6 +11,35 @@ namespace atom {
namespace api {
App::App(v8::Handle<v8::Object> wrapper)
: EventEmitter(wrapper) {
Browser::Get()->AddObserver(this);
}
App::~App() {
Browser::Get()->RemoveObserver(this);
}
void App::OnWillQuit(bool* prevent_default) {
*prevent_default = Emit("will-quit");
}
void App::OnWindowAllClosed() {
Emit("window-all-closed");
}
// static
v8::Handle<v8::Value> App::New(const v8::Arguments &args) {
v8::HandleScope scope;
if (!args.IsConstructCall())
return node::ThrowError("Require constructor call");
new App(args.This());
return args.This();
}
// static
v8::Handle<v8::Value> App::Quit(const v8::Arguments &args) {
v8::HandleScope scope;
@ -31,8 +60,16 @@ v8::Handle<v8::Value> App::Terminate(const v8::Arguments &args) {
// static
void App::Initialize(v8::Handle<v8::Object> target) {
node::SetMethod(target, "quit", Quit);
node::SetMethod(target, "terminate", Terminate);
v8::HandleScope scope;
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(App::New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(v8::String::NewSymbol("Application"));
NODE_SET_PROTOTYPE_METHOD(t, "quit", Quit);
NODE_SET_PROTOTYPE_METHOD(t, "terminate", Terminate);
target->Set(v8::String::NewSymbol("Application"), t->GetFunction());
}
} // namespace api

View file

@ -5,22 +5,35 @@
#ifndef ATOM_BROWSER_API_ATOM_API_APP_H_
#define ATOM_BROWSER_API_ATOM_API_APP_H_
#include "base/basictypes.h"
#include "v8/include/v8.h"
#include "base/compiler_specific.h"
#include "browser/api/atom_api_event_emitter.h"
#include "browser/browser_observer.h"
namespace atom {
namespace api {
class App {
class App : public EventEmitter,
public BrowserObserver{
public:
virtual ~App();
static void Initialize(v8::Handle<v8::Object> target);
protected:
explicit App(v8::Handle<v8::Object> wrapper);
// BrowserObserver implementations:
virtual void OnWillQuit(bool* prevent_default) OVERRIDE;
virtual void OnWindowAllClosed() OVERRIDE;
private:
static v8::Handle<v8::Value> New(const v8::Arguments &args);
static v8::Handle<v8::Value> Quit(const v8::Arguments &args);
static v8::Handle<v8::Value> Terminate(const v8::Arguments &args);
DISALLOW_IMPLICIT_CONSTRUCTORS(App);
DISALLOW_COPY_AND_ASSIGN(App);
};
} // namespace api

View file

@ -1,8 +1,7 @@
binding = process.atomBinding 'app'
EventEmitter = require('events').EventEmitter
class App extends EventEmitter
quit: binding.quit
terminate: binding.terminate
Application = process.atomBinding('app').Application
Application.prototype.__proto__ = EventEmitter.prototype
module.exports = new App
# Only one App object pemitted.
module.exports = new Application