This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
280 lines (236 loc) · 7.71 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Built-in modules
const { spawn } = require('child_process');
const path = require('path');
// Electron modules
const { app, BrowserWindow, ipcMain } = require('electron');
// Extra modules
const getPort = require('get-port');
const isDevMode = require('electron-is-dev');
const { get } = require('axios');
/**
* @description - Shuts down Electron & Flask.
* @param {number} port - Port that Flask server is running on.
*/
const shutdown = (port) => {
get(`http://localhost:${port}/quit`)
.then(app.quit)
.catch(app.quit);
};
/**
* @namespace BrowserWindow
* @description - Electron browser windows.
* @tutorial - https://www.electronjs.org/docs/api/browser-window
*/
const browserWindows = {};
/**
* @description - Creates main window.
* @param {number} port - Port that Flask server is running on.
*
* @memberof BrowserWindow
*/
const createMainWindow = (port) => {
const { loadingWindow, mainWindow } = browserWindows;
/**
* @description - Function to use custom JavaSCript in the DOM.
* @param {string} command - JavaScript to execute in DOM.
* @param {function} callback - Callback to execute here once complete.
* @returns {Promise}
*/
const executeOnWindow = (command, callback) => {
return mainWindow.webContents.executeJavaScript(command)
.then(callback)
.catch(console.error);
};
/**
* If in developer mode, show a loading window while
* the app and developer server compile.
*/
if (isDevMode) {
mainWindow.loadURL('http://localhost:3000');
mainWindow.hide();
/**
* Hide loading window and show main window
* once the main window is ready.
*/
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.webContents.openDevTools({ mode: 'undocked' });
/**
* Checks page for errors that may have occurred
* during the hot-loading process.
*/
const isPageLoaded = `
var isBodyFull = document.body.innerHTML !== "";
var isHeadFull = document.head.innerHTML !== "";
var isLoadSuccess = isBodyFull && isHeadFull;
isLoadSuccess || Boolean(location.reload());
`;
/**
* @description Updates windows if page is loaded
* @param {*} isLoaded
*/
const handleLoad = (isLoaded) => {
if (isLoaded) {
/**
* Keep show() & hide() in this order to prevent
* unresponsive behavior during page load.
*/
mainWindow.show();
loadingWindow.hide();
}
};
/**
* Checks if the page has been populated with
* React project. if so, shows the main page.
*/
executeOnWindow(isPageLoaded, handleLoad);
});
}
/**
* If using in production, the built version of the
* React project will be used instead of localhost.
*/
else mainWindow.loadFile(path.join(__dirname, 'build/index.html'));
/**
* @description - Controls the opacity of title bar on focus/blur.
* @param {number} value - Opacity to set for title bar.
*/
const setTitleOpacity = (value) => `
if(document.readyState === 'complete') {
const titleBar = document.getElementById('electron-window-title-text');
const titleButtons = document.getElementById('electron-window-title-buttons');
if(titleBar) titleBar.style.opacity = ${value};
if(titleButtons) titleButtons.style.opacity = ${value};
}
`;
mainWindow.on('focus', () => executeOnWindow(setTitleOpacity(1)));
mainWindow.on('blur', () => executeOnWindow(setTitleOpacity(0.5)));
/**
* Listen and respond to ipcRenderer events on the frontend.
* @see `src\utils\services.js`
*/
ipcMain.on('app-maximize', (_event, _arg) => mainWindow.maximize());
ipcMain.on('app-minimize', (_event, _arg) => mainWindow.minimize());
ipcMain.on('app-quit', (_event, _arg) => shutdown(port));
ipcMain.on('app-unmaximize', (_event, _arg) => mainWindow.unmaximize());
ipcMain.on('get-port-number', (event, _arg) => {
event.returnValue = port;
});
};
/**
* @description - Creates loading window to show while build is created.
* @memberof BrowserWindow
*/
const createLoadingWindow = () => {
return new Promise((resolve, reject) => {
const { loadingWindow } = browserWindows;
// Variants of developer loading screen
const loaderConfig = {
react: 'utilities/loaders/react/index.html',
redux: 'utilities/loaders/redux/index.html'
};
try {
loadingWindow.loadFile(path.join(__dirname, loaderConfig.redux));
loadingWindow.webContents.on('did-finish-load', () => {
loadingWindow.show();
resolve();
});
} catch (error) {
console.error(error);
reject();
}
});
};
/**
* @description - Installs developer extensions.
* @returns {Promise}
*/
const installExtensions = async () => {
const isForceDownload = Boolean(process.env.UPGRADE_EXTENSIONS);
const installer = require('electron-devtools-installer');
const extensions = ['REACT_DEVELOPER_TOOLS', 'REDUX_DEVTOOLS']
.map((extension) => installer.default(installer[extension], isForceDownload));
return Promise
.allSettled(extensions)
.catch(console.error);
};
/**
* This method will be called when Electron has finished
* initialization and is ready to create browser windows.
* Some APIs can only be used after this event occurs.
*/
app.whenReady().then(async () => {
/**
* Method to set port in range of 3001-3999,
* based on availability.
*/
const port = await getPort({
port: getPort.makeRange(3001, 3999)
});
/**
* Assigns the main browser window on the
* browserWindows object.
*/
browserWindows.mainWindow = new BrowserWindow({
frame: false,
webPreferences: {
contextIsolation: false,
enableRemoteModule: true,
nodeIntegration: true,
preload: path.join(app.getAppPath(), 'preload.js')
}
});
/**
* If not using in production, use the loading window
* and run Flask in shell.
*/
if (isDevMode) {
await installExtensions(); // React, Redux devTools
browserWindows.loadingWindow = new BrowserWindow({ frame: false });
createLoadingWindow().then(() => createMainWindow(port));
spawn(`python3 py/crud.py ${port}`, { detached: true, shell: true, stdio: 'inherit' });
}
/**
* If using in production, use the main window
* and run bundled app (dmg, elf, or exe) file.
*/
else {
createMainWindow(port);
// Dynamic script assignment for starting Flask in production
const runFlask = {
darwin: `open -gj "${path.join(app.getAppPath(), 'resources', 'app.app')}" --args`,
linux: './resources/app/app',
win32: 'start ./resources/app/app.exe'
}[process.platform];
spawn(`${runFlask} ${port}`, { detached: false, shell: true, stdio: 'pipe' });
}
app.on('activate', () => {
/**
* On macOS it's common to re-create a window in the app when the
* dock icon is clicked and there are no other windows open.
*/
if (BrowserWindow.getAllWindows().length === 0) createMainWindow(port);
});
/**
* Ensures that only a single instance of the app
* can run, this correlates with the "name" property
* used in `package.json`.
*/
const initialInstance = app.requestSingleInstanceLock();
if (!initialInstance) app.quit();
else {
app.on('second-instance', () => {
if (browserWindows.mainWindow?.isMinimized()) browserWindows.mainWindow?.restore();
browserWindows.mainWindow?.focus();
});
}
/**
* Quit when all windows are closed, except on macOS. There, it's common
* for applications and their menu bar to stay active until the user quits
* explicitly with Cmd + Q.
*/
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
shutdown(port);
}
});
});