qortal-ui/electron.js

208 lines
5.2 KiB
JavaScript
Raw Normal View History

2022-12-19 18:33:45 +01:00
const { app, BrowserWindow, ipcMain, Menu, Notification, Tray, nativeImage, dialog, webContents, nativeTheme } = require('electron')
const { autoUpdater } = require('electron-updater')
const server = require('./server.js')
const log = require('electron-log')
const path = require('path')
const i18n = require("./lib/i18n.js")
2021-12-25 14:39:47 +01:00
2022-12-19 18:33:45 +01:00
app.disableHardwareAcceleration()
app.enableSandbox()
2021-12-25 14:39:47 +01:00
process.env['APP_PATH'] = app.getAppPath()
2021-12-25 14:39:47 +01:00
autoUpdater.autoDownload = false
autoUpdater.autoInstallOnAppQuit = false
autoUpdater.logger = log
autoUpdater.logger.transports.file.level = 'info'
log.info('App starting...')
2021-12-25 14:39:47 +01:00
const editMenu = Menu.buildFromTemplate([
{
label: "Qortal",
submenu: [{
label: "Quit",
click() {
app.quit()
2021-12-25 14:39:47 +01:00
}
}]
},
{
label: "Edit",
submenu: [
{label: "Undo", accelerator: "CommandOrControl+Z", selector: "undo:"},
{label: "Redo", accelerator: "CommandOrControl+Shift+Z", selector: "redo:"},
{type: "separator"},
{label: "Cut", accelerator: "CommandOrControl+X", selector: "cut:"},
{label: "Copy", accelerator: "CommandOrControl+C", selector: "copy:"},
{label: "Paste", accelerator: "CommandOrControl+V", selector: "paste:"},
{label: "Select All", accelerator: "CommandOrControl+A", selector: "selectAll:"}
]
}
])
2021-12-25 14:39:47 +01:00
Menu.setApplicationMenu(editMenu)
2021-12-25 14:39:47 +01:00
2022-12-31 18:59:31 +01:00
let myWindow = null
2021-12-25 14:39:47 +01:00
function createWindow() {
myWindow = new BrowserWindow({
backgroundColor: '#eee',
width: 1280,
height: 720,
minWidth: 700,
minHeight: 640,
2022-12-19 18:33:45 +01:00
icon: path.join(__dirname + '/img/icons/png/256x256.png'),
2022-10-03 11:18:39 +02:00
title: "Qortal UI",
2021-12-25 14:39:47 +01:00
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: false,
2022-12-19 18:33:45 +01:00
nodeIntegrationInWorker: true,
2021-12-25 14:39:47 +01:00
partition: 'persist:webviewsession',
2022-12-19 18:33:45 +01:00
enableRemoteModule: false
2021-12-25 14:39:47 +01:00
},
show: false
})
2022-10-03 11:18:39 +02:00
myWindow.maximize()
myWindow.show()
2021-12-25 14:39:47 +01:00
myWindow.loadURL('http://localhost:12388/app/wallet')
myWindow.on('closed', function () {
myWindow = null
})
2022-12-19 18:33:45 +01:00
myWindow.on('minimize', function (event) {
2022-10-03 11:18:39 +02:00
event.preventDefault()
myWindow.hide()
})
2022-12-19 18:33:45 +01:00
ipcMain.handle('dark-mode:toggle', () => {
if (nativeTheme.shouldUseDarkColors) {
nativeTheme.themeSource = 'light'
} else {
nativeTheme.themeSource = 'dark'
}
return nativeTheme.shouldUseDarkColors
})
ipcMain.handle('dark-mode:system', () => {
nativeTheme.themeSource = 'system'
})
2021-12-25 14:39:47 +01:00
}
const createTray = () => {
2022-12-19 18:33:45 +01:00
let myTray = new Tray(path.join(__dirname + '/img/icons/png/tray/tray.png'))
2022-10-03 11:18:39 +02:00
const contextMenu = Menu.buildFromTemplate([
{
label: `Qortal UI v${app.getVersion()}`,
enabled: false,
},
{
type: 'separator',
},
{
label: i18n.__("electron_translate_1"),
2022-10-03 11:18:39 +02:00
click: function () {
myWindow.maximize()
myWindow.show()
},
},
{
label: i18n.__("electron_translate_2"),
2022-12-19 18:33:45 +01:00
click: function () {
2022-10-03 11:18:39 +02:00
myTray.destroy()
app.quit()
},
2021-12-25 14:39:47 +01:00
},
2022-10-03 11:18:39 +02:00
])
2021-12-25 14:39:47 +01:00
myTray.setTitle("QORTAL UI")
2022-10-03 11:18:39 +02:00
myTray.setToolTip(`Qortal UI v${app.getVersion()}`)
2021-12-25 14:39:47 +01:00
myTray.setContextMenu(contextMenu)
2022-10-03 11:18:39 +02:00
myTray.on("double-click", () => myWindow.maximize() , myWindow.show())
2021-12-25 14:39:47 +01:00
}
const isLock = app.requestSingleInstanceLock()
2021-12-25 14:39:47 +01:00
if (!isLock) {
app.quit()
2021-12-25 14:39:47 +01:00
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
if (myWindow) {
if (myWindow.isMinimized())
myWindow.maximize()
myWindow.show()
}
})
2022-12-19 18:33:45 +01:00
app.whenReady().then(() => {
createWindow()
createTray()
2021-12-25 14:39:47 +01:00
if (process.platform === 'win32') {
app.setAppUserModelId("org.qortal.QortalUI")
2021-12-25 14:39:47 +01:00
}
autoUpdater.checkForUpdatesAndNotify()
setInterval(() => {
autoUpdater.checkForUpdatesAndNotify()
}, 1000 * 60 * 720)
2022-12-19 18:33:45 +01:00
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
createTray()
}
})
2021-12-25 14:39:47 +01:00
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit();
}
})
ipcMain.on('app_version', (event) => {
log.info(app.getVersion())
mainWindow.webContents.send('app_version', { version: app.getVersion() })
})
autoUpdater.on('update-available', (event) => {
const downloadOpts = {
type: 'info',
buttons: ['YES', 'NO'],
title: i18n.__("electron_translate_3"),
detail: i18n.__("electron_translate_4")
}
dialog.showMessageBox(downloadOpts).then((returnValue) => {
if (returnValue.response === 0) {
autoUpdater.downloadUpdate()
const dl = new Notification({
title: i18n.__("electron_translate_11"),
body: i18n.__("electron_translate_12")
})
dl.show()
} else {
return
}
2021-12-25 14:39:47 +01:00
})
})
autoUpdater.on('download-progress', (progressObj) => {
myWindow.webContents.send('downloadProgress', progressObj)
2021-12-25 14:39:47 +01:00
})
2022-02-15 19:23:10 +01:00
autoUpdater.on('update-downloaded', (event) => {
const dialogOpts = {
type: 'info',
buttons: [i18n.__("electron_translate_5"), i18n.__("electron_translate_6")],
title: i18n.__("electron_translate_7"),
message: i18n.__("electron_translate_8"),
detail: i18n.__("electron_translate_9")
2022-02-15 19:23:10 +01:00
}
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) {
autoUpdater.quitAndInstall()
} else {
return
}
})
})
2021-12-25 14:39:47 +01:00
autoUpdater.on('error', (err) => {
const n = new Notification({
title: i18n.__("electron_translate_10"),
2021-12-25 14:39:47 +01:00
body: err
})
n.show()
2021-12-25 14:39:47 +01:00
})
2022-12-19 18:33:45 +01:00
process.on('uncaughtException', function (err) {
log.info("***WHOOPS TIME****"+err)
})
2022-12-31 18:59:31 +01:00
}