-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
129 lines (118 loc) · 2.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const { app, Menu } = require("electron");
const PLATFORM = process.platform;
const SEPARATOR = platform => ({ type: "separator", showOn: platform });
const passThrough = value => value;
const createTemplate = i18nFunc => [
{
label: app.name,
showOn: ["darwin"],
submenu: [
{ role: "about" },
SEPARATOR(),
{ role: "services" },
SEPARATOR(),
{ role: "hide" },
{ role: "hideothers" },
{ role: "unhide" },
SEPARATOR(),
{ role: "quit" }
]
},
{
label: i18nFunc("File"),
hideOn: ["darwin"],
submenu: [{ role: "quit" }]
},
{
label: i18nFunc("Edit"),
submenu: [
{ role: "undo" },
{ role: "redo" },
SEPARATOR(),
{ role: "cut" },
{ role: "copy" },
{ role: "paste" },
{ role: "pasteandmatchstyle" },
{ role: "delete" },
{ role: "selectall" },
SEPARATOR("darwin"),
{
label: i18nFunc("Speech"),
showOn: ["darwin"],
submenu: [{ role: "startspeaking" }, { role: "stopspeaking" }]
}
]
},
{
label: i18nFunc("View"),
submenu: [
{ role: "reload" },
{ role: "forcereload" },
{ role: "toggledevtools" },
SEPARATOR(),
{ role: "resetzoom" },
{ role: "zoomin" },
{ role: "zoomout" },
SEPARATOR(),
{ role: "togglefullscreen" }
]
},
{
role: "window",
submenu: [
{ role: "minimize" },
{ role: "close" },
{ role: "zoom", showOn: ["darwin"] },
SEPARATOR("darwin"),
{ role: "front", showOn: ["darwin"] }
]
},
{
role: "help",
showOn: ["darwin"],
submenu: []
}
];
const shouldShowItem = item => {
let shouldShow = item.hideOn || item.showOn ? false : true;
if (item.hideOn) {
if (typeof item.hideOn === "string") {
shouldShow = item.hideOn !== PLATFORM;
} else if (Array.isArray(item.hideOn)) {
shouldShow = !item.hideOn.includes(PLATFORM);
} else {
throw Error(
`hideOn for item "${JSON.stringify(item)}" is not a string or an array`
);
}
}
if (item.showOn) {
if (typeof item.showOn === "string") {
shouldShow = item.showOn === PLATFORM;
} else if (Array.isArray(item.showOn)) {
shouldShow = item.showOn.includes(PLATFORM);
} else {
throw Error(
`showOn for item "${JSON.stringify(item)}" is not a string or an array`
);
}
}
return shouldShow;
};
const filterMenu = list =>
list
.map(item => {
if (item.submenu) {
item.submenu = filterMenu(item.submenu);
}
return shouldShowItem(item) ? item : false;
})
.filter(x => x);
const electronMenu = (callback = passThrough, i18nFunc = passThrough) => {
const createdMenu = Menu.buildFromTemplate(
filterMenu(callback(createTemplate(i18nFunc), SEPARATOR))
);
Menu.setApplicationMenu(createdMenu);
return createdMenu;
};
module.exports = electronMenu;