109 lines
3.5 KiB
JavaScript
109 lines
3.5 KiB
JavaScript
|
var MenuItem, methodInBrowserWindow, nextCommandId, rolesMap, v8Util;
|
||
|
|
||
|
v8Util = process.atomBinding('v8_util');
|
||
|
|
||
|
nextCommandId = 0;
|
||
|
|
||
|
|
||
|
/* Maps role to methods of webContents */
|
||
|
|
||
|
rolesMap = {
|
||
|
undo: 'undo',
|
||
|
redo: 'redo',
|
||
|
cut: 'cut',
|
||
|
copy: 'copy',
|
||
|
paste: 'paste',
|
||
|
selectall: 'selectAll',
|
||
|
minimize: 'minimize',
|
||
|
close: 'close'
|
||
|
};
|
||
|
|
||
|
|
||
|
/* Maps methods that should be called directly on the BrowserWindow instance */
|
||
|
|
||
|
methodInBrowserWindow = {
|
||
|
minimize: true,
|
||
|
close: true
|
||
|
};
|
||
|
|
||
|
MenuItem = (function() {
|
||
|
MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio'];
|
||
|
|
||
|
function MenuItem(options) {
|
||
|
var Menu, click, ref;
|
||
|
Menu = require('electron').Menu;
|
||
|
click = options.click, this.selector = options.selector, this.type = options.type, this.role = options.role, this.label = options.label, this.sublabel = options.sublabel, this.accelerator = options.accelerator, this.icon = options.icon, this.enabled = options.enabled, this.visible = options.visible, this.checked = options.checked, this.submenu = options.submenu;
|
||
|
if ((this.submenu != null) && this.submenu.constructor !== Menu) {
|
||
|
this.submenu = Menu.buildFromTemplate(this.submenu);
|
||
|
}
|
||
|
if ((this.type == null) && (this.submenu != null)) {
|
||
|
this.type = 'submenu';
|
||
|
}
|
||
|
if (this.type === 'submenu' && ((ref = this.submenu) != null ? ref.constructor : void 0) !== Menu) {
|
||
|
throw new Error('Invalid submenu');
|
||
|
}
|
||
|
this.overrideReadOnlyProperty('type', 'normal');
|
||
|
this.overrideReadOnlyProperty('role');
|
||
|
this.overrideReadOnlyProperty('accelerator');
|
||
|
this.overrideReadOnlyProperty('icon');
|
||
|
this.overrideReadOnlyProperty('submenu');
|
||
|
this.overrideProperty('label', '');
|
||
|
this.overrideProperty('sublabel', '');
|
||
|
this.overrideProperty('enabled', true);
|
||
|
this.overrideProperty('visible', true);
|
||
|
this.overrideProperty('checked', false);
|
||
|
if (MenuItem.types.indexOf(this.type) === -1) {
|
||
|
throw new Error("Unknown menu type " + this.type);
|
||
|
}
|
||
|
this.commandId = ++nextCommandId;
|
||
|
this.click = (function(_this) {
|
||
|
return function(focusedWindow) {
|
||
|
|
||
|
/* Manually flip the checked flags when clicked. */
|
||
|
var methodName, ref1, ref2;
|
||
|
if ((ref1 = _this.type) === 'checkbox' || ref1 === 'radio') {
|
||
|
_this.checked = !_this.checked;
|
||
|
}
|
||
|
if (_this.role && rolesMap[_this.role] && process.platform !== 'darwin' && (focusedWindow != null)) {
|
||
|
methodName = rolesMap[_this.role];
|
||
|
if (methodInBrowserWindow[methodName]) {
|
||
|
return focusedWindow[methodName]();
|
||
|
} else {
|
||
|
return (ref2 = focusedWindow.webContents) != null ? ref2[methodName]() : void 0;
|
||
|
}
|
||
|
} else if (typeof click === 'function') {
|
||
|
return click(_this, focusedWindow);
|
||
|
} else if (typeof _this.selector === 'string') {
|
||
|
return Menu.sendActionToFirstResponder(_this.selector);
|
||
|
}
|
||
|
};
|
||
|
})(this);
|
||
|
}
|
||
|
|
||
|
MenuItem.prototype.overrideProperty = function(name, defaultValue) {
|
||
|
if (defaultValue == null) {
|
||
|
defaultValue = null;
|
||
|
}
|
||
|
return this[name] != null ? this[name] : this[name] = defaultValue;
|
||
|
};
|
||
|
|
||
|
MenuItem.prototype.overrideReadOnlyProperty = function(name, defaultValue) {
|
||
|
if (defaultValue == null) {
|
||
|
defaultValue = null;
|
||
|
}
|
||
|
if (this[name] == null) {
|
||
|
this[name] = defaultValue;
|
||
|
}
|
||
|
return Object.defineProperty(this, name, {
|
||
|
enumerable: true,
|
||
|
writable: false,
|
||
|
value: this[name]
|
||
|
});
|
||
|
};
|
||
|
|
||
|
return MenuItem;
|
||
|
|
||
|
})();
|
||
|
|
||
|
module.exports = MenuItem;
|