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