jitsi-meet-electron/main.js

227 lines
5.9 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,
setupPowerMonitorMain,
setupScreenSharingMain
} = require('jitsi-meet-electron-utils');
const path = require('path');
2018-05-09 19:57:26 +02:00
const URL = require('url');
2020-02-20 15:30:09 +01:00
const config = require('./app/features/config');
2016-12-12 21:46:47 +01:00
2019-10-02 09:20:43 +02:00
// We need this because of https://github.com/electron/electron/issues/18214
app.commandLine.appendSwitch('disable-site-isolation-trials');
2018-07-15 21:27:03 +02:00
autoUpdater.logger = require('electron-log');
autoUpdater.logger.transports.file.level = 'info';
/**
* When in development mode:
* - Load debug utilities (don't open the DevTools window by default though)
* - Enable automatic reloads
*/
if (isDev) {
require('electron-debug')({ showDevTools: false });
require('electron-reload')(path.join(__dirname, 'build'));
}
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.name,
2018-08-13 09:04:19 +02:00
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.
2020-04-07 11:55:54 +02:00
// Use a preload script in order to provide node specific functionality
// to a isolated BrowserWindow in accordance with electron security
// guideline.
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
webPreferences: {
2019-10-02 09:20:43 +02:00
nativeWindowOpen: true,
2020-04-07 11:55:54 +02:00
nodeIntegration: false,
preload: path.resolve(basePath, './build/preload.js')
2018-06-20 21:38:53 +02:00
}
};
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);
setupPowerMonitorMain(mainWindow);
2020-02-20 15:30:09 +01:00
setupScreenSharingMain(mainWindow, config.default.appName);
2018-08-13 09:04:19 +02:00
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 gotInstanceLock = app.requestSingleInstanceLock();
2018-08-13 09:04:19 +02:00
if (!gotInstanceLock) {
2018-08-13 09:04:19 +02:00
app.quit();
process.exit(0);
}
/**
* Run the application.
*/
2018-08-13 09:04:19 +02:00
app.on('activate', () => {
if (mainWindow === null) {
createJitsiMeetWindow();
}
});
2018-08-13 09:04:19 +02:00
app.on('certificate-error',
// eslint-disable-next-line max-params
(event, webContents, url, error, certificate, callback) => {
2020-04-07 11:55:54 +02:00
if (isDev) {
2018-08-13 09:04:19 +02:00
event.preventDefault();
callback(true);
} else {
callback(false);
}
}
);
2018-08-13 09:04:19 +02:00
app.on('ready', createJitsiMeetWindow);
app.on('second-instance', () => {
/**
* If someone creates second instance of the application, set focus on
* existing window.
*/
if (mainWindow) {
mainWindow.isMinimized() && mainWindow.restore();
mainWindow.focus();
}
});
2018-08-13 09:04:19 +02:00
app.on('window-all-closed', () => {
// Don't quit the application on macOS.
2018-08-13 09:04:19 +02:00
if (process.platform !== 'darwin') {
app.quit();
}
});