| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- if (process.platform == 'win32' && require('electron-squirrel-startup')) process.exit(0)
- const electron = require('electron')
- let electronVibrancy
- if (process.platform != 'linux') {
- electronVibrancy = require('electron-vibrancy')
- }
- if (process.argv.indexOf('--debug') !== -1) {
- require('electron-debug')({enabled: true, showDevTools: 'undocked'})
- }
- let app = electron.app
- let secondInstance = app.makeSingleInstance((argv, cwd) => {
- app.window.webContents.send('host:second-instance', argv, cwd)
- })
- if (secondInstance) {
- app.quit()
- return
- }
- const yaml = require('js-yaml')
- const path = require('path')
- const fs = require('fs')
- const Config = require('electron-config')
- let windowConfig = new Config({name: 'window'})
- if (!process.env.TERMINUS_PLUGINS) {
- process.env.TERMINUS_PLUGINS = ''
- }
- setWindowVibrancy = (enabled) => {
- if (enabled && !app.window.vibrancyViewID) {
- app.window.vibrancyViewID = electronVibrancy.SetVibrancy(app.window, 0)
- } else if (!enabled && app.window.vibrancyViewID) {
- electronVibrancy.RemoveView(app.window, app.window.vibrancyViewID)
- app.window.vibrancyViewID = null
- }
- }
- setupWindowManagement = () => {
- app.window.on('show', () => {
- app.window.webContents.send('host:window-shown')
- if (app.tray) {
- app.tray.destroy()
- app.tray = null
- }
- })
- app.window.on('hide', (e) => {
- if (!app.tray) {
- setupTray()
- }
- })
- app.window.on('enter-full-screen', () => app.window.webContents.send('host:window-enter-full-screen'))
- app.window.on('leave-full-screen', () => app.window.webContents.send('host:window-leave-full-screen'))
- 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()
- })
- electron.ipcMain.on('window-maximize', () => {
- app.window.maximize()
- })
- electron.ipcMain.on('window-unmaximize', () => {
- app.window.unmaximize()
- })
- electron.ipcMain.on('window-toggle-maximize', () => {
- if (app.window.isMaximized()) {
- app.window.unmaximize()
- } else {
- app.window.maximize()
- }
- })
- electron.ipcMain.on('window-minimize', () => {
- app.window.minimize()
- })
- electron.ipcMain.on('window-set-bounds', (event, bounds) => {
- app.window.setBounds(bounds)
- })
- electron.ipcMain.on('window-set-always-on-top', (event, flag) => {
- app.window.setAlwaysOnTop(flag)
- })
- electron.ipcMain.on('window-set-vibrancy', (event, enabled) => {
- setWindowVibrancy(enabled)
- })
- }
- setupTray = () => {
- if (process.platform == 'darwin') {
- app.tray = new electron.Tray(`${app.getAppPath()}/assets/tray-darwinTemplate.png`)
- app.tray.setPressedImage(`${app.getAppPath()}/assets/tray-darwinHighlightTemplate.png`)
- } else {
- app.tray = new electron.Tray(`${app.getAppPath()}/assets/tray.png`)
- }
- app.tray.on('click', () => {
- app.window.show()
- app.window.focus()
- })
- const contextMenu = electron.Menu.buildFromTemplate([{
- label: 'Show',
- click () {
- app.window.show()
- app.window.focus()
- }
- }])
- if (process.platform != 'darwin') {
- app.tray.setContextMenu(contextMenu)
- }
- app.tray.setToolTip(`Terminus ${app.getVersion()}`)
- }
- setupMenu = () => {
- let template = [{
- label: "Application",
- submenu: [
- { 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 () {
- app.quit()
- }
- }
- ]
- },
- {
- label: "Edit",
- submenu: [
- {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: 'minimize'},
- {role: 'zoom'},
- {type: 'separator'},
- {role: 'front'}
- ]
- },
- {
- role: 'help',
- submenu: [
- {
- label: 'Website',
- click () { electron.shell.openExternal('https://eugeny.github.io/terminus') }
- }
- ]
- }]
- electron.Menu.setApplicationMenu(electron.Menu.buildFromTemplate(template))
- }
- start = () => {
- let t0 = Date.now()
- 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 = {}
- }
- let options = {
- width: 800,
- height: 600,
- title: 'Terminus',
- minWidth: 400,
- minHeight: 300,
- webPreferences: {webSecurity: false},
- frame: false,
- show: false,
- }
- Object.assign(options, windowConfig.get('windowBoundaries'))
- if ((configData.appearance || {}).frame == 'native') {
- options.frame = true
- } else {
- if (process.platform == 'darwin') {
- options.titleBarStyle = 'hiddenInset'
- }
- }
- if (process.platform == 'linux') {
- options.backgroundColor = '#131d27'
- }
- app.commandLine.appendSwitch('disable-http-cache')
- app.window = new electron.BrowserWindow(options)
- app.window.once('ready-to-show', () => {
- if (process.platform == 'darwin') {
- app.window.setVibrancy('dark')
- } else if (process.platform == 'windows') {
- setWindowVibrancy(true)
- }
- app.window.show()
- app.window.focus()
- })
- app.window.loadURL(`file://${app.getAppPath()}/dist/index.html`, {extraHeaders: "pragma: no-cache\n"})
- if (process.platform != 'darwin') {
- app.window.setMenu(null)
- }
- setupWindowManagement()
- if (process.platform == 'darwin') {
- setupMenu()
- } else {
- app.window.setMenu(null)
- }
- 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)
- })
|