2018-08-31 16:41:28 +03:00
|
|
|
import { app, ipcMain, Menu, Tray, shell } from 'electron'
|
2018-10-06 21:50:06 +03:00
|
|
|
import { loadConfig } from './config'
|
2018-11-11 15:24:27 +03:00
|
|
|
import { Window, WindowOptions } from './window'
|
2018-08-31 16:41:28 +03:00
|
|
|
|
|
|
|
export class Application {
|
|
|
|
private tray: Tray
|
|
|
|
private windows: Window[] = []
|
|
|
|
|
|
|
|
constructor () {
|
|
|
|
ipcMain.on('app:config-change', () => {
|
|
|
|
this.broadcast('host:config-change')
|
|
|
|
})
|
2018-10-06 21:50:06 +03:00
|
|
|
|
|
|
|
const configData = loadConfig()
|
|
|
|
if (process.platform === 'linux' && ((configData.appearance || {}).opacity || 1) !== 1) {
|
|
|
|
app.commandLine.appendSwitch('enable-transparent-visuals')
|
|
|
|
app.disableHardwareAcceleration()
|
|
|
|
}
|
|
|
|
|
|
|
|
app.commandLine.appendSwitch('disable-http-cache')
|
2019-01-28 01:58:55 +03:00
|
|
|
app.commandLine.appendSwitch('lang', 'EN')
|
2018-08-31 16:41:28 +03:00
|
|
|
}
|
|
|
|
|
2018-11-11 15:24:27 +03:00
|
|
|
async newWindow (options?: WindowOptions): Promise<Window> {
|
|
|
|
let window = new Window(options)
|
2018-08-31 16:41:28 +03:00
|
|
|
this.windows.push(window)
|
|
|
|
window.visible$.subscribe(visible => {
|
|
|
|
if (visible) {
|
|
|
|
this.disableTray()
|
|
|
|
} else {
|
|
|
|
this.enableTray()
|
|
|
|
}
|
|
|
|
})
|
2018-09-20 14:01:25 +03:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
this.setupMenu()
|
|
|
|
}
|
2018-08-31 16:41:28 +03: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()
|
|
|
|
}
|
|
|
|
this.windows[0].send(event, ...args)
|
|
|
|
}
|
|
|
|
|
|
|
|
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`)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.tray.on('click', () => this.focus())
|
|
|
|
|
|
|
|
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()
|
|
|
|
window.focus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private setupMenu () {
|
|
|
|
let template: Electron.MenuItemConstructorOptions[] = [
|
|
|
|
{
|
|
|
|
label: 'Application',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'about', label: 'About Terminus' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{
|
|
|
|
label: 'Preferences',
|
|
|
|
accelerator: 'Cmd+,',
|
2018-12-30 19:32:30 +03:00
|
|
|
click: async () => {
|
2018-08-31 16:41:28 +03:00
|
|
|
if (!this.hasWindows()) {
|
|
|
|
await this.newWindow()
|
|
|
|
}
|
|
|
|
this.windows[0].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 () {
|
|
|
|
shell.openExternal('https://eugeny.github.io/terminus')
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
|
|
|
|
}
|
|
|
|
}
|