-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathutils.js
More file actions
245 lines (215 loc) · 7.24 KB
/
Copy pathutils.js
File metadata and controls
245 lines (215 loc) · 7.24 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const Store = require('electron-store');
const electron = require('electron');
const os = require('os');
const log = require('@jitsi/logger');
const { SIZE, ASPECT_RATIO, STORAGE } = require('../constants');
/**
* Stores the current size of the AOT during the conference
* @type {{width: number, height: number}}
*/
let size = Object.assign({}, SIZE);
/**
* Keeps the old size of the window between resize handler calls
*/
let oldSize;
/**
* The logger instance
*/
let logger;
/**
* The electron-store instance to store last position of the aot window.
*/
const store = new Store();
/**
* Changes the window resize functionality to respect the passed aspect ratio.
*
* @param {BrowserWindow} win - The target window.
* @param {number} aspectRatio - The aspect ratio to be set.
* @returns {void}
*/
const setAspectRatioToResizeableWindow = (win, aspectRatio = ASPECT_RATIO) => {
//for macOS we use the built-in setAspectRatio on resize, for other we use custom implementation
if (os.type() === 'Darwin') {
win.setAspectRatio(aspectRatio);
win.on('resize', () => {
const [ width, height ] = win.getSize();
size.width = width;
size.height = height;
});
} else {
win.on('will-resize', (e, newBounds) => {
oldSize = win.getSize();
const mousePos = electron.screen.getCursorScreenPoint();
const windowBottomRightPos = {
x: newBounds.x + newBounds.width - 16,
y: newBounds.y + newBounds.height - 16,
};
//prevent resize from bottom right corner as it is buggy.
if (mousePos.x >= windowBottomRightPos.x && mousePos.y >= windowBottomRightPos.y) {
e.preventDefault();
}
});
win.on('resize', () => {
if (!Array.isArray(oldSize) || oldSize.length !== 2) {
// Adding this check because of reports for JS errors that oldSize is undefined.
return;
}
let [ width, height ] = win.getSize();
//we scale either width or height according to the other by checking which of the 2
//changed the most since last resize.
if (Math.abs(oldSize[0] - width) >= Math.abs(oldSize[1] - height)) {
height = Math.round(width / aspectRatio);
} else {
width = Math.round(height * aspectRatio);
}
win.setSize(width, height);
size.width = width;
size.height = height;
});
}
};
/**
* Calculates the coordinates for a window (based on its current coordinates and
* size) to place it in the boundaries of a given screen.
*
* @param {Rectangle} windowRectangle - The current position and dimensions of
* the window.
* @param {Rectangle} screenRectangle - The position and dimensions of the
* screen.
* @returns {Point} - The new coordinates for the window.
*
* NOTE: All x and y coordinates are representing the top-left corner of the
* window or screen.
*/
const positionWindowWithinScreenBoundaries = (windowRectangle, screenRectangle) => {
// The min value for y coordinate of the window in order to place it within
// the boundaries of the screen. This will be the use case where the top
// edge of the window is exactly on the top boundary of the screen.
const minY = screenRectangle.y;
// The min value for x coordinate of the window in order to place it within
// the boundaries of the screen. This will be the use case where the left
// edge of the window is exactly on the left boundary of the screen.
const minX = screenRectangle.x;
// The max value for y coordinate of the window in order to place it within
// the boundaries of the screen. This will be the use case where the bottom
// edge of the window is exactly on the bottom boundary of the screen.
const maxY
= screenRectangle.y + screenRectangle.height - windowRectangle.height;
// The max value for x coordinate of the window in order to place it within
// the boundaries of the screen. This will be the use case where the right
// edge of the window is exactly on the right boundary of the screen.
const maxX
= screenRectangle.x + screenRectangle.width - windowRectangle.width;
return {
x: Math.min(Math.max(windowRectangle.x, minX), maxX),
y: Math.min(Math.max(windowRectangle.y, minY), maxY)
};
};
const setLogger = loggerTransports => {
logger = log.getLogger('AOT', loggerTransports || []);
};
/**
* Wrapper over the loger's info
*
* @param {string} info - The info text
*/
const logInfo = info => {
if (!logger) {
return;
}
logger.info(`[MAIN] ${info}`);
};
/**
* Wrapper over the loger's error
*
* @param {Object} err - the error object
*/
const logError = err => {
if (!logger) {
return;
}
logger.error({ err }, '[MAIN ERROR]');
};
/**
* Returns the stored coordinates for the always on top window of its previous
* location or if the last location is unknown returns coordinates to display
* the window in the top right corner of the screen.
*
* @returns {{x: number, y: number}}
*/
const getPosition = () => {
const Screen = electron.screen;
const position = {
x: store.get(STORAGE.AOT_X),
y: store.get(STORAGE.AOT_Y)
};
if (typeof position.x === 'number' && typeof position.y === 'number') {
// Position the window within the screen boundaries. This is needed
// only for windows. On Mac and Linux it is working as expected without
// changing the coordinates.
if (os.platform() === 'win32') {
const windowRectangle = Object.assign({}, position, size);
const matchingScreen = Screen.getDisplayMatching(windowRectangle);
if (matchingScreen) {
return positionWindowWithinScreenBoundaries(
windowRectangle,
matchingScreen.workArea);
}
}
return position;
}
const {
x,
y,
width
} = Screen.getDisplayNearestPoint(Screen.getCursorScreenPoint()).workArea;
return {
x: x + width - size.width,
y
};
};
/**
* Saves window position
* @param {BrowserWindow} browserWindow - the aot window
*/
const savePosition = aotWindow => {
if (!windowExists(aotWindow)) {
return;
}
const [x, y] = aotWindow.getPosition();
store.set(STORAGE.AOT_X, x);
store.set(STORAGE.AOT_Y, y);
};
/**
* Gets the size to be set to the new AOT window.
* This is used in order to preserve the size on close and open of AOT during the same meeting
* @returns {{width: number, height: number}}
*/
const getSize = () => {
if (typeof size.width === 'number' && typeof size.height === 'number') {
return size;
}
return SIZE;
};
const resetSize = () => {
size = Object.assign({}, SIZE);
};
/**
* Checks whether the window exists
* @param {BrowserWindow} win
* @returns
*/
const windowExists = browserWindow => {
return browserWindow && !browserWindow.isDestroyed();
};
module.exports = {
getPosition,
getSize,
logError,
logInfo,
resetSize,
savePosition,
setAspectRatioToResizeableWindow,
setLogger,
windowExists,
};