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