diff --git a/config/electron.js b/config/electron.js new file mode 100644 index 0000000..0b5bfec --- /dev/null +++ b/config/electron.js @@ -0,0 +1,104 @@ +// @flow +import path from 'path' + +import { app, BrowserWindow, powerMonitor, Tray } from 'electron' +import { autoUpdater } from 'electron-updater' +import log from 'electron-log' +import Positioner from 'electron-positioner' +import isDev from 'electron-is-dev' + +import { registerDebugShortcut } from '../utils/debugShortcut' + +import type { BrowserWindow as BrowserWindowType, Tray as TrayType } from 'electron' + +let mainWindow: BrowserWindowType +let tray: TrayType +let updateAvailable = false + +const showStatus = text => { + if (text === 'Update downloaded') updateAvailable = true + + mainWindow.webContents.send('update', { + updateAvailable, + updateInfo: text, + }) +} + +function createWindow() { + 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(` + Download speed: ${progress.bytesPerSecond} - Downloaded ${progress.percent}% (${progress.transferred}/${ + progress.total + }) + `), + ) + autoUpdater.on('update-downloaded', () => { + updateAvailable = true + showStatus('Update downloaded') + }) + + mainWindow = new BrowserWindow({ + width: 800, + height: 600, + transparent: true, + frame: false, + resizable: true, + webPreferences: { + devTools: true, + webSecurity: false, + }, + }) + + mainWindow.setVisibleOnAllWorkspaces(true) + + // TODO: Update to right icon location + tray = new Tray(path.join(__dirname, '../public/images', 'zcash-icon.png')) + + registerDebugShortcut(app, mainWindow) + + tray.setToolTip('ZCash') + mainWindow.loadURL(isDev ? 'http://0.0.0.0:8080/' : `file://${path.join(__dirname, '../dist/index.html')}`) + + const positioner = new Positioner(mainWindow) + let bounds = tray.getBounds() + positioner.move('trayCenter', bounds) + + powerMonitor.on('suspend', () => mainWindow.webContents.send('suspend', 'suspended')) + powerMonitor.on('resume', () => mainWindow.webContents.send('resume', 'resumed')) + + mainWindow.once('ready-to-show', () => mainWindow.show()) + mainWindow.on('blur', () => mainWindow.hide()) + mainWindow.on('show', () => tray.setHighlightMode('always')) + mainWindow.on('hide', () => tray.setHighlightMode('never')) + mainWindow.on('closed', () => { + mainWindow = null + }) + + tray.on('click', () => { + bounds = tray.getBounds() + positioner.move('trayCenter', bounds) + + if (mainWindow.isVisible()) { + mainWindow.hide() + } else { + mainWindow.show() + } + }) + + exports.app = app + exports.tray = tray +} + +app.on('ready', createWindow) +app.on('activate', () => { + if (mainWindow === null) createWindow() +}) +app.on('window-all-closed', () => { + if (process.platform !== 'darwin') app.quit() +}) diff --git a/index.js b/index.js new file mode 100644 index 0000000..796a750 --- /dev/null +++ b/index.js @@ -0,0 +1,2 @@ +require('@babel/register') +require('./config/electron') diff --git a/public/images/zcash-icon.png b/public/images/zcash-icon.png new file mode 100644 index 0000000..060bd00 Binary files /dev/null and b/public/images/zcash-icon.png differ diff --git a/utils/debugShortcut.js b/utils/debugShortcut.js new file mode 100644 index 0000000..a84cade --- /dev/null +++ b/utils/debugShortcut.js @@ -0,0 +1,9 @@ +// @flow + +import { globalShortcut } from 'electron' + +export const registerDebugShortcut = (app: Object, mainWindow: Object) => + globalShortcut.register('CommandOrControl+Option+B', () => { + app.dock.show() + mainWindow.webContents.openDevTools() + })