window.ts 7.2 KB
Newer Older
1
import { Subject, Observable } from 'rxjs'
E
Eugene Pankov 已提交
2
import { BrowserWindow, app, ipcMain, Rectangle } from 'electron'
3
import ElectronConfig = require('electron-config')
4
import * as os from 'os'
E
Eugene Pankov 已提交
5 6

import { loadConfig } from './config'
7

8 9 10 11 12 13 14 15
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
}
16 17 18 19 20 21

export class Window {
    ready: Promise<void>
    private visible = new Subject<boolean>()
    private window: BrowserWindow
    private windowConfig: ElectronConfig
B
BBJip 已提交
22
    private windowBounds: Rectangle
23 24 25 26

    get visible$ (): Observable<boolean> { return this.visible }

    constructor () {
E
Eugene Pankov 已提交
27
        let configData = loadConfig()
28 29

        this.windowConfig = new ElectronConfig({ name: 'window' })
B
BBJip 已提交
30
        this.windowBounds = this.windowConfig.get('windowBoundaries')
31

B
BBJip 已提交
32
        let maximized = this.windowConfig.get('maximized')
33 34 35 36 37 38 39 40 41
        let options: Electron.BrowserWindowConstructorOptions = {
            width: 800,
            height: 600,
            title: 'Terminus',
            minWidth: 400,
            minHeight: 300,
            webPreferences: { webSecurity: false },
            frame: false,
            show: false,
42
            backgroundColor: '#00000000'
43
        }
B
BBJip 已提交
44
        Object.assign(options, this.windowBounds)
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

        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)
            }
B
BBJip 已提交
65 66 67 68 69
            if (maximized) {
                this.window.maximize()
            } else {
                this.window.show()
            }
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
            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)
        })
    }

91
    setVibrancy (enabled: boolean, type?: string) {
92 93 94 95 96
        if (process.platform === 'win32') {
            if (parseFloat(os.release()) >= 10) {
                let attribValue = AccentState.ACCENT_DISABLED
                let color = 0x00000000
                if (enabled) {
97
                    if (parseInt(os.release().split('.')[2]) >= 17063 && type === 'fluent') {
98 99 100 101 102 103 104 105 106 107
                        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)
            }
108 109 110 111 112 113 114 115 116 117 118 119
        }
    }

    show () {
        this.window.show()
    }

    focus () {
        this.window.focus()
    }

    send (event, ...args) {
E
Eugene Pankov 已提交
120 121 122
        if (!this.window) {
            return
        }
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        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', () => {
B
BBJip 已提交
140 141
            this.windowConfig.set('windowBoundaries', this.windowBounds)
            this.windowConfig.set('maximized', this.window.isMaximized())
142 143 144 145 146 147
        })

        this.window.on('closed', () => {
            this.destroy()
        })

B
BBJip 已提交
148
        this.window.on('resize', () => {
E
lint  
Eugene Pankov 已提交
149
            if (!this.window.isMaximized()) {
B
BBJip 已提交
150
                this.windowBounds = this.window.getBounds()
E
lint  
Eugene Pankov 已提交
151
            }
B
BBJip 已提交
152 153 154
        })

        this.window.on('move', () => {
E
lint  
Eugene Pankov 已提交
155
            if (!this.window.isMaximized()) {
B
BBJip 已提交
156
                this.windowBounds = this.window.getBounds()
E
lint  
Eugene Pankov 已提交
157
            }
B
BBJip 已提交
158 159
        })

E
Eugene Pankov 已提交
160 161 162 163
        ipcMain.on('window-focus', event => {
            if (event.sender !== this.window.webContents) {
                return
            }
164 165 166
            this.window.focus()
        })

E
Eugene Pankov 已提交
167 168 169 170
        ipcMain.on('window-maximize', event => {
            if (event.sender !== this.window.webContents) {
                return
            }
171 172 173
            this.window.maximize()
        })

E
Eugene Pankov 已提交
174 175 176 177
        ipcMain.on('window-unmaximize', event => {
            if (event.sender !== this.window.webContents) {
                return
            }
178 179 180
            this.window.unmaximize()
        })

E
Eugene Pankov 已提交
181 182 183 184
        ipcMain.on('window-toggle-maximize', event => {
            if (event.sender !== this.window.webContents) {
                return
            }
185 186 187 188 189 190 191
            if (this.window.isMaximized()) {
                this.window.unmaximize()
            } else {
                this.window.maximize()
            }
        })

E
Eugene Pankov 已提交
192 193 194 195
        ipcMain.on('window-minimize', event => {
            if (event.sender !== this.window.webContents) {
                return
            }
196 197 198
            this.window.minimize()
        })

E
Eugene Pankov 已提交
199 200 201 202
        ipcMain.on('window-set-bounds', (event, bounds) => {
            if (event.sender !== this.window.webContents) {
                return
            }
203 204 205
            this.window.setBounds(bounds)
        })

E
Eugene Pankov 已提交
206 207 208 209
        ipcMain.on('window-set-always-on-top', (event, flag) => {
            if (event.sender !== this.window.webContents) {
                return
            }
210 211 212
            this.window.setAlwaysOnTop(flag)
        })

E
Eugene Pankov 已提交
213 214 215 216
        ipcMain.on('window-set-vibrancy', (event, enabled, type) => {
            if (event.sender !== this.window.webContents) {
                return
            }
217
            this.setVibrancy(enabled, type)
218
        })
E
Eugene Pankov 已提交
219

E
Eugene Pankov 已提交
220 221 222 223
        ipcMain.on('window-set-title', (event, title) => {
            if (event.sender !== this.window.webContents) {
                return
            }
E
Eugene Pankov 已提交
224 225
            this.window.setTitle(title)
        })
E
Eugene Pankov 已提交
226 227

        this.window.webContents.on('new-window', event => event.preventDefault())
228 229 230 231 232 233 234
    }

    private destroy () {
        this.window = null
        this.visible.complete()
    }
}