2 Replies Latest reply: Mar 27, 2011 1:14 PM by isabelle loyer RSS

    Adding native menus to an AIR application using ActionScript

    isabelle loyer Community Member

      Hi,

       

      I'm working on Mac and PC with Air application.

       

      I'd creates a class extends Menu witch goal is to create a nativeMenu.

       

      My class is

       

      package

      {

          import flash.desktop.DockIcon;

          import flash.desktop.NativeApplication;

          import flash.desktop.SystemTrayIcon;

          import flash.display.BitmapData;

          import flash.display.Loader;

          import flash.display.NativeMenu;

          import flash.display.NativeMenuItem;

          import flash.display.NativeWindow;

          import flash.display.NativeWindowDisplayState;

          import flash.display.NativeWindowSystemChrome;

          import flash.display.Screen;

          import flash.display.Sprite;

          import flash.events.Event;

          import flash.events.MouseEvent;

          import flash.events.NativeWindowDisplayStateEvent;

          import flash.geom.Rectangle;

          import flash.net.URLRequest;

          import flash.sampler.NewObjectSample;

       

       

          import mx.controls.Alert;

          import mx.controls.Menu;

          import mx.core.Application;

          import mx.core.FlexGlobals;

          import mx.core.WindowedApplication;

       

          import spark.components.Application;

          import spark.components.WindowedApplication;

       

       

       

          public class createMenuBar extends Menu

          {

       

       

              public function createMenuBar()

              {

       

              }

       

              /**

               * Initializes the app after loading

               *

               */   

       

              public function initWindow():void{

       

                  // Create root menu

                  var rootMenu:NativeMenu = new NativeMenu();

       

                  // Create root submenus

                  rootMenu.addSubmenu(creerMenuFichier(),"Fichier");

                  rootMenu.addSubmenu(creerMenuEdition(),"Edition");

       

       

                  // Attach event listener routine to root menu

                  rootMenu.addEventListener(Event.SELECT, dispatchMenuCommand);

       

       

                  // Assign application menu (Mac OS X)

       

                  if(NativeApplication.supportsMenu){

                      NativeApplication.nativeApplication.menu = rootMenu;

                  }

       

                  // Assign window menu (MS Windows)

                  if(NativeWindow.supportsMenu ){

                      stage.nativeWindow.menu = rootMenu;

                  }

       

              }

       

       

       

       

       

              /**

               * createMenuCommand()

               * Creates a menu command based on parameters

               *

               */

       

              public function createMenuCommand(menuContainer:NativeMenu,

                  itemLabel: String, itemKey:String,itemModifiers:Array,

                  itemMnemonic:int, selectHandler:Function):NativeMenuItem{

       

                  var cmd:NativeMenuItem = NativeMenu(menuContainer).addItem(new NativeMenuItem(itemLabel));

                  cmd.mnemonicIndex = itemMnemonic; // index de la lettre de rappel du raccourci par dŽfaut 0

                  cmd.keyEquivalent = itemKey;

                  if(itemModifiers !=null){

                      cmd.keyEquivalentModifiers = itemModifiers;

                  }

       

                  if (selectHandler !=null){

                      cmd.addEventListener(Event.SELECT, selectHandler);

                  }

       

                  return cmd;

              }

       

              /**

               * createMenuSeparator()

               * Creates a menu separator

               */ 

       

              private function createMenuSeparator(menuContainer:NativeMenu):NativeMenuItem{

                  var sep : NativeMenuItem = NativeMenu(menuContainer).addItem(new NativeMenuItem("sep", true));

                  return sep;

              }

       

       

              /**

               * Creates the File menu for app

               */   

       

              private function creerMenuFichier(): NativeMenu{

                  var mnu:NativeMenu = new NativeMenu();

                  createMenuCommand(mnu, 'Changer Utilisateur','k',null, 0, hChangeUser);

                  createMenuCommand(mnu, 'Verrouiller/DŽverrouiller','', null, 0, hLock);

                  createMenuCommand(mnu, 'Maintenance','', null, 0, hMaintenance);

       

                  createMenuSeparator(mnu);

       

                  // If Mac OS X, then use Quit label

                  if (NativeApplication.supportsMenu) {

                      createMenuCommand( mnu, 'Quitter', 'q', null, 0, hExit); 

                  }

                  // If Windows, then use Exit

                  else {

                      createMenuCommand( mnu, 'Fermer', 'x', null, 0, hExit);

                  }   

                  return mnu; 

              }

       

              public function creerMenuEdition() : NativeMenu{

                  var mnu:NativeMenu = new NativeMenu();

                  createMenuCommand( mnu, 'Annuler', 'z', null, 0, null); 

                  createMenuCommand( mnu, 'RŽpŽter', 'y', null, 0, null);

                  createMenuSeparator(mnu);   

                  createMenuCommand( mnu, 'Couper', 'x', null, 2, null); 

                  createMenuCommand( mnu, 'Copier', 'c', null, 0, null); 

                  createMenuCommand( mnu, 'Coller', 'v', null, 0, null); 

                  return mnu;

              }

       

       

              /**

               * Catch-all menu dispatcher for all menus

               *

               */ 

              public function dispatchMenuCommand(evt: Event):void {

                  var menuItem:NativeMenuItem = evt.target as NativeMenuItem;

       

                  if (!menuItem.hasEventListener('select')) { 

                      Alert.show(menuItem.label + " a ŽtŽ selectionnŽ!");

                  }

              }    

       

              /**

               * Simple handlers for certain menu commands

               *

               */     

              public function hChangeUser(evt: Event):void {

                  Alert.show( "Vous allez changer d'utilisateur");

              }          

       

       

          }

      }

       

       

      On main air app.mxml file I create menu with

       

      var menu : createMenuBar = new createMenuBar();

        menu.initWindow();

       

       

      application menu appear but when I work on Windows system, windows menu is not visible.

       

      Can you help me to solve that?

       

      Thanks

        • 1. Re: Adding native menus to an AIR application using ActionScript
          injpix Community Member

          I believe you ran into an AIR limitation.  In the class header for NativeMenu it mentions:

           

          A native menu is a menu that is controlled and drawn by the operating system rather than by your application.       AIR supports the following types of native menus:

           

           

          • Application menus are supported on OS X. Use the NativeApplication.supportsMenu property to test whether        application menus are supported on the host operating system. An  application menu is displayed on the Menu bar at the top of the        Mac desktop. OS X provides a default menu for every application, but  many of the menu commands are not functional. You can add       event listeners to the default items, replace individual menus and  items, or even replace the default menu entirely.       Access the application menu object using the NativeApplication menu property.
          • Window menus are supported on Windows and Linux. Use the NativeWindow.supportsMenu property to       test whether window menus are supported on the host operating system. A  window menu is displayed below the window title bar. The area       occupied by the menu is not part of the window stage. Applications  cannot draw into this area. Assign a menu to a window using the        NativeWindow menu property.


          -ref: http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/flash/display/Nati veMenu.html

           

          Use the NativeMenu.isSupported property to check if OS supports it.  There is also NativeWindow.supportsMenu property that may help you.  I hope that helps.

          • 2. Re: Adding native menus to an AIR application using ActionScript
            isabelle loyer Community Member

            thanks for this answer.

             

            In my case even if I do this test the problem is the same

            indeed my air application is transparent, so I don't find how to work native menu in this context

             

            thanks for helping