Detecting various output options in an AIR application on Mac

I have an AIR application running on a Mac and I want the behavior to hide the window when someone "closes" the application (for example, hits a red "x" or cmd-w button). However, if someone hits cmd-q or selects "Quit" from the dock context menu or top level menu, I want the application to actually close.

I can prevent Default for the "close" event dispatched by the application, however this causes all "closed" methods to simply hide the window. The only way to close the app at this point is by ForceQuit (or through a separate interface I provide, such as the context menu option on the dock icon).

I also tried to capture the cmd-q keyDown event manually, but it is not dispatched. Also, it won't help if people try to log out of the app using menu options.

Also, if I prevent the Default method on the close method, it causes my application to immediately cancel the closing process (which is a terrible user experience).

Is there a way to detect the different methods for closing an AIR application? I want to be able to distinguish between these closing methods and react accordingly.

+2


source to share


2 answers


Try this to close, from what I understand there was / is a bug in the framework so that if you enable AIR update it breaks support for cmd-q, with this thread being used here: http: //www.adobe. com / cfusion / webforums / forum / messageview.cfm? forumid = 72 & catid = 670 & threadid = 1373568

This may or may not be applicable to your situation.



NativeApplication.nativeApplication.addEventListener(Event.EXITING, 
        function(e:Event):void {
            var opened:Array = NativeApplication.nativeApplication.openedWindows;
            for (var i:int = 0; i < opened.length; i ++) {
                opened[i].close();
            }
    });

      

+6


source


Try this, I'm sure there must be a better way to handle this, but it worked for me.



<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onCreationComplete()">
    <mx:Script>
        <![CDATA[
            import mx.core.Application;
            import mx.events.AIREvent;
            import mx.core.Window;

            private function onCreationComplete():void {
                addMacSupport();
            }

            private var macsupport_allowExit:Boolean = false;

            private function addMacSupport():void {
                if ( Capabilities.os.indexOf("Mac") == 0 ) {
                    //open a hidden window that will prevent the application from
                    //exiting when the user presses Cmd+W
                    var win:Window = new Window();
                    win.visible = false;
                    win.open(false);

                    //add a closing listener on the hidden window, this event will only
                    //be fired when the user pressed Cmd+Q or selects quit from the menu
                    //then set macsupport_allowExit to true
                    win.addEventListener(Event.CLOSING, function(e:Event):void {
                        macsupport_allowExit = true;
                    });

                    //add an event listener to this window on closing
                    addEventListener(Event.CLOSING, function(e:Event):void {
                        //always preventDefault
                        e.preventDefault();

                        //wait one frame, then check the macsupport_allowExit variable
                        //if it is true, we nedd to exit the app, otherwise just hide
                        //the app window
                        callLater(function():void {
                            if ( macsupport_allowExit ) {
                                nativeApplication.exit();
                            }
                            else {
                                nativeWindow.visible = false;
                            }
                        });
                    });

                    //add an event listener for INVOKE to show our main app window
                    //when the dock icon is clicked.
                    addEventListener(InvokeEvent.INVOKE, function(e:InvokeEvent):void {
                        if ( nativeWindow && !nativeWindow.visible ) {
                            nativeWindow.visible = true;
                            nativeWindow.activate();
                        }
                    });
                }
            }
        ]]>
    </mx:Script>
</mx:WindowedApplication>

      

+1


source







All Articles