mirror of
https://github.com/pavlobu/deskreen.git
synced 2025-05-17 07:50:17 -07:00
168 lines
4.5 KiB
TypeScript
168 lines
4.5 KiB
TypeScript
/* eslint global-require: off, no-console: off */
|
|
|
|
/**
|
|
* This module executes inside of electron's main process. You can start
|
|
* electron renderer process from here and communicate with the other processes
|
|
* through IPC.
|
|
*
|
|
* When running `yarn build` or `yarn build-main`, this file is compiled to
|
|
* `./app/main.prod.js` using webpack. This gives us some performance wins.
|
|
*/
|
|
import 'core-js/stable';
|
|
import 'regenerator-runtime/runtime';
|
|
import path from 'path';
|
|
import { app, BrowserWindow, ipcMain } from 'electron';
|
|
import { autoUpdater } from 'electron-updater';
|
|
import log from 'electron-log';
|
|
import config from './configs/app.config';
|
|
import i18n from './configs/i18next.config';
|
|
import signalingServer from './server/signalingServer';
|
|
import MenuBuilder from './menu';
|
|
|
|
const globalAny: any = global;
|
|
globalAny.appPath = __dirname;
|
|
|
|
signalingServer.start();
|
|
|
|
export default class AppUpdater {
|
|
constructor() {
|
|
log.transports.file.level = 'info';
|
|
autoUpdater.logger = log;
|
|
autoUpdater.checkForUpdatesAndNotify();
|
|
}
|
|
}
|
|
|
|
let mainWindow: BrowserWindow | null = null;
|
|
let menuBuilder: MenuBuilder | null = null;
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
const sourceMapSupport = require('source-map-support');
|
|
sourceMapSupport.install();
|
|
}
|
|
|
|
if (
|
|
process.env.NODE_ENV === 'development' ||
|
|
process.env.DEBUG_PROD === 'true'
|
|
) {
|
|
require('electron-debug')();
|
|
}
|
|
|
|
const installExtensions = async () => {
|
|
const installer = require('electron-devtools-installer');
|
|
const forceDownload = !!process.env.UPGRADE_EXTENSIONS;
|
|
const extensions = ['REACT_DEVELOPER_TOOLS', 'REDUX_DEVTOOLS'];
|
|
|
|
return Promise.all(
|
|
extensions.map((name) => installer.default(installer[name], forceDownload))
|
|
).catch(console.log);
|
|
};
|
|
|
|
const createWindow = async () => {
|
|
if (
|
|
process.env.NODE_ENV === 'development' ||
|
|
process.env.DEBUG_PROD === 'true'
|
|
) {
|
|
await installExtensions();
|
|
}
|
|
|
|
mainWindow = new BrowserWindow({
|
|
show: false,
|
|
width: 1024,
|
|
height: 728,
|
|
webPreferences:
|
|
(process.env.NODE_ENV === 'development' ||
|
|
process.env.E2E_BUILD === 'true') &&
|
|
process.env.ERB_SECURE !== 'true'
|
|
? {
|
|
nodeIntegration: true,
|
|
}
|
|
: {
|
|
preload: path.join(__dirname, 'dist/renderer.prod.js'),
|
|
},
|
|
});
|
|
|
|
mainWindow.loadURL(`file://${__dirname}/app.html`);
|
|
|
|
// @TODO: Use 'ready-to-show' event
|
|
// https://github.com/electron/electron/blob/master/docs/api/browser-window.md#using-ready-to-show-event
|
|
mainWindow.webContents.on('did-finish-load', () => {
|
|
if (!mainWindow) {
|
|
throw new Error('"mainWindow" is not defined');
|
|
}
|
|
if (process.env.START_MINIMIZED) {
|
|
mainWindow.minimize();
|
|
} else {
|
|
mainWindow.show();
|
|
mainWindow.focus();
|
|
}
|
|
});
|
|
|
|
mainWindow.on('closed', () => {
|
|
mainWindow = null;
|
|
});
|
|
|
|
menuBuilder = new MenuBuilder(mainWindow, i18n);
|
|
menuBuilder.buildMenu();
|
|
|
|
// i18n.on('loaded', (loaded) => {
|
|
i18n.on('loaded', () => {
|
|
i18n.changeLanguage('en');
|
|
i18n.off('loaded');
|
|
});
|
|
|
|
i18n.on('languageChanged', (lng) => {
|
|
if (mainWindow === null) return;
|
|
menuBuilder = new MenuBuilder(mainWindow, i18n);
|
|
menuBuilder.buildMenu();
|
|
mainWindow.webContents.send('sending-language-from-main', lng);
|
|
console.log(`Language changed! ${lng}`);
|
|
});
|
|
|
|
// Remove this if your app does not use auto updates
|
|
// eslint-disable-next-line
|
|
new AppUpdater();
|
|
};
|
|
|
|
/**
|
|
* Add event listeners...
|
|
*/
|
|
|
|
app.on('window-all-closed', () => {
|
|
// Respect the OSX convention of having the application in memory even
|
|
// after all windows have been closed
|
|
if (process.platform !== 'darwin') {
|
|
app.quit();
|
|
}
|
|
});
|
|
|
|
if (process.env.E2E_BUILD === 'true') {
|
|
// eslint-disable-next-line promise/catch-or-return
|
|
app.whenReady().then(createWindow);
|
|
} else {
|
|
app.on('ready', createWindow);
|
|
}
|
|
|
|
app.on('activate', () => {
|
|
// On macOS 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();
|
|
});
|
|
|
|
ipcMain.handle('get-signaling-server-port', () => {
|
|
if (mainWindow === null) return;
|
|
mainWindow.webContents.send('sending-port-from-main', signalingServer.port);
|
|
});
|
|
|
|
// ipcMain.on('get-initial-translations', (event, arg) => {
|
|
ipcMain.on('get-initial-translations', (event, _) => {
|
|
// i18n.loadLanguages('en', (err, t) => {
|
|
i18n.loadLanguages('en', () => {
|
|
const initial = {
|
|
en: {
|
|
translation: i18n.getResourceBundle('en', config.namespace),
|
|
},
|
|
};
|
|
event.returnValue = initial;
|
|
});
|
|
});
|