forked from fyears/electron-python-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
136 lines (105 loc) · 2.76 KB
/
Copy pathmain.js
File metadata and controls
136 lines (105 loc) · 2.76 KB
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
const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const path = require('path')
const manualDebug = true
const windowStateKeeper = require('electron-window-state')
/*************************************************************
* py process
*************************************************************/
const PY_DIST_FOLDER = 'pycalcdist'
const PY_FOLDER = 'pycalc'
const PY_MODULE = 'api' // without .py suffix
let pyProc = null
let pyPort = null
const guessPackaged = () => {
const fullPath = path.join(__dirname, PY_DIST_FOLDER)
return require('fs').existsSync(fullPath)
}
const getScriptPath = () => {
if (!guessPackaged()) {
return path.join(__dirname, PY_FOLDER, PY_MODULE + '.py')
}
if (process.platform === 'win32') {
return path.join(__dirname, PY_DIST_FOLDER, PY_MODULE, PY_MODULE + '.exe')
}
return path.join(__dirname, PY_DIST_FOLDER, PY_MODULE, PY_MODULE)
}
const selectPort = () => {
pyPort = 4242
return pyPort
}
const createPyProc = () => {
let script = getScriptPath()
let port = '' + selectPort()
if (!manualDebug) {
if (guessPackaged()) {
pyProc = require('child_process').execFile(script, [port])
} else {
pyProc = require('child_process').spawn('python', [script, port])
}
if (pyProc != null) {
//console.log(pyProc)
console.log('child process success on port ' + port)
}
}
}
const exitPyProc = () => {
if (!manualDebug) {
pyProc.kill();
pyProc = null;
pyPort = null;
}
}
process.on('SIGINT', function () {
console.log("Caught interrupt signal");
if (i_should_exit)
process.exit();
});
app.on('ready', createPyProc)
app.on('will-quit', exitPyProc)
app.on('quit', exitPyProc)
/*************************************************************
* window management
*************************************************************/
let mainWindow = null;
let secondaryWindow = null;
function createWindow() {
let winState = windowStateKeeper({
defaultWidth: 1000,
defaultHeight: 800
})
mainWindow = new BrowserWindow({
minWidth: 500,
width: winState.width,
x: winState.x,
minHeight: 400,
height: winState.height,
y: winState.y,
webPreferences: {
nodeIntegration: true
},
})
let wc = mainWindow.webContents;
console.log(wc);
// Load the index.html
mainWindow.loadFile('index.html')
winState.manage(mainWindow)
//Load devtools
mainWindow.webContents.openDevTools()
//Close the windows
mainWindow.on('closed', () => {
mainWindow = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})