summaryrefslogtreecommitdiffstats
path: root/src/main/index.ts
blob: 3a94ae5c498f4bdeb0826cd9d9eeda67ca19a748 (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
57
58
59
60
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();
});