browser-window.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { BaseWindow, WebContents, BrowserView, TouchBar } from 'electron/main';
  2. import type { BrowserWindow as BWT } from 'electron/main';
  3. const { BrowserWindow } = process._linkedBinding('electron_browser_window') as { BrowserWindow: typeof BWT };
  4. Object.setPrototypeOf(BrowserWindow.prototype, BaseWindow.prototype);
  5. BrowserWindow.prototype._init = function (this: BWT) {
  6. // Call parent class's _init.
  7. BaseWindow.prototype._init.call(this);
  8. // Avoid recursive require.
  9. const { app } = require('electron');
  10. // Set ID at constructon time so it's accessible after
  11. // underlying window destruction.
  12. const id = this.id;
  13. Object.defineProperty(this, 'id', {
  14. value: id,
  15. writable: false
  16. });
  17. const nativeSetBounds = this.setBounds;
  18. this.setBounds = (bounds, ...opts) => {
  19. bounds = {
  20. ...this.getBounds(),
  21. ...bounds
  22. };
  23. nativeSetBounds.call(this, bounds, ...opts);
  24. };
  25. // Redirect focus/blur event to app instance too.
  26. this.on('blur', (event: Electron.Event) => {
  27. app.emit('browser-window-blur', event, this);
  28. });
  29. this.on('focus', (event: Electron.Event) => {
  30. app.emit('browser-window-focus', event, this);
  31. });
  32. // Subscribe to visibilityState changes and pass to renderer process.
  33. let isVisible = this.isVisible() && !this.isMinimized();
  34. const visibilityChanged = () => {
  35. const newState = this.isVisible() && !this.isMinimized();
  36. if (isVisible !== newState) {
  37. isVisible = newState;
  38. const visibilityState = isVisible ? 'visible' : 'hidden';
  39. this.webContents.emit('-window-visibility-change', visibilityState);
  40. }
  41. };
  42. const visibilityEvents = ['show', 'hide', 'minimize', 'maximize', 'restore'];
  43. for (const event of visibilityEvents) {
  44. this.on(event as any, visibilityChanged);
  45. }
  46. // Notify the creation of the window.
  47. app.emit('browser-window-created', { preventDefault () {} }, this);
  48. Object.defineProperty(this, 'devToolsWebContents', {
  49. enumerable: true,
  50. configurable: false,
  51. get () {
  52. return this.webContents.devToolsWebContents;
  53. }
  54. });
  55. };
  56. const isBrowserWindow = (win: any) => {
  57. return win && win.constructor.name === 'BrowserWindow';
  58. };
  59. BrowserWindow.fromId = (id: number) => {
  60. const win = BaseWindow.fromId(id);
  61. return isBrowserWindow(win) ? win as any as BWT : null;
  62. };
  63. BrowserWindow.getAllWindows = () => {
  64. return BaseWindow.getAllWindows().filter(isBrowserWindow) as any[] as BWT[];
  65. };
  66. BrowserWindow.getFocusedWindow = () => {
  67. for (const window of BrowserWindow.getAllWindows()) {
  68. if (!window.isDestroyed() && window.webContents && !window.webContents.isDestroyed()) {
  69. if (window.isFocused() || window.webContents.isDevToolsFocused()) return window;
  70. }
  71. }
  72. return null;
  73. };
  74. BrowserWindow.fromWebContents = (webContents: WebContents) => {
  75. return webContents.getOwnerBrowserWindow();
  76. };
  77. BrowserWindow.fromBrowserView = (browserView: BrowserView) => {
  78. return BrowserWindow.fromWebContents(browserView.webContents);
  79. };
  80. BrowserWindow.prototype.setTouchBar = function (touchBar) {
  81. (TouchBar as any)._setOnWindow(touchBar, this);
  82. };
  83. // Forwarded to webContents:
  84. BrowserWindow.prototype.loadURL = function (...args) {
  85. return this.webContents.loadURL(...args);
  86. };
  87. BrowserWindow.prototype.getURL = function () {
  88. return this.webContents.getURL();
  89. };
  90. BrowserWindow.prototype.loadFile = function (...args) {
  91. return this.webContents.loadFile(...args);
  92. };
  93. BrowserWindow.prototype.reload = function (...args) {
  94. return this.webContents.reload(...args);
  95. };
  96. BrowserWindow.prototype.send = function (...args) {
  97. return this.webContents.send(...args);
  98. };
  99. BrowserWindow.prototype.openDevTools = function (...args) {
  100. return this.webContents.openDevTools(...args);
  101. };
  102. BrowserWindow.prototype.closeDevTools = function () {
  103. return this.webContents.closeDevTools();
  104. };
  105. BrowserWindow.prototype.isDevToolsOpened = function () {
  106. return this.webContents.isDevToolsOpened();
  107. };
  108. BrowserWindow.prototype.isDevToolsFocused = function () {
  109. return this.webContents.isDevToolsFocused();
  110. };
  111. BrowserWindow.prototype.toggleDevTools = function () {
  112. return this.webContents.toggleDevTools();
  113. };
  114. BrowserWindow.prototype.inspectElement = function (...args) {
  115. return this.webContents.inspectElement(...args);
  116. };
  117. BrowserWindow.prototype.inspectSharedWorker = function () {
  118. return this.webContents.inspectSharedWorker();
  119. };
  120. BrowserWindow.prototype.inspectServiceWorker = function () {
  121. return this.webContents.inspectServiceWorker();
  122. };
  123. BrowserWindow.prototype.showDefinitionForSelection = function () {
  124. return this.webContents.showDefinitionForSelection();
  125. };
  126. BrowserWindow.prototype.capturePage = function (...args) {
  127. return this.webContents.capturePage(...args);
  128. };
  129. BrowserWindow.prototype.getBackgroundThrottling = function () {
  130. return this.webContents.getBackgroundThrottling();
  131. };
  132. BrowserWindow.prototype.setBackgroundThrottling = function (allowed: boolean) {
  133. return this.webContents.setBackgroundThrottling(allowed);
  134. };
  135. module.exports = BrowserWindow;