2020-03-20 20:28:31 +00:00
|
|
|
'use strict';
|
2016-03-10 19:54:17 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const roles = require('@electron/internal/browser/api/menu-item-roles');
|
2016-06-22 17:45:01 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let nextCommandId = 0;
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-06-21 22:41:37 +00:00
|
|
|
const MenuItem = function (options) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { Menu } = require('electron');
|
2016-06-21 00:16:34 +00:00
|
|
|
|
2016-10-10 21:40:49 +00:00
|
|
|
// Preserve extra fields specified by user
|
2018-10-02 01:56:31 +00:00
|
|
|
for (const key in options) {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (!(key in this)) this[key] = options[key];
|
2016-10-10 21:40:49 +00:00
|
|
|
}
|
2017-12-31 18:23:32 +00:00
|
|
|
if (typeof this.role === 'string' || this.role instanceof String) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this.role = this.role.toLowerCase();
|
2017-12-31 18:23:32 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
this.submenu = this.submenu || roles.getDefaultSubmenu(this.role);
|
2016-06-21 22:41:37 +00:00
|
|
|
if (this.submenu != null && this.submenu.constructor !== Menu) {
|
2020-05-04 15:19:21 +00:00
|
|
|
this.submenu = Menu.buildFromTemplate(this.submenu);
|
2016-06-21 22:41:37 +00:00
|
|
|
}
|
|
|
|
if (this.type == null && this.submenu != null) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this.type = 'submenu';
|
2016-06-21 22:41:37 +00:00
|
|
|
}
|
|
|
|
if (this.type === 'submenu' && (this.submenu == null || this.submenu.constructor !== Menu)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('Invalid submenu');
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
this.overrideReadOnlyProperty('type', 'normal');
|
|
|
|
this.overrideReadOnlyProperty('role');
|
|
|
|
this.overrideReadOnlyProperty('accelerator');
|
|
|
|
this.overrideReadOnlyProperty('icon');
|
|
|
|
this.overrideReadOnlyProperty('submenu');
|
2016-05-25 23:20:49 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
this.overrideProperty('label', roles.getDefaultLabel(this.role));
|
|
|
|
this.overrideProperty('sublabel', '');
|
|
|
|
this.overrideProperty('toolTip', '');
|
|
|
|
this.overrideProperty('enabled', true);
|
|
|
|
this.overrideProperty('visible', true);
|
|
|
|
this.overrideProperty('checked', false);
|
|
|
|
this.overrideProperty('acceleratorWorksWhenHidden', true);
|
|
|
|
this.overrideProperty('registerAccelerator', roles.shouldRegisterAccelerator(this.role));
|
2016-06-21 22:41:37 +00:00
|
|
|
|
|
|
|
if (!MenuItem.types.includes(this.type)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Unknown menu item type: ${this.type}`);
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
this.overrideReadOnlyProperty('commandId', ++nextCommandId);
|
2016-06-21 22:41:37 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const click = options.click;
|
2016-07-13 19:18:44 +00:00
|
|
|
this.click = (event, focusedWindow, focusedWebContents) => {
|
2016-06-21 22:41:37 +00:00
|
|
|
// Manually flip the checked flags when clicked.
|
|
|
|
if (this.type === 'checkbox' || this.type === 'radio') {
|
2020-03-20 20:28:31 +00:00
|
|
|
this.checked = !this.checked;
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-06-21 22:41:37 +00:00
|
|
|
|
2016-07-13 19:18:44 +00:00
|
|
|
if (!roles.execute(this.role, focusedWindow, focusedWebContents)) {
|
2016-06-22 20:48:26 +00:00
|
|
|
if (typeof click === 'function') {
|
2020-03-20 20:28:31 +00:00
|
|
|
click(this, focusedWindow, event);
|
2016-06-22 20:48:26 +00:00
|
|
|
} else if (typeof this.selector === 'string' && process.platform === 'darwin') {
|
2020-03-20 20:28:31 +00:00
|
|
|
Menu.sendActionToFirstResponder(this.selector);
|
2016-06-21 22:41:37 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
|
|
|
};
|
2016-06-21 22:41:37 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio'];
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-07-02 02:47:40 +00:00
|
|
|
MenuItem.prototype.getDefaultRoleAccelerator = function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
return roles.getDefaultAccelerator(this.role);
|
|
|
|
};
|
2016-07-02 02:47:40 +00:00
|
|
|
|
2016-10-05 19:24:08 +00:00
|
|
|
MenuItem.prototype.overrideProperty = function (name, defaultValue = null) {
|
2016-06-21 22:41:37 +00:00
|
|
|
if (this[name] == null) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this[name] = defaultValue;
|
2016-06-21 22:41:37 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-06-21 22:41:37 +00:00
|
|
|
|
|
|
|
MenuItem.prototype.overrideReadOnlyProperty = function (name, defaultValue) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this.overrideProperty(name, defaultValue);
|
2016-06-21 22:41:37 +00:00
|
|
|
Object.defineProperty(this, name, {
|
|
|
|
enumerable: true,
|
|
|
|
writable: false,
|
|
|
|
value: this[name]
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
};
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
module.exports = MenuItem;
|