Add 'quit' and 'window-all-closed' events for app module.
This commit is contained in:
parent
66404b5f15
commit
9b75019898
10 changed files with 123 additions and 15 deletions
1
atom.gyp
1
atom.gyp
|
@ -52,6 +52,7 @@
|
||||||
'browser/browser.cc',
|
'browser/browser.cc',
|
||||||
'browser/browser.h',
|
'browser/browser.h',
|
||||||
'browser/browser_mac.mm',
|
'browser/browser_mac.mm',
|
||||||
|
'browser/browser_observer.h',
|
||||||
'browser/native_window.cc',
|
'browser/native_window.cc',
|
||||||
'browser/native_window.h',
|
'browser/native_window.h',
|
||||||
'browser/native_window_mac.h',
|
'browser/native_window_mac.h',
|
||||||
|
|
|
@ -11,6 +11,35 @@ namespace atom {
|
||||||
|
|
||||||
namespace api {
|
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
|
// static
|
||||||
v8::Handle<v8::Value> App::Quit(const v8::Arguments &args) {
|
v8::Handle<v8::Value> App::Quit(const v8::Arguments &args) {
|
||||||
v8::HandleScope scope;
|
v8::HandleScope scope;
|
||||||
|
@ -31,8 +60,16 @@ v8::Handle<v8::Value> App::Terminate(const v8::Arguments &args) {
|
||||||
|
|
||||||
// static
|
// static
|
||||||
void App::Initialize(v8::Handle<v8::Object> target) {
|
void App::Initialize(v8::Handle<v8::Object> target) {
|
||||||
node::SetMethod(target, "quit", Quit);
|
v8::HandleScope scope;
|
||||||
node::SetMethod(target, "terminate", Terminate);
|
|
||||||
|
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
|
} // namespace api
|
||||||
|
|
|
@ -5,22 +5,35 @@
|
||||||
#ifndef ATOM_BROWSER_API_ATOM_API_APP_H_
|
#ifndef ATOM_BROWSER_API_ATOM_API_APP_H_
|
||||||
#define ATOM_BROWSER_API_ATOM_API_APP_H_
|
#define ATOM_BROWSER_API_ATOM_API_APP_H_
|
||||||
|
|
||||||
#include "base/basictypes.h"
|
#include "base/compiler_specific.h"
|
||||||
#include "v8/include/v8.h"
|
#include "browser/api/atom_api_event_emitter.h"
|
||||||
|
#include "browser/browser_observer.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
class App {
|
class App : public EventEmitter,
|
||||||
|
public BrowserObserver{
|
||||||
public:
|
public:
|
||||||
|
virtual ~App();
|
||||||
|
|
||||||
static void Initialize(v8::Handle<v8::Object> target);
|
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:
|
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> Quit(const v8::Arguments &args);
|
||||||
static v8::Handle<v8::Value> Terminate(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
|
} // namespace api
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
binding = process.atomBinding 'app'
|
|
||||||
EventEmitter = require('events').EventEmitter
|
EventEmitter = require('events').EventEmitter
|
||||||
|
|
||||||
class App extends EventEmitter
|
Application = process.atomBinding('app').Application
|
||||||
quit: binding.quit
|
Application.prototype.__proto__ = EventEmitter.prototype
|
||||||
terminate: binding.terminate
|
|
||||||
|
|
||||||
module.exports = new App
|
# Only one App object pemitted.
|
||||||
|
module.exports = new Application
|
||||||
|
|
|
@ -26,12 +26,22 @@ Browser* Browser::Get() {
|
||||||
void Browser::Quit() {
|
void Browser::Quit() {
|
||||||
atom::WindowList* window_list = atom::WindowList::GetInstance();
|
atom::WindowList* window_list = atom::WindowList::GetInstance();
|
||||||
if (window_list->size() == 0)
|
if (window_list->size() == 0)
|
||||||
Terminate();
|
NotifyAndTerminate();
|
||||||
|
|
||||||
is_quiting_ = true;
|
is_quiting_ = true;
|
||||||
window_list->CloseAllWindows();
|
window_list->CloseAllWindows();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Browser::NotifyAndTerminate() {
|
||||||
|
bool prevent_default = false;
|
||||||
|
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWillQuit(&prevent_default));
|
||||||
|
|
||||||
|
if (prevent_default)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Terminate();
|
||||||
|
}
|
||||||
|
|
||||||
void Browser::OnWindowCloseCancelled(NativeWindow* window) {
|
void Browser::OnWindowCloseCancelled(NativeWindow* window) {
|
||||||
// Once a beforeunload handler has prevented the closing, we think the quit
|
// Once a beforeunload handler has prevented the closing, we think the quit
|
||||||
// is cancelled too.
|
// is cancelled too.
|
||||||
|
@ -40,7 +50,9 @@ void Browser::OnWindowCloseCancelled(NativeWindow* window) {
|
||||||
|
|
||||||
void Browser::OnWindowAllClosed() {
|
void Browser::OnWindowAllClosed() {
|
||||||
if (is_quiting_)
|
if (is_quiting_)
|
||||||
Terminate();
|
NotifyAndTerminate();
|
||||||
|
else
|
||||||
|
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWindowAllClosed());
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace atom
|
} // namespace atom
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
|
|
||||||
#include "base/basictypes.h"
|
#include "base/basictypes.h"
|
||||||
#include "base/compiler_specific.h"
|
#include "base/compiler_specific.h"
|
||||||
|
#include "base/observer_list.h"
|
||||||
|
#include "browser/browser_observer.h"
|
||||||
#include "browser/window_list_observer.h"
|
#include "browser/window_list_observer.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
@ -25,9 +27,17 @@ class Browser : public WindowListObserver {
|
||||||
// Quit the application immediately without cleanup work.
|
// Quit the application immediately without cleanup work.
|
||||||
void Terminate();
|
void Terminate();
|
||||||
|
|
||||||
bool is_quiting() const { return is_quiting_; }
|
void AddObserver(BrowserObserver* obs) {
|
||||||
|
observers_.AddObserver(obs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RemoveObserver(BrowserObserver* obs) {
|
||||||
|
observers_.RemoveObserver(obs);
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
void NotifyAndTerminate();
|
||||||
|
|
||||||
bool is_quiting_;
|
bool is_quiting_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -35,6 +45,9 @@ class Browser : public WindowListObserver {
|
||||||
virtual void OnWindowCloseCancelled(NativeWindow* window) OVERRIDE;
|
virtual void OnWindowCloseCancelled(NativeWindow* window) OVERRIDE;
|
||||||
virtual void OnWindowAllClosed() OVERRIDE;
|
virtual void OnWindowAllClosed() OVERRIDE;
|
||||||
|
|
||||||
|
// Observers of the browser.
|
||||||
|
ObserverList<BrowserObserver> observers_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(Browser);
|
DISALLOW_COPY_AND_ASSIGN(Browser);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
25
browser/browser_observer.h
Normal file
25
browser/browser_observer.h
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
// 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_BROSER_BROWSER_OBSERVER_H_
|
||||||
|
#define ATOM_BROSER_BROWSER_OBSERVER_H_
|
||||||
|
|
||||||
|
namespace atom {
|
||||||
|
|
||||||
|
class BrowserObserver {
|
||||||
|
public:
|
||||||
|
// The browser has closed all windows and will quit.
|
||||||
|
virtual void OnWillQuit(bool* prevent_default) {}
|
||||||
|
|
||||||
|
// The browser has closed all windows. If the browser is quiting, then this
|
||||||
|
// method will not be called, instead it will call OnWillQuit.
|
||||||
|
virtual void OnWindowAllClosed() {}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual ~BrowserObserver() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace atom
|
||||||
|
|
||||||
|
#endif // ATOM_BROSER_BROWSER_OBSERVER_H_
|
|
@ -1,9 +1,15 @@
|
||||||
|
var app = require('app');
|
||||||
var atom = require('atom');
|
var atom = require('atom');
|
||||||
var ipc = require('ipc');
|
var ipc = require('ipc');
|
||||||
var Window = require('window');
|
var Window = require('window');
|
||||||
|
|
||||||
var mainWindow = null;
|
var mainWindow = null;
|
||||||
|
|
||||||
|
// Quit when all windows are closed.
|
||||||
|
app.on('window-all-closed', function() {
|
||||||
|
app.terminate();
|
||||||
|
});
|
||||||
|
|
||||||
// Echo every message back.
|
// Echo every message back.
|
||||||
ipc.on('message', function(process_id, routing_id) {
|
ipc.on('message', function(process_id, routing_id) {
|
||||||
ipc.send.apply(ipc, arguments);
|
ipc.send.apply(ipc, arguments);
|
||||||
|
|
|
@ -10,7 +10,7 @@ namespace atom {
|
||||||
class NativeWindow;
|
class NativeWindow;
|
||||||
|
|
||||||
class WindowListObserver {
|
class WindowListObserver {
|
||||||
public:
|
public:
|
||||||
// Called immediately after a window is added to the list.
|
// Called immediately after a window is added to the list.
|
||||||
virtual void OnWindowAdded(NativeWindow* window) {}
|
virtual void OnWindowAdded(NativeWindow* window) {}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,8 @@ cd "`dirname $0`/.."
|
||||||
# List all Objective-C headers here, should not cpplint them.
|
# List all Objective-C headers here, should not cpplint them.
|
||||||
OBJC_HEADERS='
|
OBJC_HEADERS='
|
||||||
browser/atom_event_processing_window.h
|
browser/atom_event_processing_window.h
|
||||||
|
browser/atom_application_mac.h
|
||||||
|
browser/atom_application_delegate_mac.h
|
||||||
browser/native_window_mac.h
|
browser/native_window_mac.h
|
||||||
common/api/api_messages.cc
|
common/api/api_messages.cc
|
||||||
common/api/api_messages.h'
|
common/api/api_messages.h'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue