Skip to content

Commit edf8938

Browse files
committed
Update main.ts
- Updated `main` to follow official quick start code from Electron.
1 parent b67d500 commit edf8938

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

src/main/main.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,26 @@
22
* Entry point of the Election app.
33
*/
44
import * as path from 'path';
5-
import * as url from 'url';
65
import * as nodeEnv from '_utils/node-env';
76
// eslint-disable-next-line import/no-extraneous-dependencies
87
import { BrowserWindow, app, ipcMain } from 'electron';
98

109
let mainWindow: Electron.BrowserWindow | undefined;
1110

12-
function createWindow(): void {
11+
function createWindow() {
1312
// Create the browser window.
1413
mainWindow = new BrowserWindow({
1514
height: 600,
1615
width: 800,
1716
webPreferences: {
1817
devTools: nodeEnv.dev,
1918
preload: path.join(__dirname, './preload.bundle.js'),
20-
webSecurity: false,
19+
webSecurity: nodeEnv.prod,
2120
},
2221
});
2322

2423
// and load the index.html of the app.
25-
mainWindow.loadURL(
26-
url.format({
27-
pathname: path.join(__dirname, './index.html'),
28-
protocol: 'file:',
29-
slashes: true,
30-
}),
31-
).finally(() => { /* no action */ });
24+
mainWindow.loadFile('index.html').finally(() => { /* no action */ });
3225

3326
// Emitted when the window is closed.
3427
mainWindow.on('closed', () => {
@@ -42,23 +35,19 @@ function createWindow(): void {
4235
// This method will be called when Electron has finished
4336
// initialization and is ready to create browser windows.
4437
// Some APIs can only be used after this event occurs.
45-
app.on('ready', createWindow);
38+
app.whenReady().then(() => {
39+
createWindow();
4640

47-
// Quit when all windows are closed.
48-
app.on('window-all-closed', () => {
49-
// On OS X it is common for applications and their menu bar
50-
// to stay active until the user quits explicitly with Cmd + Q
51-
if (process.platform !== 'darwin') {
52-
app.quit();
53-
}
54-
});
41+
app.on('activate', () => {
42+
if (BrowserWindow.getAllWindows.length === 0) createWindow();
43+
});
44+
}).finally(() => { /* no action */ });
5545

56-
app.on('activate', () => {
57-
// On OS X it"s common to re-create a window in the app when the
58-
// dock icon is clicked and there are no other windows open.
59-
if (mainWindow === undefined) {
60-
createWindow();
61-
}
46+
// Quit when all windows are closed, except on macOS. There, it's common
47+
// for applications and their menu bar to stay active until the user quits
48+
// explicitly with Cmd + Q.
49+
app.on('window-all-closed', () => {
50+
if (process.platform !== 'darwin') app.quit();
6251
});
6352

6453
ipcMain.on('renderer-ready', () => {

0 commit comments

Comments
 (0)