window.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import { Subject, Observable } from 'rxjs'
  2. import { debounceTime } from 'rxjs/operators'
  3. import { BrowserWindow, app, ipcMain, Rectangle, screen } from 'electron'
  4. import ElectronConfig = require('electron-config')
  5. import * as os from 'os'
  6. import * as path from 'path'
  7. import { loadConfig } from './config'
  8. let SetWindowCompositionAttribute: any
  9. let AccentState: any
  10. let DwmEnableBlurBehindWindow: any
  11. if (process.platform === 'win32') {
  12. SetWindowCompositionAttribute = require('windows-swca').SetWindowCompositionAttribute
  13. AccentState = require('windows-swca').ACCENT_STATE
  14. DwmEnableBlurBehindWindow = require('windows-blurbehind').DwmEnableBlurBehindWindow
  15. }
  16. export interface WindowOptions {
  17. hidden?: boolean
  18. }
  19. export class Window {
  20. ready: Promise<void>
  21. private visible = new Subject<boolean>()
  22. private window: BrowserWindow
  23. private windowConfig: ElectronConfig
  24. private windowBounds: Rectangle
  25. private closing = false
  26. private lastVibrancy: {enabled: boolean, type?: string} | null = null
  27. private disableVibrancyWhileDragging = false
  28. get visible$ (): Observable<boolean> { return this.visible }
  29. constructor (options?: WindowOptions) {
  30. let configData = loadConfig()
  31. options = options || {}
  32. this.windowConfig = new ElectronConfig({ name: 'window' })
  33. this.windowBounds = this.windowConfig.get('windowBoundaries')
  34. let maximized = this.windowConfig.get('maximized')
  35. let bwOptions: Electron.BrowserWindowConstructorOptions = {
  36. width: 800,
  37. height: 600,
  38. title: 'Terminus',
  39. minWidth: 400,
  40. minHeight: 300,
  41. webPreferences: {
  42. nodeIntegration: true,
  43. preload: path.join(__dirname, 'sentry.js'),
  44. backgroundThrottling: false,
  45. },
  46. frame: false,
  47. show: false,
  48. backgroundColor: '#00000000',
  49. }
  50. if (this.windowBounds) {
  51. Object.assign(bwOptions, this.windowBounds)
  52. const closestDisplay = screen.getDisplayNearestPoint( { x: this.windowBounds.x, y: this.windowBounds.y } )
  53. const [left1, top1, right1, bottom1] = [this.windowBounds.x, this.windowBounds.y, this.windowBounds.x + this.windowBounds.width, this.windowBounds.y + this.windowBounds.height]
  54. const [left2, top2, right2, bottom2] = [closestDisplay.bounds.x, closestDisplay.bounds.y, closestDisplay.bounds.x + closestDisplay.bounds.width, closestDisplay.bounds.y + closestDisplay.bounds.height]
  55. if ((left2 > right1 || right2 < left1 || top2 > bottom1 || bottom2 < top1) && !maximized) {
  56. bwOptions.x = closestDisplay.bounds.width / 2 - bwOptions.width / 2
  57. bwOptions.y = closestDisplay.bounds.height / 2 - bwOptions.height / 2
  58. }
  59. }
  60. if ((configData.appearance || {}).frame === 'native') {
  61. bwOptions.frame = true
  62. } else {
  63. if (process.platform === 'darwin') {
  64. bwOptions.titleBarStyle = 'hiddenInset'
  65. }
  66. }
  67. if (process.platform === 'linux') {
  68. bwOptions.backgroundColor = '#131d27'
  69. }
  70. this.window = new BrowserWindow(bwOptions)
  71. this.window.once('ready-to-show', () => {
  72. if (process.platform === 'darwin') {
  73. this.window.setVibrancy('window')
  74. } else if (process.platform === 'win32' && (configData.appearance || {}).vibrancy) {
  75. this.setVibrancy(true)
  76. }
  77. if (!options.hidden) {
  78. if (maximized) {
  79. this.window.maximize()
  80. } else {
  81. this.window.show()
  82. }
  83. this.window.focus()
  84. }
  85. })
  86. this.window.loadURL(`file://${app.getAppPath()}/dist/index.html?${this.window.id}`, { extraHeaders: 'pragma: no-cache\n' })
  87. if (process.platform !== 'darwin') {
  88. this.window.setMenu(null)
  89. }
  90. this.setupWindowManagement()
  91. this.ready = new Promise(resolve => {
  92. const listener = event => {
  93. if (event.sender === this.window.webContents) {
  94. ipcMain.removeListener('app:ready', listener as any)
  95. resolve()
  96. }
  97. }
  98. ipcMain.on('app:ready', listener)
  99. })
  100. }
  101. setVibrancy (enabled: boolean, type?: string) {
  102. this.lastVibrancy = { enabled, type }
  103. if (process.platform === 'win32') {
  104. if (parseFloat(os.release()) >= 10) {
  105. let attribValue = AccentState.ACCENT_DISABLED
  106. if (enabled) {
  107. if (type === 'fluent') {
  108. attribValue = AccentState.ACCENT_ENABLE_ACRYLICBLURBEHIND
  109. } else {
  110. attribValue = AccentState.ACCENT_ENABLE_BLURBEHIND
  111. }
  112. }
  113. SetWindowCompositionAttribute(this.window.getNativeWindowHandle(), attribValue, 0x00000000)
  114. } else {
  115. DwmEnableBlurBehindWindow(this.window, enabled)
  116. }
  117. } else {
  118. this.window.setVibrancy(enabled ? 'dark' : null as any) // electron issue 20269
  119. }
  120. }
  121. show () {
  122. this.window.show()
  123. }
  124. focus () {
  125. this.window.focus()
  126. }
  127. send (event, ...args) {
  128. if (!this.window) {
  129. return
  130. }
  131. this.window.webContents.send(event, ...args)
  132. }
  133. isDestroyed () {
  134. return !this.window || this.window.isDestroyed()
  135. }
  136. private setupWindowManagement () {
  137. this.window.on('show', () => {
  138. this.visible.next(true)
  139. this.send('host:window-shown')
  140. })
  141. this.window.on('hide', () => {
  142. this.visible.next(false)
  143. })
  144. let moveSubscription = new Observable<void>(observer => {
  145. this.window.on('move', () => observer.next())
  146. }).pipe(debounceTime(250)).subscribe(() => {
  147. this.send('host:window-moved')
  148. })
  149. this.window.on('closed', () => {
  150. moveSubscription.unsubscribe()
  151. })
  152. this.window.on('enter-full-screen', () => this.send('host:window-enter-full-screen'))
  153. this.window.on('leave-full-screen', () => this.send('host:window-leave-full-screen'))
  154. this.window.on('close', event => {
  155. if (!this.closing) {
  156. event.preventDefault()
  157. this.send('host:window-close-request')
  158. return
  159. }
  160. this.windowConfig.set('windowBoundaries', this.windowBounds)
  161. this.windowConfig.set('maximized', this.window.isMaximized())
  162. })
  163. this.window.on('closed', () => {
  164. this.destroy()
  165. })
  166. this.window.on('resize', () => {
  167. if (!this.window.isMaximized()) {
  168. this.windowBounds = this.window.getBounds()
  169. }
  170. })
  171. this.window.on('move', () => {
  172. if (!this.window.isMaximized()) {
  173. this.windowBounds = this.window.getBounds()
  174. }
  175. })
  176. this.window.on('focus', () => {
  177. this.send('host:window-focused')
  178. })
  179. ipcMain.on('window-focus', event => {
  180. if (!this.window || event.sender !== this.window.webContents) {
  181. return
  182. }
  183. this.window.focus()
  184. })
  185. ipcMain.on('window-maximize', event => {
  186. if (!this.window || event.sender !== this.window.webContents) {
  187. return
  188. }
  189. this.window.maximize()
  190. })
  191. ipcMain.on('window-unmaximize', event => {
  192. if (!this.window || event.sender !== this.window.webContents) {
  193. return
  194. }
  195. this.window.unmaximize()
  196. })
  197. ipcMain.on('window-toggle-maximize', event => {
  198. if (!this.window || event.sender !== this.window.webContents) {
  199. return
  200. }
  201. if (this.window.isMaximized()) {
  202. this.window.unmaximize()
  203. } else {
  204. this.window.maximize()
  205. }
  206. })
  207. ipcMain.on('window-minimize', event => {
  208. if (!this.window || event.sender !== this.window.webContents) {
  209. return
  210. }
  211. this.window.minimize()
  212. })
  213. ipcMain.on('window-set-bounds', (event, bounds) => {
  214. if (!this.window || event.sender !== this.window.webContents) {
  215. return
  216. }
  217. this.window.setBounds(bounds)
  218. })
  219. ipcMain.on('window-set-always-on-top', (event, flag) => {
  220. if (!this.window || event.sender !== this.window.webContents) {
  221. return
  222. }
  223. this.window.setAlwaysOnTop(flag)
  224. })
  225. ipcMain.on('window-set-vibrancy', (event, enabled, type) => {
  226. if (!this.window || event.sender !== this.window.webContents) {
  227. return
  228. }
  229. this.setVibrancy(enabled, type)
  230. })
  231. ipcMain.on('window-set-title', (event, title) => {
  232. if (!this.window || event.sender !== this.window.webContents) {
  233. return
  234. }
  235. this.window.setTitle(title)
  236. })
  237. ipcMain.on('window-bring-to-front', event => {
  238. if (!this.window || event.sender !== this.window.webContents) {
  239. return
  240. }
  241. if (this.window.isMinimized()) {
  242. this.window.restore()
  243. }
  244. this.window.show()
  245. this.window.moveTop()
  246. })
  247. ipcMain.on('window-close', event => {
  248. if (!this.window || event.sender !== this.window.webContents) {
  249. return
  250. }
  251. this.closing = true
  252. this.window.close()
  253. })
  254. this.window.webContents.on('new-window', event => event.preventDefault())
  255. ipcMain.on('window-set-disable-vibrancy-while-dragging', (_event, value) => {
  256. this.disableVibrancyWhileDragging = value
  257. })
  258. this.window.on('will-move', () => {
  259. if (!this.lastVibrancy?.enabled || !this.disableVibrancyWhileDragging) {
  260. return
  261. }
  262. let timeout: number|null = null
  263. const oldVibrancy = this.lastVibrancy
  264. this.setVibrancy(false)
  265. const onMove = () => {
  266. if (timeout) {
  267. clearTimeout(timeout)
  268. }
  269. timeout = setTimeout(() => {
  270. this.window.off('move', onMove)
  271. this.setVibrancy(oldVibrancy.enabled, oldVibrancy.type)
  272. }, 500)
  273. }
  274. this.window.on('move', onMove)
  275. })
  276. }
  277. private destroy () {
  278. this.window = null
  279. this.visible.complete()
  280. }
  281. }