Skip to content

Commit 6fdf863

Browse files
committed
update main process files to es6
1 parent 730f159 commit 6fdf863

File tree

5 files changed

+42
-51
lines changed

5 files changed

+42
-51
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const ipc = require('electron').ipcMain
1+
const {ipcMain} = require('electron')
22

3-
ipc.on('asynchronous-message', function (event, arg) {
3+
ipcMain.on('asynchronous-message', (event, arg) => {
44
event.sender.send('asynchronous-reply', 'pong')
55
})
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const ipc = require('electron').ipcMain
1+
const {ipcMain} = require('electron')
22

3-
ipc.on('synchronous-message', function (event, arg) {
3+
ipcMain.on('synchronous-message', (event, arg) => {
44
event.returnValue = 'pong'
55
})

main-process/media/pdf.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
const fs = require('fs')
22
const os = require('os')
33
const path = require('path')
4-
const electron = require('electron')
5-
const BrowserWindow = electron.BrowserWindow
6-
const ipc = electron.ipcMain
7-
const shell = electron.shell
4+
const {BrowserWindow, ipcMain, shell} = require('electron')
85

9-
ipc.on('print-to-pdf', function (event) {
6+
ipcMain.on('print-to-pdf', (event) => {
107
const pdfPath = path.join(os.tmpdir(), 'print.pdf')
118
const win = BrowserWindow.fromWebContents(event.sender)
129
// Use default printing options
13-
win.webContents.printToPDF({}, function (error, data) {
10+
win.webContents.printToPDF({}, (error, data) => {
1411
if (error) throw error
15-
fs.writeFile(pdfPath, data, function (error) {
16-
if (error) {
17-
throw error
18-
}
19-
shell.openExternal('file://' + pdfPath)
12+
fs.writeFile(pdfPath, data, (error) => {
13+
if (error) throw error
14+
shell.openExternal(`file://${pdfPath}`)
2015
event.sender.send('wrote-pdf', pdfPath)
2116
})
2217
})

main-process/menus/application-menu.js

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
const electron = require('electron')
2-
const BrowserWindow = electron.BrowserWindow
3-
const Menu = electron.Menu
4-
const app = electron.app
1+
const {BrowserWindow, Menu, app, shell, dialog} = require('electron')
52

63
let template = [{
74
label: 'Edit',
@@ -37,44 +34,42 @@ let template = [{
3734
submenu: [{
3835
label: 'Reload',
3936
accelerator: 'CmdOrCtrl+R',
40-
click: function (item, focusedWindow) {
37+
click: (item, focusedWindow) => {
4138
if (focusedWindow) {
4239
// on reload, start fresh and close any old
4340
// open secondary windows
4441
if (focusedWindow.id === 1) {
45-
BrowserWindow.getAllWindows().forEach(function (win) {
46-
if (win.id > 1) {
47-
win.close()
48-
}
42+
BrowserWindow.getAllWindows().forEach(win => {
43+
if (win.id > 1) win.close()
4944
})
5045
}
5146
focusedWindow.reload()
5247
}
5348
}
5449
}, {
5550
label: 'Toggle Full Screen',
56-
accelerator: (function () {
51+
accelerator: (() => {
5752
if (process.platform === 'darwin') {
5853
return 'Ctrl+Command+F'
5954
} else {
6055
return 'F11'
6156
}
6257
})(),
63-
click: function (item, focusedWindow) {
58+
click: (item, focusedWindow) => {
6459
if (focusedWindow) {
6560
focusedWindow.setFullScreen(!focusedWindow.isFullScreen())
6661
}
6762
}
6863
}, {
6964
label: 'Toggle Developer Tools',
70-
accelerator: (function () {
65+
accelerator: (() => {
7166
if (process.platform === 'darwin') {
7267
return 'Alt+Command+I'
7368
} else {
7469
return 'Ctrl+Shift+I'
7570
}
7671
})(),
77-
click: function (item, focusedWindow) {
72+
click: (item, focusedWindow) => {
7873
if (focusedWindow) {
7974
focusedWindow.toggleDevTools()
8075
}
@@ -91,7 +86,7 @@ let template = [{
9186
buttons: ['Ok'],
9287
message: 'This demo is for the Menu section, showing how to create a clickable menu item in the application menu.'
9388
}
94-
electron.dialog.showMessageBox(focusedWindow, options, function () {})
89+
dialog.showMessageBox(focusedWindow, options, function () {})
9590
}
9691
}
9792
}]
@@ -113,7 +108,7 @@ let template = [{
113108
accelerator: 'CmdOrCtrl+Shift+T',
114109
enabled: false,
115110
key: 'reopenMenuItem',
116-
click: function () {
111+
click: () => {
117112
app.emit('activate')
118113
}
119114
}]
@@ -122,16 +117,16 @@ let template = [{
122117
role: 'help',
123118
submenu: [{
124119
label: 'Learn More',
125-
click: function () {
126-
electron.shell.openExternal('http://electron.atom.io')
120+
click: () => {
121+
shell.openExternal('http://electron.atom.io')
127122
}
128123
}]
129124
}]
130125

131126
function addUpdateMenuItems (items, position) {
132127
if (process.mas) return
133128

134-
const version = electron.app.getVersion()
129+
const version = app.getVersion()
135130
let updateItems = [{
136131
label: `Version ${version}`,
137132
enabled: false
@@ -143,15 +138,15 @@ function addUpdateMenuItems (items, position) {
143138
label: 'Check for Update',
144139
visible: false,
145140
key: 'checkForUpdate',
146-
click: function () {
141+
click: () => {
147142
require('electron').autoUpdater.checkForUpdates()
148143
}
149144
}, {
150145
label: 'Restart and Install Update',
151146
enabled: true,
152147
visible: false,
153148
key: 'restartToUpdate',
154-
click: function () {
149+
click: () => {
155150
require('electron').autoUpdater.quitAndInstall()
156151
}
157152
}]
@@ -164,9 +159,9 @@ function findReopenMenuItem () {
164159
if (!menu) return
165160

166161
let reopenMenuItem
167-
menu.items.forEach(function (item) {
162+
menu.items.forEach(item => {
168163
if (item.submenu) {
169-
item.submenu.items.forEach(function (item) {
164+
item.submenu.items.forEach(item => {
170165
if (item.key === 'reopenMenuItem') {
171166
reopenMenuItem = item
172167
}
@@ -177,7 +172,7 @@ function findReopenMenuItem () {
177172
}
178173

179174
if (process.platform === 'darwin') {
180-
const name = electron.app.getName()
175+
const name = app.getName()
181176
template.unshift({
182177
label: name,
183178
submenu: [{
@@ -207,7 +202,7 @@ if (process.platform === 'darwin') {
207202
}, {
208203
label: 'Quit',
209204
accelerator: 'Command+Q',
210-
click: function () {
205+
click: () => {
211206
app.quit()
212207
}
213208
}]
@@ -229,17 +224,17 @@ if (process.platform === 'win32') {
229224
addUpdateMenuItems(helpMenu, 0)
230225
}
231226

232-
app.on('ready', function () {
227+
app.on('ready', () => {
233228
const menu = Menu.buildFromTemplate(template)
234229
Menu.setApplicationMenu(menu)
235230
})
236231

237-
app.on('browser-window-created', function () {
232+
app.on('browser-window-created', () => {
238233
let reopenMenuItem = findReopenMenuItem()
239234
if (reopenMenuItem) reopenMenuItem.enabled = false
240235
})
241236

242-
app.on('window-all-closed', function () {
237+
app.on('window-all-closed', () => {
243238
let reopenMenuItem = findReopenMenuItem()
244239
if (reopenMenuItem) reopenMenuItem.enabled = true
245240
})

main-process/menus/context-menu.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
const electron = require('electron')
2-
const BrowserWindow = electron.BrowserWindow
3-
const Menu = electron.Menu
4-
const MenuItem = electron.MenuItem
5-
const ipc = electron.ipcMain
6-
const app = electron.app
1+
const {
2+
BrowserWindow,
3+
Menu,
4+
MenuItem,
5+
ipcMain,
6+
app
7+
} = require('electron')
78

89
const menu = new Menu()
910
menu.append(new MenuItem({ label: 'Hello' }))
1011
menu.append(new MenuItem({ type: 'separator' }))
1112
menu.append(new MenuItem({ label: 'Electron', type: 'checkbox', checked: true }))
1213

13-
app.on('browser-window-created', function (event, win) {
14-
win.webContents.on('context-menu', function (e, params) {
14+
app.on('browser-window-created', (event, win) => {
15+
win.webContents.on('context-menu', (e, params) => {
1516
menu.popup(win, params.x, params.y)
1617
})
1718
})
1819

19-
ipc.on('show-context-menu', function (event) {
20+
ipcMain.on('show-context-menu', (event) => {
2021
const win = BrowserWindow.fromWebContents(event.sender)
2122
menu.popup(win)
2223
})

0 commit comments

Comments
 (0)