refactor the exit of nvda and gui.terminate (#12286)
Summary of the issue:
When restarting NVDA, WM_QUIT is posted as an event to the window, forcibly exiting the app. This leaves objects such as the system tray icon left behind.
Additionally, changes introduced in #12183
- caused the braille viewer to be closed without saving state properly
- lost code that destroyed the system tray and menu in some instances
- made most of gui.terminate no longer necessary/redundant
Description of how this pull request fixes the issue:
- A windows event winUser.WM_EXIT_NVDA is registered that triggers safeAppExit and can be called across instances of NVDA.
- move the safe destruction of the brailleviewer to safeAppExit so that it is exited properly before destruction
- reintroduce the destruction of the system tray icon and menu, and remove the icon manually.
- ensured safeAppExit is not called from gui.terminate if it has been called elsewhere to terminate the app. WM_QUIT is the other way to exit the MainLoop other than safeAppExit
- removed restarting the MainLoop in gui.terminate to process pending events as this doesn't work.
Known issues with pull request:
- When starting a new instance of NVDA with an existing instance running, where one is version <2020.4, NVDA will not exit safely. Instead, the running NVDA copy will terminate directly using the behaviour of 2020.4. This is because WM_EXIT_NVDA won't be registered on the older instance.
- Issues with terminating NVDA across instances cannot be logged properly as the loghandler hasn't been initialized