From 9b75019898c9525e9a519026360c4da0b98d886d Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 3 May 2013 10:53:54 +0800 Subject: [PATCH] Add 'quit' and 'window-all-closed' events for app module. --- atom.gyp | 1 + browser/api/atom_api_app.cc | 41 ++++++++++++++++++++++++++++++++-- browser/api/atom_api_app.h | 21 +++++++++++++---- browser/api/lib/app.coffee | 9 ++++---- browser/browser.cc | 16 +++++++++++-- browser/browser.h | 15 ++++++++++++- browser/browser_observer.h | 25 +++++++++++++++++++++ browser/default_app/main.js | 6 +++++ browser/window_list_observer.h | 2 +- script/cpplint | 2 ++ 10 files changed, 123 insertions(+), 15 deletions(-) create mode 100644 browser/browser_observer.h diff --git a/atom.gyp b/atom.gyp index 091aaa8b..3627a3d5 100644 --- a/atom.gyp +++ b/atom.gyp @@ -52,6 +52,7 @@ 'browser/browser.cc', 'browser/browser.h', 'browser/browser_mac.mm', + 'browser/browser_observer.h', 'browser/native_window.cc', 'browser/native_window.h', 'browser/native_window_mac.h', diff --git a/browser/api/atom_api_app.cc b/browser/api/atom_api_app.cc index 797e03f8..eece7af0 100644 --- a/browser/api/atom_api_app.cc +++ b/browser/api/atom_api_app.cc @@ -11,6 +11,35 @@ namespace atom { namespace api { +App::App(v8::Handle 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 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 App::Quit(const v8::Arguments &args) { v8::HandleScope scope; @@ -31,8 +60,16 @@ v8::Handle App::Terminate(const v8::Arguments &args) { // static void App::Initialize(v8::Handle target) { - node::SetMethod(target, "quit", Quit); - node::SetMethod(target, "terminate", Terminate); + v8::HandleScope scope; + + v8::Local 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 diff --git a/browser/api/atom_api_app.h b/browser/api/atom_api_app.h index 0c8fdc7a..f2b45eea 100644 --- a/browser/api/atom_api_app.h +++ b/browser/api/atom_api_app.h @@ -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 target); + protected: + explicit App(v8::Handle wrapper); + + // BrowserObserver implementations: + virtual void OnWillQuit(bool* prevent_default) OVERRIDE; + virtual void OnWindowAllClosed() OVERRIDE; + private: + static v8::Handle New(const v8::Arguments &args); + static v8::Handle Quit(const v8::Arguments &args); static v8::Handle Terminate(const v8::Arguments &args); - DISALLOW_IMPLICIT_CONSTRUCTORS(App); + DISALLOW_COPY_AND_ASSIGN(App); }; } // namespace api diff --git a/browser/api/lib/app.coffee b/browser/api/lib/app.coffee index 3ee09d29..3b699c4e 100644 --- a/browser/api/lib/app.coffee +++ b/browser/api/lib/app.coffee @@ -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 diff --git a/browser/browser.cc b/browser/browser.cc index b85901f1..fafe86ea 100644 --- a/browser/browser.cc +++ b/browser/browser.cc @@ -26,12 +26,22 @@ Browser* Browser::Get() { void Browser::Quit() { atom::WindowList* window_list = atom::WindowList::GetInstance(); if (window_list->size() == 0) - Terminate(); + NotifyAndTerminate(); is_quiting_ = true; 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) { // Once a beforeunload handler has prevented the closing, we think the quit // is cancelled too. @@ -40,7 +50,9 @@ void Browser::OnWindowCloseCancelled(NativeWindow* window) { void Browser::OnWindowAllClosed() { if (is_quiting_) - Terminate(); + NotifyAndTerminate(); + else + FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWindowAllClosed()); } } // namespace atom diff --git a/browser/browser.h b/browser/browser.h index ef07e36a..118ce008 100644 --- a/browser/browser.h +++ b/browser/browser.h @@ -7,6 +7,8 @@ #include "base/basictypes.h" #include "base/compiler_specific.h" +#include "base/observer_list.h" +#include "browser/browser_observer.h" #include "browser/window_list_observer.h" namespace atom { @@ -25,9 +27,17 @@ class Browser : public WindowListObserver { // Quit the application immediately without cleanup work. void Terminate(); - bool is_quiting() const { return is_quiting_; } + void AddObserver(BrowserObserver* obs) { + observers_.AddObserver(obs); + } + + void RemoveObserver(BrowserObserver* obs) { + observers_.RemoveObserver(obs); + } protected: + void NotifyAndTerminate(); + bool is_quiting_; private: @@ -35,6 +45,9 @@ class Browser : public WindowListObserver { virtual void OnWindowCloseCancelled(NativeWindow* window) OVERRIDE; virtual void OnWindowAllClosed() OVERRIDE; + // Observers of the browser. + ObserverList observers_; + DISALLOW_COPY_AND_ASSIGN(Browser); }; diff --git a/browser/browser_observer.h b/browser/browser_observer.h new file mode 100644 index 00000000..e79884bb --- /dev/null +++ b/browser/browser_observer.h @@ -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_ diff --git a/browser/default_app/main.js b/browser/default_app/main.js index 5238372b..721caeb6 100644 --- a/browser/default_app/main.js +++ b/browser/default_app/main.js @@ -1,9 +1,15 @@ +var app = require('app'); var atom = require('atom'); var ipc = require('ipc'); var Window = require('window'); var mainWindow = null; +// Quit when all windows are closed. +app.on('window-all-closed', function() { + app.terminate(); +}); + // Echo every message back. ipc.on('message', function(process_id, routing_id) { ipc.send.apply(ipc, arguments); diff --git a/browser/window_list_observer.h b/browser/window_list_observer.h index 76778cda..e68e4925 100644 --- a/browser/window_list_observer.h +++ b/browser/window_list_observer.h @@ -10,7 +10,7 @@ namespace atom { class NativeWindow; class WindowListObserver { - public: + public: // Called immediately after a window is added to the list. virtual void OnWindowAdded(NativeWindow* window) {} diff --git a/script/cpplint b/script/cpplint index ff723935..48a5a4a4 100755 --- a/script/cpplint +++ b/script/cpplint @@ -5,6 +5,8 @@ cd "`dirname $0`/.." # List all Objective-C headers here, should not cpplint them. OBJC_HEADERS=' browser/atom_event_processing_window.h +browser/atom_application_mac.h +browser/atom_application_delegate_mac.h browser/native_window_mac.h common/api/api_messages.cc common/api/api_messages.h' -- 2.43.0