antares/src/main/index.js

113 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-04-30 17:48:53 +02:00
'use strict';
import { app, BrowserWindow, /* session, */ nativeImage, Menu } from 'electron';
2020-04-30 17:48:53 +02:00
import * as path from 'path';
import Store from 'electron-store';
2020-05-14 15:21:57 +02:00
import ipcHandlers from './ipc-handlers';
2020-05-11 18:05:34 +02:00
Store.initRenderer();
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,
contextIsolation: false,
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
});
try {
if (isDevelopment) { //
await window.loadURL(`http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}`);
// const { default: installExtension, VUEJS3_DEVTOOLS } = require('electron-devtools-installer');
// const oldDevToolsID = session.defaultSession.getAllExtensions().find(ext => ext.name === 'Vue.js devtools').id;
// session.defaultSession.removeExtension(oldDevToolsID);
// const toolName = await installExtension(VUEJS3_DEVTOOLS);
// console.log(toolName, 'installed');
}
else
await window.loadURL(new URL(`file:///${path.join(__dirname, 'index.html')}`).href);
2020-04-30 17:48:53 +02:00
}
catch (err) {
console.log(err);
2020-07-29 15:56:29 +02:00
}
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 {
require('@electron/remote/main').initialize();
2020-09-15 14:44:29 +02:00
// 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
app.on('activate', async () => {
2020-09-15 14:44:29 +02:00
// on macOS it is common to re-create a window even after all windows have been closed
if (mainWindow === null) {
mainWindow = await createMainWindow();
2021-04-30 14:14:01 +02:00
if (isDevelopment)
mainWindow.webContents.openDevTools();
}
2020-09-15 14:44:29 +02:00
});
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 () => {
mainWindow = await createMainWindow();
Menu.setApplicationMenu(null);
2021-04-30 14:14:01 +02:00
if (isDevelopment)
mainWindow.webContents.openDevTools();
process.on('uncaughtException', error => {
mainWindow.webContents.send('unhandled-exception', error);
});
process.on('unhandledRejection', error => {
mainWindow.webContents.send('unhandled-exception', error);
});
2020-09-15 14:44:29 +02:00
});
}