2009-08-26 55 views
2

我有一個在Mac上運行的AIR應用程序,我希望在某人「關閉」應用程序時隱藏窗口的行爲(例如,點擊紅色的「x」按鈕或cmd -w)。但是,如果有人點擊了cmd-q或從dock上下文菜單或頂級菜單中選擇了「Quit」,我希望應用程序實際關閉。在Mac上檢測AIR應用程序中的不同退出選項

我可以阻止應用程序發送的「關閉」事件的默認值,但是,這會導致所有「關閉」方法隱藏窗口。當前關閉應用程序的唯一方法是ForceQuit(或通過我提供的單獨界面,例如停靠欄圖標上的上下文菜單選項)。

我也嘗試手動捕獲cmd-q keyDown事件,但它不會被髮送。另外,這對於人們嘗試使用菜單選項退出應用程序時無效。此外,如果我在關閉方法上使用preventDefault,它會導致我的應用程序立即取消關閉進程(這是一種糟糕的用戶體驗)。

有沒有辦法檢測關閉AIR應用程序的不同方法?我希望能夠分辨這些關閉方法之間的區別,並對相應的問題做出反應。

+0

請說明一下:您正試圖執行標準的Mac行爲,關閉窗口不是關閉應用程序的同義詞嗎?隱藏是你對應用程序做的事情,與關閉應用程序或應用程序的窗口不同。 – user57368 2009-08-26 23:51:38

回答

6

嘗試此閉幕,從我的理解有/是在框架中的bug,因此如果包括AIR更新它打破CMD-Q的支持,線程使用在這裏:http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=72&catid=670&threadid=1373568

這可能會或可能不適用於您的情況。

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(); 
      } 
    }); 
+0

EXITING與CLOSING事件非常有用(出於某種原因,Flex Builder在WindowedApplication組件的自動完成中不顯示退出選項)。這對我有效。 – 2009-08-28 19:04:43

+0

謝謝!我一直在努力解決這個問題好幾個月!我不知道這是一個更新問題。這是提到它的線程:http://forums.adobe.com/thread/246228?tstart=0 – leolobato 2009-08-31 12:14:19

1

試試這個,我確定有一個更好的方法來處理這個,但這對我來說很有效。

<?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> 
+0

感謝您的代碼 - 我其實不需要做隱藏的窗口部分,因爲CLOSING事件的preventDefault爲我處理。我遇到的主要問題是EXITING事件。我只需要添加一個循環來關閉EXITING事件中的所有窗口。出於某種原因,即使在EXITING事件中調用nativeApplication.exit()時,調用preventDefault on CLOSING事件也會使應用程序保持打開狀態。 – 2009-08-28 19:03:11

相關問題