tags:

views:

157

answers:

2

I'm developing an AIR application which uses multiple windows. I'm running into an issue where I want to open new windows (toaster notifications for example) when the primary application window is not visible, but the behavior is different depending on how the window is closed.

When a user hides all application windows with CMD-H, opening a new window causes all application windows to come back to the foreground (instead of just that new window, like I would expect). If the user closed a window with CMD-W, however, that window does not become visible when I open a new window.

Is there a way to either 1) tell when the user uses cmd-h to hide all windows OR 2) tell whether a window is hidden using cmd-h vs. closed cmd-w?

Thanks

+1  A: 

Check out this post: http://stackoverflow.com/questions/1338152/detecting-different-quit-options-in-air-application-on-mac/1341543#1341543

maclema
That question was also mine, strangely enough =) I would ideally like to avoid using any strange invisible window tricks to accomplish this task (which only deals with cmd-w vs cmd-h, not with exiting the app completely), but I may give it a try anyway
Chris R
A: 

I actually just figured out a good answer to this problem. Apparently, the reason cmd-H and cmd-W don't trigger keyDown events are because they are capturee and stopped by the native application menu.

By default, several "normal" mac OS menu options are put into AIR applications by the framework - these include cmd-w to close the window, cmd-h to hide and shortcuts around copy/cut/paste. In order to avoid the default behavior, I either removed these menu options or changed their key equivalents (the shortcut combination that triggers them).

The code to add a preferences shortcut (cmd-,), override cmd-w, change cmd-w to cmd-shift-w, and override the cmd-h functionality looks like this:

if (NativeApplication.supportsMenu) {
    var prefItem:NativeMenuItem = new NativeMenuItem("Preferences...");
    prefItem.addEventListener(Event.SELECT, handlePreferencesMenuSelect);
    prefItem.keyEquivalent = ",";

    var closeItem:NativeMenuItem = new NativeMenuItem("Close Tab");
    closeItem.addEventListener(Event.SELECT, handleCloseTabMenuSelect);
    closeItem.keyEquivalent = "w";

    // Add the preferences option under the first menu
    // Also add a spacer line (like most other applications)
    // Also change the hide command to our own handler
    var baseMenu:NativeMenuItem = NativeMenuItem(NativeApplication.nativeApplication.menu.items[0]);
    baseMenu.submenu.addItemAt(new NativeMenuItem("", true), 1);
    baseMenu.submenu.addItemAt(prefItem, 2);
    for (var idx:String in baseMenu.submenu.items) {
        var menuItem:NativeMenuItem = baseMenu.submenu.items[idx]; 
        if (menuItem && menuItem.keyEquivalent == 'h' && menuItem.keyEquivalentModifiers.length == 1) {
            baseMenu.submenu.removeItemAt(int(idx));

            var hideItem:NativeMenuItem = new NativeMenuItem("Hide Application");
            hideItem.addEventListener(Event.SELECT, handleHideWindowSelect);
            hideItem.keyEquivalent = "h";
            baseMenu.submenu.addItemAt(hideItem, int(idx));
        }
    }

    // Set the close window shortcut to cmd+shift+w, instead of cmd+w
    var fileMenu:NativeMenuItem = NativeMenuItem(NativeApplication.nativeApplication.menu.items[1]);
    NativeMenuItem(fileMenu.submenu.getItemAt(0)).keyEquivalent = 'W';
    fileMenu.submenu.addItem(closeItem);
}

Thanks for the help figuring it out.

Chris R