2018-08-31 13:41:28 +00:00
|
|
|
import { app, ipcMain, Menu, Tray, shell } from 'electron'
|
2019-02-09 23:23:49 +00:00
|
|
|
import * as electron from 'electron'
|
2018-10-06 18:50:06 +00:00
|
|
|
import { loadConfig } from './config'
|
2018-11-11 12:24:27 +00:00
|
|
|
import { Window, WindowOptions } from './window'
|
2018-08-31 13:41:28 +00:00
|
|
|
|
|
|
|
export class Application {
|
|
|
|
private tray: Tray
|
|
|
|
private windows: Window[] = []
|
|
|
|
|
|
|
|
constructor () {
|
|
|
|
ipcMain.on('app:config-change', () => {
|
|
|
|
this.broadcast('host:config-change')
|
|
|
|
})
|
2018-10-06 18:50:06 +00:00
|
|
|
|
|
|
|
const configData = loadConfig()
|
2019-12-29 18:07:32 +00:00
|
|
|
if (process.platform === 'linux') {
|
|
|
|
app.commandLine.appendSwitch('no-sandbox')
|
|
|
|
if (((configData.appearance || {}).opacity || 1) !== 1) {
|
|
|
|
app.commandLine.appendSwitch('enable-transparent-visuals')
|
|
|
|
app.disableHardwareAcceleration()
|
|
|
|
}
|
2018-10-06 18:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.commandLine.appendSwitch('disable-http-cache')
|
2019-01-27 22:58:55 +00:00
|
|
|
app.commandLine.appendSwitch('lang', 'EN')
|
2019-06-14 15:15:52 +00:00
|
|
|
|
|
|
|
for (const flag of configData.flags || [['force_discrete_gpu', '0']]) {
|
|
|
|
app.commandLine.appendSwitch(flag[0], flag[1])
|
|
|
|
}
|
2018-08-31 13:41:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 23:23:49 +00:00
|
|
|
init () {
|
2019-06-14 15:15:52 +00:00
|
|
|
electron.screen.on('display-metrics-changed', () => this.broadcast('host:display-metrics-changed'))
|
2019-02-09 23:23:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-11 12:24:27 +00:00
|
|
|
async newWindow (options?: WindowOptions): Promise<Window> {
|
|
|
|
let window = new Window(options)
|
2018-08-31 13:41:28 +00:00
|
|
|
this.windows.push(window)
|
|
|
|
window.visible$.subscribe(visible => {
|
|
|
|
if (visible) {
|
|
|
|
this.disableTray()
|
|
|
|
} else {
|
|
|
|
this.enableTray()
|
|
|
|
}
|
|
|
|
})
|
2018-09-20 11:01:25 +00:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
this.setupMenu()
|
|
|
|
}
|
2018-08-31 13:41:28 +00:00
|
|
|
await window.ready
|
|
|
|
return window
|
|
|
|
}
|
|
|
|
|
|
|
|
broadcast (event, ...args) {
|
|
|
|
for (let window of this.windows) {
|
|
|
|
window.send(event, ...args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async send (event, ...args) {
|
|
|
|
if (!this.hasWindows()) {
|
|
|
|
await this.newWindow()
|
|
|
|
}
|
2019-07-17 03:36:24 +00:00
|
|
|
this.windows.filter(w => !w.isDestroyed())[0].send(event, ...args)
|
2018-08-31 13:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enableTray () {
|
|
|
|
if (this.tray) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
this.tray = new Tray(`${app.getAppPath()}/assets/tray-darwinTemplate.png`)
|
|
|
|
this.tray.setPressedImage(`${app.getAppPath()}/assets/tray-darwinHighlightTemplate.png`)
|
|
|
|
} else {
|
|
|
|
this.tray = new Tray(`${app.getAppPath()}/assets/tray.png`)
|
|
|
|
}
|
|
|
|
|
2019-07-18 17:48:11 +00:00
|
|
|
this.tray.on('click', () => setTimeout(() => this.focus()));
|
2018-08-31 13:41:28 +00:00
|
|
|
|
|
|
|
const contextMenu = Menu.buildFromTemplate([{
|
|
|
|
label: 'Show',
|
|
|
|
click: () => this.focus(),
|
|
|
|
}])
|
|
|
|
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.tray.setContextMenu(contextMenu)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.tray.setToolTip(`Terminus ${app.getVersion()}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
disableTray () {
|
|
|
|
if (this.tray) {
|
|
|
|
this.tray.destroy()
|
|
|
|
this.tray = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hasWindows () {
|
|
|
|
return !!this.windows.length
|
|
|
|
}
|
|
|
|
|
|
|
|
focus () {
|
|
|
|
for (let window of this.windows) {
|
|
|
|
window.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private setupMenu () {
|
|
|
|
let template: Electron.MenuItemConstructorOptions[] = [
|
|
|
|
{
|
|
|
|
label: 'Application',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'about', label: 'About Terminus' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{
|
|
|
|
label: 'Preferences',
|
|
|
|
accelerator: 'Cmd+,',
|
2018-12-30 16:32:30 +00:00
|
|
|
click: async () => {
|
2018-08-31 13:41:28 +00:00
|
|
|
if (!this.hasWindows()) {
|
|
|
|
await this.newWindow()
|
|
|
|
}
|
|
|
|
this.windows[0].send('host:preferences-menu')
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'services', submenu: [] },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'hide' },
|
2019-08-27 09:52:52 +00:00
|
|
|
{ role: 'hideOthers' },
|
2018-08-31 13:41:28 +00:00
|
|
|
{ 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' },
|
2019-08-27 09:52:52 +00:00
|
|
|
{ role: 'pasteAndMatchStyle' },
|
2018-08-31 13:41:28 +00:00
|
|
|
{ role: 'delete' },
|
2019-08-27 09:52:52 +00:00
|
|
|
{ role: 'selectAll' },
|
2018-08-31 13:41:28 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'View',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'reload' },
|
2019-08-27 09:52:52 +00:00
|
|
|
{ role: 'forceReload' },
|
|
|
|
{ role: 'toggleDevTools' },
|
2018-08-31 13:41:28 +00:00
|
|
|
{ type: 'separator' },
|
2019-08-27 09:52:52 +00:00
|
|
|
{ role: 'resetZoom' },
|
|
|
|
{ role: 'zoomIn' },
|
|
|
|
{ role: 'zoomOut' },
|
2018-08-31 13:41:28 +00:00
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'togglefullscreen' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'window',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'minimize' },
|
|
|
|
{ role: 'zoom' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'front' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'help',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'Website',
|
|
|
|
click () {
|
|
|
|
shell.openExternal('https://eugeny.github.io/terminus')
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
|
|
|
|
}
|
|
|
|
}
|