tabby/app/main.js

212 lines
5.6 KiB
JavaScript
Raw Normal View History

2017-04-30 22:01:12 +00:00
if (process.platform == 'win32' && require('electron-squirrel-startup')) process.exit(0)
2017-03-20 16:46:25 +00:00
const electron = require('electron')
2017-04-16 15:13:03 +00:00
require('electron-debug')({enabled: true, showDevTools: (process.argv.indexOf('--debug') != -1) ? 'undocked' : false})
2017-03-20 16:46:25 +00:00
let app = electron.app
let secondInstance = app.makeSingleInstance((argv) => {
app.window.webContents.send('host:second-instance')
})
if (secondInstance) {
app.quit()
return
}
2017-03-18 10:48:07 +00:00
const yaml = require('js-yaml')
const path = require('path')
const fs = require('fs')
2016-12-23 09:06:53 +00:00
const Config = require('electron-config')
require('electron-debug')({enabled: true, showDevTools: process.argv.indexOf('--debug') != -1})
let windowConfig = new Config({name: 'window'})
2017-04-23 22:34:07 +00:00
if (!process.env.TERMINUS_PLUGINS) {
process.env.TERMINUS_PLUGINS = ''
}
2016-12-23 09:06:53 +00:00
setupWindowManagement = () => {
let windowCloseable
2017-03-18 20:47:52 +00:00
app.window.on('show', () => {
2017-03-20 16:46:25 +00:00
app.window.webContents.send('host:window-shown')
2017-03-18 20:47:52 +00:00
})
2016-12-23 09:06:53 +00:00
app.window.on('close', (e) => {
windowConfig.set('windowBoundaries', app.window.getBounds())
if (!windowCloseable) {
2016-12-26 22:21:50 +00:00
app.window.minimize()
2016-12-23 09:06:53 +00:00
e.preventDefault()
}
})
app.window.on('closed', () => {
app.window = null
})
electron.ipcMain.on('window-closeable', (event, flag) => {
windowCloseable = flag
})
electron.ipcMain.on('window-focus', () => {
app.window.focus()
})
2016-12-27 21:21:02 +00:00
electron.ipcMain.on('window-toggle-focus', () => {
if (app.window.isFocused()) {
app.window.minimize()
} else {
app.window.focus()
}
})
2016-12-26 22:21:50 +00:00
electron.ipcMain.on('window-maximize', () => {
2017-03-20 16:46:25 +00:00
app.window.maximize()
})
electron.ipcMain.on('window-unmaximize', () => {
app.window.unmaximize()
})
electron.ipcMain.on('window-toggle-maximize', () => {
2016-12-26 22:21:50 +00:00
if (app.window.isMaximized()) {
app.window.unmaximize()
} else {
app.window.maximize()
}
})
electron.ipcMain.on('window-minimize', () => {
app.window.minimize()
})
2017-03-18 20:47:52 +00:00
electron.ipcMain.on('window-set-bounds', (event, bounds) => {
2017-04-28 14:55:51 +00:00
let actualBounds = app.window.getBounds()
actualBounds.x = bounds.x
actualBounds.y = bounds.y
app.window.setBounds(actualBounds)
2017-04-23 20:37:19 +00:00
setTimeout(() => {
2017-04-28 14:55:51 +00:00
actualBounds = app.window.getBounds()
2017-04-23 20:37:19 +00:00
bounds.width += bounds.x - actualBounds.x
bounds.height += bounds.y - actualBounds.y
2017-04-28 14:55:51 +00:00
bounds.x = actualBounds.x
bounds.y = actualBounds.y
2017-04-23 20:37:19 +00:00
app.window.setBounds(bounds)
2017-04-28 14:55:51 +00:00
}, 100)
2017-03-18 20:47:52 +00:00
})
2017-03-20 16:46:25 +00:00
electron.ipcMain.on('window-set-always-on-top', (event, flag) => {
app.window.setAlwaysOnTop(flag)
})
2016-12-23 09:06:53 +00:00
app.on('before-quit', () => windowCloseable = true)
}
setupMenu = () => {
var template = [{
label: "Application",
submenu: [
{ type: "separator" },
{ label: "Quit", accelerator: "CmdOrCtrl+Q", click: () => {
app.window.webContents.send('host:quit-request')
}}
2017-03-18 20:47:52 +00:00
]
},
{
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:" }
]
}]
2016-12-23 09:06:53 +00:00
electron.Menu.setApplicationMenu(electron.Menu.buildFromTemplate(template))
}
start = () => {
let t0 = Date.now()
2017-03-18 10:48:07 +00:00
let configPath = path.join(electron.app.getPath('userData'), 'config.yaml')
let configData
if (fs.existsSync(configPath)) {
configData = yaml.safeLoad(fs.readFileSync(configPath, 'utf8'))
} else {
configData = {}
}
2016-12-23 09:06:53 +00:00
let options = {
width: 800,
height: 400,
2016-12-25 23:04:56 +00:00
//icon: `${app.getAppPath()}/assets/img/icon.png`,
2017-04-15 13:20:18 +00:00
title: 'Terminus',
2017-04-28 14:55:51 +00:00
minWidth: 400,
minHeight: 300,
2016-12-23 09:06:53 +00:00
'web-preferences': {'web-security': false},
//- background to avoid the flash of unstyled window
2017-04-23 22:34:07 +00:00
backgroundColor: '#131d27',
2016-12-26 22:21:50 +00:00
frame: false,
2017-03-21 23:18:52 +00:00
//type: 'toolbar',
2016-12-23 09:06:53 +00:00
}
Object.assign(options, windowConfig.get('windowBoundaries'))
2017-04-28 14:55:51 +00:00
if ((configData.appearance || {}).frame == 'native') {
2017-03-18 10:48:07 +00:00
options.frame = true
2017-04-17 20:43:26 +00:00
} else {
2017-04-30 22:01:12 +00:00
if (process.platform == 'darwin') {
2017-04-17 20:43:26 +00:00
options.titleBarStyle = 'hidden-inset'
}
2017-03-18 10:48:07 +00:00
}
2016-12-26 22:21:50 +00:00
app.commandLine.appendSwitch('disable-http-cache')
2016-12-23 09:06:53 +00:00
app.window = new electron.BrowserWindow(options)
2017-04-23 22:34:07 +00:00
app.window.loadURL(`file://${app.getAppPath()}/dist/index.html`, {extraHeaders: "pragma: no-cache\n"})
2016-12-23 09:06:53 +00:00
2017-04-30 22:01:12 +00:00
if (process.platform != 'darwin') {
2016-12-23 09:06:53 +00:00
app.window.setMenu(null)
}
app.window.show()
app.window.focus()
setupWindowManagement()
2017-03-18 20:47:52 +00:00
2017-04-30 22:01:12 +00:00
if (process.platform == 'darwin') {
2017-03-18 20:47:52 +00:00
setupMenu()
} else {
app.window.setMenu(null)
}
2016-12-23 09:06:53 +00:00
console.info(`Host startup: ${Date.now() - t0}ms`)
t0 = Date.now()
electron.ipcMain.on('app:ready', () => {
console.info(`App startup: ${Date.now() - t0}ms`)
})
}
app.on('ready', start)
app.on('activate', () => {
if (!app.window)
start()
else {
app.window.show()
app.window.focus()
}
})
process.on('uncaughtException', function(err) {
console.log(err)
app.window.webContents.send('uncaughtException', err)
})