base-window.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { EventEmitter } from 'events';
  2. import type { BaseWindow as TLWT } from 'electron/main';
  3. import * as deprecate from '@electron/internal/common/deprecate';
  4. const { BaseWindow } = process._linkedBinding('electron_browser_base_window') as { BaseWindow: typeof TLWT };
  5. Object.setPrototypeOf(BaseWindow.prototype, EventEmitter.prototype);
  6. BaseWindow.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.applicationMenu;
  12. if (menu) this.setMenu(menu);
  13. }
  14. };
  15. // Deprecation.
  16. const setTrafficLightPositionDeprecated = deprecate.warnOnce('setTrafficLightPosition', 'setWindowButtonPosition');
  17. // Converting to any as the methods are defined under BrowserWindow in our docs.
  18. (BaseWindow as any).prototype.setTrafficLightPosition = function (pos: Electron.Point) {
  19. setTrafficLightPositionDeprecated();
  20. if (typeof pos === 'object' && pos.x === 0 && pos.y === 0) {
  21. this.setWindowButtonPosition(null);
  22. } else {
  23. this.setWindowButtonPosition(pos);
  24. }
  25. };
  26. const getTrafficLightPositionDeprecated = deprecate.warnOnce('getTrafficLightPosition', 'getWindowButtonPosition');
  27. (BaseWindow as any).prototype.getTrafficLightPosition = function () {
  28. getTrafficLightPositionDeprecated();
  29. const pos = this.getWindowButtonPosition();
  30. return pos === null ? { x: 0, y: 0 } : pos;
  31. };
  32. // Properties
  33. Object.defineProperty(BaseWindow.prototype, 'autoHideMenuBar', {
  34. get: function () { return this.isMenuBarAutoHide(); },
  35. set: function (autoHide) { this.setAutoHideMenuBar(autoHide); }
  36. });
  37. Object.defineProperty(BaseWindow.prototype, 'visibleOnAllWorkspaces', {
  38. get: function () { return this.isVisibleOnAllWorkspaces(); },
  39. set: function (visible) { this.setVisibleOnAllWorkspaces(visible); }
  40. });
  41. Object.defineProperty(BaseWindow.prototype, 'fullScreen', {
  42. get: function () { return this.isFullScreen(); },
  43. set: function (full) { this.setFullScreen(full); }
  44. });
  45. Object.defineProperty(BaseWindow.prototype, 'simpleFullScreen', {
  46. get: function () { return this.isSimpleFullScreen(); },
  47. set: function (simple) { this.setSimpleFullScreen(simple); }
  48. });
  49. Object.defineProperty(BaseWindow.prototype, 'focusable', {
  50. get: function () { return this.isFocusable(); },
  51. set: function (focusable) { this.setFocusable(focusable); }
  52. });
  53. Object.defineProperty(BaseWindow.prototype, 'kiosk', {
  54. get: function () { return this.isKiosk(); },
  55. set: function (kiosk) { this.setKiosk(kiosk); }
  56. });
  57. Object.defineProperty(BaseWindow.prototype, 'documentEdited', {
  58. get: function () { return this.isDocumentEdited(); },
  59. set: function (edited) { this.setDocumentEdited(edited); }
  60. });
  61. Object.defineProperty(BaseWindow.prototype, 'shadow', {
  62. get: function () { return this.hasShadow(); },
  63. set: function (shadow) { this.setHasShadow(shadow); }
  64. });
  65. Object.defineProperty(BaseWindow.prototype, 'representedFilename', {
  66. get: function () { return this.getRepresentedFilename(); },
  67. set: function (filename) { this.setRepresentedFilename(filename); }
  68. });
  69. Object.defineProperty(BaseWindow.prototype, 'minimizable', {
  70. get: function () { return this.isMinimizable(); },
  71. set: function (min) { this.setMinimizable(min); }
  72. });
  73. Object.defineProperty(BaseWindow.prototype, 'title', {
  74. get: function () { return this.getTitle(); },
  75. set: function (title) { this.setTitle(title); }
  76. });
  77. Object.defineProperty(BaseWindow.prototype, 'maximizable', {
  78. get: function () { return this.isMaximizable(); },
  79. set: function (max) { this.setMaximizable(max); }
  80. });
  81. Object.defineProperty(BaseWindow.prototype, 'resizable', {
  82. get: function () { return this.isResizable(); },
  83. set: function (res) { this.setResizable(res); }
  84. });
  85. Object.defineProperty(BaseWindow.prototype, 'menuBarVisible', {
  86. get: function () { return this.isMenuBarVisible(); },
  87. set: function (visible) { this.setMenuBarVisibility(visible); }
  88. });
  89. Object.defineProperty(BaseWindow.prototype, 'fullScreenable', {
  90. get: function () { return this.isFullScreenable(); },
  91. set: function (full) { this.setFullScreenable(full); }
  92. });
  93. Object.defineProperty(BaseWindow.prototype, 'closable', {
  94. get: function () { return this.isClosable(); },
  95. set: function (close) { this.setClosable(close); }
  96. });
  97. Object.defineProperty(BaseWindow.prototype, 'movable', {
  98. get: function () { return this.isMovable(); },
  99. set: function (move) { this.setMovable(move); }
  100. });
  101. BaseWindow.getFocusedWindow = () => {
  102. return BaseWindow.getAllWindows().find((win) => win.isFocused());
  103. };
  104. module.exports = BaseWindow;