forked from oxen-io/oxen-electron-gui-wallet
/
electron-main.js
221 lines (194 loc) · 5.44 KB
/
electron-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
import { app, ipcMain, BrowserWindow, Menu, dialog } from "electron";
import { version, productName } from "../../package.json";
import { Backend } from "./modules/backend";
import { checkForUpdate } from "./auto-updater";
import menuTemplate from "./menu";
import isDev from "electron-is-dev";
const portscanner = require("portscanner");
const windowStateKeeper = require("electron-window-state");
const path = require("upath");
/**
* Set `__statics` path to static files in production;
* The reason we are setting it here is that the path needs to be evaluated at runtime
*/
if (process.env.PROD) {
global.__statics = path.join(__dirname, "statics").replace(/\\/g, "\\\\");
global.__ryo_bin = path.join(__dirname, "..", "bin").replace(/\\/g, "\\\\");
} else {
global.__ryo_bin = path.join(process.cwd(), "bin").replace(/\\/g, "\\\\");
}
let mainWindow, backend;
let showConfirmClose = true;
let forceQuit = false;
let installUpdate = false;
const title = `${productName} v${version}`;
const selectionMenu = Menu.buildFromTemplate([
{ role: "copy" },
{ type: "separator" },
{ role: "selectall" }
]);
const inputMenu = Menu.buildFromTemplate([
{ role: "cut" },
{ role: "copy" },
{ role: "paste" },
{ type: "separator" },
{ role: "selectall" }
]);
function createWindow() {
/**
* Initial window options
*/
let mainWindowState = windowStateKeeper({
defaultWidth: 900,
defaultHeight: 700
});
mainWindow = new BrowserWindow({
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
minWidth: 640,
minHeight: 480,
icon: require("path").join(__statics, "icon_512x512.png"),
title,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true,
// anything we want preloaded, e.g. global vars
preload: path.resolve(__dirname, "electron-preload.js")
}
});
mainWindow.on("close", e => {
// Don't ask for confirmation if we're installing an update
if (installUpdate) {
return;
}
if (process.platform === "darwin") {
if (forceQuit) {
forceQuit = false;
if (showConfirmClose) {
e.preventDefault();
mainWindow.show();
mainWindow.webContents.send("confirmClose");
} else {
e.defaultPrevented = false;
}
} else {
e.preventDefault();
mainWindow.hide();
}
} else {
if (showConfirmClose) {
e.preventDefault();
mainWindow.webContents.send("confirmClose");
} else {
e.defaultPrevented = false;
}
}
});
ipcMain.on("confirmClose", (e, restart) => {
showConfirmClose = false;
// In dev mode, this will launch a blank white screen
if (restart && !isDev) app.relaunch();
const promise = backend ? backend.quit() : Promise.resolve();
promise.then(() => {
backend = null;
app.quit();
});
});
mainWindow.webContents.on("did-finish-load", () => {
// Set the title
mainWindow.setTitle(title);
require("crypto").randomBytes(64, (err, buffer) => {
// if err, then we may have to use insecure token generation perhaps
if (err) throw err;
let config = {
port: 12313,
token: buffer.toString("hex")
};
portscanner.checkPortStatus(config.port, "127.0.0.1", (error, status) => {
if (error) {
console.error(error);
}
if (status === "closed") {
backend = new Backend(mainWindow);
backend.init(config);
mainWindow.webContents.send("initialize", config);
} else {
dialog.showMessageBox(
mainWindow,
{
title: "Startup error",
message: `Loki Wallet is already open, or port ${config.port} is in use`,
type: "error",
buttons: ["ok"]
},
() => {
showConfirmClose = false;
app.quit();
}
);
}
});
});
});
mainWindow.webContents.on("context-menu", (e, props) => {
const { selectionText, isEditable } = props;
if (isEditable) {
inputMenu.popup(mainWindow);
} else if (selectionText && selectionText.trim() !== "") {
selectionMenu.popup(mainWindow);
}
});
mainWindow.loadURL(process.env.APP_URL);
mainWindowState.manage(mainWindow);
}
app.on("ready", () => {
checkForUpdate(
() => mainWindow,
autoUpdater => {
if (mainWindow) {
mainWindow.webContents.send("showQuitScreen");
}
const promise = backend ? backend.quit() : Promise.resolve();
promise.then(() => {
installUpdate = true;
backend = null;
autoUpdater.quitAndInstall();
});
}
);
if (process.platform === "darwin") {
const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu);
}
createWindow();
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
if (mainWindow === null) {
createWindow();
} else if (process.platform === "darwin") {
mainWindow.show();
}
});
app.on("before-quit", () => {
// Quit instantly if we are installing an update
if (installUpdate) {
return;
}
if (process.platform === "darwin") {
forceQuit = true;
} else {
if (backend) {
backend.quit().then(() => {
mainWindow.close();
});
}
}
});
app.on("quit", () => {});