summaryrefslogtreecommitdiffstats
path: root/src/main/index.ts
blob: e1169b9a5986599f1c59471525257d965076f48a (plain)
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
import { app, BrowserWindow } from 'electron';
import * as path from 'path';
import { format as formatUrl } from 'url';

const isDevelopment = process.env.NODE_ENV !== 'production';

// global reference to mainWindow (necessary to prevent window from being garbage collected)
let mainWindow: BrowserWindow|null = null;

function getIndexURL(): string {
	if (isDevelopment)
		return `http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}`;

	return formatUrl({
		pathname: path.join(__dirname, 'index.html'),
		protocol: 'file',
		slashes: true,
	});
}

function createMainWindow(): BrowserWindow {
	const window = new BrowserWindow({webPreferences: {nodeIntegration: true}});

	if (isDevelopment)
		window.webContents.openDevTools();

	window.loadURL(getIndexURL());

	window.on('closed', () => {
		mainWindow = null;
	});

	window.webContents.on('devtools-opened', () => {
		window.webContents.focus();
	})

	return window;
}

// quit application when all windows are closed
app.on('window-all-closed', () => {
	// on macOS it is common for applications to stay open until the user explicitly quits
	if (process.platform !== 'darwin')
		app.quit();
});

app.on('activate', () => {
	// on macOS it is common to re-create a window even after all windows have been closed
	if (!mainWindow)
		mainWindow = createMainWindow();
});

// create main BrowserWindow when electron is ready
app.on('ready', () => {
	mainWindow = createMainWindow();
});