2018-08-31 16:41:28 +03:00
|
|
|
import { Subject, Observable } from 'rxjs'
|
2018-12-29 15:27:45 +03:00
|
|
|
import { debounceTime } from 'rxjs/operators'
|
2019-06-28 05:46:47 +03:00
|
|
|
import { BrowserWindow, app, ipcMain, Rectangle, screen } 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
|
2019-01-29 23:33:50 +03:00
|
|
|
AccentState = require('windows-swca').ACCENT_STATE
|
2019-05-06 15:09:30 +03:00
|
|
|
DwmEnableBlurBehindWindow = require('windows-blurbehind').DwmEnableBlurBehindWindow
|
2018-10-05 21:12:06 +03:00
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
|
2018-11-11 15:24:27 +03:00
|
|
|
export interface WindowOptions {
|
|
|
|
hidden?: boolean
|
|
|
|
}
|
|
|
|
|
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-11-22 19:51:12 +03:00
|
|
|
private closing = false
|
2018-08-31 16:41:28 +03:00
|
|
|
|
|
|
|
get visible$ (): Observable<boolean> { return this.visible }
|
|
|
|
|
2018-11-11 15:24:27 +03:00
|
|
|
constructor (options?: WindowOptions) {
|
2018-10-06 21:50:06 +03:00
|
|
|
let configData = loadConfig()
|
2018-08-31 16:41:28 +03:00
|
|
|
|
2018-11-11 15:24:27 +03:00
|
|
|
options = options || {}
|
|
|
|
|
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-11-11 15:24:27 +03:00
|
|
|
let bwOptions: Electron.BrowserWindowConstructorOptions = {
|
2018-08-31 16:41:28 +03:00
|
|
|
width: 800,
|
|
|
|
height: 600,
|
|
|
|
title: 'Terminus',
|
|
|
|
minWidth: 400,
|
|
|
|
minHeight: 300,
|
2019-02-20 03:57:38 +03:00
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true,
|
|
|
|
},
|
2018-08-31 16:41:28 +03:00
|
|
|
frame: false,
|
|
|
|
show: false,
|
2018-10-05 21:12:06 +03:00
|
|
|
backgroundColor: '#00000000'
|
2018-08-31 16:41:28 +03:00
|
|
|
}
|
2018-11-11 15:24:27 +03:00
|
|
|
Object.assign(bwOptions, this.windowBounds)
|
2018-08-31 16:41:28 +03:00
|
|
|
|
2019-06-28 05:46:47 +03:00
|
|
|
const closestDisplay = screen.getDisplayNearestPoint( {x: this.windowBounds.x, y: this.windowBounds.y} )
|
|
|
|
|
|
|
|
const [left1, top1, right1, bottom1] = [this.windowBounds.x, this.windowBounds.y, this.windowBounds.x + this.windowBounds.width, this.windowBounds.y + this.windowBounds.height];
|
|
|
|
const [left2, top2, right2, bottom2] = [closestDisplay.bounds.x, closestDisplay.bounds.y, closestDisplay.bounds.x + closestDisplay.bounds.width, closestDisplay.bounds.y + closestDisplay.bounds.height];
|
|
|
|
|
|
|
|
if ((left2 > right1 || right2 < left1 || top2 > bottom1 || bottom2 < top1) && !maximized) {
|
|
|
|
bwOptions.x = closestDisplay.bounds.width / 2 - bwOptions.width / 2;
|
|
|
|
bwOptions.y = closestDisplay.bounds.height / 2 - bwOptions.height / 2;
|
|
|
|
}
|
|
|
|
|
2018-08-31 16:41:28 +03:00
|
|
|
if ((configData.appearance || {}).frame === 'native') {
|
2018-11-11 15:24:27 +03:00
|
|
|
bwOptions.frame = true
|
2018-08-31 16:41:28 +03:00
|
|
|
} else {
|
|
|
|
if (process.platform === 'darwin') {
|
2018-11-11 15:24:27 +03:00
|
|
|
bwOptions.titleBarStyle = 'hiddenInset'
|
2018-08-31 16:41:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.platform === 'linux') {
|
2018-11-11 15:24:27 +03:00
|
|
|
bwOptions.backgroundColor = '#131d27'
|
2018-08-31 16:41:28 +03:00
|
|
|
}
|
|
|
|
|
2018-11-11 15:24:27 +03:00
|
|
|
this.window = new BrowserWindow(bwOptions)
|
2018-08-31 16:41:28 +03:00
|
|
|
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-11-11 15:24:27 +03:00
|
|
|
|
|
|
|
if (!options.hidden) {
|
|
|
|
if (maximized) {
|
|
|
|
this.window.maximize()
|
|
|
|
} else {
|
|
|
|
this.window.show()
|
|
|
|
}
|
|
|
|
this.window.focus()
|
2018-09-13 02:25:08 +03:00
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
})
|
2019-06-28 05:46:47 +03:00
|
|
|
|
2018-08-31 16:41:28 +03:00
|
|
|
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
|
|
|
|
if (enabled) {
|
2018-10-13 14:30:12 +03:00
|
|
|
if (parseInt(os.release().split('.')[2]) >= 17063 && type === 'fluent') {
|
2019-01-29 23:33:50 +03:00
|
|
|
attribValue = AccentState.ACCENT_ENABLE_ACRYLICBLURBEHIND
|
2018-10-05 21:12:06 +03:00
|
|
|
} else {
|
|
|
|
attribValue = AccentState.ACCENT_ENABLE_BLURBEHIND
|
|
|
|
}
|
|
|
|
}
|
2019-01-29 23:33:50 +03:00
|
|
|
SetWindowCompositionAttribute(this.window.getNativeWindowHandle(), attribValue, 0x00000000)
|
2018-10-05 21:12:06 +03:00
|
|
|
} else {
|
|
|
|
DwmEnableBlurBehindWindow(this.window, enabled)
|
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
show () {
|
|
|
|
this.window.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
focus () {
|
|
|
|
this.window.focus()
|
|
|
|
}
|
|
|
|
|
|
|
|
send (event, ...args) {
|
2018-10-26 17:17:20 +03:00
|
|
|
if (!this.window) {
|
|
|
|
return
|
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2018-12-29 15:27:45 +03:00
|
|
|
let moveSubscription = new Observable<void>(observer => {
|
|
|
|
this.window.on('move', () => observer.next())
|
|
|
|
}).pipe(debounceTime(250)).subscribe(() => {
|
|
|
|
this.window.webContents.send('host:window-moved')
|
|
|
|
})
|
|
|
|
|
|
|
|
this.window.on('closed', () => {
|
|
|
|
moveSubscription.unsubscribe()
|
|
|
|
})
|
|
|
|
|
2018-08-31 16:41:28 +03:00
|
|
|
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'))
|
|
|
|
|
2018-11-22 19:51:12 +03:00
|
|
|
this.window.on('close', event => {
|
|
|
|
if (!this.closing) {
|
|
|
|
event.preventDefault()
|
|
|
|
this.window.webContents.send('host:window-close-request')
|
|
|
|
return
|
|
|
|
}
|
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-10-26 17:17:20 +03:00
|
|
|
ipcMain.on('window-focus', event => {
|
2019-02-10 00:38:45 +03:00
|
|
|
if (!this.window || event.sender !== this.window.webContents) {
|
2018-10-26 17:17:20 +03:00
|
|
|
return
|
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
this.window.focus()
|
|
|
|
})
|
|
|
|
|
2018-10-26 17:17:20 +03:00
|
|
|
ipcMain.on('window-maximize', event => {
|
2019-02-10 00:38:45 +03:00
|
|
|
if (!this.window || event.sender !== this.window.webContents) {
|
2018-10-26 17:17:20 +03:00
|
|
|
return
|
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
this.window.maximize()
|
|
|
|
})
|
|
|
|
|
2018-10-26 17:17:20 +03:00
|
|
|
ipcMain.on('window-unmaximize', event => {
|
2019-02-10 00:38:45 +03:00
|
|
|
if (!this.window || event.sender !== this.window.webContents) {
|
2018-10-26 17:17:20 +03:00
|
|
|
return
|
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
this.window.unmaximize()
|
|
|
|
})
|
|
|
|
|
2018-10-26 17:17:20 +03:00
|
|
|
ipcMain.on('window-toggle-maximize', event => {
|
2019-02-10 00:38:45 +03:00
|
|
|
if (!this.window || event.sender !== this.window.webContents) {
|
2018-10-26 17:17:20 +03:00
|
|
|
return
|
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
if (this.window.isMaximized()) {
|
|
|
|
this.window.unmaximize()
|
|
|
|
} else {
|
|
|
|
this.window.maximize()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-10-26 17:17:20 +03:00
|
|
|
ipcMain.on('window-minimize', event => {
|
2019-02-10 00:38:45 +03:00
|
|
|
if (!this.window || event.sender !== this.window.webContents) {
|
2018-10-26 17:17:20 +03:00
|
|
|
return
|
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
this.window.minimize()
|
|
|
|
})
|
|
|
|
|
2018-10-26 17:17:20 +03:00
|
|
|
ipcMain.on('window-set-bounds', (event, bounds) => {
|
2019-02-10 00:38:45 +03:00
|
|
|
if (!this.window || event.sender !== this.window.webContents) {
|
2018-10-26 17:17:20 +03:00
|
|
|
return
|
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
this.window.setBounds(bounds)
|
|
|
|
})
|
|
|
|
|
2018-10-26 17:17:20 +03:00
|
|
|
ipcMain.on('window-set-always-on-top', (event, flag) => {
|
2019-02-10 00:38:45 +03:00
|
|
|
if (!this.window || event.sender !== this.window.webContents) {
|
2018-10-26 17:17:20 +03:00
|
|
|
return
|
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
this.window.setAlwaysOnTop(flag)
|
|
|
|
})
|
|
|
|
|
2018-10-26 17:17:20 +03:00
|
|
|
ipcMain.on('window-set-vibrancy', (event, enabled, type) => {
|
2019-02-10 00:38:45 +03:00
|
|
|
if (!this.window || event.sender !== this.window.webContents) {
|
2018-10-26 17:17:20 +03:00
|
|
|
return
|
|
|
|
}
|
2018-10-13 14:30:12 +03:00
|
|
|
this.setVibrancy(enabled, type)
|
2018-08-31 16:41:28 +03:00
|
|
|
})
|
2018-09-20 12:42:51 +03:00
|
|
|
|
2018-10-26 17:17:20 +03:00
|
|
|
ipcMain.on('window-set-title', (event, title) => {
|
2019-02-10 00:38:45 +03:00
|
|
|
if (!this.window || event.sender !== this.window.webContents) {
|
2018-10-26 17:17:20 +03:00
|
|
|
return
|
|
|
|
}
|
2018-09-20 12:42:51 +03:00
|
|
|
this.window.setTitle(title)
|
|
|
|
})
|
2018-10-13 14:35:16 +03:00
|
|
|
|
2018-10-31 19:37:34 +03:00
|
|
|
ipcMain.on('window-bring-to-front', event => {
|
2019-02-10 00:38:45 +03:00
|
|
|
if (!this.window || event.sender !== this.window.webContents) {
|
2018-10-31 19:37:34 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if (this.window.isMinimized()) {
|
|
|
|
this.window.restore()
|
|
|
|
}
|
|
|
|
this.window.show()
|
|
|
|
this.window.moveTop()
|
|
|
|
})
|
|
|
|
|
2018-12-29 14:41:32 +03:00
|
|
|
ipcMain.on('window-close', event => {
|
2019-02-10 00:38:45 +03:00
|
|
|
if (!this.window || event.sender !== this.window.webContents) {
|
2018-12-29 14:41:32 +03:00
|
|
|
return
|
|
|
|
}
|
2018-11-22 19:51:12 +03:00
|
|
|
this.closing = true
|
|
|
|
this.window.close()
|
|
|
|
})
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|