Source: index.js

  1. /*
  2. * NPM Module "electron-window-position" by KIMB-technologies
  3. * https://github.com/kimbtech/electron-window-position
  4. * MIT License
  5. */
  6. /*
  7. // load electron
  8. const electron = require('electron');
  9. // link to electron app
  10. const app = electron.app;
  11. */
  12. // fires a custom exception, defined here
  13. function NotReadyException( m ){
  14. this.message = m;
  15. this.name = "NotReadyException";
  16. }
  17. // export class for NodeJS
  18. /**
  19. * A class which allows easy and intuitive window positioning for electron browser windows.
  20. */
  21. /*module.exports =*/ class WindowPosition {
  22. /**
  23. * Initiates calculation, waits for electron app become ready before calculation.
  24. * @param {function(JSON)} gets calculated top left position as JSON {x: ?, y: ?}
  25. */
  26. constructor ( callback ){
  27. // position
  28. this.activeScreenTopLeftData = {x : 0, y : 0};
  29. // nothing cacled until now
  30. this.activeScreenTopLeftCalced = false;
  31. // refers to choosen display for positioning
  32. this.display = null;
  33. // is electron app module ready?
  34. if( app.isReady() ){
  35. // calc the position
  36. this.calcActiveScreenTopLeft();
  37. // give the position back
  38. if( typeof callback == "function" ){
  39. callback( this.getActiveScreenTopLeft() );
  40. }
  41. }
  42. else{
  43. // wait for electron app module to become ready
  44. app.on('ready', () => {
  45. // calc the position
  46. this.calcActiveScreenTopLeft();
  47. // give the position back
  48. if( typeof callback == "function" ){
  49. callback( this.getActiveScreenTopLeft() );
  50. }
  51. });
  52. }
  53. }
  54. /**
  55. * Getter for browser window position so that the window opens at the top left corner,
  56. * of the screen where the mouse is.
  57. * @return {JSON} calculated top left position as JSON {x: ?, y: ?}
  58. * @throws {NotReadyException} fired if electron app module is not ready
  59. */
  60. getActiveScreenTopLeft(){
  61. // already calced position?
  62. if( this.activeScreenTopLeftCalced ){
  63. // return it
  64. return this.activeScreenTopLeftData;
  65. }
  66. else{
  67. // no position calced and not possible now
  68. throw new NotReadyException( 'This query is impossible now, because electron is not ready yet. [electron.app.isReady()]' );
  69. }
  70. }
  71. /**
  72. * Getter for browser window position so that the window opens centered
  73. * on the screen where the mouse is.
  74. * @param {number} width Width of the window which should be centered
  75. * @param {number} height Height of the window which should be centered
  76. * @return {JSON} calculated centered position as JSON {x: ?, y: ?}
  77. * @throws {NotReadyException} fired if electron app module is not ready
  78. */
  79. getActiveScreenCenter( width, height ){
  80. // top left position
  81. var topleft = this.getActiveScreenTopLeft();
  82. topleft.x = topleft.x - 20;
  83. topleft.y = topleft.y - 20;
  84. // calculate center
  85. var center = {
  86. x : topleft.x + Math.floor( this.display.bounds.width / 2 ),
  87. y : topleft.y + Math.floor( this.display.bounds.height / 2)
  88. };
  89. // substract half window size => top left corner for centered window
  90. center.x = center.x - Math.floor( width / 2 );
  91. center.y = center.y - Math.floor( height / 2 );
  92. return center;
  93. }
  94. /**
  95. * Calculates top left position and saves it to class variables.
  96. * @private
  97. */
  98. calcActiveScreenTopLeft(){
  99. // electron app module ist ready, so we can access screen module
  100. const screen = electron.screen;
  101. // get the mouse's position, coordinates range over all displays
  102. var mouse = screen.getCursorScreenPoint();
  103. // loop over all displays
  104. screen.getAllDisplays().some( display => {
  105. //check if mouse keeps in this display
  106. if(
  107. ( display.bounds.x - mouse.x ) < 0 &&
  108. ( display.bounds.x + display.bounds.width - mouse.x ) > 0
  109. &&
  110. ( display.bounds.y - mouse.y ) < 0 &&
  111. ( display.bounds.y + display.bounds.height - mouse.y ) > 0
  112. ){
  113. // get top left corner (each +20, looks better)
  114. this.activeScreenTopLeftData.x = display.bounds.x + 20;
  115. this.activeScreenTopLeftData.y = display.bounds.y + 20;
  116. // remeber this display
  117. this.display = display;
  118. //no we have calculated data
  119. this.activeScreenTopLeftCalced = true;
  120. //leave
  121. return;
  122. }
  123. });
  124. }
  125. }