antares/src/main/index.js

105 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-04-30 17:48:53 +02:00
'use strict';
2020-05-31 17:56:33 +02:00
import { app, BrowserWindow, nativeImage } from 'electron';
2020-04-30 17:48:53 +02:00
import * as path from 'path';
import crypto from 'crypto';
2020-04-30 17:48:53 +02:00
import { format as formatUrl } from 'url';
import keytar from 'keytar';
2020-05-14 15:21:57 +02:00
import ipcHandlers from './ipc-handlers';
2020-05-11 18:05:34 +02:00
2020-06-14 19:02:07 +02:00
const isDevelopment = process.env.NODE_ENV !== 'production';
2020-09-15 14:44:29 +02:00
const gotTheLock = app.requestSingleInstanceLock();
2020-04-30 17:48:53 +02:00
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true';
// global reference to mainWindow (necessary to prevent window from being garbage collected)
let mainWindow;
2020-07-29 15:56:29 +02:00
async function createMainWindow () {
2020-05-31 17:56:33 +02:00
const icon = require('../renderer/images/logo-32.png');
2020-04-30 17:48:53 +02:00
const window = new BrowserWindow({
2020-07-31 15:45:32 +02:00
width: 1024,
height: 800,
2020-05-30 12:54:05 +02:00
minWidth: 900,
2020-07-31 15:45:32 +02:00
minHeight: 550,
2020-05-07 17:45:04 +02:00
title: 'Antares',
autoHideMenuBar: true,
2020-05-31 17:56:33 +02:00
icon: nativeImage.createFromDataURL(icon.default),
2020-04-30 17:48:53 +02:00
webPreferences: {
nodeIntegration: true,
2020-07-29 15:56:29 +02:00
'web-security': false,
2020-07-31 18:16:28 +02:00
enableRemoteModule: true,
spellcheck: false
2020-06-02 19:13:57 +02:00
},
frame: false,
backgroundColor: '#1d1d1d'
2020-04-30 17:48:53 +02:00
});
if (isDevelopment) {
2020-07-29 15:56:29 +02:00
await window.loadURL(`http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}`);
2020-05-31 17:56:33 +02:00
const { default: installExtension, VUEJS_DEVTOOLS } = require('electron-devtools-installer');
2020-04-30 17:48:53 +02:00
window.webContents.openDevTools();
installExtension(VUEJS_DEVTOOLS)
.then(name => {
console.log(name, 'installed');
})
.catch(err => {
console.log(err);
});
}
2020-07-29 15:56:29 +02:00
else {
await window.loadURL(formatUrl({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file',
slashes: true
}));
}
2020-04-30 17:48:53 +02:00
window.on('closed', () => {
mainWindow = null;
});
window.webContents.on('devtools-opened', () => {
window.focus();
setImmediate(() => {
window.focus();
});
});
return window;
};
2020-09-15 14:44:29 +02:00
if (!gotTheLock)
app.quit();
else {
// Initialize ipcHandlers
ipcHandlers();
2020-09-15 14:44:29 +02:00
// quit application when all windows are closed
app.on('window-all-closed', () => {
// on macOS it is common for applications to stay open until the user explicitly quits
if (process.platform !== 'darwin')
app.quit();
});
2020-04-30 17:48:53 +02:00
2020-09-15 14:44:29 +02:00
app.on('activate', () => {
// on macOS it is common to re-create a window even after all windows have been closed
if (mainWindow === null)
mainWindow = createMainWindow();
});
2020-04-30 17:48:53 +02:00
2020-09-15 14:44:29 +02:00
// create main BrowserWindow when electron is ready
app.on('ready', async () => {
let key = await keytar.getPassword('antares', 'user');
if (!key) {
key = crypto.randomBytes(16).toString('hex');
keytar.setPassword('antares', 'user', key);
}
2020-09-15 14:44:29 +02:00
mainWindow = createMainWindow();
});
}