This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
positioner.js
211 lines (188 loc) · 6.14 KB
/
positioner.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const electron = require('electron')
/**
* Get the display nearest the current cursor position
*
* @return {Display} - the display closest to the current cursor position
*/
function getDisplay () {
const screen = electron.screen
return screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
}
/**
* Get cursor position
*
* @return {Point} - the position of the cursor
*/
function getCursorPosition () {
return electron.screen.getCursorScreenPoint()
}
/**
* Calculates the x position of the tray window
*
* @param {Rectangle} windowBounds - electron BrowserWindow bounds of tray window to position
* @param {Rectangle} trayBounds - tray bounds from electron Tray.getBounds()
* @param {string} [align] - align left|center|right, default: center
*
* @return {integer} - calculated x position
*/
function calculateXAlign (windowBounds, trayBounds, align) {
const display = getDisplay()
let x
switch (align) {
case 'right':
x = trayBounds.x
break
case 'left':
x = trayBounds.x + trayBounds.width - windowBounds.width
break
case 'center':
default:
x = Math.round(trayBounds.x + (trayBounds.width / 2) - (windowBounds.width / 2))
}
if (x + windowBounds.width > display.bounds.width && align !== 'left') {
// if window would overlap on right side align it to the end of the screen
x = display.bounds.width - windowBounds.width
} else if (x < 0 && align !== 'right') {
// if window would overlap on the left side align it to the beginning
x = 0
}
return x
}
/**
* Calculates the y position of the tray window
*
* @param {Rectangle} windowBounds - electron BrowserWindow bounds
* @param {Rectangle} trayBounds - tray bounds from electron Tray.getBounds()
* @param {string} [align] - align up|middle|down, default: down
*
* @return {integer} - calculated y position
*/
function calculateYAlign (windowBounds, trayBounds, align) {
const display = getDisplay()
let y
switch (align) {
case 'up':
y = trayBounds.y + trayBounds.height - windowBounds.height
break
case 'down':
y = trayBounds.y
break
case 'center':
default:
y = Math.round((trayBounds.y + (trayBounds.height / 2)) - (windowBounds.height / 2))
break
}
if (y + windowBounds.height > display.bounds.height && align !== 'up') {
y = trayBounds.y + trayBounds.height - windowBounds.height
} else if (y < 0 && align !== 'down') {
y = 0
}
return y
}
/**
* Calculates the position of the tray window based on current cursor position
* This method is used on linux where trayBounds are not available
*
* @param {Rectangle} windowBounds - electron BrowserWindow bounds of tray window to position
* @param {Eelectron.Display} display - display on which the cursor is currently
* @param {Point} cursor - current cursor position
*
* @return {Point} - Calculated point {x, y} where the window should be positioned
*/
function calculateByCursorPosition (windowBounds, display, cursor) {
let x = cursor.x
let y = cursor.y
if (x + windowBounds.width > display.bounds.width) {
// if window would overlap on right side of screen, align it to the left of the cursor
x -= windowBounds.width
}
if (y + windowBounds.height > display.bounds.height) {
// if window would overlap at bottom of screen, align it up from cursor position
y -= windowBounds.height
}
return {
x: x,
y: y
}
}
class Positioner {
/**
* Calculates the position of the tray window
*
* @param {Rectangle} windowBounds - electron BrowserWindow bounds of tray window to position
* @param {Rectangle} trayBounds - tray bounds from electron Tray
* @param {Object} [alignment] - alignment of window to tray
* @param {string} [alignment.x] - x align if tray bar is on top or bottom (left|center|right),
default: center
* @param {string} [alignment.y] - y align if tray bar is left or right (up|middle|down),
default: down
* @return {Point} - Calculated point {x, y} where the window should be positioned
*/
static calculate (windowBounds, trayBounds, alignment) {
if (process.platform === 'linux') {
const cursor = getCursorPosition()
return calculateByCursorPosition(windowBounds, getDisplay(), cursor)
}
const _alignment = alignment || {}
const taskbarPosition = this.getTaskbarPosition()
const display = getDisplay()
let x
let y
switch (taskbarPosition) {
case 'left':
x = display.workArea.x
y = calculateYAlign(windowBounds, trayBounds, _alignment.y)
break
case 'right':
x = display.workArea.width - windowBounds.width
y = calculateYAlign(windowBounds, trayBounds, _alignment.y)
break
case 'bottom':
x = calculateXAlign(windowBounds, trayBounds, _alignment.x)
y = display.workArea.height - windowBounds.height
break
case 'top':
default:
x = calculateXAlign(windowBounds, trayBounds, _alignment.x)
y = display.workArea.y
}
return {
x,
y
}
}
/**
* Calculates the position of the tray window
*
* @param {BrowserWindow} window - window to position
* @param {Rectangle} trayBounds - tray bounds from electron Tray
* @param {Object} [alignment] - alignment of window to tray
* @param {string} [alignment.x] - x align if tray bar is on top or bottom (left|center|right),
default: center
* @param {string} [alignment.y] - y align if tray bar is left or right (up|middle|down),
default: down
*
* @return {Void}
*/
static position (window, trayBounds, alignment) {
const position = this.calculate(window.getBounds(), trayBounds, alignment)
window.setPosition(position.x, position.y, false)
}
/**
* Calculates the position of the tray window
*
* @return {string} - the position of the taskbar (top|right|bottom|left)
*/
static getTaskbarPosition () {
const { bounds, workArea } = getDisplay()
if (workArea.y > bounds.y) {
return 'top'
} else if (workArea.x > bounds.x) {
return 'left'
} else if (workArea.width === bounds.width) {
return 'bottom'
}
return 'right'
}
}
module.exports = Positioner