@@ -4,12 +4,12 @@ function centralize() {
4
4
function ( currentWindow ) {
5
5
chrome . system . display . getInfo ( function ( info ) {
6
6
if ( info && info . length > 0 ) {
7
- const display = info [ 0 ] ;
7
+ const display = getClosestDisplayForWindowPosition ( info , currentWindow ) ;
8
8
const displayWidth = display . workArea . width ;
9
9
const displayHeight = display . workArea . height ;
10
10
11
11
const toBeWidth = Math . round ( ( displayWidth / 4 ) * 3 ) ;
12
- const leftPos = Math . round ( ( displayWidth - toBeWidth ) / 2 ) ;
12
+ const leftPos = display . workArea . left + Math . round ( ( displayWidth - toBeWidth ) / 2 ) ;
13
13
14
14
const toBeHeight = displayHeight - 20 ;
15
15
const topPos = display . workArea . top + 10 ;
@@ -29,6 +29,32 @@ function centralize() {
29
29
) ;
30
30
}
31
31
32
+ function getClosestDisplayForWindowPosition ( displays , window ) {
33
+ if ( displays . length == 1 ) {
34
+ return displays [ 0 ] ;
35
+ }
36
+
37
+ const windowPosition = { x : window . top , y : window . left } ;
38
+
39
+ let points = [ ] ;
40
+
41
+ for ( let i = 0 ; i < displays . length ; i ++ ) {
42
+ points . push ( {
43
+ x : displays [ i ] . workArea . top ,
44
+ y : displays [ i ] . workArea . left ,
45
+ display : displays [ i ]
46
+ } ) ;
47
+ }
48
+
49
+ closest = points . reduce ( ( a , b ) => distance ( a , windowPosition ) < distance ( b , windowPosition ) ? a : b ) ;
50
+
51
+ return closest . display ;
52
+ }
53
+
54
+ function distance ( p , point ) {
55
+ return Math . sqrt ( Math . pow ( point . x - p . x , 2 ) + Math . pow ( point . y - p . y , 2 ) )
56
+ }
57
+
32
58
chrome . commands . onCommand . addListener ( ( command ) => {
33
59
if ( command === "centralize" ) {
34
60
centralize ( ) ;
0 commit comments