Browse Source

Merge pull request #11625 from nitsakh/mac-terminate

Change macos app termination process
Cheng Zhao 7 years ago
parent
commit
4dab824c6b

+ 6 - 0
atom/browser/mac/atom_application.mm

@@ -4,6 +4,7 @@
 
 #import "atom/browser/mac/atom_application.h"
 
+#import "atom/browser/mac/atom_application_delegate.h"
 #include "atom/browser/mac/dict_util.h"
 #include "atom/browser/browser.h"
 #include "base/auto_reset.h"
@@ -27,6 +28,11 @@ inline void dispatch_sync_main(dispatch_block_t block) {
   return (AtomApplication*)[super sharedApplication];
 }
 
+- (void)terminate:(id)sender {
+  AtomApplicationDelegate* atomDelegate = (AtomApplicationDelegate*) [NSApp delegate];
+  [atomDelegate tryToTerminateApp:self];
+}
+
 - (BOOL)isHandlingSendEvent {
   return handlingSendEvent_;
 }

+ 2 - 0
atom/browser/mac/atom_application_delegate.h

@@ -11,6 +11,8 @@
   base::scoped_nsobject<AtomMenuController> menu_controller_;
 }
 
+- (void)tryToTerminateApp:(NSApplication*)app;
+
 // Sets the menu that will be returned in "applicationDockMenu:".
 - (void)setApplicationDockMenu:(atom::AtomMenuModel*)model;
 

+ 5 - 9
atom/browser/mac/atom_application_delegate.mm

@@ -87,15 +87,11 @@ static base::mac::ScopedObjCClassSwizzler* g_swizzle_imk_input_session;
   return atom::Browser::Get()->OpenFile(filename_str) ? YES : NO;
 }
 
-- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender {
-  atom::Browser* browser = atom::Browser::Get();
-  if (browser->is_quiting()) {
-    return NSTerminateNow;
-  } else {
-    // System started termination.
-    atom::Browser::Get()->Quit();
-    return NSTerminateCancel;
-  }
+// We simply try to close the browser, which in turn will try to close the windows.
+// Termination can proceed if all windows are closed or window close can be cancelled
+// which will abort termination.
+- (void)tryToTerminateApp:(NSApplication*)app {
+  atom::Browser::Get()->Quit();
 }
 
 - (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication

+ 18 - 0
spec/api-app-spec.js

@@ -157,6 +157,24 @@ describe('app module', () => {
         done()
       })
     })
+
+    it('exits gracefully on macos', function (done) {
+      if (process.platform !== 'darwin') {
+        this.skip()
+      }
+      const appPath = path.join(__dirname, 'fixtures', 'api', 'singleton')
+      const electronPath = remote.getGlobal('process').execPath
+      appProcess = ChildProcess.spawn(electronPath, [appPath])
+      appProcess.stdout.once('data', () => {
+        // The apple script will try to terminate the app
+        // If there's an error terminating the app, then it will print to stderr
+        ChildProcess.exec('osascript -e \'quit app "Electron"\'', (err, stdout, stderr) => {
+          assert(!err)
+          assert(!stderr.trim())
+          done()
+        })
+      })
+    })
   })
 
   describe('app.makeSingleInstance', () => {