Permalink
Browse files

Wrappers for menu and tray

  • Loading branch information...
1 parent ad5a9fa commit 0c6a9eb670397171caeb0ce370e16f52cea1769a @milani milani committed Nov 6, 2012
Showing with 123 additions and 28 deletions.
  1. +56 −20 examples/hello-world/app.js
  2. +18 −8 lib/App.js
  3. +49 −0 lib/menu.js
@@ -2,50 +2,86 @@ var app = module.exports = require('appjs');
app.serveFilesFrom(__dirname + '/content');
-var window = app.createWindow({
- width : 640,
- height : 460,
- icons : __dirname + '/content/icons'
-});
-
-var menu = app.createMenu([{
- label:'_File',
+var menubar = app.createMenu([{
+ label:'&File',
submenu:[
{
- label:'E_xit',
+ label:'E&xit',
action: function(){
window.close();
}
}
]
},{
- label:'_Edit',
+ label:'&Window',
submenu:[
{
- label:'Undo'
- },{
- label:''//separator
+ label:'Fullscreen',
+ action:function(item) {
+ window.frame.fullscreen();
+ console.log(item.label+" called.");
+ }
},
{
- label:'Copy',
- action:function(item){
- console.log("item "+item.label+" clicked");
+ label:'Minimize',
+ action:function(){
+ window.frame.minimize();
}
},
{
- label:'Paste',
- action:function(item){
- console.log("item "+item.label+" clicked");
+ label:'Maximize',
+ action:function(){
+ window.frame.maximize();
+ }
+ },{
+ label:''//separator
+ },{
+ label:'Restore',
+ action:function(){
+ window.frame.restore();
}
}
]
}]);
+menubar.on('select',function(item){
+ console.log("menu item "+item.label+" clicked");
+});
+
+var trayMenu = app.createMenu([{
+ label:'Show',
+ action:function(){
+ window.frame.show();
+ },
+},{
+ label:'Minimize',
+ action:function(){
+ window.frame.hide();
+ }
+},{
+ label:'Exit',
+ action:function(){
+ window.close();
+ }
+}]);
+
+var statusIcon = app.createStatusIcon({
+ icon:'./data/content/icons/32.png',
+ tooltip:'AppJS Hello World',
+ menu:trayMenu
+});
+
+var window = app.createWindow({
+ width : 640,
+ height : 460,
+ icons : __dirname + '/content/icons'
+});
+
window.on('create', function(){
console.log("Window Created");
window.frame.show();
window.frame.center();
- window.frame.setMenuBar(menu);
+ window.frame.setMenuBar(menubar);
});
window.on('ready', function(){
View
@@ -1,6 +1,7 @@
var EventEmitter = require('events').EventEmitter,
Router = require('./router').Router,
Window = require('./window'),
+ Menu = require('./menu'),
_App = require('./bindings').App,
NativeWindow = require('./bindings').NativeWindow,
NativeMenu = require('./bindings').NativeMenu,
@@ -12,7 +13,8 @@ var _init = require('./bindings').init,
decorate = require('./utils').decorate,
inherit = require('./utils').inherit,
isObject = require('./utils').isObject,
- staticRouter = require('./router').staticRouter;
+ staticRouter = require('./router').staticRouter,
+ pathResolve = require('path').resolve;
var concat = Array.prototype.concat;
@@ -38,16 +40,24 @@ function App(){
inherit(App, EventEmitter, [
function createMenu(options){
+ if (!this.settings) {
+ this.init();
+ }
+
var self = this,
- nativeMenu = new NativeMenu(options);
- inherit(nativeMenu, EventEmitter, [
- function toString(){
- return '[object Menu]';
- }
- ]);
- return nativeMenu;
+ menu = new Menu(options);
+
+ return menu;
},
function createStatusIcon(options){
+ if (!this.settings) {
+ this.init();
+ }
+
+ if(options.icon) {
+ options.icon = pathResolve(options.icon);
+ }
+
var self = this,
nativeStatusIcon = new NativeStatusIcon(options);
inherit(nativeStatusIcon, EventEmitter, [
View
@@ -0,0 +1,49 @@
+var fs = require('fs'),
+ path = require('path');
+
+var NativeMenu = require('./bindings').NativeMenu,
+ EventEmitter = require('events').EventEmitter,
+ inherit = require('./utils').inherit;
+
+module.exports = Menu;
+
+function Menu(options){
+
+ normalizeItems(options);
+
+ var nativeMenu = new NativeMenu(options);
+
+ inherit(nativeMenu, EventEmitter, [
+ function toString(){
+ return '[object Menu]';
+ }
+ ]);
+
+ return nativeMenu;
+}
+
+var normalizeItems = function(options){
+ normalizeMnemonic(options);
+ normalize(options);
+}
+
+var normalizeMnemonic = function(options){
+
+ if( process.platform != 'linux') {
+ return;
+ }
+
+ for(i in options) {
+ options[i]['label'] = options[i]['label'].replace(/(&)(\w)/g,'_$2');
+ normalizeMnemonic(options[i]['submenu']);
+ }
+}
+
+var normalize = function(options){
+ for(i in options) {
+ if(options[i].hasOwnProperty('icon')){
+ options[i]['icon'] = path.resolve(options[i]['icon']);
+ }
+ normalize(options[i]['submenu']);
+ }
+}

0 comments on commit 0c6a9eb

Please sign in to comment.