title: How to catch the event of clicking the app window's close button in Electron app
body: <p>I wish to catch the event of clicking the app window's close button  in Electron app.</p>
  
  <p>I'm trying to develope Electron app for Mac OSX.
  I want to hide the app window, not to terminate the app when a user clicks the window's close button like other Mac apps.</p>
  
  <p>However, I can not detect wether the system should be terminated or it should be hidden, because in any case, a <code>close</code> event of <code>browser-window</code> is called when a close button is clicked, the OS is shut down or the app is terminated with quit command, <code>Cmd+Q</code>.</p>
  
  <p>Is there any way to catch the event of clicking the app window's close button  in Electron app?</p>
  
  <p>Thank you for your help.</p>
  
  <hr>
  
  <h3>Postscript</h3>
  
  <p>To detect the event of clicking a close button, I tried this code </p>
  
  <pre><code>var app = require('app');
  var BrowserWindow = require('browser-window');
  var Menu = require('menu');
  
  var force_quit = false;
  
  var menu = Menu.buildFromTemplate([
    {
      label: 'Sample',
      submenu: [
        {label: 'About App', selector: 'orderFrontStandardAboutPanel:'},
        {label: 'Quit', accelerator: 'CmdOrCtrl+Q', click: function() {force_quit=true; app.quit();}}
      ]
    }]);
  
  app.on('window-all-closed', function(){
      if(process.platform != 'darwin')
          app.quit();
  });
  
  app.on('ready', function(){
  
      Menu.setApplicationMenu(menu);
  
      mainWindow = new BrowserWindow({width:800, height:600});
  
      mainWindow.on('close', function(e){
          if(!force_quit){
              e.preventDefault();
              mainWindow.hide();
          }
      });
  
      mainWindow.on('closed', function(){
          console.log("closed");
          mainWindow = null;
          app.quit();
      });
  
      app.on('activate-with-no-open-windows', function(){
          mainWindow.show();
      });
  });
  </code></pre>
  
  <p>With this code, the app is hidden when a close button of the app window is clicked, and the app is terminated when <code>Cmd+Q</code> is typed. However, when I try to shut down the OS, the shutdown event is prevented. </p>