2022-10-08 16:23:30 +02:00
|
|
|
const path = require('path');
|
|
|
|
const isDev = require('electron-is-dev');
|
|
|
|
const { format } = require('url');
|
|
|
|
const { BrowserWindow, app, Menu } = require('electron');
|
|
|
|
const { setContentSecurityPolicy } = require('electron-util');
|
|
|
|
|
2022-10-15 16:44:43 +02:00
|
|
|
const menuTemplate = require('./app/menu-template');
|
2023-09-22 17:08:45 +02:00
|
|
|
const LastOpenedCollections = require('./store/last-opened-collections');
|
2022-10-15 16:44:43 +02:00
|
|
|
const registerNetworkIpc = require('./ipc/network');
|
2023-01-22 14:05:57 +01:00
|
|
|
const registerCollectionsIpc = require('./ipc/collection');
|
2023-10-15 12:29:57 +02:00
|
|
|
const registerPreferencesIpc = require('./ipc/preferences');
|
2022-10-15 16:44:43 +02:00
|
|
|
const Watcher = require('./app/watcher');
|
2023-10-07 14:27:24 +02:00
|
|
|
const { loadWindowState, saveWindowState } = require('./utils/window');
|
2022-10-08 16:23:30 +02:00
|
|
|
|
2022-10-15 22:36:58 +02:00
|
|
|
const lastOpenedCollections = new LastOpenedCollections();
|
|
|
|
|
2023-10-09 15:37:34 +02:00
|
|
|
const contentSecurityPolicy = [
|
|
|
|
isDev ? "default-src 'self' 'unsafe-inline' 'unsafe-eval'" : "default-src 'self'",
|
|
|
|
"connect-src 'self' https://api.github.com/repos/usebruno/bruno",
|
|
|
|
"font-src 'self' https://fonts.gstatic.com",
|
|
|
|
"form-action 'none'",
|
|
|
|
"img-src 'self' blob: data:",
|
|
|
|
"style-src 'self' https://fonts.googleapis.com"
|
|
|
|
];
|
|
|
|
|
|
|
|
setContentSecurityPolicy(contentSecurityPolicy.join(';'));
|
2022-10-08 16:23:30 +02:00
|
|
|
|
|
|
|
const menu = Menu.buildFromTemplate(menuTemplate);
|
|
|
|
Menu.setApplicationMenu(menu);
|
|
|
|
|
|
|
|
let mainWindow;
|
2022-10-15 16:44:43 +02:00
|
|
|
let watcher;
|
2022-10-08 16:23:30 +02:00
|
|
|
|
|
|
|
// Prepare the renderer once the app is ready
|
|
|
|
app.on('ready', async () => {
|
2023-10-07 14:27:24 +02:00
|
|
|
const { x, y, width, height } = loadWindowState();
|
|
|
|
|
2022-10-08 16:23:30 +02:00
|
|
|
mainWindow = new BrowserWindow({
|
2023-10-07 14:27:24 +02:00
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width,
|
|
|
|
height,
|
2023-10-15 12:29:57 +02:00
|
|
|
minWidth: 1000,
|
|
|
|
minHeight: 640,
|
2022-10-08 16:23:30 +02:00
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: true,
|
2023-10-02 14:26:24 +02:00
|
|
|
preload: path.join(__dirname, 'preload.js'),
|
2023-10-04 04:15:53 +02:00
|
|
|
webviewTag: true
|
2023-10-05 16:53:33 +02:00
|
|
|
},
|
2023-10-05 16:59:50 +02:00
|
|
|
title: 'Bruno',
|
2023-10-06 23:56:12 +02:00
|
|
|
icon: path.join(__dirname, 'about/256x256.png')
|
|
|
|
// we will bring this back
|
|
|
|
// see https://github.com/usebruno/bruno/issues/440
|
|
|
|
// autoHideMenuBar: true
|
2022-10-08 16:23:30 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const url = isDev
|
2022-10-13 18:38:36 +02:00
|
|
|
? 'http://localhost:3000'
|
2022-10-08 16:23:30 +02:00
|
|
|
: format({
|
2022-10-17 17:34:05 +02:00
|
|
|
pathname: path.join(__dirname, '../web/index.html'),
|
2022-10-08 16:23:30 +02:00
|
|
|
protocol: 'file:',
|
|
|
|
slashes: true
|
|
|
|
});
|
|
|
|
|
|
|
|
mainWindow.loadURL(url);
|
2022-10-15 16:44:43 +02:00
|
|
|
watcher = new Watcher();
|
2022-10-08 16:23:30 +02:00
|
|
|
|
2023-10-07 14:27:24 +02:00
|
|
|
mainWindow.on('resize', () => saveWindowState(mainWindow));
|
|
|
|
mainWindow.on('move', () => saveWindowState(mainWindow));
|
|
|
|
|
2023-09-21 21:06:42 +02:00
|
|
|
mainWindow.webContents.on('new-window', function (e, url) {
|
2023-02-01 17:22:46 +01:00
|
|
|
e.preventDefault();
|
|
|
|
require('electron').shell.openExternal(url);
|
|
|
|
});
|
|
|
|
|
2022-10-08 16:23:30 +02:00
|
|
|
// register all ipc handlers
|
2023-10-08 23:12:03 +02:00
|
|
|
registerNetworkIpc(mainWindow);
|
2023-01-22 14:05:57 +01:00
|
|
|
registerCollectionsIpc(mainWindow, watcher, lastOpenedCollections);
|
2023-10-15 12:29:57 +02:00
|
|
|
registerPreferencesIpc(mainWindow, watcher, lastOpenedCollections);
|
2022-10-08 16:23:30 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Quit the app once all windows are closed
|
|
|
|
app.on('window-all-closed', app.quit);
|