1
1
mirror of https://github.com/Eugeny/tabby.git synced 2024-12-26 03:51:49 +03:00
tabby/app/main.js

256 lines
6.3 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-06-29 15:26:39 +03:00
if (process.argv.indexOf('--debug') !== -1) {
require('electron-debug')({enabled: true, showDevTools: 'undocked'})
}
2017-04-16 18:13:03 +03:00
2017-03-20 19:46:25 +03:00
let app = electron.app
2017-07-05 17:22:44 +03:00
let secondInstance = app.makeSingleInstance((argv, cwd) => {
app.window.webContents.send('host:second-instance', argv, cwd)
2017-03-20 19:46:25 +03:00
})
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')
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 = () => {
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())
})
app.window.on('closed', () => {
app.window = null
})
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()
2017-07-25 22:49:20 +03:00
actualBounds.width -= bounds.x - actualBounds.x
actualBounds.height -= bounds.y - actualBounds.y
2017-04-28 17:55:51 +03:00
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
}
setupMenu = () => {
2017-07-05 20:05:45 +03:00
let template = [{
2016-12-23 12:06:53 +03:00
label: "Application",
submenu: [
2017-07-05 20:05:45 +03: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 21:24:35 +03:00
app.quit()
2017-07-05 20:05:45 +03:00
}
}
2017-03-18 23:47:52 +03:00
]
2017-07-05 20:05:45 +03:00
},
{
2017-03-18 23:47:52 +03:00
label: "Edit",
submenu: [
2017-07-05 20:05:45 +03: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 23:47:52 +03:00
]
}]
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,
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)
})