1
1
mirror of https://github.com/Eugeny/tabby.git synced 2024-11-24 06:04:04 +03:00
tabby/app/main.js

212 lines
5.6 KiB
JavaScript
Raw Normal View History

2017-05-01 01:01:12 +03:00
if (process.platform == 'win32' && require('electron-squirrel-startup')) process.exit(0)
2017-03-20 19:46:25 +03:00
const electron = require('electron')
2017-04-16 18:13:03 +03:00
require('electron-debug')({enabled: true, showDevTools: (process.argv.indexOf('--debug') != -1) ? 'undocked' : false})
2017-03-20 19:46:25 +03: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 13:48:07 +03:00
const yaml = require('js-yaml')
const path = require('path')
const fs = require('fs')
2016-12-23 12:06:53 +03: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-24 01:34:07 +03:00
if (!process.env.TERMINUS_PLUGINS) {
process.env.TERMINUS_PLUGINS = ''
}
2016-12-23 12:06:53 +03:00
setupWindowManagement = () => {
let windowCloseable
2017-03-18 23:47:52 +03:00
app.window.on('show', () => {
2017-03-20 19:46:25 +03:00
app.window.webContents.send('host:window-shown')
2017-03-18 23:47:52 +03:00
})
2016-12-23 12:06:53 +03:00
app.window.on('close', (e) => {
windowConfig.set('windowBoundaries', app.window.getBounds())
if (!windowCloseable) {
2016-12-27 01:21:50 +03:00
app.window.minimize()
2016-12-23 12:06:53 +03: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-28 00:21:02 +03:00
electron.ipcMain.on('window-toggle-focus', () => {
if (app.window.isFocused()) {
app.window.minimize()
} else {
app.window.focus()
}
})
2016-12-27 01:21:50 +03:00
electron.ipcMain.on('window-maximize', () => {
2017-03-20 19:46:25 +03:00
app.window.maximize()
})
electron.ipcMain.on('window-unmaximize', () => {
app.window.unmaximize()
})
electron.ipcMain.on('window-toggle-maximize', () => {
2016-12-27 01:21:50 +03:00
if (app.window.isMaximized()) {
app.window.unmaximize()
} else {
app.window.maximize()
}
})
electron.ipcMain.on('window-minimize', () => {
app.window.minimize()
})
2017-03-18 23:47:52 +03:00
electron.ipcMain.on('window-set-bounds', (event, bounds) => {
2017-04-28 17:55:51 +03:00
let actualBounds = app.window.getBounds()
actualBounds.x = bounds.x
actualBounds.y = bounds.y
app.window.setBounds(actualBounds)
2017-04-23 23:37:19 +03:00
setTimeout(() => {
2017-04-28 17:55:51 +03:00
actualBounds = app.window.getBounds()
2017-04-23 23:37:19 +03:00
bounds.width += bounds.x - actualBounds.x
bounds.height += bounds.y - actualBounds.y
2017-04-28 17:55:51 +03:00
bounds.x = actualBounds.x
bounds.y = actualBounds.y
2017-04-23 23:37:19 +03:00
app.window.setBounds(bounds)
2017-04-28 17:55:51 +03:00
}, 100)
2017-03-18 23:47:52 +03:00
})
2017-03-20 19:46:25 +03:00
electron.ipcMain.on('window-set-always-on-top', (event, flag) => {
app.window.setAlwaysOnTop(flag)
})
2016-12-23 12:06:53 +03: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 23:47:52 +03: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 12:06:53 +03:00
electron.Menu.setApplicationMenu(electron.Menu.buildFromTemplate(template))
}
start = () => {
let t0 = Date.now()
2017-03-18 13:48:07 +03: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 12:06:53 +03:00
let options = {
width: 800,
2017-06-28 17:55:09 +03:00
height: 600,
2016-12-26 02:04:56 +03:00
//icon: `${app.getAppPath()}/assets/img/icon.png`,
2017-04-15 16:20:18 +03:00
title: 'Terminus',
2017-04-28 17:55:51 +03:00
minWidth: 400,
minHeight: 300,
2016-12-23 12:06:53 +03:00
'web-preferences': {'web-security': false},
//- background to avoid the flash of unstyled window
2017-04-24 01:34:07 +03:00
backgroundColor: '#131d27',
2016-12-27 01:21:50 +03:00
frame: false,
2017-03-22 02:18:52 +03:00
//type: 'toolbar',
2016-12-23 12:06:53 +03:00
}
Object.assign(options, windowConfig.get('windowBoundaries'))
2017-04-28 17:55:51 +03:00
if ((configData.appearance || {}).frame == 'native') {
2017-03-18 13:48:07 +03:00
options.frame = true
2017-04-17 23:43:26 +03:00
} else {
2017-05-01 01:01:12 +03:00
if (process.platform == 'darwin') {
2017-04-17 23:43:26 +03:00
options.titleBarStyle = 'hidden-inset'
}
2017-03-18 13:48:07 +03:00
}
2016-12-27 01:21:50 +03:00
app.commandLine.appendSwitch('disable-http-cache')
2016-12-23 12:06:53 +03:00
app.window = new electron.BrowserWindow(options)
2017-04-24 01:34:07 +03:00
app.window.loadURL(`file://${app.getAppPath()}/dist/index.html`, {extraHeaders: "pragma: no-cache\n"})
2016-12-23 12:06:53 +03:00
2017-05-01 01:01:12 +03:00
if (process.platform != 'darwin') {
2016-12-23 12:06:53 +03:00
app.window.setMenu(null)
}
app.window.show()
app.window.focus()
setupWindowManagement()
2017-03-18 23:47:52 +03:00
2017-05-01 01:01:12 +03:00
if (process.platform == 'darwin') {
2017-03-18 23:47:52 +03:00
setupMenu()
} else {
app.window.setMenu(null)
}
2016-12-23 12:06:53 +03: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)
})