window.ts 9.4 KB

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