zepio/config/electron.js

108 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-11-26 12:08:52 -08:00
// @flow
2018-11-26 12:19:12 -08:00
import path from 'path';
2018-11-26 12:08:52 -08:00
2018-11-26 12:19:12 -08:00
/* eslint-disable import/no-extraneous-dependencies */
import {
app, BrowserWindow, powerMonitor, Tray,
} from 'electron';
import { autoUpdater } from 'electron-updater';
import Positioner from 'electron-positioner';
import isDev from 'electron-is-dev';
/* eslint-enable import/no-extraneous-dependencies */
2018-11-26 12:08:52 -08:00
2018-11-26 12:19:12 -08:00
import type { BrowserWindow as BrowserWindowType, Tray as TrayType } from 'electron';
2018-11-26 12:08:52 -08:00
2018-11-26 12:19:12 -08:00
import { registerDebugShortcut } from '../utils/debugShortcut';
2018-11-26 12:08:52 -08:00
2018-11-26 12:19:12 -08:00
let mainWindow: BrowserWindowType;
let tray: TrayType;
let updateAvailable = false;
2018-11-26 12:08:52 -08:00
2018-11-26 12:19:12 -08:00
const showStatus = (text) => {
if (text === 'Update downloaded') updateAvailable = true;
2018-11-26 12:08:52 -08:00
mainWindow.webContents.send('update', {
updateAvailable,
updateInfo: text,
2018-11-26 12:19:12 -08:00
});
};
2018-11-26 12:08:52 -08:00
const createWindow = () => {
2018-11-26 12:19:12 -08:00
autoUpdater.checkForUpdatesAndNotify();
autoUpdater.on('checking-for-update', () => showStatus('Checking for update'));
autoUpdater.on('update-available', () => showStatus('Update available'));
autoUpdater.on('update-not-available', () => showStatus('No updates available'));
autoUpdater.on('error', err => showStatus(`Error while updating: ${err}`));
autoUpdater.on('download-progress', progress => showStatus(
/* eslint-disable-next-line max-len */
`Download speed: ${progress.bytesPerSecond} - Downloaded ${progress.percent}% (${progress.transferred}/${
2018-11-26 12:08:52 -08:00
progress.total
2018-11-26 12:19:12 -08:00
})`,
));
2018-11-26 12:08:52 -08:00
autoUpdater.on('update-downloaded', () => {
2018-11-26 12:19:12 -08:00
updateAvailable = true;
showStatus('Update downloaded');
});
2018-11-26 12:08:52 -08:00
mainWindow = new BrowserWindow({
width: 800,
height: 600,
transparent: true,
frame: false,
resizable: true,
webPreferences: {
devTools: true,
webSecurity: false,
},
2018-11-26 12:19:12 -08:00
});
2018-11-26 12:08:52 -08:00
2018-11-26 12:19:12 -08:00
mainWindow.setVisibleOnAllWorkspaces(true);
2018-11-26 12:08:52 -08:00
// TODO: Update to right icon location
2018-11-26 12:19:12 -08:00
tray = new Tray(path.join(__dirname, '../public/images', 'zcash-icon.png'));
2018-11-26 12:08:52 -08:00
2018-11-26 12:19:12 -08:00
registerDebugShortcut(app, mainWindow);
2018-11-26 12:08:52 -08:00
2018-11-26 12:19:12 -08:00
tray.setToolTip('ZCash');
2018-11-26 13:16:12 -08:00
mainWindow.loadURL(isDev ? 'http://0.0.0.0:8080/' : `file://${path.join(__dirname, '../build/index.html')}`);
2018-11-26 12:08:52 -08:00
2018-11-26 12:19:12 -08:00
const positioner = new Positioner(mainWindow);
let bounds = tray.getBounds();
positioner.move('trayCenter', bounds);
2018-11-26 12:08:52 -08:00
2018-11-26 12:19:12 -08:00
powerMonitor.on('suspend', () => mainWindow.webContents.send('suspend', 'suspended'));
powerMonitor.on('resume', () => mainWindow.webContents.send('resume', 'resumed'));
2018-11-26 12:08:52 -08:00
2018-11-26 12:19:12 -08:00
mainWindow.once('ready-to-show', () => mainWindow.show());
mainWindow.on('blur', () => mainWindow.hide());
mainWindow.on('show', () => tray.setHighlightMode('always'));
mainWindow.on('hide', () => tray.setHighlightMode('never'));
2018-11-26 12:08:52 -08:00
mainWindow.on('closed', () => {
2018-11-26 12:19:12 -08:00
mainWindow = null;
});
2018-11-26 12:08:52 -08:00
tray.on('click', () => {
2018-11-26 12:19:12 -08:00
bounds = tray.getBounds();
positioner.move('trayCenter', bounds);
2018-11-26 12:08:52 -08:00
if (mainWindow.isVisible()) {
2018-11-26 12:19:12 -08:00
mainWindow.hide();
2018-11-26 12:08:52 -08:00
} else {
2018-11-26 12:19:12 -08:00
mainWindow.show();
2018-11-26 12:08:52 -08:00
}
2018-11-26 12:19:12 -08:00
});
2018-11-26 12:08:52 -08:00
2018-11-26 12:19:12 -08:00
exports.app = app;
exports.tray = tray;
};
2018-11-26 12:08:52 -08:00
2018-11-26 12:19:12 -08:00
app.on('ready', createWindow);
2018-11-26 12:08:52 -08:00
app.on('activate', () => {
2018-11-26 12:19:12 -08:00
if (mainWindow === null) createWindow();
});
2018-11-26 12:08:52 -08:00
app.on('window-all-closed', () => {
2018-11-26 12:19:12 -08:00
if (process.platform !== 'darwin') app.quit();
});