1
- import { app , Menu } from 'electron' ;
1
+ import { app , Menu , MenuItemConstructorOptions } from 'electron' ;
2
2
3
- const template = [
4
- {
5
- label : '&File' ,
6
- submenu : [
7
- { role : 'quit' }
8
- ]
9
- } ,
3
+ const menuTemplate : MenuItemConstructorOptions [ ] = [
10
4
{
11
5
label : '&Edit' ,
12
6
submenu : [
13
7
{ role : 'undo' } ,
14
8
{ role : 'redo' } ,
15
9
{ type : 'separator' } ,
16
- { role : 'cut' } ,
17
- { role : 'copy' } ,
18
- { role : 'paste' } ,
19
- { role : 'pasteandmatchstyle' } ,
10
+ { role : 'cut' , registerAccelerator : false } ,
11
+ { role : 'copy' , registerAccelerator : false } ,
12
+ { role : 'paste' , registerAccelerator : false } ,
13
+ { role : 'pasteandmatchstyle' , registerAccelerator : false } ,
20
14
{ role : 'delete' } ,
21
15
{ role : 'selectall' }
22
16
]
@@ -53,10 +47,10 @@ const template = [
53
47
}
54
48
]
55
49
}
56
- ]
50
+ ] ;
57
51
58
52
if ( process . platform === 'darwin' ) {
59
- template . unshift ( {
53
+ menuTemplate . unshift ( {
60
54
label : app . getName ( ) ,
61
55
submenu : [
62
56
{ role : 'about' } ,
@@ -69,10 +63,10 @@ if (process.platform === 'darwin') {
69
63
{ type : 'separator' } ,
70
64
{ role : 'quit' }
71
65
]
72
- } )
66
+ } ) ;
73
67
74
68
// Edit menu
75
- template [ 1 ] . submenu . push (
69
+ ( menuTemplate [ 1 ] . submenu as MenuItemConstructorOptions [ ] ) . push (
76
70
{ type : 'separator' } ,
77
71
{
78
72
label : 'Speech' ,
@@ -81,16 +75,47 @@ if (process.platform === 'darwin') {
81
75
{ role : 'stopspeaking' }
82
76
]
83
77
}
84
- )
78
+ ) ;
85
79
86
80
// Window menu
87
- template [ 3 ] . submenu = [
81
+ menuTemplate [ 3 ] . submenu = [
88
82
{ role : 'close' } ,
89
83
{ role : 'minimize' } ,
90
84
{ role : 'zoom' } ,
91
85
{ type : 'separator' } ,
92
86
{ role : 'front' }
93
- ]
87
+ ] ;
88
+ } else {
89
+ menuTemplate . unshift ( {
90
+ label : '&File' ,
91
+ submenu : [
92
+ { role : 'quit' }
93
+ ]
94
+ } ) ;
95
+ }
96
+
97
+ // Mutate menu templates to fix https://github.com/electron/electron/issues/16303
98
+ // by forcibly defaulting registerAccelerator to true on role menu items.
99
+ function fixAccelerators ( menuTemplates : MenuItemConstructorOptions [ ] ) : MenuItemConstructorOptions [ ] {
100
+ return menuTemplates . map ( ( template ) => {
101
+ if ( template . role && ! template . hasOwnProperty ( 'registerAccelerator' ) ) {
102
+ template . registerAccelerator = true ;
103
+ }
104
+
105
+ const { submenu } = template ;
106
+
107
+ if ( submenu ) {
108
+ if ( Array . isArray ( submenu ) ) {
109
+ template . submenu = fixAccelerators ( submenu ) ;
110
+ } else {
111
+ template . submenu = fixAccelerators ( [ submenu ] ) ;
112
+ }
113
+ }
114
+
115
+ return template ;
116
+ } ) ;
94
117
}
95
118
96
- export const menu = Menu . buildFromTemplate ( template )
119
+ export const menu = Menu . buildFromTemplate (
120
+ fixAccelerators ( menuTemplate )
121
+ ) ;
0 commit comments