-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
135 lines (119 loc) · 3.62 KB
/
renderer.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
"use strict";
const { app, BrowserWindow } = require("electron");
const path = require("path");
// Keep a global reference of the mainWindowdow object, if you don't, the mainWindowdow will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow = null;
let subpy = null;
const PY_DIST_FOLDER = "dist-python"; // python distributable folder
const PY_SRC_FOLDER = "web_app"; // path to the python source
const PY_MODULE = "run_app.py"; // the name of the main module
const isRunningInBundle = () => {
return require("fs").existsSync(path.join(__dirname, PY_DIST_FOLDER));
};
const getPythonScriptPath = () => {
if (!isRunningInBundle()) {
return path.join(__dirname, PY_SRC_FOLDER, PY_MODULE);
}
if (process.platform === "win32") {
return path.join(
__dirname,
PY_DIST_FOLDER,
PY_MODULE.slice(0, -3) + ".exe"
);
}
return path.join(__dirname, PY_DIST_FOLDER, PY_MODULE);
};
const startPythonSubprocess = () => {
let script = getPythonScriptPath();
if (isRunningInBundle()) {
subpy = require("child_process").execFile(script, []);
} else {
subpy = require("child_process").spawn("python", [script]);
}
};
const killPythonSubprocesses = main_pid => {
const python_script_name = path.basename(getPythonScriptPath());
let cleanup_completed = false;
const psTree = require("ps-tree");
psTree(main_pid, function(err, children) {
let python_pids = children
.filter(function(el) {
return el.COMMAND == python_script_name;
})
.map(function(p) {
return p.PID;
});
// kill all the spawned python processes
python_pids.forEach(function(pid) {
process.kill(pid);
});
subpy = null;
cleanup_completed = true;
});
return new Promise(function(resolve, reject) {
(function waitForSubProcessCleanup() {
if (cleanup_completed) return resolve();
setTimeout(waitForSubProcessCleanup, 30);
})();
});
};
const createMainWindow = () => {
// Create the browser mainWindow
mainWindow = new BrowserWindow({
width: 800,
height: 600,
// transparent: true, // transparent header bar
icon: __dirname + "/icon.png",
// fullscreen: true,
// opacity:0.8,
// darkTheme: true,
// frame: false,
resizeable: true
});
// Load the index page
mainWindow.loadURL("http://localhost:4040/");
// Open the DevTools.
//mainWindow.webContents.openDevTools();
// Emitted when the mainWindow is closed.
mainWindow.on("closed", function() {
// Dereference the mainWindow object
mainWindow = null;
});
};
// 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.on("ready", function() {
// start the backend server
startPythonSubprocess();
createMainWindow();
});
// disable menu
app.on("browser-window-created", function(e, window) {
window.setMenu(null);
});
// Quit when all windows are closed.
app.on("window-all-closed", () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") {
let main_process_pid = process.pid;
killPythonSubprocesses(main_process_pid).then(() => {
app.quit();
});
}
});
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 (subpy == null) {
startPythonSubprocess();
}
if (win === null) {
createWindow();
}
});
app.on("quit", function() {
// do some additional cleanup
});