menu-item.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import * as roles from '@electron/internal/browser/api/menu-item-roles';
  2. import { Menu, Event, BrowserWindow, WebContents } from 'electron/main';
  3. let nextCommandId = 0;
  4. const MenuItem = function (this: any, options: any) {
  5. // Preserve extra fields specified by user
  6. for (const key in options) {
  7. if (!(key in this)) this[key] = options[key];
  8. }
  9. if (typeof this.role === 'string' || this.role instanceof String) {
  10. this.role = this.role.toLowerCase();
  11. }
  12. this.submenu = this.submenu || roles.getDefaultSubmenu(this.role);
  13. if (this.submenu != null && this.submenu.constructor !== Menu) {
  14. this.submenu = Menu.buildFromTemplate(this.submenu);
  15. }
  16. if (this.type == null && this.submenu != null) {
  17. this.type = 'submenu';
  18. }
  19. if (this.type === 'submenu' && (this.submenu == null || this.submenu.constructor !== Menu)) {
  20. throw new Error('Invalid submenu');
  21. }
  22. this.overrideReadOnlyProperty('type', roles.getDefaultType(this.role));
  23. this.overrideReadOnlyProperty('role');
  24. this.overrideReadOnlyProperty('accelerator');
  25. this.overrideReadOnlyProperty('icon');
  26. this.overrideReadOnlyProperty('submenu');
  27. this.overrideProperty('label', roles.getDefaultLabel(this.role));
  28. this.overrideProperty('sublabel', '');
  29. this.overrideProperty('toolTip', '');
  30. this.overrideProperty('enabled', true);
  31. this.overrideProperty('visible', true);
  32. this.overrideProperty('checked', false);
  33. this.overrideProperty('acceleratorWorksWhenHidden', true);
  34. this.overrideProperty('registerAccelerator', roles.shouldRegisterAccelerator(this.role));
  35. if (!MenuItem.types.includes(this.type)) {
  36. throw new Error(`Unknown menu item type: ${this.type}`);
  37. }
  38. this.overrideReadOnlyProperty('commandId', ++nextCommandId);
  39. const click = options.click;
  40. this.click = (event: Event, focusedWindow: BrowserWindow, focusedWebContents: WebContents) => {
  41. // Manually flip the checked flags when clicked.
  42. if (!roles.shouldOverrideCheckStatus(this.role) &&
  43. (this.type === 'checkbox' || this.type === 'radio')) {
  44. this.checked = !this.checked;
  45. }
  46. if (!roles.execute(this.role, focusedWindow, focusedWebContents)) {
  47. if (typeof click === 'function') {
  48. click(this, focusedWindow, event);
  49. } else if (typeof this.selector === 'string' && process.platform === 'darwin') {
  50. Menu.sendActionToFirstResponder(this.selector);
  51. }
  52. }
  53. };
  54. };
  55. MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio'];
  56. MenuItem.prototype.getDefaultRoleAccelerator = function () {
  57. return roles.getDefaultAccelerator(this.role);
  58. };
  59. MenuItem.prototype.getCheckStatus = function () {
  60. if (!roles.shouldOverrideCheckStatus(this.role)) return this.checked;
  61. return roles.getCheckStatus(this.role);
  62. };
  63. MenuItem.prototype.overrideProperty = function (name: string, defaultValue: any = null) {
  64. if (this[name] == null) {
  65. this[name] = defaultValue;
  66. }
  67. };
  68. MenuItem.prototype.overrideReadOnlyProperty = function (name: string, defaultValue: any) {
  69. this.overrideProperty(name, defaultValue);
  70. Object.defineProperty(this, name, {
  71. enumerable: true,
  72. writable: false,
  73. value: this[name]
  74. });
  75. };
  76. module.exports = MenuItem;