antares/src/main/main.js

145 lines
3.7 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';
import * as remoteMain from '@electron/remote/main';
2020-05-14 15:21:57 +02:00
import ipcHandlers from './ipc-handlers';
2020-05-11 18:05:34 +02:00
// remoteMain.initialize();
Store.initRenderer();
2020-06-14 19:02:07 +02:00
const isDevelopment = process.env.NODE_ENV !== 'production';
const isMacOS = process.platform === 'darwin';
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
spellcheck: false
2020-06-02 19:13:57 +02:00
},
frame: false,
titleBarStyle: isMacOS ? 'hidden' : 'default',
trafficLightPosition: isMacOS ? { x: 10, y: 8 } : undefined,
2020-06-02 19:13:57 +02:00
backgroundColor: '#1d1d1d'
2020-04-30 17:48:53 +02:00
});
remoteMain.enable(window.webContents);
try {
if (isDevelopment) {
const { default: installExtension, VUEJS3_DEVTOOLS } = require('electron-devtools-installer');
const options = {
loadExtensionOptions: { allowFileAccess: true }
};
try {
const name = await installExtension(VUEJS3_DEVTOOLS, options);
console.log(`Added Extension: ${name}`);
}
catch (err) {
console.log('An error occurred: ', err);
}
await window.loadURL('http://localhost:9080');
}
else {
const indexPath = path.resolve(__dirname, 'index.html');
await window.loadFile(indexPath);
}
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-04-30 17:48:53 +02:00
if (!gotTheLock) app.quit();
2020-09-15 14:44:29 +02:00
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 (isMacOS) app.quit();
2020-09-15 14:44:29 +02:00
});
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();
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();
createAppMenu();
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
});
}
function createAppMenu () {
let menu = null;
if (isMacOS) {
menu = Menu.buildFromTemplate([
{
role: 'appMenu'
},
{
role: 'editMenu'
},
{
role: 'viewMenu'
},
{
role: 'windowMenu'
}
]);
}
Menu.setApplicationMenu(menu);
}