Skip to content

Instantly share code, notes, and snippets.

@asus4
Last active November 14, 2017 19:03
Show Gist options
  • Save asus4/9223707 to your computer and use it in GitHub Desktop.
Save asus4/9223707 to your computer and use it in GitHub Desktop.
Create Air apllication's native menu easy
package com.github.asus4.aslib.app
{
import flash.desktop.NativeApplication;
import flash.display.NativeMenu;
import flash.display.NativeMenuItem;
import flash.events.Event;
/**
* Cretae Native Menu easy
*
* @author asus4
*
*/
public class NativeMenuBuilder
{
private var _root:NativeMenu;
public function NativeMenuBuilder(root:NativeMenu=null)
{
if(root == null) {
root = new NativeMenu();
}
_root = root;
}
public function commit():void {
NativeApplication.nativeApplication.menu = _root;
}
public function addMenu(path:String, keyEquivalent:String, keyEquivalentModifiers:Array, func:Function):NativeMenuBuilder {
var paths:Array = path.split("/");
var parent:NativeMenu = _root;
for(var i:int=0; i<paths.length-1; ++i) {
if(paths[i] == "") {
continue;
}
parent = setMenu(parent, paths[i]);
}
var item:NativeMenuItem = new NativeMenuItem(paths[paths.length-1]);
item.keyEquivalent = keyEquivalent;
item.keyEquivalentModifiers = keyEquivalentModifiers;
item.addEventListener(Event.SELECT, function(e:Event):void {
func();
}, false, 0, true);
parent.addItem(item);
return this;
}
private function setMenu(parent:NativeMenu, label:String):NativeMenu {
var menu:NativeMenu;
for each(var item:NativeMenuItem in parent.items) {
if(item.label == label) {
menu = item.submenu;
}
}
if(menu == null) {
menu = new NativeMenu();
parent.addSubmenu(menu, label);
}
return menu;
}
static public function get currentMenu():NativeMenu {
return NativeApplication.nativeApplication.menu;
}
}
}
var buidler:NativeMenuBuilder = new NativeMenuBuilder();
buidler.addMenu("/Appname/Quit", "q", [Keyboard.COMMAND], function():void{
NativeApplication.nativeApplication.exit();
});
buidler.addMenu("/Window/Fullscreen", "f", [Keyboard.COMMAND], function():void {
if(stage.displayState == StageDisplayState.FULL_SCREEN_INTERACTIVE) {
stage.displayState = StageDisplayState.NORMAL;
}
else {
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
}
});
buidler.commit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment