tabby/app/lib/window.ts

385 lines
12 KiB
TypeScript
Raw Normal View History

import { Subject, Observable } from 'rxjs'
2018-12-29 12:27:45 +00:00
import { debounceTime } from 'rxjs/operators'
import { BrowserWindow, app, ipcMain, Rectangle, Menu, screen } from 'electron'
import ElectronConfig = require('electron-config')
import * as os from 'os'
2019-11-26 14:51:31 +00:00
import * as path from 'path'
2018-10-06 18:50:06 +00:00
import { loadConfig } from './config'
let SetWindowCompositionAttribute: any
let AccentState: any
let DwmEnableBlurBehindWindow: any
if (process.platform === 'win32') {
SetWindowCompositionAttribute = require('windows-swca').SetWindowCompositionAttribute
2019-01-29 20:33:50 +00:00
AccentState = require('windows-swca').ACCENT_STATE
2019-05-06 12:09:30 +00:00
DwmEnableBlurBehindWindow = require('windows-blurbehind').DwmEnableBlurBehindWindow
}
export interface WindowOptions {
hidden?: boolean
}
export class Window {
ready: Promise<void>
private visible = new Subject<boolean>()
private closed = new Subject<void>()
private window: BrowserWindow
private windowConfig: ElectronConfig
2018-09-12 23:25:08 +00:00
private windowBounds: Rectangle
private closing = false
private lastVibrancy: {enabled: boolean, type?: string} | null = null
private disableVibrancyWhileDragging = false
private configStore: any
get visible$ (): Observable<boolean> { return this.visible }
get closed$ (): Observable<void> { return this.closed }
constructor (options?: WindowOptions) {
this.configStore = loadConfig()
options = options || {}
this.windowConfig = new ElectronConfig({ name: 'window' })
2018-09-12 23:25:08 +00:00
this.windowBounds = this.windowConfig.get('windowBoundaries')
2018-09-12 23:25:08 +00:00
let maximized = this.windowConfig.get('maximized')
let bwOptions: Electron.BrowserWindowConstructorOptions = {
width: 800,
height: 600,
title: 'Terminus',
minWidth: 400,
minHeight: 300,
2019-02-20 00:57:38 +00:00
webPreferences: {
nodeIntegration: true,
2019-11-26 14:51:31 +00:00
preload: path.join(__dirname, 'sentry.js'),
2020-02-05 12:22:35 +00:00
backgroundThrottling: false,
2019-02-20 00:57:38 +00:00
},
frame: false,
show: false,
2019-10-26 19:11:27 +00:00
backgroundColor: '#00000000',
}
if (this.windowBounds) {
Object.assign(bwOptions, this.windowBounds)
2020-02-05 12:16:31 +00:00
const closestDisplay = screen.getDisplayNearestPoint( { x: this.windowBounds.x, y: this.windowBounds.y } )
2020-02-05 12:16:31 +00:00
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) {
2020-02-05 12:16:31 +00:00
bwOptions.x = closestDisplay.bounds.width / 2 - bwOptions.width / 2
bwOptions.y = closestDisplay.bounds.height / 2 - bwOptions.height / 2
}
}
if ((this.configStore.appearance || {}).frame === 'native') {
bwOptions.frame = true
} else {
if (process.platform === 'darwin') {
bwOptions.titleBarStyle = 'hiddenInset'
}
}
if (process.platform === 'linux') {
bwOptions.backgroundColor = '#131d27'
}
this.window = new BrowserWindow(bwOptions)
this.window.once('ready-to-show', () => {
if (process.platform === 'darwin') {
2019-10-26 19:11:27 +00:00
this.window.setVibrancy('window')
} else if (process.platform === 'win32' && (this.configStore.appearance || {}).vibrancy) {
this.setVibrancy(true)
}
if (!options.hidden) {
if (maximized) {
this.window.maximize()
} else {
this.window.show()
}
this.window.focus()
2018-09-12 23:25:08 +00:00
}
})
2020-04-20 17:21:48 +00:00
this.window.on('blur', () => {
2020-04-20 17:01:10 +00:00
if (this.configStore.appearance?.dockHideOnBlur) {
this.hide()
}
})
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) {
2019-09-09 14:23:47 +00:00
ipcMain.removeListener('app:ready', listener as any)
resolve()
}
}
ipcMain.on('app:ready', listener)
})
}
2020-03-01 15:10:45 +00:00
setVibrancy (enabled: boolean, type?: string): void {
this.lastVibrancy = { enabled, type }
if (process.platform === 'win32') {
if (parseFloat(os.release()) >= 10) {
let attribValue = AccentState.ACCENT_DISABLED
if (enabled) {
if (type === 'fluent') {
2019-01-29 20:33:50 +00:00
attribValue = AccentState.ACCENT_ENABLE_ACRYLICBLURBEHIND
} else {
attribValue = AccentState.ACCENT_ENABLE_BLURBEHIND
}
}
2019-01-29 20:33:50 +00:00
SetWindowCompositionAttribute(this.window.getNativeWindowHandle(), attribValue, 0x00000000)
} else {
DwmEnableBlurBehindWindow(this.window, enabled)
}
} else {
this.window.setVibrancy(enabled ? 'dark' : null as any) // electron issue 20269
}
}
2020-03-01 15:10:45 +00:00
show (): void {
this.window.show()
}
2020-03-01 15:10:45 +00:00
focus (): void {
this.window.focus()
}
2020-03-01 15:10:45 +00:00
send (event: string, ...args): void {
2018-10-26 14:17:20 +00:00
if (!this.window) {
return
}
this.window.webContents.send(event, ...args)
if (event === 'host:config-change') {
this.configStore = args[0]
}
}
2020-03-01 16:07:11 +00:00
isDestroyed (): boolean {
2020-02-05 12:16:31 +00:00
return !this.window || this.window.isDestroyed()
}
isFocused (): boolean {
return this.window.isFocused()
}
2020-04-20 09:25:20 +00:00
hide (): void {
if (process.platform === 'darwin') {
// Lose focus
Menu.sendActionToFirstResponder('hide:')
}
this.window.blur()
if (process.platform !== 'darwin') {
this.window.hide()
}
}
2020-04-20 09:25:20 +00:00
present (): void {
if (!this.window.isVisible()) {
// unfocused, invisible
this.window.show()
this.window.focus()
} else {
if (!this.configStore.appearance?.dock || this.configStore.appearance?.dock === 'off') {
// not docked, visible
setTimeout(() => {
this.window.show()
this.window.focus()
})
} else {
2020-04-20 17:19:41 +00:00
if (this.configStore.appearance?.dockAlwaysOnTop) {
2020-04-20 16:38:02 +00:00
// docked, visible, on top
this.window.hide()
} else {
// docked, visible, not on top
this.window.focus()
}
}
}
}
private setupWindowManagement () {
this.window.on('show', () => {
this.visible.next(true)
2019-11-26 14:51:31 +00:00
this.send('host:window-shown')
})
this.window.on('hide', () => {
this.visible.next(false)
})
2018-12-29 12:27:45 +00:00
let moveSubscription = new Observable<void>(observer => {
this.window.on('move', () => observer.next())
}).pipe(debounceTime(250)).subscribe(() => {
2019-11-26 14:51:31 +00:00
this.send('host:window-moved')
2018-12-29 12:27:45 +00:00
})
this.window.on('closed', () => {
moveSubscription.unsubscribe()
})
2019-11-26 14:51:31 +00:00
this.window.on('enter-full-screen', () => this.send('host:window-enter-full-screen'))
this.window.on('leave-full-screen', () => this.send('host:window-leave-full-screen'))
this.window.on('close', event => {
if (!this.closing) {
event.preventDefault()
2019-11-26 14:51:31 +00:00
this.send('host:window-close-request')
return
}
2018-09-12 23:25:08 +00:00
this.windowConfig.set('windowBoundaries', this.windowBounds)
this.windowConfig.set('maximized', this.window.isMaximized())
})
this.window.on('closed', () => {
this.destroy()
})
2018-09-12 23:25:08 +00:00
this.window.on('resize', () => {
2018-09-20 09:46:24 +00:00
if (!this.window.isMaximized()) {
2018-09-12 23:25:08 +00:00
this.windowBounds = this.window.getBounds()
2018-09-20 09:46:24 +00:00
}
2018-09-12 23:25:08 +00:00
})
this.window.on('move', () => {
2018-09-20 09:46:24 +00:00
if (!this.window.isMaximized()) {
2018-09-12 23:25:08 +00:00
this.windowBounds = this.window.getBounds()
2018-09-20 09:46:24 +00:00
}
2018-09-12 23:25:08 +00:00
})
this.window.on('focus', () => {
this.send('host:window-focused')
})
2018-10-26 14:17:20 +00:00
ipcMain.on('window-focus', event => {
2019-02-09 21:38:45 +00:00
if (!this.window || event.sender !== this.window.webContents) {
2018-10-26 14:17:20 +00:00
return
}
this.window.focus()
})
2018-10-26 14:17:20 +00:00
ipcMain.on('window-maximize', event => {
2019-02-09 21:38:45 +00:00
if (!this.window || event.sender !== this.window.webContents) {
2018-10-26 14:17:20 +00:00
return
}
this.window.maximize()
})
2018-10-26 14:17:20 +00:00
ipcMain.on('window-unmaximize', event => {
2019-02-09 21:38:45 +00:00
if (!this.window || event.sender !== this.window.webContents) {
2018-10-26 14:17:20 +00:00
return
}
this.window.unmaximize()
})
2018-10-26 14:17:20 +00:00
ipcMain.on('window-toggle-maximize', event => {
2019-02-09 21:38:45 +00:00
if (!this.window || event.sender !== this.window.webContents) {
2018-10-26 14:17:20 +00:00
return
}
if (this.window.isMaximized()) {
this.window.unmaximize()
} else {
this.window.maximize()
}
})
2018-10-26 14:17:20 +00:00
ipcMain.on('window-minimize', event => {
2019-02-09 21:38:45 +00:00
if (!this.window || event.sender !== this.window.webContents) {
2018-10-26 14:17:20 +00:00
return
}
this.window.minimize()
})
2018-10-26 14:17:20 +00:00
ipcMain.on('window-set-bounds', (event, bounds) => {
2019-02-09 21:38:45 +00:00
if (!this.window || event.sender !== this.window.webContents) {
2018-10-26 14:17:20 +00:00
return
}
this.window.setBounds(bounds)
})
2018-10-26 14:17:20 +00:00
ipcMain.on('window-set-always-on-top', (event, flag) => {
2019-02-09 21:38:45 +00:00
if (!this.window || event.sender !== this.window.webContents) {
2018-10-26 14:17:20 +00:00
return
}
this.window.setAlwaysOnTop(flag)
})
2018-10-26 14:17:20 +00:00
ipcMain.on('window-set-vibrancy', (event, enabled, type) => {
2019-02-09 21:38:45 +00:00
if (!this.window || event.sender !== this.window.webContents) {
2018-10-26 14:17:20 +00:00
return
}
this.setVibrancy(enabled, type)
})
2018-09-20 09:42:51 +00:00
2018-10-26 14:17:20 +00:00
ipcMain.on('window-set-title', (event, title) => {
2019-02-09 21:38:45 +00:00
if (!this.window || event.sender !== this.window.webContents) {
2018-10-26 14:17:20 +00:00
return
}
2018-09-20 09:42:51 +00:00
this.window.setTitle(title)
})
2018-10-13 11:35:16 +00:00
ipcMain.on('window-bring-to-front', event => {
2019-02-09 21:38:45 +00:00
if (!this.window || event.sender !== this.window.webContents) {
return
}
if (this.window.isMinimized()) {
this.window.restore()
}
this.window.show()
this.window.moveTop()
})
2018-12-29 11:41:32 +00:00
ipcMain.on('window-close', event => {
2019-02-09 21:38:45 +00:00
if (!this.window || event.sender !== this.window.webContents) {
2018-12-29 11:41:32 +00:00
return
}
this.closing = true
this.window.close()
})
2018-10-13 11:35:16 +00:00
this.window.webContents.on('new-window', event => event.preventDefault())
ipcMain.on('window-set-disable-vibrancy-while-dragging', (_event, value) => {
this.disableVibrancyWhileDragging = value
})
this.window.on('will-move', () => {
if (!this.lastVibrancy?.enabled || !this.disableVibrancyWhileDragging) {
return
}
let timeout: number|null = null
const oldVibrancy = this.lastVibrancy
this.setVibrancy(false)
const onMove = () => {
if (timeout) {
clearTimeout(timeout)
}
timeout = setTimeout(() => {
this.window.off('move', onMove)
this.setVibrancy(oldVibrancy.enabled, oldVibrancy.type)
}, 500)
}
this.window.on('move', onMove)
})
}
private destroy () {
this.window = null
this.closed.next()
this.visible.complete()
this.closed.complete()
}
}