tabby/app/main.js

256 lines
6.3 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-06-29 12:26:39 +00:00
if (process.argv.indexOf('--debug') !== -1) {
require('electron-debug')({enabled: true, showDevTools: 'undocked'})
}
2017-04-16 15:13:03 +00:00
2017-03-20 16:46:25 +00:00
let app = electron.app
2017-07-05 14:22:44 +00:00
let secondInstance = app.makeSingleInstance((argv, cwd) => {
app.window.webContents.send('host:second-instance', argv, cwd)
2017-03-20 16:46:25 +00:00
})
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')
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 = () => {
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())
})
app.window.on('closed', () => {
app.window = null
})
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()
2017-07-25 19:49:20 +00:00
actualBounds.width -= bounds.x - actualBounds.x
actualBounds.height -= bounds.y - actualBounds.y
2017-04-28 14:55:51 +00:00
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
}
setupMenu = () => {
2017-07-05 17:05:45 +00:00
let template = [{
2016-12-23 09:06:53 +00:00
label: "Application",
submenu: [
2017-07-05 17:05:45 +00:00
{ role: 'about', label: 'About Terminus' },
{ type: 'separator' },
{
label: 'Preferences',
accelerator: 'Cmd+,',
click () {
app.window.webContents.send('host:preferences-menu')
}
},
{ type: 'separator' },
{ role: 'services', submenu: [] },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{
label: 'Quit',
accelerator: 'Cmd+Q',
click () {
2017-07-12 18:24:35 +00:00
app.quit()
2017-07-05 17:05:45 +00:00
}
}
2017-03-18 20:47:52 +00:00
]
2017-07-05 17:05:45 +00:00
},
{
2017-03-18 20:47:52 +00:00
label: "Edit",
submenu: [
2017-07-05 17:05:45 +00:00
{role: 'undo'},
{role: 'redo'},
{type: 'separator'},
{role: 'cut'},
{role: 'copy'},
{role: 'paste'},
{role: 'pasteandmatchstyle'},
{role: 'delete'},
{role: 'selectall'}
]
},
{
label: 'View',
submenu: [
{role: 'reload'},
{role: 'forcereload'},
{role: 'toggledevtools'},
{type: 'separator'},
{role: 'resetzoom'},
{role: 'zoomin'},
{role: 'zoomout'},
{type: 'separator'},
{role: 'togglefullscreen'}
]
},
{
role: 'window',
submenu: [
{role: 'close'},
{role: 'minimize'},
{role: 'zoom'},
{type: 'separator'},
{role: 'front'}
]
},
{
role: 'help',
submenu: [
{
label: 'Website',
click () { electron.shell.openExternal('https://eugeny.github.io/terminus') }
}
2017-03-18 20:47:52 +00:00
]
}]
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,
2017-06-28 14:55:09 +00:00
height: 600,
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)
})