-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
122 lines (110 loc) · 3.74 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
'use strict'
module.exports = class Positioner {
constructor (browserWindow) {
this.browserWindow = browserWindow
this.electronScreen = require('electron').screen
}
_getCoords (position, trayPosition) {
let screenSize = this._getScreenSize(trayPosition)
let windowSize = this._getWindowSize()
if (trayPosition === undefined) trayPosition = {}
// Positions
let positions = {
trayLeft: {
x: Math.floor(trayPosition.x),
y: screenSize.y
},
trayBottomLeft: {
x: Math.floor(trayPosition.x),
y: Math.floor(screenSize.height - (windowSize[1] - screenSize.y))
},
trayRight: {
x: Math.floor(trayPosition.x - (windowSize[0]) + trayPosition.width),
y: screenSize.y
},
trayBottomRight: {
x: Math.floor(trayPosition.x - (windowSize[0]) + trayPosition.width),
y: Math.floor(screenSize.height - (windowSize[1] - screenSize.y))
},
trayCenter: {
x: Math.floor(trayPosition.x - ((windowSize[0] / 2)) + (trayPosition.width / 2)),
y: screenSize.y
},
trayBottomCenter: {
x: Math.floor(trayPosition.x - ((windowSize[0] / 2)) + (trayPosition.width / 2)),
y: Math.floor(screenSize.height - (windowSize[1] - screenSize.y))
},
topLeft: {
x: screenSize.x,
y: screenSize.y
},
topRight: {
x: Math.floor(screenSize.x + (screenSize.width - windowSize[0])),
y: screenSize.y
},
bottomLeft: {
x: screenSize.x,
y: Math.floor(screenSize.height - (windowSize[1] - screenSize.y))
},
bottomRight: {
x: Math.floor(screenSize.x + (screenSize.width - windowSize[0])),
y: Math.floor(screenSize.height - (windowSize[1] - screenSize.y))
},
topCenter: {
x: Math.floor(screenSize.x + ((screenSize.width / 2) - (windowSize[0] / 2))),
y: screenSize.y
},
bottomCenter: {
x: Math.floor(screenSize.x + ((screenSize.width / 2) - (windowSize[0] / 2))),
y: Math.floor(screenSize.height - (windowSize[1] - screenSize.y))
},
leftCenter: {
x: screenSize.x,
y: screenSize.y + Math.floor(screenSize.height / 2) - Math.floor(windowSize[1] / 2)
},
rightCenter: {
x: Math.floor(screenSize.x + (screenSize.width - windowSize[0])),
y: screenSize.y + Math.floor(screenSize.height / 2) - Math.floor(windowSize[1] / 2)
},
center: {
x: Math.floor(screenSize.x + ((screenSize.width / 2) - (windowSize[0] / 2))),
y: Math.floor(((screenSize.height + screenSize.y) / 2) - (windowSize[1] / 2))
}
}
// Default to right if the window is bigger than the space left.
// Because on Windows the window might get out of bounds and dissappear.
if (position.substr(0, 4) === 'tray') {
if ((positions[position].x + windowSize[0]) > (screenSize.width + screenSize.x)) {
return {
x: positions['topRight'].x,
y: positions[position].y
}
}
}
return positions[position]
}
_getWindowSize () {
return this.browserWindow.getSize()
}
_getScreenSize (trayPosition) {
if (trayPosition) {
return this.electronScreen.getDisplayMatching(trayPosition).workArea
} else {
return this.electronScreen.getDisplayNearestPoint(this.electronScreen.getCursorScreenPoint()).workArea
}
}
move (position, trayPos) {
// Get positions coords
var coords = this._getCoords(position, trayPos)
// Set the windows position
this.browserWindow.setPosition(coords.x, coords.y)
}
calculate (position, trayPos) {
// Get positions coords
var coords = this._getCoords(position, trayPos)
return {
x: coords.x,
y: coords.y
}
}
}