Repository: electron
Issue: macOS: ability to determine when app is being quit from the Dock icon
Labels: enhancement :sparkles:

<!--  As an open source project with a dedicated but small maintainer team, it can sometimes take a long time for issues to be addressed so please be patient and we will get back to you as soon as we can.
-->

### Preflight Checklist
<!-- Please ensure you've completed the following steps by replacing [ ] with [x]-->

* [x] I have read the [Contributing Guidelines](https://github.com/electron/electron/blob/master/CONTRIBUTING.md) for this project.
* [x] I agree to follow the [Code of Conduct](https://github.com/electron/electron/blob/master/CODE_OF_CONDUCT.md) that this project adheres to.
* [x] I have searched the issue tracker for a feature request that matches the one I want to file, without success.

### Problem Description
<!-- Is your feature request related to a problem? Please add a clear and concise description of what the problem is. -->
My team is working on an app that makes use of Electron's Tray functionality. One of the desired behaviors that we have for macOS is that the user should have an option they can enable called "Run in Background After Quitting". When the option is enabled, if the user selects the Quit option from the dock or the application menu, then the app should close all open windows, hide the dock icon, and continue to do work in the background with the Tray icon still visible. Selecting the Quit option from the Tray menu will completely quit the app.

The way we initially implemented this is to have a flag that gets set when the user quits from the Tray, and if the flag is not set when the before-quit event is emitted then we prevent the app from quitting and instead we just close all open windows and hide the dock icon. However, this causes issues because it will also prevent the app from quitting in undesired scenarios, such as when the user tries to close the app from Activity Monitor, or when there is an app store update.

Ideally we could flip the logic so that we will allow the app to quit unless the app is quit from the dock or the application menu, however with Electron there doesn't seem to be a way to differentiate between the app being told to quit because the user selected "Quit" from the dock icon, and the app being told to quit because e.g. the user is trying to install an app store update while the app is running.

### Proposed Solution
<!-- Describe the solution you'd like in a clear and concise manner -->

To resolve our problem I think we would need Electron to somehow expose a way for us to determine when the app is being quit from the dock icon.

I am not overly familiar with Cocoa or the inner-workings of Electron, but perhaps Electron could implement [applicationShouldTerminate()](https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428642-applicationshouldterminate?language=objc) on the [Electron app delegate](https://github.com/electron/electron/blob/master/shell/browser/mac/electron_application_delegate.mm) and set a flag or emit an event when the app is being quit from the dock. This StackOverflow post describes how to determine when a Cocoa app is being quit from the dock: https://stackoverflow.com/a/57102139

### Alternatives Considered
<!-- A clear and concise description of any alternative solutions or features you've considered. -->
We have investigated using a native Node add-on to determine when the app is quitting from the dock, but we have been unsuccessful thus far. It is unclear if it is even possible to implement this behavior using a native add-on since it appears necessary to implement `applicationShouldTerminate()`, which I think would have to be defined in Electron's app delegate.

### Additional Information
<!-- Add any other context about the problem here. -->
N/A