Implement shutdown event for macOS

This commit is contained in:
Cheng Zhao 2018-02-05 16:13:35 +09:00
Родитель c470e758cc
Коммит 983e1b1a70
7 изменённых файлов: 25 добавлений и 6 удалений

Просмотреть файл

@ -18,8 +18,10 @@ namespace api {
PowerMonitor::PowerMonitor(v8::Isolate* isolate) {
#if defined(OS_LINUX)
SetShutdownHandler(base::Bind(&PowerMonitor::ShouldShutdown,
// Passed to base class, no need for weak ptr.
base::Unretained(this)));
#elif defined(OS_MACOSX)
Browser::Get()->SetShutdownHandler(base::Bind(&PowerMonitor::ShouldShutdown,
base::Unretained(this)));
#endif
base::PowerMonitor::Get()->AddObserver(this);
Init(isolate);
@ -30,7 +32,7 @@ PowerMonitor::~PowerMonitor() {
}
bool PowerMonitor::ShouldShutdown() {
return Emit("shutdown");
return !Emit("shutdown");
}
#if defined(OS_LINUX)

Просмотреть файл

@ -105,6 +105,9 @@ class Browser : public WindowListObserver {
LoginItemSettings GetLoginItemSettings(const LoginItemSettings& options);
#if defined(OS_MACOSX)
// Set the handler which decides whether to shutdown.
void SetShutdownHandler(base::Callback<bool()> handler);
// Hide the application.
void Hide();

Просмотреть файл

@ -21,6 +21,10 @@
namespace atom {
void Browser::SetShutdownHandler(base::Callback<bool()> handler) {
[[AtomApplication sharedApplication] setShutdownHandler:std::move(handler)];
}
void Browser::Focus() {
[[AtomApplication sharedApplication] activateIgnoringOtherApps:YES];
}

Просмотреть файл

@ -148,7 +148,7 @@ void PowerObserverLinux::OnPrepareForShutdown(dbus::Signal* signal) {
return;
}
if (shutting_down) {
if (!should_shutdown_ || !should_shutdown_.Run()) {
if (!should_shutdown_ || should_shutdown_.Run()) {
// The user didn't try to prevent shutdown. Release the lock and allow the
// shutdown to continue normally.
shutdown_lock_.reset();

Просмотреть файл

@ -2,8 +2,9 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#import "base/mac/scoped_sending_event.h"
#import "base/mac/scoped_nsobject.h"
#include "base/callback.h"
#include "base/mac/scoped_sending_event.h"
#include "base/mac/scoped_nsobject.h"
@interface AtomApplication : NSApplication<CrAppProtocol,
CrAppControlProtocol,
@ -13,10 +14,13 @@
base::scoped_nsobject<NSUserActivity> currentActivity_;
NSCondition* handoffLock_;
BOOL updateReceived_;
base::Callback<bool()> shouldShutdown_;
}
+ (AtomApplication*)sharedApplication;
- (void)setShutdownHandler:(base::Callback<bool()>)handler;
// CrAppProtocol:
- (BOOL)isHandlingSendEvent;

Просмотреть файл

@ -29,10 +29,16 @@ inline void dispatch_sync_main(dispatch_block_t block) {
}
- (void)terminate:(id)sender {
if (shouldShutdown_ && !shouldShutdown_.Run())
return; // User will call Quit later.
AtomApplicationDelegate* atomDelegate = (AtomApplicationDelegate*) [NSApp delegate];
[atomDelegate tryToTerminateApp:self];
}
- (void)setShutdownHandler:(base::Callback<bool()>)handler {
shouldShutdown_ = std::move(handler);
}
- (BOOL)isHandlingSendEvent {
return handlingSendEvent_;
}

Просмотреть файл

@ -40,7 +40,7 @@ Emitted when the system changes to AC power.
Emitted when system changes to battery power.
### Event: 'shutdown' _Linux_
### Event: 'shutdown' _Linux_ _macOS_
Emitted when the system is about to reboot or shut down. If the event handler
invokes `e.preventDefault()`, Electron will attempt to delay system shutdown in