Repository: atom/atom
File: src/main-process/atom-application.js (JavaScript)

Code before:
        devMode,
        safeMode,
        profileStartup,
        clearWindowState,
        addToLastWindow,
        env
      });
    }
  }

  // Public: Create a new {AtomWindow} bound to this application.
  createWindow(settings) {
    return new AtomWindow(this, this.fileRecoveryService, settings);
  }

  // Public: Removes the {AtomWindow} from the global window list.
  removeWindow(window) {
    this.windowStack.removeWindow(window);
    if (this.getAllWindows().length === 0 && process.platform !== 'darwin') {
      // We should be quitting now in the 'window-all-closed' event handler.
      // We can skip anything past this point in removeWindow().
      return;
    }
    if (!window.isSpec) this.saveCurrentWindowOptions(true);
  }

  // Public: Adds the {AtomWindow} to the global window list.
  addWindow(window) {
    this.windowStack.addWindow(window);
    if (this.applicationMenu)
      this.applicationMenu.addWindow(window.browserWindow);

    window.once('window:loaded', () => {
      this.autoUpdateManager &&
        this.autoUpdateManager.emitUpdateAvailableEvent(window);
    });

    if (!window.isSpec) {
      const focusHandler = () => this.windowStack.touch(window);
      const blurHandler = () => this.saveCurrentWindowOptions(false);
      window.browserWindow.on('focus', focusHandler);
      window.browserWindow.on('blur', blurHandler);
      window.browserWindow.once('closed', () => {
        this.windowStack.removeWindow(window);
        window.browserWindow.removeListener('focus', focusHandler);
        window.browserWindow.removeListener('blur', blurHandler);
      });
      window.browserWindow.webContents.once('did-finish-load', blurHandler);
      this.saveCurrentWindowOptions(false);
    }
  }

Review comment (bug):
If the `app.quit()` call is removed from `removeWindow()`, then there are background process left not-cleaned-up after the last window is closed on Windows. I have to put that back in. (Unless there is some sneaky more-elegant solution hiding somewhere or that I haven't thought of, suggestions welcome. Have debugger, will test.)

An observation: Most of these event handlers seem to expect to be called more than once. The `'close'` and `'closed'` handlers can run many times with no issue. The `'before-quit'` almost doesn't seem to work fully if not called multiple times. And I've had it called multiple times in a lot (all?) of my non-buggy quits that I've inspected and stepped through.

Not to get into too broad a discussion, but: I wish these handlers were re-written to be simpler, less interdependent on one-another, and most especially I would like to see them reliably work when run only once each.

Code after:
        devMode,
        safeMode,
        profileStartup,
        clearWindowState,
        addToLastWindow,
        env
      });
    }
  }

  // Public: Create a new {AtomWindow} bound to this application.
  createWindow(settings) {
    return new AtomWindow(this, this.fileRecoveryService, settings);
  }

  // Public: Removes the {AtomWindow} from the global window list.
  removeWindow(window) {
    this.windowStack.removeWindow(window);
    if (this.getAllWindows().length === 0 && process.platform !== 'darwin') {
      app.quit();
      return;
    }
    if (!window.isSpec) this.saveCurrentWindowOptions(true);
  }

  // Public: Adds the {AtomWindow} to the global window list.
  addWindow(window) {
    this.windowStack.addWindow(window);
    if (this.applicationMenu)
      this.applicationMenu.addWindow(window.browserWindow);

    window.once('window:loaded', () => {
      this.autoUpdateManager &&
        this.autoUpdateManager.emitUpdateAvailableEvent(window);
    });

    if (!window.isSpec) {
      const focusHandler = () => this.windowStack.touch(window);
      const blurHandler = () => this.saveCurrentWindowOptions(false);
      window.browserWindow.on('focus', focusHandler);
      window.browserWindow.on('blur', blurHandler);
      window.browserWindow.once('closed', () => {
        this.windowStack.removeWindow(window);
        window.browserWindow.removeListener('focus', focusHandler);
        window.browserWindow.removeListener('blur', blurHandler);
      });
      window.browserWindow.webContents.once('did-finish-load', blurHandler);
      this.saveCurrentWindowOptions(false);
    }
  }