dbgate/app/src/electron.js

219 lines
5.5 KiB
JavaScript
Raw Normal View History

2019-12-24 16:48:25 +00:00
const electron = require('electron');
2020-05-18 18:23:53 +00:00
const os = require('os');
2020-05-18 17:05:45 +00:00
const { Menu } = require('electron');
2019-12-25 21:30:49 +00:00
const { fork } = require('child_process');
2020-05-18 17:05:45 +00:00
var { autoUpdater } = require('electron-updater');
2020-04-10 20:01:00 +00:00
const Store = require('electron-store');
2019-12-24 16:48:25 +00:00
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
const path = require('path');
const url = require('url');
2020-04-10 20:01:00 +00:00
const store = new Store();
2019-12-24 16:48:25 +00:00
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
2020-04-10 19:53:00 +00:00
let splashWindow;
function hideSplash() {
if (splashWindow) {
splashWindow.destroy();
splashWindow = null;
}
mainWindow.show();
}
2019-12-24 16:48:25 +00:00
2020-05-18 17:05:45 +00:00
function buildMenu() {
const template = [
{
label: 'File',
submenu: [
{
label: 'Connect to database',
click() {
mainWindow.webContents.executeJavaScript(`dbgate_createNewConnection()`);
},
},
{
label: 'New query',
click() {
mainWindow.webContents.executeJavaScript(`dbgate_newQuery()`);
},
},
],
},
{
label: 'Edit',
2020-05-18 18:23:53 +00:00
submenu: [{ role: 'copy' }, { role: 'paste' }],
2020-05-18 17:05:45 +00:00
},
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forcereload' },
{ role: 'toggledevtools' },
{ type: 'separator' },
{ role: 'resetzoom' },
{ role: 'zoomin' },
{ role: 'zoomout' },
{ type: 'separator' },
{ role: 'togglefullscreen' },
],
},
{
role: 'window',
submenu: [
{
label: 'Close all tabs',
click() {
mainWindow.webContents.executeJavaScript('dbgate_closeAll()');
},
},
{ type: 'separator' },
{ role: 'minimize' },
{ role: 'close' },
],
},
{
role: 'help',
submenu: [
{
label: 'dbgate.org',
click() {
require('electron').shell.openExternal('https://dbgate.org');
},
},
{
label: 'DbGate on GitHub',
click() {
require('electron').shell.openExternal('https://github.com/dbshell/dbgate');
},
},
{
label: 'DbGate on docker hub',
click() {
require('electron').shell.openExternal('https://hub.docker.com/r/dbgate/dbgate');
},
},
2020-12-10 12:31:37 +00:00
{
label: 'About',
click() {
mainWindow.webContents.executeJavaScript(`dbgate_showAbout()`);
},
},
2020-05-18 17:05:45 +00:00
],
},
];
return Menu.buildFromTemplate(template);
}
2019-12-24 16:48:25 +00:00
function createWindow() {
2020-04-10 20:01:00 +00:00
const bounds = store.get('winBounds');
2020-04-10 19:21:57 +00:00
mainWindow = new BrowserWindow({
2020-04-10 20:01:00 +00:00
width: 1200,
height: 800,
2020-05-18 17:05:45 +00:00
title: 'DbGate',
2020-05-18 18:43:30 +00:00
icon: os.platform() == 'win32' ? 'icon.ico' : 'icon512.png',
2020-04-10 20:01:00 +00:00
...bounds,
2020-04-10 19:53:00 +00:00
show: false,
2020-04-10 19:21:57 +00:00
webPreferences: {
nodeIntegration: true,
},
});
2019-12-24 16:48:25 +00:00
2020-05-18 17:05:45 +00:00
mainWindow.setMenu(buildMenu());
2020-04-10 19:21:57 +00:00
function loadMainWindow() {
const startUrl =
process.env.ELECTRON_START_URL ||
url.format({
pathname: path.join(__dirname, '../packages/web/build/index.html'),
protocol: 'file:',
slashes: true,
});
2020-04-10 19:53:00 +00:00
mainWindow.webContents.on('did-finish-load', function () {
hideSplash();
});
2020-04-10 20:01:00 +00:00
mainWindow.on('close', () => {
store.set('winBounds', mainWindow.getBounds());
});
2020-04-10 19:21:57 +00:00
mainWindow.loadURL(startUrl);
}
2019-12-25 21:30:49 +00:00
2020-04-10 19:53:00 +00:00
splashWindow = new BrowserWindow({
width: 300,
height: 120,
transparent: true,
frame: false,
});
splashWindow.loadURL(
url.format({
pathname: path.join(__dirname, '../packages/web/build/splash.html'),
protocol: 'file:',
slashes: true,
})
);
2020-04-10 19:21:57 +00:00
if (process.env.ELECTRON_START_URL) {
loadMainWindow();
} else {
const apiProcess = fork(path.join(__dirname, '../packages/api/dist/bundle.js'), ['--dynport']);
apiProcess.on('message', (msg) => {
if (msg.msgtype == 'listening') {
const { port } = msg;
global['port'] = port;
loadMainWindow();
}
2019-12-24 16:48:25 +00:00
});
2020-04-10 19:21:57 +00:00
}
2019-12-24 16:48:25 +00:00
2020-03-14 13:43:48 +00:00
// and load the index.html of the app.
// mainWindow.loadURL('http://localhost:3000');
2019-12-24 16:48:25 +00:00
2020-03-14 13:43:48 +00:00
// Open the DevTools.
// mainWindow.webContents.openDevTools();
2019-12-24 16:48:25 +00:00
2020-03-14 13:43:48 +00:00
// Emitted when the window is closed.
2020-04-10 19:21:57 +00:00
mainWindow.on('closed', function () {
2020-03-14 13:43:48 +00:00
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
2019-12-24 16:48:25 +00:00
}
2020-05-18 17:05:45 +00:00
function onAppReady() {
autoUpdater.checkForUpdatesAndNotify();
createWindow();
}
2019-12-24 16:48:25 +00:00
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
2020-05-18 17:05:45 +00:00
app.on('ready', onAppReady);
2019-12-24 16:48:25 +00:00
// Quit when all windows are closed.
2020-04-10 19:21:57 +00:00
app.on('window-all-closed', function () {
2020-03-14 13:43:48 +00:00
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
2019-12-24 16:48:25 +00:00
});
2020-04-10 19:21:57 +00:00
app.on('activate', function () {
2020-03-14 13:43:48 +00:00
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
2019-12-24 16:48:25 +00:00
});
// In this file you can include the rest of your app's specific main process
2020-03-14 13:43:48 +00:00
// code. You can also put them in separate files and require them here.