window.ts 9.6 KB

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