2022-07-13 18:25:42 +02:00
|
|
|
import { app, BrowserWindow, globalShortcut, nativeImage, Menu, ipcMain } from 'electron';
|
2020-04-30 17:48:53 +02:00
|
|
|
import * as path from 'path';
|
2022-04-12 17:59:22 +02:00
|
|
|
import * as Store from 'electron-store';
|
2021-12-28 17:12:10 +01:00
|
|
|
import * as windowStateKeeper from 'electron-window-state';
|
2021-09-21 11:16:07 +02:00
|
|
|
import * as remoteMain from '@electron/remote/main';
|
2021-01-25 09:28:57 +01:00
|
|
|
|
2020-05-14 15:21:57 +02:00
|
|
|
import ipcHandlers from './ipc-handlers';
|
2022-07-13 18:25:42 +02:00
|
|
|
import { shortcuts } from 'common/shortcuts';
|
2020-05-11 18:05:34 +02:00
|
|
|
|
2021-01-25 09:28:57 +01:00
|
|
|
Store.initRenderer();
|
2022-06-07 18:32:37 +02:00
|
|
|
const persistentStore = new Store({ name: 'settings' });
|
2021-01-25 09:28:57 +01:00
|
|
|
|
2022-06-07 18:32:37 +02:00
|
|
|
const appTheme = persistentStore.get('application_theme');
|
2020-06-14 19:02:07 +02:00
|
|
|
const isDevelopment = process.env.NODE_ENV !== 'production';
|
2021-10-23 18:15:32 +02:00
|
|
|
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({
|
2021-12-28 17:12:10 +01:00
|
|
|
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,
|
2022-06-23 11:57:25 +02:00
|
|
|
show: !isWindows,
|
2021-11-12 13:12:05 +01:00
|
|
|
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,
|
2021-04-26 10:07:47 +02:00
|
|
|
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,
|
2021-10-23 18:15:32 +02:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2021-12-28 17:12:10 +01:00
|
|
|
mainWindowState.manage(window);
|
|
|
|
window.on('moved', saveWindowState);
|
|
|
|
|
2021-09-21 11:16:07 +02:00
|
|
|
remoteMain.enable(window.webContents);
|
|
|
|
|
2021-04-26 10:07:47 +02:00
|
|
|
try {
|
2022-04-27 18:23:05 +02:00
|
|
|
if (isDevelopment)
|
2021-10-31 10:36:45 +01:00
|
|
|
await window.loadURL('http://localhost:9080');
|
|
|
|
else {
|
|
|
|
const indexPath = path.resolve(__dirname, 'index.html');
|
|
|
|
await window.loadFile(indexPath);
|
2021-04-26 10:07:47 +02:00
|
|
|
}
|
2020-04-30 17:48:53 +02:00
|
|
|
}
|
2021-04-26 10:07:47 +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', () => {
|
2021-12-28 17:12:10 +01:00
|
|
|
window.removeListener('moved', saveWindowState);
|
2020-04-30 17:48:53 +02:00
|
|
|
mainWindow = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
return window;
|
2021-10-16 17:05:26 +02:00
|
|
|
}
|
2020-04-30 17:48:53 +02:00
|
|
|
|
2021-10-16 17:05:26 +02:00
|
|
|
if (!gotTheLock) app.quit();
|
2020-09-15 14:44:29 +02:00
|
|
|
else {
|
2021-04-26 10:07:47 +02:00
|
|
|
require('@electron/remote/main').initialize();
|
|
|
|
|
2020-09-15 14:44:29 +02:00
|
|
|
// Initialize ipcHandlers
|
|
|
|
ipcHandlers();
|
2020-07-30 19:12:29 +02:00
|
|
|
|
2022-06-07 18:32:37 +02:00
|
|
|
ipcMain.on('refresh-theme-settings', () => {
|
|
|
|
const appTheme = persistentStore.get('application_theme');
|
2022-06-24 18:17:37 +02:00
|
|
|
if (isWindows && mainWindow) {
|
2022-06-11 14:26:51 +02:00
|
|
|
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) => {
|
2022-06-08 14:55:29 +02:00
|
|
|
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
|
2022-06-07 09:33:03 +02:00
|
|
|
if (!isMacOS) app.quit();
|
2020-09-15 14:44:29 +02:00
|
|
|
});
|
2020-04-30 17:48:53 +02:00
|
|
|
|
2021-04-26 10:07:47 +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
|
2021-10-31 10:36:45 +01:00
|
|
|
if (mainWindow === null)
|
2021-04-26 10:07:47 +02:00
|
|
|
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
|
2020-12-18 18:44:32 +01:00
|
|
|
app.on('ready', async () => {
|
2021-12-28 17:12:10 +01:00
|
|
|
mainWindowState = windowStateKeeper({
|
|
|
|
defaultWidth: 1024,
|
|
|
|
defaultHeight: 800
|
|
|
|
});
|
|
|
|
|
2021-04-26 10:07:47 +02:00
|
|
|
mainWindow = await createMainWindow();
|
2021-10-16 17:05:26 +02:00
|
|
|
createAppMenu();
|
2021-09-15 15:31:57 +02:00
|
|
|
|
2022-06-23 11:57:25 +02:00
|
|
|
if (isWindows)
|
|
|
|
mainWindow.show();
|
|
|
|
|
2022-04-21 14:39:24 +02:00
|
|
|
if (isDevelopment)
|
|
|
|
mainWindow.webContents.openDevTools();
|
2021-09-15 15:31:57 +02:00
|
|
|
|
2021-11-19 13:13:35 +01:00
|
|
|
process.on('uncaughtException', error => {
|
2021-09-15 15:31:57 +02:00
|
|
|
mainWindow.webContents.send('unhandled-exception', error);
|
|
|
|
});
|
|
|
|
|
2021-11-19 13:13:35 +01:00
|
|
|
process.on('unhandledRejection', error => {
|
2021-09-15 15:31:57 +02:00
|
|
|
mainWindow.webContents.send('unhandled-exception', error);
|
|
|
|
});
|
2020-09-15 14:44:29 +02:00
|
|
|
});
|
2022-04-28 10:34:44 +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);
|
|
|
|
}
|
|
|
|
});
|
2022-07-13 18:25:42 +02:00
|
|
|
|
|
|
|
app.on('browser-window-focus', () => {
|
|
|
|
// Send registered shortcut events to window
|
|
|
|
for (const shortcut of shortcuts) {
|
|
|
|
globalShortcut.register(shortcut.keys, () => {
|
|
|
|
mainWindow.webContents.send(shortcut.event);
|
|
|
|
if (isDevelopment) console.log('EVENT:', shortcut);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main process shortcuts
|
|
|
|
if (isDevelopment) {
|
|
|
|
globalShortcut.register('CommandOrControl+F12', () => {
|
|
|
|
mainWindow.webContents.openDevTools();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.on('browser-window-blur', () => {
|
|
|
|
globalShortcut.unregisterAll();
|
|
|
|
});
|
2020-09-15 14:44:29 +02:00
|
|
|
}
|
2021-10-16 17:05:26 +02:00
|
|
|
|
|
|
|
function createAppMenu () {
|
2022-04-12 17:08:05 +02:00
|
|
|
let menu: Electron.Menu = null;
|
2021-10-16 17:05:26 +02:00
|
|
|
|
2021-10-23 18:15:32 +02:00
|
|
|
if (isMacOS) {
|
2021-10-16 17:05:26 +02:00
|
|
|
menu = Menu.buildFromTemplate([
|
|
|
|
{
|
2021-10-24 13:02:37 +02:00
|
|
|
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' }
|
|
|
|
]
|
2021-10-23 17:56:42 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'editMenu'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'viewMenu'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'windowMenu'
|
2021-10-16 17:05:26 +02:00
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Menu.setApplicationMenu(menu);
|
|
|
|
}
|
2021-12-28 17:12:10 +01:00
|
|
|
|
|
|
|
function saveWindowState () {
|
|
|
|
mainWindowState.saveState(mainWindow);
|
|
|
|
}
|