Skip to content

Commit 132cdfd

Browse files
committed
📦 customize application menu
1 parent ba3b70e commit 132cdfd

3 files changed

Lines changed: 186 additions & 23 deletions

File tree

package-lock.json

Lines changed: 51 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
},
2525
"dependencies": {
2626
"axios": "^0.16.1",
27-
"electron-default-menu": "^1.0.1",
2827
"qr-image": "^3.2.0",
2928
"vue": "^2.3.3",
3029
"vue-electron": "^1.0.6",

src/main/index.js

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { app, BrowserWindow, ipcMain, Menu, shell } from 'electron' // eslint-disable-line
22
import { image } from 'qr-image';
33
import { join as joinPath } from 'path';
4-
import defaultMenu from 'electron-default-menu';
54

65
/**
76
* Set `__static` path to static files in production
@@ -31,8 +30,142 @@ function createWindow() {
3130
});
3231
}
3332

33+
function createMenuTemplate(app, shell) {
34+
const menuTemplate = [
35+
{
36+
label: 'Edit',
37+
submenu: [
38+
{
39+
label: 'Undo',
40+
accelerator: 'CmdOrCtrl+Z',
41+
role: 'undo',
42+
},
43+
{
44+
label: 'Redo',
45+
accelerator: 'Shift+CmdOrCtrl+Z',
46+
role: 'redo',
47+
},
48+
{
49+
type: 'separator',
50+
},
51+
{
52+
label: 'Cut',
53+
accelerator: 'CmdOrCtrl+X',
54+
role: 'cut',
55+
},
56+
{
57+
label: 'Copy',
58+
accelerator: 'CmdOrCtrl+C',
59+
role: 'copy',
60+
},
61+
{
62+
label: 'Paste',
63+
accelerator: 'CmdOrCtrl+V',
64+
role: 'paste',
65+
},
66+
{
67+
label: 'Select All',
68+
accelerator: 'CmdOrCtrl+A',
69+
role: 'selectall',
70+
},
71+
],
72+
},
73+
{
74+
label: 'Window',
75+
role: 'window',
76+
submenu: [
77+
{
78+
label: 'Minimize',
79+
accelerator: 'CmdOrCtrl+M',
80+
role: 'minimize',
81+
},
82+
{
83+
label: 'Close',
84+
accelerator: 'CmdOrCtrl+W',
85+
role: 'close',
86+
},
87+
],
88+
},
89+
{
90+
label: 'Help',
91+
role: 'help',
92+
submenu: [
93+
{
94+
label: 'Learn More',
95+
click() {
96+
shell.openExternal('http://electron.atom.io');
97+
},
98+
},
99+
],
100+
},
101+
];
102+
103+
if (process.platform === 'darwin') {
104+
const name = app.getName();
105+
106+
menuTemplate.unshift({
107+
label: name,
108+
submenu: [
109+
{
110+
label: `About ${name}`,
111+
role: 'about',
112+
},
113+
{
114+
type: 'separator',
115+
},
116+
{
117+
label: 'Services',
118+
role: 'services',
119+
submenu: [],
120+
},
121+
{
122+
type: 'separator',
123+
},
124+
{
125+
label: `Hide ${name}`,
126+
accelerator: 'Command+H',
127+
role: 'hide',
128+
},
129+
{
130+
label: 'Hide Others',
131+
accelerator: 'Command+Shift+H',
132+
role: 'hideothers',
133+
},
134+
{
135+
label: 'Show All',
136+
role: 'unhide',
137+
},
138+
{
139+
type: 'separator',
140+
},
141+
{
142+
label: 'Quit',
143+
accelerator: 'Command+Q',
144+
click() {
145+
app.quit();
146+
},
147+
},
148+
],
149+
});
150+
151+
const windowMenu = menuTemplate.find(m => m.role === 'window');
152+
153+
if (windowMenu) {
154+
windowMenu.submenu.push({
155+
type: 'separator',
156+
});
157+
windowMenu.submenu.push({
158+
label: 'Bring All to Front',
159+
role: 'front',
160+
});
161+
}
162+
}
163+
164+
return menuTemplate;
165+
}
166+
34167
function createMenu() {
35-
const menu = defaultMenu(app, shell);
168+
const menu = createMenuTemplate(app, shell);
36169

37170
Menu.setApplicationMenu(Menu.buildFromTemplate(menu));
38171
}

0 commit comments

Comments
 (0)