antares/src/main/main.ts

213 lines
6.2 KiB
TypeScript
Raw Normal View History

import { app, BrowserWindow, globalShortcut, nativeImage, Menu, ipcMain } from 'electron';
2020-04-30 17:48:53 +02:00
import * as path from 'path';
import * as Store from 'electron-store';
import * as windowStateKeeper from 'electron-window-state';
import * as remoteMain from '@electron/remote/main';
2020-05-14 15:21:57 +02:00
import ipcHandlers from './ipc-handlers';
import { ShortcutRegister } from './libs/ShortcutRegister';
2020-05-11 18:05:34 +02:00
Store.initRenderer();
const settingsStore = new Store({ name: 'settings' });
let shortCutRegister: ShortcutRegister;
const appTheme = settingsStore.get('application_theme');
2020-06-14 19:02:07 +02:00
const isDevelopment = process.env.NODE_ENV !== 'production';
const isMacOS = process.platform === 'darwin';
2022-06-07 18:32:37 +02:00
const isLinux = process.platform === 'linux';
const isWindows = process.platform === 'win32';
2022-06-08 13:24:14 +02:00
const gotTheLock = app.requestSingleInstanceLock();
2020-09-15 14:44:29 +02:00
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)
2022-04-12 17:08:05 +02:00
let mainWindow: BrowserWindow;
let mainWindowState: windowStateKeeper.State;
2020-04-30 17:48:53 +02:00
2020-07-29 15:56:29 +02:00
async function createMainWindow () {
2022-06-02 11:17:53 +02:00
const icon = require('../renderer/images/logo-64.png');
2020-04-30 17:48:53 +02:00
const window = new BrowserWindow({
width: mainWindowState.width,
height: mainWindowState.height,
x: mainWindowState.x,
y: mainWindowState.y,
2020-05-30 12:54:05 +02:00
minWidth: 900,
2020-07-31 15:45:32 +02:00
minHeight: 550,
show: !isWindows,
title: 'Antares SQL',
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-31 18:16:28 +02:00
spellcheck: false
2020-06-02 19:13:57 +02:00
},
2022-06-07 18:32:37 +02:00
autoHideMenuBar: true,
titleBarStyle: isLinux ? 'default' :'hidden',
titleBarOverlay: isWindows
? {
color: appTheme === 'dark' ? '#3f3f3f' : '#fff',
symbolColor: appTheme === 'dark' ? '#fff' : '#000',
height: 30
}
: false,
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
});
mainWindowState.manage(window);
window.on('moved', saveWindowState);
remoteMain.enable(window.webContents);
try {
2022-04-27 18:23:05 +02:00
if (isDevelopment)
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', () => {
window.removeListener('moved', saveWindowState);
2020-04-30 17:48:53 +02:00
mainWindow = null;
});
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();
2022-06-07 18:32:37 +02:00
ipcMain.on('refresh-theme-settings', () => {
const appTheme = settingsStore.get('application_theme');
if (isWindows && mainWindow) {
mainWindow.setTitleBarOverlay({
color: appTheme === 'dark' ? '#3f3f3f' : '#fff',
symbolColor: appTheme === 'dark' ? '#fff' : '#000'
});
}
2022-06-07 18:32:37 +02:00
});
2022-06-27 18:28:04 +02:00
ipcMain.on('change-window-title', (_, title: string) => {
if (mainWindow) mainWindow.setTitle(title);
2022-06-08 13:04:19 +02:00
});
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 () => {
mainWindowState = windowStateKeeper({
defaultWidth: 1024,
defaultHeight: 800
});
mainWindow = await createMainWindow();
createAppMenu();
shortCutRegister = new ShortcutRegister({ mainWindow });
if (isWindows)
mainWindow.show();
2022-04-21 14:39:24 +02:00
if (isDevelopment)
mainWindow.webContents.openDevTools();
2021-11-19 13:13:35 +01:00
process.on('uncaughtException', error => {
mainWindow.webContents.send('unhandled-exception', error);
});
2021-11-19 13:13:35 +01:00
process.on('unhandledRejection', error => {
mainWindow.webContents.send('unhandled-exception', error);
});
2020-09-15 14:44:29 +02:00
});
app.on('browser-window-created', (event, window) => {
if (isDevelopment) {
const { antares } = require('../../package.json');
const extensionPath = path.resolve(__dirname, `../../misc/${antares.devtoolsId}`);
window.webContents.session.loadExtension(extensionPath, { allowFileAccess: true }).catch(console.error);
}
});
app.on('browser-window-focus', () => {
// Send registered shortcut events to window
shortCutRegister.init();
2022-07-16 12:01:37 +02:00
if (isDevelopment) { // Dev shortcuts
globalShortcut.register('Shift+CommandOrControl+F5', () => {
mainWindow.reload();
});
globalShortcut.register('Shift+CommandOrControl+F12', () => {
mainWindow.webContents.openDevTools();
});
}
});
app.on('browser-window-blur', () => {
shortCutRegister.unregister();
});
2020-09-15 14:44:29 +02:00
}
function createAppMenu () {
2022-04-12 17:08:05 +02:00
let menu: Electron.Menu = null;
if (isMacOS) {
menu = Menu.buildFromTemplate([
{
label: app.name,
submenu: [
{ role: 'about' },
{ type: 'separator' },
{
label: 'Check for Updates...',
click: (_menuItem, win) => win.webContents.send('open-updates-preferences')
},
{
label: 'Preferences',
click: (_menuItem, win) => win.webContents.send('toggle-preferences'),
accelerator: 'CmdOrCtrl+,'
},
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideOthers' },
{ type: 'separator' },
{ role: 'quit' }
]
},
{
role: 'editMenu'
},
{
role: 'viewMenu'
},
{
role: 'windowMenu'
}
]);
}
Menu.setApplicationMenu(menu);
}
function saveWindowState () {
mainWindowState.saveState(mainWindow);
}