browser-window.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 'use strict'
  2. const {ipcMain} = require('electron')
  3. const {EventEmitter} = require('events')
  4. const {BrowserWindow} = process.atomBinding('window')
  5. Object.setPrototypeOf(BrowserWindow.prototype, EventEmitter.prototype)
  6. BrowserWindow.prototype._init = function () {
  7. // Avoid recursive require.
  8. const {app} = require('electron')
  9. // Simulate the application menu on platforms other than macOS.
  10. if (process.platform !== 'darwin') {
  11. const menu = app.getApplicationMenu()
  12. if (menu) this.setMenu(menu)
  13. }
  14. // Make new windows requested by links behave like "window.open"
  15. this.webContents.on('-new-window', (event, url, frameName, disposition) => {
  16. const options = {
  17. show: true,
  18. width: 800,
  19. height: 600
  20. }
  21. ipcMain.emit('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', event, url, frameName, disposition, options)
  22. })
  23. // window.resizeTo(...)
  24. // window.moveTo(...)
  25. this.webContents.on('move', (event, size) => {
  26. this.setBounds(size)
  27. })
  28. // Hide the auto-hide menu when webContents is focused.
  29. this.webContents.on('activate', () => {
  30. if (process.platform !== 'darwin' && this.isMenuBarAutoHide() && this.isMenuBarVisible()) {
  31. this.setMenuBarVisibility(false)
  32. }
  33. })
  34. // Change window title to page title.
  35. this.webContents.on('page-title-updated', (event, title) => {
  36. // The page-title-updated event is not emitted immediately (see #3645), so
  37. // when the callback is called the BrowserWindow might have been closed.
  38. if (this.isDestroyed()) return
  39. // Route the event to BrowserWindow.
  40. this.emit('page-title-updated', event, title)
  41. if (!event.defaultPrevented) this.setTitle(title)
  42. })
  43. // Sometimes the webContents doesn't get focus when window is shown, so we
  44. // have to force focusing on webContents in this case. The safest way is to
  45. // focus it when we first start to load URL, if we do it earlier it won't
  46. // have effect, if we do it later we might move focus in the page.
  47. //
  48. // Though this hack is only needed on macOS when the app is launched from
  49. // Finder, we still do it on all platforms in case of other bugs we don't
  50. // know.
  51. this.webContents.once('load-url', function () {
  52. this.focus()
  53. })
  54. // Redirect focus/blur event to app instance too.
  55. this.on('blur', (event) => {
  56. app.emit('browser-window-blur', event, this)
  57. })
  58. this.on('focus', (event) => {
  59. app.emit('browser-window-focus', event, this)
  60. })
  61. // Subscribe to visibilityState changes and pass to renderer process.
  62. let isVisible = this.isVisible() && !this.isMinimized()
  63. const visibilityChanged = () => {
  64. const newState = this.isVisible() && !this.isMinimized()
  65. if (isVisible !== newState) {
  66. isVisible = newState
  67. this.webContents.send('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', isVisible ? 'visible' : 'hidden')
  68. }
  69. }
  70. const visibilityEvents = ['show', 'hide', 'minimize', 'maximize', 'restore']
  71. for (let event of visibilityEvents) {
  72. this.on(event, visibilityChanged)
  73. }
  74. // Notify the creation of the window.
  75. app.emit('browser-window-created', {}, this)
  76. Object.defineProperty(this, 'devToolsWebContents', {
  77. enumerable: true,
  78. configurable: false,
  79. get () {
  80. return this.webContents.devToolsWebContents
  81. }
  82. })
  83. }
  84. BrowserWindow.getFocusedWindow = () => {
  85. for (let window of BrowserWindow.getAllWindows()) {
  86. if (window.isFocused()) return window
  87. }
  88. return null
  89. }
  90. BrowserWindow.fromWebContents = (webContents) => {
  91. for (let window of BrowserWindow.getAllWindows()) {
  92. if (window.webContents.equal(webContents)) return window
  93. }
  94. }
  95. BrowserWindow.fromDevToolsWebContents = (webContents) => {
  96. for (let window of BrowserWindow.getAllWindows()) {
  97. if (window.devToolsWebContents.equal(webContents)) return window
  98. }
  99. }
  100. // Helpers.
  101. Object.assign(BrowserWindow.prototype, {
  102. loadURL (...args) {
  103. return this.webContents.loadURL(...args)
  104. },
  105. getURL (...args) {
  106. return this.webContents.getURL()
  107. },
  108. reload (...args) {
  109. return this.webContents.reload(...args)
  110. },
  111. send (...args) {
  112. return this.webContents.send(...args)
  113. },
  114. openDevTools (...args) {
  115. return this.webContents.openDevTools(...args)
  116. },
  117. closeDevTools () {
  118. return this.webContents.closeDevTools()
  119. },
  120. isDevToolsOpened () {
  121. return this.webContents.isDevToolsOpened()
  122. },
  123. isDevToolsFocused () {
  124. return this.webContents.isDevToolsFocused()
  125. },
  126. toggleDevTools () {
  127. return this.webContents.toggleDevTools()
  128. },
  129. inspectElement (...args) {
  130. return this.webContents.inspectElement(...args)
  131. },
  132. inspectServiceWorker () {
  133. return this.webContents.inspectServiceWorker()
  134. },
  135. showDefinitionForSelection () {
  136. return this.webContents.showDefinitionForSelection()
  137. },
  138. capturePage (...args) {
  139. return this.webContents.capturePage(...args)
  140. }
  141. })
  142. module.exports = BrowserWindow