feature: add electron starter

This commit is contained in:
George Lima 2018-11-26 17:08:52 -03:00
parent 09db7ea6a2
commit c84fe27386
4 changed files with 115 additions and 0 deletions

104
config/electron.js Normal file
View File

@ -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()
})

2
index.js Normal file
View File

@ -0,0 +1,2 @@
require('@babel/register')
require('./config/electron')

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

9
utils/debugShortcut.js Normal file
View File

@ -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()
})