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

209 lines
5.7 KiB
JavaScript
Raw Normal View History

import { app, ipcMain, BrowserWindow, Menu, dialog } from "electron"
2019-06-19 02:01:04 +02:00
import { version, productName } from "../../package.json"
2018-09-08 23:44:19 +02:00
import { Backend } from "./modules/backend"
import { checkForUpdate } from "./auto-updater"
import menuTemplate from "./menu"
import isDev from "electron-is-dev"
2018-09-08 23:44:19 +02:00
const portscanner = require("portscanner")
const windowStateKeeper = require("electron-window-state")
2019-10-14 04:09:29 +02:00
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) {
2019-10-14 04:09:29 +02: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 {
2019-10-14 04:09:29 +02:00
global.__ryo_bin = path.join(process.cwd(), "bin").replace(/\\/g, "\\\\")
2018-09-08 23:44:19 +02:00
}
let mainWindow, backend
let showConfirmClose = true
let forceQuit = false
let installUpdate = false
2018-09-08 23:44:19 +02:00
2019-06-19 02:01:04 +02:00
const title = `${productName} v${version}`
2019-06-07 02:11:28 +02:00
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 () {
2018-09-08 23:44:19 +02:00
/**
* Initial window options
*/
let mainWindowState = windowStateKeeper({
defaultWidth: 900,
defaultHeight: 700
})
2018-09-08 23:44:19 +02:00
mainWindow = new BrowserWindow({
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
minWidth: 640,
minHeight: 480,
2019-06-19 02:01:04 +02:00
icon: require("path").join(__statics, "icon_512x512.png"),
title
2018-09-08 23:44:19 +02: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")
} else {
e.defaultPrevented = false
}
} else {
e.preventDefault()
mainWindow.hide()
}
2018-09-08 23:44:19 +02:00
} else {
if (showConfirmClose) {
e.preventDefault()
mainWindow.webContents.send("confirmClose")
} else {
e.defaultPrevented = false
}
2018-09-08 23:44:19 +02:00
}
})
ipcMain.on("confirmClose", (e, restart) => {
2018-09-08 23:44:19 +02:00
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
2018-09-08 23:44:19 +02:00
app.quit()
})
2018-09-08 23:44:19 +02:00
})
mainWindow.webContents.on("did-finish-load", () => {
2019-06-19 02:01:04 +02:00
// Set the title
mainWindow.setTitle(title)
2018-09-08 23:44:19 +02: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
let config = {
2019-03-27 03:48:25 +01:00
port: 12313,
2018-09-08 23:44:19 +02:00
token: buffer.toString("hex")
}
portscanner.checkPortStatus(config.port, "127.0.0.1", (error, status) => {
if (error) {
console.error(error)
}
2019-06-07 02:16:41 +02:00
if (status === "closed") {
backend = new Backend(mainWindow)
2018-09-08 23:44:19 +02:00
backend.init(config)
mainWindow.webContents.send("initialize", config)
2018-09-08 23:44:19 +02:00
} else {
dialog.showMessageBox(mainWindow, {
title: "Startup error",
message: `Loki Wallet is already open, or port ${config.port} is in use`,
2018-09-08 23:44:19 +02:00
type: "error",
buttons: ["ok"]
}, () => {
showConfirmClose = false
app.quit()
})
}
})
})
})
2019-06-07 02:11:28 +02:00
mainWindow.webContents.on("context-menu", (e, props) => {
const { selectionText, isEditable } = props
if (isEditable) {
inputMenu.popup(mainWindow)
} else if (selectionText && selectionText.trim() !== "") {
selectionMenu.popup(mainWindow)
}
})
2018-09-08 23:44:19 +02:00
mainWindow.loadURL(process.env.APP_URL)
mainWindowState.manage(mainWindow)
2018-09-08 23:44:19 +02:00
}
app.on("ready", () => {
checkForUpdate(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()
})
2018-09-08 23:44:19 +02:00
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()
2018-09-08 23:44:19 +02:00
}
})
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()
})
}
}
2018-09-08 23:44:19 +02:00
})
app.on("quit", () => {
})