Monday, August 22, 2011

More Adventures with an Adobe AIR on Mac Shutdown Issue

My friend Christian looked into an issue I was having with Adobe AIR on Mac. Specifically, the application I created was canceling the shutdown process - obviously not what I intended.

Christian suggested a few solutions to this problem, and after playing with it further, I'd like to suggest yet another work around.

Here's what's been working for me:

napp.openedWindows[0].addEventListener(Event.CLOSING,
   function(e:Event):void {
     var w:NativeWindow = e.currentTarget as NativeWindow;
     if(w.active) {
       e.preventDefault();
       napp.openedWindows[0].visible = false;
     } else {
       napp.exit();
     }
   });

(Assume: napp is an instance of NativeApplication, which is trivial to get a hold of).

The work around I'm using is to check to see if the window that delivered the closing event is active. When you click on the X in the title bar, you're requesting a close, and thanks to the click, you've brought the window into focus and therefore into the active state. So, in that case, I simply hide the window.

On the other hand, when the OS requests the window to close, it doesn't (at least not in my testing) bring the window into focus or make active. Therefore, when the event comes from an inactive window, I directly invoke NativeApplication.exit.

Technically, I could probably avoid the explicit exit request, and do as Christian suggested and just avoid the call to e.preventDefault(). But, for my app's purposes, the explicit exit call works.

Thanks again to Christian for making huge headway on this issue, and to Adobe to always being responsive to any sort of questions/issues I may have.

No comments:

Post a Comment