1
- 'use strict' ;
2
-
3
- const { app } = require ( 'electron' ) ;
1
+ import { app , BrowserWindow , WebContents , MenuItemConstructorOptions } from 'electron' ;
4
2
5
3
const isMac = process . platform === 'darwin' ;
6
4
const isWindows = process . platform === 'win32' ;
7
5
const isLinux = process . platform === 'linux' ;
8
6
9
- const roles = {
7
+ type RoleId = 'about' | 'close' | 'copy' | 'cut' | 'delete' | 'forcereload' | 'front' | 'help' | 'hide' | 'hideothers' | 'minimize' |
8
+ 'paste' | 'pasteandmatchstyle' | 'quit' | 'redo' | 'reload' | 'resetzoom' | 'selectall' | 'services' | 'recentdocuments' | 'clearrecentdocuments' | 'startspeaking' | 'stopspeaking' |
9
+ 'toggledevtools' | 'togglefullscreen' | 'undo' | 'unhide' | 'window' | 'zoom' | 'zoomin' | 'zoomout' | 'appmenu' | 'filemenu' | 'editmenu' | 'viewmenu' | 'windowmenu'
10
+ interface Role {
11
+ label : string ;
12
+ accelerator ?: string ;
13
+ windowMethod ?: ( ( window : BrowserWindow ) => void ) ;
14
+ webContentsMethod ?: ( ( webContents : WebContents ) => void ) ;
15
+ appMethod ?: ( ) => void ;
16
+ registerAccelerator ?: boolean ;
17
+ nonNativeMacOSRole ?: boolean ;
18
+ submenu ?: MenuItemConstructorOptions [ ] ;
19
+ }
20
+
21
+ export const roleList : Record < RoleId , Role > = {
10
22
about : {
11
23
get label ( ) {
12
24
return isLinux ? 'About' : `About ${ app . name } ` ;
13
25
} ,
14
- ...( isWindows && { appMethod : ' showAboutPanel' } )
26
+ ...( isWindows && { appMethod : ( ) => app . showAboutPanel ( ) } )
15
27
} ,
16
28
close : {
17
29
label : isMac ? 'Close Window' : 'Close' ,
18
30
accelerator : 'CommandOrControl+W' ,
19
- windowMethod : ' close'
31
+ windowMethod : w => w . close ( )
20
32
} ,
21
33
copy : {
22
34
label : 'Copy' ,
23
35
accelerator : 'CommandOrControl+C' ,
24
- webContentsMethod : ' copy' ,
36
+ webContentsMethod : wc => wc . copy ( ) ,
25
37
registerAccelerator : false
26
38
} ,
27
39
cut : {
28
40
label : 'Cut' ,
29
41
accelerator : 'CommandOrControl+X' ,
30
- webContentsMethod : ' cut' ,
42
+ webContentsMethod : wc => wc . cut ( ) ,
31
43
registerAccelerator : false
32
44
} ,
33
45
delete : {
34
46
label : 'Delete' ,
35
- webContentsMethod : ' delete'
47
+ webContentsMethod : wc => wc . delete ( )
36
48
} ,
37
49
forcereload : {
38
50
label : 'Force Reload' ,
39
51
accelerator : 'Shift+CmdOrCtrl+R' ,
40
52
nonNativeMacOSRole : true ,
41
- windowMethod : ( window ) => {
53
+ windowMethod : ( window : BrowserWindow ) => {
42
54
window . webContents . reloadIgnoringCache ( ) ;
43
55
}
44
56
} ,
@@ -61,18 +73,18 @@ const roles = {
61
73
minimize : {
62
74
label : 'Minimize' ,
63
75
accelerator : 'CommandOrControl+M' ,
64
- windowMethod : ' minimize'
76
+ windowMethod : w => w . minimize ( )
65
77
} ,
66
78
paste : {
67
79
label : 'Paste' ,
68
80
accelerator : 'CommandOrControl+V' ,
69
- webContentsMethod : ' paste' ,
81
+ webContentsMethod : wc => wc . paste ( ) ,
70
82
registerAccelerator : false
71
83
} ,
72
84
pasteandmatchstyle : {
73
85
label : 'Paste and Match Style' ,
74
86
accelerator : isMac ? 'Cmd+Option+Shift+V' : 'Shift+CommandOrControl+V' ,
75
- webContentsMethod : ' pasteAndMatchStyle' ,
87
+ webContentsMethod : wc => wc . pasteAndMatchStyle ( ) ,
76
88
registerAccelerator : false
77
89
} ,
78
90
quit : {
@@ -84,31 +96,31 @@ const roles = {
84
96
}
85
97
} ,
86
98
accelerator : isWindows ? undefined : 'CommandOrControl+Q' ,
87
- appMethod : ' quit'
99
+ appMethod : ( ) => app . quit ( )
88
100
} ,
89
101
redo : {
90
102
label : 'Redo' ,
91
103
accelerator : isWindows ? 'Control+Y' : 'Shift+CommandOrControl+Z' ,
92
- webContentsMethod : ' redo'
104
+ webContentsMethod : wc => wc . redo ( )
93
105
} ,
94
106
reload : {
95
107
label : 'Reload' ,
96
108
accelerator : 'CmdOrCtrl+R' ,
97
109
nonNativeMacOSRole : true ,
98
- windowMethod : ' reload'
110
+ windowMethod : w => w . reload ( )
99
111
} ,
100
112
resetzoom : {
101
113
label : 'Actual Size' ,
102
114
accelerator : 'CommandOrControl+0' ,
103
115
nonNativeMacOSRole : true ,
104
- webContentsMethod : ( webContents ) => {
116
+ webContentsMethod : ( webContents : WebContents ) => {
105
117
webContents . zoomLevel = 0 ;
106
118
}
107
119
} ,
108
120
selectall : {
109
121
label : 'Select All' ,
110
122
accelerator : 'CommandOrControl+A' ,
111
- webContentsMethod : ' selectAll'
123
+ webContentsMethod : wc => wc . selectAll ( )
112
124
} ,
113
125
services : {
114
126
label : 'Services'
@@ -129,19 +141,19 @@ const roles = {
129
141
label : 'Toggle Developer Tools' ,
130
142
accelerator : isMac ? 'Alt+Command+I' : 'Ctrl+Shift+I' ,
131
143
nonNativeMacOSRole : true ,
132
- windowMethod : ' toggleDevTools'
144
+ windowMethod : w => w . webContents . toggleDevTools ( )
133
145
} ,
134
146
togglefullscreen : {
135
147
label : 'Toggle Full Screen' ,
136
148
accelerator : isMac ? 'Control+Command+F' : 'F11' ,
137
- windowMethod : ( window ) => {
149
+ windowMethod : ( window : BrowserWindow ) => {
138
150
window . setFullScreen ( ! window . isFullScreen ( ) ) ;
139
151
}
140
152
} ,
141
153
undo : {
142
154
label : 'Undo' ,
143
155
accelerator : 'CommandOrControl+Z' ,
144
- webContentsMethod : ' undo'
156
+ webContentsMethod : wc => wc . undo ( )
145
157
} ,
146
158
unhide : {
147
159
label : 'Show All'
@@ -156,15 +168,15 @@ const roles = {
156
168
label : 'Zoom In' ,
157
169
accelerator : 'CommandOrControl+Plus' ,
158
170
nonNativeMacOSRole : true ,
159
- webContentsMethod : ( webContents ) => {
171
+ webContentsMethod : ( webContents : WebContents ) => {
160
172
webContents . zoomLevel += 0.5 ;
161
173
}
162
174
} ,
163
175
zoomout : {
164
176
label : 'Zoom Out' ,
165
177
accelerator : 'CommandOrControl+-' ,
166
178
nonNativeMacOSRole : true ,
167
- webContentsMethod : ( webContents ) => {
179
+ webContentsMethod : ( webContents : WebContents ) => {
168
180
webContents . zoomLevel -= 0.5 ;
169
181
}
170
182
} ,
@@ -214,11 +226,11 @@ const roles = {
214
226
{ role : 'stopSpeaking' }
215
227
]
216
228
}
217
- ] : [
229
+ ] as MenuItemConstructorOptions [ ] : [
218
230
{ role : 'delete' } ,
219
231
{ type : 'separator' } ,
220
232
{ role : 'selectAll' }
221
- ] )
233
+ ] as MenuItemConstructorOptions [ ] )
222
234
]
223
235
} ,
224
236
// View submenu
@@ -245,76 +257,66 @@ const roles = {
245
257
...( isMac ? [
246
258
{ type : 'separator' } ,
247
259
{ role : 'front' }
248
- ] : [
260
+ ] as MenuItemConstructorOptions [ ] : [
249
261
{ role : 'close' }
250
- ] )
262
+ ] as MenuItemConstructorOptions [ ] )
251
263
]
252
264
}
253
265
} ;
254
266
255
- exports . roleList = roles ;
256
-
257
- const canExecuteRole = ( role ) => {
258
- if ( ! Object . prototype . hasOwnProperty . call ( roles , role ) ) return false ;
267
+ const canExecuteRole = ( role : keyof typeof roleList ) => {
268
+ if ( ! Object . prototype . hasOwnProperty . call ( roleList , role ) ) return false ;
259
269
if ( ! isMac ) return true ;
260
270
261
271
// macOS handles all roles natively except for a few
262
- return roles [ role ] . nonNativeMacOSRole ;
272
+ return roleList [ role ] . nonNativeMacOSRole ;
263
273
} ;
264
274
265
- exports . getDefaultLabel = ( role ) => {
266
- return Object . prototype . hasOwnProperty . call ( roles , role ) ? roles [ role ] . label : '' ;
267
- } ;
275
+ export function getDefaultLabel ( role : RoleId ) {
276
+ return Object . prototype . hasOwnProperty . call ( roleList , role ) ? roleList [ role ] . label : '' ;
277
+ }
268
278
269
- exports . getDefaultAccelerator = ( role ) => {
270
- if ( Object . prototype . hasOwnProperty . call ( roles , role ) ) return roles [ role ] . accelerator ;
271
- } ;
279
+ export function getDefaultAccelerator ( role : RoleId ) {
280
+ if ( Object . prototype . hasOwnProperty . call ( roleList , role ) ) return roleList [ role ] . accelerator ;
281
+ }
272
282
273
- exports . shouldRegisterAccelerator = ( role ) => {
274
- const hasRoleRegister = Object . prototype . hasOwnProperty . call ( roles , role ) && roles [ role ] . registerAccelerator !== undefined ;
275
- return hasRoleRegister ? roles [ role ] . registerAccelerator : true ;
276
- } ;
283
+ export function shouldRegisterAccelerator ( role : RoleId ) {
284
+ const hasRoleRegister = Object . prototype . hasOwnProperty . call ( roleList , role ) && roleList [ role ] . registerAccelerator !== undefined ;
285
+ return hasRoleRegister ? roleList [ role ] . registerAccelerator : true ;
286
+ }
277
287
278
- exports . getDefaultSubmenu = ( role ) => {
279
- if ( ! Object . prototype . hasOwnProperty . call ( roles , role ) ) return ;
288
+ export function getDefaultSubmenu ( role : RoleId ) {
289
+ if ( ! Object . prototype . hasOwnProperty . call ( roleList , role ) ) return ;
280
290
281
- let { submenu } = roles [ role ] ;
291
+ let { submenu } = roleList [ role ] ;
282
292
283
293
// remove null items from within the submenu
284
294
if ( Array . isArray ( submenu ) ) {
285
295
submenu = submenu . filter ( ( item ) => item != null ) ;
286
296
}
287
297
288
298
return submenu ;
289
- } ;
299
+ }
290
300
291
- exports . execute = ( role , focusedWindow , focusedWebContents ) => {
301
+ export function execute ( role : RoleId , focusedWindow : BrowserWindow , focusedWebContents : WebContents ) {
292
302
if ( ! canExecuteRole ( role ) ) return false ;
293
303
294
- const { appMethod, webContentsMethod, windowMethod } = roles [ role ] ;
304
+ const { appMethod, webContentsMethod, windowMethod } = roleList [ role ] ;
295
305
296
306
if ( appMethod ) {
297
- app [ appMethod ] ( ) ;
307
+ appMethod ( ) ;
298
308
return true ;
299
309
}
300
310
301
311
if ( windowMethod && focusedWindow != null ) {
302
- if ( typeof windowMethod === 'function' ) {
303
- windowMethod ( focusedWindow ) ;
304
- } else {
305
- focusedWindow [ windowMethod ] ( ) ;
306
- }
312
+ windowMethod ( focusedWindow ) ;
307
313
return true ;
308
314
}
309
315
310
316
if ( webContentsMethod && focusedWebContents != null ) {
311
- if ( typeof webContentsMethod === 'function' ) {
312
- webContentsMethod ( focusedWebContents ) ;
313
- } else {
314
- focusedWebContents [ webContentsMethod ] ( ) ;
315
- }
317
+ webContentsMethod ( focusedWebContents ) ;
316
318
return true ;
317
319
}
318
320
319
321
return false ;
320
- } ;
322
+ }
0 commit comments