HumbleUI / JWM

Cross-platform window management and OS integration library for Java
Apache License 2.0
546 stars 44 forks source link

Intercept CMD-Q in OS/X #291

Open sufrin opened 4 weeks ago

sufrin commented 4 weeks ago

First: thanks to all contributors. JWM does indeed play nicely with Skija (I've been making a UI toolkit for Scala -- soon to go open).

I've been unable to intercept CMD-Q in order to ask the user what she wants to do next (I can, of course, handle window-close requests from the traffic-light). The usual code for installing a quit handler runs without reporting any problem: but cmd-Q just quits the app. I realize there's no reason I should expect the awt.desktop stuff to work; but that's all I have.

   /**
     * This doesn't fail; but it doesn't prevent CMD-Q from torpedoing an in-flight app.
     */
    locally {
      if (java.awt.Desktop.isDesktopSupported) {
        val desk = java.awt.Desktop.getDesktop
        desk.disableSuddenTermination()
        if (!desk.isSupported(java.awt.Desktop.Action.APP_QUIT_HANDLER)) println(s"Can't handle Quit")
        desk.setQuitHandler(new java.awt.desktop.QuitHandler {
          def handleQuitRequestWith(e: QuitEvent, response: QuitResponse): Unit = {
            println(s"Quit: $e")
            if (false) response.performQuit() else response.cancelQuit()
          }
        })
      } else println("Desktop not supported")
tonsky commented 4 weeks ago

Yeah there needs to be a special handler for that which we don’t have yet

https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428642-applicationshouldterminate?language=objc