Handle events better, get ready for save support.

  * Move window/GUI logic to main application.
  * Detect window close requests.
  * Allow plugins to gracefully shutdown.