-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
203 lines (170 loc) · 5.43 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
'use strict';
const { spawn } = require('child_process');
const electron = require('electron');
const {app, BrowserWindow, ipcMain} = electron;
const fs = require('fs');
const { exit } = require('process');
const WebSocket = require('ws');
const controller = new AbortController();
const {signal} = controller;
const DEBUG = false;
const DEBUG_RENDER = false;
let settings = require(`${__dirname}/settings.json`)
// Keep a global reference of the mainWindowdow object to avoid garbage collector
let mainWindow = null;
let python_server_process = null;
let phaseportrait_socket = null;
// let mpl_websocket = null;
let FigId = null;
let ws_uri = "ws://127.0.0.1:8080/";
function createMainWindow() {
console.log(settings);
// Create the browser mainWindow
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
icon: __dirname + '/icons/phaseportrait_icon.ico',
resizeable: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
slashes: true
});
// Load the index page
mainWindow.loadFile('index.html');
if (DEBUG||DEBUG_RENDER)
mainWindow.openDevTools();
// Link handler
mainWindow.webContents.setWindowOpenHandler(({url}) => {
electron.shell.openExternal(url);
return {action: 'deny'};
});
// Dereference the mainWindow object when the mainWindow is closed.
mainWindow.on('closed', () => {
mainWindow = null;
});
};
function launchPython() {
if (!DEBUG) {
console.log("Python launched on:" + settings["python"]);
python_server_process = spawn(settings["python"],[`${__dirname}/phaseportrait-launcher.py`, `&>${__dirname}/python_log.log`], {signal})
python_server_process.stdout.on('data', (data) => {
if (DEBUG || DEBUG_RENDER) console.log(String(data));
FigId = String(data).split(',')[1];
setupMPLWebSocket();
if (phaseportrait_socket === null){
setupPPWebSocket();
};
updatePlot();
});
python_server_process.on('error', (err) => {
console.error('Failed to start subprocess.');
});
}
}
app.on('ready', () => {
// emptySVGDir();
createMainWindow();
if (!DEBUG){
launchPython();
}
else{
setupPPWebSocket();
setupMPLWebSocket();
updatePlot();
}
ipcMain.on('request-plot', (event, plotParams) => {
plot(plotParams);
})
ipcMain.on('request-code', (event, plotParams) => {
generateCode(plotParams);
})
ipcMain.on('save-configuration', (event, settings) => {
fs.writeFileSync("settings.json", JSON.stringify(settings), 'utf-8')
})
ipcMain.on('request-configuration', (e) => {
let readSettings = require(`${__dirname}/settings.json`)
mainWindow.webContents.send('load-configuration', readSettings);
})
});
// disable menu
app.on('browser-window-created', (e, window) => {
window.setMenu(null);
});
app.on('activate', () => {
if (mainWindow === null) {
createMainWindow();
}
});
app.on('quit', () => {
// do some additional cleanup
controller.abort()
});
app.on('window-all-closed', () => {
// For MacOs
if (process.platform !== 'darwin') {
app.quit()
}
});
function updatePlot() {
mainWindow.webContents.send('load-plot', FigId);
};
function get_websocket_type() {
if (typeof WebSocket !== 'undefined') {
return WebSocket;
} else if (typeof MozWebSocket !== 'undefined') {
return MozWebSocket;
} else {
alert(
'Your browser does not have WebSocket support. ' +
'Please try Chrome, Safari or Firefox ≥ 6. ' +
'Firefox 4 and 5 are also supported but you ' +
'have to enable WebSockets in about:config.'
);
}
};
function setupMPLWebSocket() {
let mpl_websocket_type = get_websocket_type();
// mpl_websocket = new mpl_websocket_type(`${ws_uri}ws`);
};
function setupPPWebSocket(){
let web_socket_type = get_websocket_type();
phaseportrait_socket = new web_socket_type(`${ws_uri}pp`);
// phaseportrait_socket.onerror = ...;
// phaseportrait_socket.onopen = ...;
// phaseportrait_socket.onmessage = ...;
phaseportrait_socket.onclose = function(){
setTimeout(setupPPWebSocket, 2000);
};
phaseportrait_socket.on("error", (err) => {
// logger.log('error', error);
launchPython();
showError(err);
});
};
function showPythonCode(message) {
mainWindow.webContents.send('show-code', message);
};
function showError(message) {
mainWindow.webContents.send('show-error', message);
};
function sendParamsToPython(plot = true, plotParams = []) {
plotParams["phaseportrait_request"] = plot ? '--plot' : '--code';
phaseportrait_socket.send(JSON.stringify(plotParams))
};
function plot(plotParams) {
phaseportrait_socket.removeAllListeners("message");
phaseportrait_socket.on("message", (data) => {
if (DEBUG || DEBUG_RENDER) console.log(String(data));
updatePlot(data.toString());
});
sendParamsToPython(true, plotParams);
};
function generateCode(codeParams) {
phaseportrait_socket.removeAllListeners("message");
phaseportrait_socket.on("message", (data) => {
showPythonCode(data.toString());
});
sendParamsToPython(false, codeParams);
};