jitsi-meet-electron/main.js

206 lines
5.2 KiB
JavaScript
Raw Normal View History

/* global __dirname, process */
2018-05-24 17:12:27 +02:00
2018-06-20 21:38:53 +02:00
const {
BrowserWindow,
Menu,
2018-08-13 09:04:19 +02:00
app,
2018-06-20 21:38:53 +02:00
shell
} = require('electron');
const isDev = require('electron-is-dev');
2018-07-15 21:27:03 +02:00
const { autoUpdater } = require('electron-updater');
2018-06-20 21:38:53 +02:00
const windowStateKeeper = require('electron-window-state');
2017-08-04 15:15:22 +02:00
const {
2018-06-13 00:39:56 +02:00
initPopupsConfigurationMain,
2018-08-13 09:04:19 +02:00
getPopupTarget,
setupAlwaysOnTopMain
} = require('jitsi-meet-electron-utils');
const path = require('path');
2018-05-09 19:57:26 +02:00
const URL = require('url');
2016-12-12 21:46:47 +01:00
2018-07-15 21:27:03 +02:00
autoUpdater.logger = require('electron-log');
autoUpdater.logger.transports.file.level = 'info';
/**
* Load debug utilities (don't open the DevTools window by default though).
*/
require('electron-debug')({ showDevTools: false });
2016-12-12 21:46:47 +01:00
/**
* The window object that will load the iframe with Jitsi Meet.
* IMPORTANT: Must be defined as global in order to not be garbage collected
* acidentally.
*/
2018-08-13 09:04:19 +02:00
let mainWindow = null;
2018-07-12 00:23:40 +02:00
2016-12-12 21:46:47 +01:00
/**
2018-08-13 09:04:19 +02:00
* Sets the application menu. It is hidden on all platforms except macOS because
* otherwise copy and paste functionality is not available.
2016-12-12 21:46:47 +01:00
*/
2018-08-13 09:04:19 +02:00
function setApplicationMenu() {
if (process.platform === 'darwin') {
const template = [ {
label: app.getName(),
submenu: [ {
label: 'Quit',
accelerator: 'Command+Q',
click() {
app.quit();
}
} ]
}, {
label: 'Edit',
submenu: [ {
label: 'Undo',
accelerator: 'CmdOrCtrl+Z',
selector: 'undo:'
},
{
label: 'Redo',
accelerator: 'Shift+CmdOrCtrl+Z',
selector: 'redo:'
},
{
type: 'separator'
},
{
label: 'Cut',
accelerator: 'CmdOrCtrl+X',
selector: 'cut:'
},
{
label: 'Copy',
accelerator: 'CmdOrCtrl+C',
selector: 'copy:'
},
{
label: 'Paste',
accelerator: 'CmdOrCtrl+V',
selector: 'paste:'
},
{
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
selector: 'selectAll:'
}
2018-08-13 09:04:19 +02:00
]
} ];
2016-12-12 21:46:47 +01:00
2018-08-13 09:04:19 +02:00
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
} else {
Menu.setApplicationMenu(null);
}
2018-08-13 09:04:19 +02:00
}
2016-12-12 21:46:47 +01:00
/**
* Opens new window with index.html(Jitsi Meet is loaded in iframe there).
*/
function createJitsiMeetWindow() {
2018-08-13 09:04:19 +02:00
// Application menu.
setApplicationMenu();
2018-06-16 08:36:52 +02:00
2018-07-15 21:27:03 +02:00
// Check for Updates.
autoUpdater.checkForUpdatesAndNotify();
2018-08-13 09:04:19 +02:00
// Load the previous window state with fallback to defaults.
const windowState = windowStateKeeper({
2018-06-20 21:38:53 +02:00
defaultWidth: 800,
defaultHeight: 600
});
2018-08-13 09:04:19 +02:00
// Path to root directory.
const basePath = isDev ? __dirname : app.getAppPath();
// URL for index.html which will be our entry point.
const indexURL = URL.format({
pathname: path.resolve(basePath, './build/index.html'),
protocol: 'file:',
slashes: true
});
2018-06-20 21:38:53 +02:00
// Options used when creating the main Jitsi Meet window.
2018-08-13 09:04:19 +02:00
const options = {
x: windowState.x,
y: windowState.y,
width: windowState.width,
height: windowState.height,
icon: path.resolve(basePath, './resources/icons/icon_512x512.png'),
2018-06-20 21:38:53 +02:00
minWidth: 800,
minHeight: 600,
2018-07-04 05:03:40 +02:00
show: false,
2018-06-20 21:38:53 +02:00
titleBarStyle: 'hidden',
webPreferences: {
nativeWindowOpen: true
}
};
2018-08-13 09:04:19 +02:00
mainWindow = new BrowserWindow(options);
windowState.manage(mainWindow);
mainWindow.loadURL(indexURL);
2016-12-12 21:46:47 +01:00
2018-08-13 09:04:19 +02:00
initPopupsConfigurationMain(mainWindow);
setupAlwaysOnTopMain(mainWindow);
mainWindow.webContents.on('new-window', (event, url, frameName) => {
2018-06-13 00:39:56 +02:00
const target = getPopupTarget(url, frameName);
if (!target || target === 'browser') {
2017-08-04 15:15:22 +02:00
event.preventDefault();
2018-06-20 21:38:53 +02:00
shell.openExternal(url);
2017-08-04 15:15:22 +02:00
}
});
2018-08-13 09:04:19 +02:00
mainWindow.on('closed', () => {
mainWindow = null;
2016-12-12 21:46:47 +01:00
});
2018-08-13 09:04:19 +02:00
mainWindow.once('ready-to-show', () => {
mainWindow.show();
2018-07-04 05:03:40 +02:00
});
2016-12-12 21:46:47 +01:00
}
2018-08-13 09:04:19 +02:00
/**
* Force Single Instance Application.
*/
const isSecondInstance = app.makeSingleInstance(() => {
/**
* If someone creates second instance of the application, set focus on
* existing window.
*/
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
}
});
if (isSecondInstance) {
app.quit();
process.exit(0);
}
/**
* Run the application.
*/
app.on('activate', () => {
if (mainWindow === null) {
createJitsiMeetWindow();
}
});
app.on('certificate-error',
// eslint-disable-next-line max-params
(event, webContents, url, error, certificate, callback) => {
if (url.startsWith('https://localhost')) {
event.preventDefault();
callback(true);
} else {
callback(false);
}
}
);
app.on('ready', createJitsiMeetWindow);
app.on('window-all-closed', () => {
// Don't quit the application for macOS.
if (process.platform !== 'darwin') {
app.quit();
}
});