-
Notifications
You must be signed in to change notification settings - Fork 297
/
main.js
139 lines (115 loc) · 3.74 KB
/
main.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// @flow
import path from 'path';
import { app, BrowserWindow, ipcMain, Menu } from 'electron';
import { environment } from '../environment';
import ipcApi from '../ipc';
import RendererErrorHandler from '../utils/rendererErrorHandler';
import { getTranslation } from '../utils/getTranslation';
import { launcherConfig } from '../config';
const rendererErrorHandler = new RendererErrorHandler();
const { isDev, isTest, buildLabel, isLinux } = environment;
const id = 'window';
const getWindowTitle = (isInSafeMode: boolean, locale: string): string => {
const translations = require(`../locales/${locale}`);
const translation = getTranslation(translations, id);
let title = buildLabel;
if (isInSafeMode) title += ` ${translation('title.gpuSafeMode')}`;
return title;
};
type WindowOptionsType = {
show: boolean,
width: number,
height: number,
webPreferences: {
nodeIntegration: boolean,
webviewTag: boolean,
enableRemoteModule: boolean,
preload: string,
},
icon?: string,
};
export const createMainWindow = (isInSafeMode: boolean, locale: string) => {
const windowOptions: WindowOptionsType = {
show: false,
width: 1150,
height: 870,
webPreferences: {
nodeIntegration: isTest,
webviewTag: false,
enableRemoteModule: isTest,
preload: path.join(__dirname, './preload.js'),
},
};
if (isLinux) {
windowOptions.icon = path.join(launcherConfig.statePath, 'icon.png');
}
// Construct new BrowserWindow
const window = new BrowserWindow(windowOptions);
rendererErrorHandler.setup(window, createMainWindow);
window.setMinimumSize(905, 600);
// Initialize our ipc api methods that can be called by the render processes
ipcApi({ window });
// Provide render process with an api to resize the main window
ipcMain.on('resize-window', (event, { width, height, animate }) => {
if (event.sender !== window.webContents) return;
window.setSize(width, height, animate);
});
// Provide render process with an api to close the main window
ipcMain.on('close-window', event => {
if (event.sender !== window.webContents) return;
window.close();
});
window.loadURL(`file://${__dirname}/../renderer/index.html`);
window.on('page-title-updated', event => {
event.preventDefault();
});
window.setTitle(getWindowTitle(isInSafeMode, locale));
window.webContents.on('context-menu', (e, props) => {
const contextMenuOptions = [
{ label: 'Copy', accelerator: 'CmdOrCtrl+C', role: 'copy' },
{ label: 'Paste', accelerator: 'CmdOrCtrl+V', role: 'paste' },
];
if (isDev || isTest) {
const { x, y } = props;
contextMenuOptions.push({
label: 'Inspect element',
click() {
window.inspectElement(x, y);
},
});
}
Menu.buildFromTemplate(contextMenuOptions).popup(window);
});
window.webContents.on('did-frame-finish-load', () => {
if (isDev) {
window.webContents.openDevTools();
// Focus the main window after dev tools opened
window.webContents.on('devtools-opened', () => {
window.focus();
setImmediate(() => {
window.focus();
});
});
}
});
window.webContents.on('did-finish-load', () => {
if (isTest) {
window.showInactive(); // show without focusing the window
} else {
window.show(); // show also focuses the window
}
});
window.on('closed', () => {
app.quit();
});
window.webContents.on('did-fail-load', err => {
rendererErrorHandler.onError('did-fail-load', err);
});
window.webContents.on('crashed', err => {
rendererErrorHandler.onError('crashed', err);
});
window.updateTitle = (locale: string) => {
window.setTitle(getWindowTitle(isInSafeMode, locale));
};
return window;
};