oxen-electron-gui-wallet/src-electron/main-process/electron-main.js

222 lines
5.4 KiB
JavaScript
Raw Normal View History

2020-03-03 04:16:23 +01:00
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");
2018-09-08 23:44:19 +02:00
/**
* 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) {
2020-03-03 04:16:23 +01:00
global.__statics = path.join(__dirname, "statics").replace(/\\/g, "\\\\");
global.__ryo_bin = path.join(__dirname, "..", "bin").replace(/\\/g, "\\\\");
2018-09-08 23:44:19 +02:00
} else {
2020-03-03 04:16:23 +01:00
global.__ryo_bin = path.join(process.cwd(), "bin").replace(/\\/g, "\\\\");
2018-09-08 23:44:19 +02:00
}
2020-03-03 04:16:23 +01:00
let mainWindow, backend;
let showConfirmClose = true;
let forceQuit = false;
let installUpdate = false;
2018-09-08 23:44:19 +02:00
2020-03-03 04:16:23 +01:00
const title = `${productName} v${version}`;
2019-06-19 02:01:04 +02:00
const selectionMenu = Menu.buildFromTemplate([
{ role: "copy" },
{ type: "separator" },
{ role: "selectall" }
]);
2019-06-07 02:11:28 +02:00
const inputMenu = Menu.buildFromTemplate([
2020-03-03 04:16:23 +01:00
{ 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,
2021-01-06 06:26:42 +01:00
icon: require("path").join(__statics, "icon.png"),
title,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true,
// anything we want preloaded, e.g. global vars
preload: path.resolve(__dirname, "electron-preload.js")
}
2020-03-03 04:16:23 +01:00
});
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");
2018-09-08 23:44:19 +02:00
} else {
2020-03-03 04:16:23 +01:00
e.defaultPrevented = false;
2018-09-08 23:44:19 +02:00
}
2020-03-03 04:16:23 +01:00
} else {
e.preventDefault();
mainWindow.hide();
}
} else {
if (showConfirmClose) {
e.preventDefault();
mainWindow.webContents.send("confirmClose");
} else {
e.defaultPrevented = false;
}
}
});
2018-09-08 23:44:19 +02:00
2020-03-03 04:16:23 +01:00
ipcMain.on("confirmClose", (e, restart) => {
showConfirmClose = false;
2020-03-03 04:16:23 +01:00
// In dev mode, this will launch a blank white screen
if (restart && !isDev) app.relaunch();
2020-03-03 04:16:23 +01:00
const promise = backend ? backend.quit() : Promise.resolve();
promise.then(() => {
backend = null;
app.quit();
});
});
2018-09-08 23:44:19 +02:00
2020-03-03 04:16:23 +01:00
mainWindow.webContents.on("did-finish-load", () => {
// Set the title
mainWindow.setTitle(title);
2019-06-19 02:01:04 +02:00
2020-03-03 04:16:23 +01:00
require("crypto").randomBytes(64, (err, buffer) => {
// if err, then we may have to use insecure token generation perhaps
if (err) throw err;
2018-09-08 23:44:19 +02:00
2020-03-03 04:16:23 +01:00
let config = {
port: 12313,
token: buffer.toString("hex")
};
2018-09-08 23:44:19 +02:00
2020-03-03 04:16:23 +01:00
portscanner.checkPortStatus(config.port, "127.0.0.1", (error, status) => {
if (error) {
console.error(error);
2019-06-07 02:11:28 +02:00
}
2020-03-03 04:16:23 +01:00
if (status === "closed") {
backend = new Backend(mainWindow);
backend.init(config);
mainWindow.webContents.send("initialize", config);
} else {
dialog.showMessageBox(
mainWindow,
{
title: "Startup error",
2021-01-06 06:26:42 +01:00
message: `Oxen Wallet is already open, or port ${config.port} is in use`,
2020-03-03 04:16:23 +01:00
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);
2018-09-08 23:44:19 +02:00
}
app.on("ready", () => {
2020-03-04 05:32:46 +01:00
checkForUpdate(
() => mainWindow,
autoUpdater => {
if (mainWindow) {
mainWindow.webContents.send("showQuitScreen");
}
2020-03-03 04:16:23 +01:00
2020-03-04 05:32:46 +01:00
const promise = backend ? backend.quit() : Promise.resolve();
promise.then(() => {
installUpdate = true;
backend = null;
autoUpdater.quitAndInstall();
});
}
);
2020-03-03 04:16:23 +01:00
if (process.platform === "darwin") {
const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu);
}
createWindow();
});
2018-09-08 23:44:19 +02:00
app.on("window-all-closed", () => {
2020-03-03 04:16:23 +01:00
if (process.platform !== "darwin") {
app.quit();
}
});
2018-09-08 23:44:19 +02:00
app.on("activate", () => {
2020-03-03 04:16:23 +01:00
if (mainWindow === null) {
createWindow();
} else if (process.platform === "darwin") {
mainWindow.show();
}
});
2018-09-08 23:44:19 +02:00
app.on("before-quit", () => {
2020-03-03 04:16:23 +01:00
// 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();
});
}
2020-03-03 04:16:23 +01:00
}
});
2020-03-03 04:16:23 +01:00
app.on("quit", () => {});