-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathmain.js
155 lines (126 loc) · 4.2 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
const {app, BrowserWindow, globalShortcut, dialog} = require('electron');
let mainWindow = null;
let serverProcess = null;
let expectedKill = false;
app.allowRendererProcessReuse = true;
// Provide API for web application
global.callElectronUiApi = function () {
console.log('Electron called from web app with args "' + JSON.stringify(arguments) + '"');
if (arguments) {
switch (arguments[0]) {
case 'exit':
console.log('Kill server process');
expectedKill = true;
const kill = require('tree-kill');
kill(serverProcess.pid, 'SIGTERM', function () {
console.log('Server process killed');
serverProcess = null;
if (mainWindow !== null) {
mainWindow.close();
}
});
break;
case 'minimize':
mainWindow.minimize();
break;
case 'maximize':
if (!mainWindow.isMaximized()) {
mainWindow.maximize();
} else {
mainWindow.unmaximize();
}
break;
case 'devtools':
mainWindow.webContents.openDevTools();
break;
}
}
};
app.on('window-all-closed', function () {
app.quit();
});
app.on('ready', function () {
platform = process.platform;
if (platform === 'win32') {
serverProcess = require('child_process')
.spawn('cmd.exe', ['/c', 'electron-vaadin.bat'],
{
cwd: './electron-vaadin/bin'
});
} else {
serverProcess = require('child_process')
.spawn(__dirname + '/electron-vaadin/bin/electron-vaadin');
}
if (!serverProcess) {
console.error('Unable to start server from ' + __dirname);
app.quit();
return;
}
serverProcess.stdout.on('data', function (data) {
process.stdout.write('Server: ' + data);
});
serverProcess.stderr.on('data', function (data) {
process.stderr.write('Server error: ' + data);
});
serverProcess.on('exit', code => {
if (expectedKill) {
return;
}
serverProcess = null;
console.warn(code);
if (code !== 0) {
console.error(`Server stopped unexpectedly with code ${code}`);
dialog.showErrorBox("An error occurred", "The server stopped unexpectedly, app will close.");
}
if (mainWindow !== null) {
mainWindow.close();
}
});
console.log("Server PID: " + serverProcess.pid);
let appUrl = 'http://localhost:8080';
const openWindow = function () {
mainWindow = new BrowserWindow({
title: 'TODO List - Electron Vaadin application',
width: 500,
height: 768,
frame: false,
webPreferences: {nodeIntegration: true}
});
mainWindow.loadURL(appUrl);
// uncomment to show debug tools
//mainWindow.webContents.openDevTools();
mainWindow.on('closed', function () {
mainWindow = null;
});
mainWindow.on('close', function (e) {
if (serverProcess) {
e.preventDefault();
mainWindow.webContents.executeJavaScript("vaadinApi.appWindowExit();");
}
});
};
const startUp = function () {
const requestPromise = require('minimal-request-promise');
requestPromise.get(appUrl).then(function (response) {
console.log('Server started!');
openWindow();
}, function (response) {
console.log('Waiting for the server start...');
setTimeout(function () {
startUp();
}, 1000);
});
};
startUp();
// Register a shortcut listener.
globalShortcut.register('CommandOrControl+Shift+`', () => {
console.log('Bring to front shortcut triggered');
if (mainWindow) {
mainWindow.focus();
}
})
});
app.on('will-quit', () => {
// Unregister all shortcuts.
globalShortcut.unregisterAll();
});