2018-08-31 16:41:28 +03:00
|
|
|
import { Subject, Observable } from 'rxjs'
|
2018-10-05 13:36:15 +03:00
|
|
|
import { BrowserWindow, app, ipcMain, Rectangle } from 'electron'
|
2018-08-31 16:41:28 +03:00
|
|
|
import ElectronConfig = require('electron-config')
|
2018-10-05 21:12:06 +03:00
|
|
|
import * as os from 'os'
|
2018-10-06 21:50:06 +03:00
|
|
|
|
|
|
|
import { loadConfig } from './config'
|
2018-08-31 16:41:28 +03:00
|
|
|
|
2018-10-05 21:12:06 +03:00
|
|
|
let SetWindowCompositionAttribute: any
|
|
|
|
let AccentState: any
|
|
|
|
let DwmEnableBlurBehindWindow: any
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
SetWindowCompositionAttribute = require('windows-swca').SetWindowCompositionAttribute
|
|
|
|
AccentState = require('windows-swca').AccentState
|
|
|
|
DwmEnableBlurBehindWindow = require('windows-blurbehind').DwmEnableBlurBehindWindow
|
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
|
|
|
|
export class Window {
|
|
|
|
ready: Promise<void>
|
|
|
|
private visible = new Subject<boolean>()
|
|
|
|
private window: BrowserWindow
|
|
|
|
private windowConfig: ElectronConfig
|
2018-09-13 02:25:08 +03:00
|
|
|
private windowBounds: Rectangle
|
2018-08-31 16:41:28 +03:00
|
|
|
|
|
|
|
get visible$ (): Observable<boolean> { return this.visible }
|
|
|
|
|
|
|
|
constructor () {
|
2018-10-06 21:50:06 +03:00
|
|
|
let configData = loadConfig()
|
2018-08-31 16:41:28 +03:00
|
|
|
|
|
|
|
this.windowConfig = new ElectronConfig({ name: 'window' })
|
2018-09-13 02:25:08 +03:00
|
|
|
this.windowBounds = this.windowConfig.get('windowBoundaries')
|
2018-08-31 16:41:28 +03:00
|
|
|
|
2018-09-13 02:25:08 +03:00
|
|
|
let maximized = this.windowConfig.get('maximized')
|
2018-08-31 16:41:28 +03:00
|
|
|
let options: Electron.BrowserWindowConstructorOptions = {
|
|
|
|
width: 800,
|
|
|
|
height: 600,
|
|
|
|
title: 'Terminus',
|
|
|
|
minWidth: 400,
|
|
|
|
minHeight: 300,
|
|
|
|
webPreferences: { webSecurity: false },
|
|
|
|
frame: false,
|
|
|
|
show: false,
|
2018-10-05 21:12:06 +03:00
|
|
|
backgroundColor: '#00000000'
|
2018-08-31 16:41:28 +03:00
|
|
|
}
|
2018-09-13 02:25:08 +03:00
|
|
|
Object.assign(options, this.windowBounds)
|
2018-08-31 16:41:28 +03:00
|
|
|
|
|
|
|
if ((configData.appearance || {}).frame === 'native') {
|
|
|
|
options.frame = true
|
|
|
|
} else {
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
options.titleBarStyle = 'hiddenInset'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.platform === 'linux') {
|
|
|
|
options.backgroundColor = '#131d27'
|
|
|
|
}
|
|
|
|
|
|
|
|
this.window = new BrowserWindow(options)
|
|
|
|
this.window.once('ready-to-show', () => {
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
this.window.setVibrancy('dark')
|
|
|
|
} else if (process.platform === 'win32' && (configData.appearance || {}).vibrancy) {
|
|
|
|
this.setVibrancy(true)
|
|
|
|
}
|
2018-09-13 02:25:08 +03:00
|
|
|
if (maximized) {
|
|
|
|
this.window.maximize()
|
|
|
|
} else {
|
|
|
|
this.window.show()
|
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
this.window.focus()
|
|
|
|
})
|
|
|
|
this.window.loadURL(`file://${app.getAppPath()}/dist/index.html?${this.window.id}`, { extraHeaders: 'pragma: no-cache\n' })
|
|
|
|
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.window.setMenu(null)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setupWindowManagement()
|
|
|
|
|
|
|
|
this.ready = new Promise(resolve => {
|
|
|
|
const listener = event => {
|
|
|
|
if (event.sender === this.window.webContents) {
|
|
|
|
ipcMain.removeListener('app:ready', listener)
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ipcMain.on('app:ready', listener)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-13 14:30:12 +03:00
|
|
|
setVibrancy (enabled: boolean, type?: string) {
|
2018-10-05 21:12:06 +03:00
|
|
|
if (process.platform === 'win32') {
|
|
|
|
if (parseFloat(os.release()) >= 10) {
|
|
|
|
let attribValue = AccentState.ACCENT_DISABLED
|
|
|
|
let color = 0x00000000
|
|
|
|
if (enabled) {
|
2018-10-13 14:30:12 +03:00
|
|
|
if (parseInt(os.release().split('.')[2]) >= 17063 && type === 'fluent') {
|
2018-10-05 21:12:06 +03:00
|
|
|
attribValue = AccentState.ACCENT_ENABLE_FLUENT
|
|
|
|
color = 0x01000000 // using a small alpha because acrylic bugs out at full transparency.
|
|
|
|
} else {
|
|
|
|
attribValue = AccentState.ACCENT_ENABLE_BLURBEHIND
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SetWindowCompositionAttribute(this.window, attribValue, color)
|
|
|
|
} else {
|
|
|
|
DwmEnableBlurBehindWindow(this.window, enabled)
|
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
show () {
|
|
|
|
this.window.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
focus () {
|
|
|
|
this.window.focus()
|
|
|
|
}
|
|
|
|
|
|
|
|
send (event, ...args) {
|
|
|
|
this.window.webContents.send(event, ...args)
|
|
|
|
}
|
|
|
|
|
|
|
|
private setupWindowManagement () {
|
|
|
|
this.window.on('show', () => {
|
|
|
|
this.visible.next(true)
|
|
|
|
this.window.webContents.send('host:window-shown')
|
|
|
|
})
|
|
|
|
|
|
|
|
this.window.on('hide', () => {
|
|
|
|
this.visible.next(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.window.on('enter-full-screen', () => this.window.webContents.send('host:window-enter-full-screen'))
|
|
|
|
this.window.on('leave-full-screen', () => this.window.webContents.send('host:window-leave-full-screen'))
|
|
|
|
|
|
|
|
this.window.on('close', () => {
|
2018-09-13 02:25:08 +03:00
|
|
|
this.windowConfig.set('windowBoundaries', this.windowBounds)
|
|
|
|
this.windowConfig.set('maximized', this.window.isMaximized())
|
2018-08-31 16:41:28 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
this.window.on('closed', () => {
|
|
|
|
this.destroy()
|
|
|
|
})
|
|
|
|
|
2018-09-13 02:25:08 +03:00
|
|
|
this.window.on('resize', () => {
|
2018-09-20 12:46:24 +03:00
|
|
|
if (!this.window.isMaximized()) {
|
2018-09-13 02:25:08 +03:00
|
|
|
this.windowBounds = this.window.getBounds()
|
2018-09-20 12:46:24 +03:00
|
|
|
}
|
2018-09-13 02:25:08 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
this.window.on('move', () => {
|
2018-09-20 12:46:24 +03:00
|
|
|
if (!this.window.isMaximized()) {
|
2018-09-13 02:25:08 +03:00
|
|
|
this.windowBounds = this.window.getBounds()
|
2018-09-20 12:46:24 +03:00
|
|
|
}
|
2018-09-13 02:25:08 +03:00
|
|
|
})
|
|
|
|
|
2018-08-31 16:41:28 +03:00
|
|
|
ipcMain.on('window-focus', () => {
|
|
|
|
this.window.focus()
|
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.on('window-maximize', () => {
|
|
|
|
this.window.maximize()
|
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.on('window-unmaximize', () => {
|
|
|
|
this.window.unmaximize()
|
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.on('window-toggle-maximize', () => {
|
|
|
|
if (this.window.isMaximized()) {
|
|
|
|
this.window.unmaximize()
|
|
|
|
} else {
|
|
|
|
this.window.maximize()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.on('window-minimize', () => {
|
|
|
|
this.window.minimize()
|
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.on('window-set-bounds', (_event, bounds) => {
|
|
|
|
this.window.setBounds(bounds)
|
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.on('window-set-always-on-top', (_event, flag) => {
|
|
|
|
this.window.setAlwaysOnTop(flag)
|
|
|
|
})
|
|
|
|
|
2018-10-13 14:30:12 +03:00
|
|
|
ipcMain.on('window-set-vibrancy', (_event, enabled, type) => {
|
|
|
|
this.setVibrancy(enabled, type)
|
2018-08-31 16:41:28 +03:00
|
|
|
})
|
2018-09-20 12:42:51 +03:00
|
|
|
|
|
|
|
ipcMain.on('window-set-title', (_event, title) => {
|
|
|
|
this.window.setTitle(title)
|
|
|
|
})
|
2018-10-13 14:35:16 +03:00
|
|
|
|
|
|
|
this.window.webContents.on('new-window', event => event.preventDefault())
|
2018-08-31 16:41:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private destroy () {
|
|
|
|
this.window = null
|
|
|
|
this.visible.complete()
|
|
|
|
}
|
|
|
|
}
|